Skip to content

Commit 65da767

Browse files
bergerstmp911de
authored andcommitted
Improved performance of Bucket.extract().
By using a TreeMap, entries starting with a certain prefix can be found much faster than before. Closes #1982
1 parent aaf3cab commit 65da767

File tree

1 file changed

+12
-11
lines changed
  • src/main/java/org/springframework/data/redis/core/convert

1 file changed

+12
-11
lines changed

src/main/java/org/springframework/data/redis/core/convert/Bucket.java

+12-11
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,28 @@
1919
import java.nio.charset.StandardCharsets;
2020
import java.util.Collection;
2121
import java.util.Collections;
22+
import java.util.Comparator;
2223
import java.util.LinkedHashMap;
2324
import java.util.LinkedHashSet;
2425
import java.util.Map;
2526
import java.util.Map.Entry;
27+
import java.util.NavigableMap;
2628
import java.util.Set;
29+
import java.util.TreeMap;
2730
import java.util.regex.Matcher;
2831
import java.util.regex.Pattern;
2932

3033
import org.springframework.lang.Nullable;
3134
import org.springframework.util.Assert;
3235
import org.springframework.util.StringUtils;
36+
import org.springframework.util.comparator.NullSafeComparator;
3337

3438
/**
3539
* Bucket is the data bag for Redis hash structures to be used with {@link RedisData}.
3640
*
3741
* @author Christoph Strobl
3842
* @author Mark Paluch
43+
* @author Stefan Berger
3944
* @since 1.7
4045
*/
4146
public class Bucket {
@@ -45,19 +50,22 @@ public class Bucket {
4550
*/
4651
public static final Charset CHARSET = StandardCharsets.UTF_8;
4752

48-
private final Map<String, byte[]> data;
53+
/**
54+
* The Redis data as {@link Map} sorted by the keys.
55+
*/
56+
private final NavigableMap<String, byte[]> data = new TreeMap<>(
57+
new NullSafeComparator<>(Comparator.<String> naturalOrder(), true));
4958

5059
/**
5160
* Creates new empty bucket
5261
*/
5362
public Bucket() {
54-
data = new LinkedHashMap<>();
63+
5564
}
5665

5766
Bucket(Map<String, byte[]> data) {
5867

5968
Assert.notNull(data, "Initial data must not be null!");
60-
this.data = new LinkedHashMap<>(data.size());
6169
this.data.putAll(data);
6270
}
6371

@@ -151,14 +159,7 @@ public Map<String, byte[]> asMap() {
151159
*/
152160
public Bucket extract(String prefix) {
153161

154-
Bucket partial = new Bucket();
155-
for (Map.Entry<String, byte[]> entry : data.entrySet()) {
156-
if (entry.getKey().startsWith(prefix)) {
157-
partial.put(entry.getKey(), entry.getValue());
158-
}
159-
}
160-
161-
return partial;
162+
return new Bucket(data.subMap(prefix, prefix + Character.MAX_VALUE));
162163
}
163164

164165
/**

0 commit comments

Comments
 (0)