19
19
import java .nio .charset .StandardCharsets ;
20
20
import java .util .Collection ;
21
21
import java .util .Collections ;
22
+ import java .util .Comparator ;
22
23
import java .util .LinkedHashMap ;
23
24
import java .util .LinkedHashSet ;
24
25
import java .util .Map ;
25
26
import java .util .Map .Entry ;
27
+ import java .util .NavigableMap ;
26
28
import java .util .Set ;
29
+ import java .util .TreeMap ;
27
30
import java .util .regex .Matcher ;
28
31
import java .util .regex .Pattern ;
29
32
30
33
import org .springframework .lang .Nullable ;
31
34
import org .springframework .util .Assert ;
32
35
import org .springframework .util .StringUtils ;
36
+ import org .springframework .util .comparator .NullSafeComparator ;
33
37
34
38
/**
35
39
* Bucket is the data bag for Redis hash structures to be used with {@link RedisData}.
36
40
*
37
41
* @author Christoph Strobl
38
42
* @author Mark Paluch
43
+ * @author Stefan Berger
39
44
* @since 1.7
40
45
*/
41
46
public class Bucket {
@@ -45,19 +50,22 @@ public class Bucket {
45
50
*/
46
51
public static final Charset CHARSET = StandardCharsets .UTF_8 ;
47
52
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 ));
49
58
50
59
/**
51
60
* Creates new empty bucket
52
61
*/
53
62
public Bucket () {
54
- data = new LinkedHashMap <>();
63
+
55
64
}
56
65
57
66
Bucket (Map <String , byte []> data ) {
58
67
59
68
Assert .notNull (data , "Initial data must not be null!" );
60
- this .data = new LinkedHashMap <>(data .size ());
61
69
this .data .putAll (data );
62
70
}
63
71
@@ -151,14 +159,7 @@ public Map<String, byte[]> asMap() {
151
159
*/
152
160
public Bucket extract (String prefix ) {
153
161
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 ));
162
163
}
163
164
164
165
/**
0 commit comments