15
15
*/
16
16
package org .springframework .data .redis .connection ;
17
17
18
+ import java .util .Arrays ;
19
+ import java .util .BitSet ;
18
20
import java .util .Collection ;
19
21
import java .util .Collections ;
20
22
import java .util .LinkedHashSet ;
@@ -166,11 +168,12 @@ public static RedisClusterNodeBuilder newRedisClusterNode() {
166
168
167
169
/**
168
170
* @author Christoph Strobl
171
+ * @author daihuabin
169
172
* @since 1.7
170
173
*/
171
174
public static class SlotRange {
172
175
173
- private final Set < Integer > range ;
176
+ private final BitSet range ;
174
177
175
178
/**
176
179
* @param lowerBound must not be {@literal null}.
@@ -181,43 +184,67 @@ public SlotRange(Integer lowerBound, Integer upperBound) {
181
184
Assert .notNull (lowerBound , "LowerBound must not be null" );
182
185
Assert .notNull (upperBound , "UpperBound must not be null" );
183
186
184
- this .range = new LinkedHashSet <>( );
187
+ this .range = new BitSet ( upperBound + 1 );
185
188
for (int i = lowerBound ; i <= upperBound ; i ++) {
186
- this .range .add (i );
189
+ this .range .set (i );
187
190
}
188
191
}
189
192
190
193
public SlotRange (Collection <Integer > range ) {
191
- this .range = CollectionUtils .isEmpty (range ) ? Collections .emptySet () : new LinkedHashSet <>(range );
194
+ if (CollectionUtils .isEmpty (range )) {
195
+ this .range = new BitSet (0 );
196
+ } else {
197
+ this .range = new BitSet (ClusterSlotHashUtil .SLOT_COUNT );
198
+ for (Integer pos : range ) {
199
+ this .range .set (pos );
200
+ }
201
+ }
202
+ }
203
+
204
+ public SlotRange (BitSet range ) {
205
+ this .range = (BitSet ) range .clone ();
192
206
}
193
207
194
208
@ Override
195
209
public String toString () {
196
- return range .toString ();
210
+ return Arrays .toString (this . getSlotsArray () );
197
211
}
198
212
199
213
/**
200
214
* @param slot
201
215
* @return true when slot is part of the range.
202
216
*/
203
217
public boolean contains (int slot ) {
204
- return range .contains (slot );
218
+ return range .get (slot );
205
219
}
206
220
207
221
/**
208
222
* @return
209
223
*/
210
224
public Set <Integer > getSlots () {
211
- return Collections .unmodifiableSet (range );
225
+ if (range .isEmpty ()) {
226
+ return Collections .emptySet ();
227
+ }
228
+ LinkedHashSet <Integer > slots = new LinkedHashSet <>(Math .max (2 * range .cardinality (), 11 ));
229
+ for (int i = 0 ; i < range .length (); i ++) {
230
+ if (range .get (i )) {
231
+ slots .add (i );
232
+ }
233
+ }
234
+ return Collections .unmodifiableSet (slots );
212
235
}
213
236
214
237
public int [] getSlotsArray () {
215
-
216
- int [] slots = new int [range .size ()];
238
+ if (range .isEmpty ()) {
239
+ return new int [0 ];
240
+ }
241
+ int [] slots = new int [range .cardinality ()];
217
242
int pos = 0 ;
218
243
219
- for (Integer value : range ) {
220
- slots [pos ++] = value .intValue ();
244
+ for (int i = 0 ; i < ClusterSlotHashUtil .SLOT_COUNT ; i ++) {
245
+ if (this .range .get (i )) {
246
+ slots [pos ++] = i ;
247
+ }
221
248
}
222
249
223
250
return slots ;
0 commit comments