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 BitSet range = new BitSet () ;
174
177
175
178
/**
176
179
* @param lowerBound must not be {@literal null}.
@@ -181,43 +184,65 @@ 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 (ClusterSlotHashUtil .SLOT_COUNT );
196
+ for (Integer pos : range ) {
197
+ this .range .set (pos );
198
+ }
199
+ }
200
+ }
201
+
202
+ public SlotRange (BitSet range ) {
203
+ this .range = (BitSet ) range .clone ();
192
204
}
193
205
194
206
@ Override
195
207
public String toString () {
196
- return range .toString ();
208
+ return Arrays .toString (this . getSlotsArray () );
197
209
}
198
210
199
211
/**
200
212
* @param slot
201
213
* @return true when slot is part of the range.
202
214
*/
203
215
public boolean contains (int slot ) {
204
- return range .contains (slot );
216
+ return range .get (slot );
205
217
}
206
218
207
219
/**
208
220
* @return
209
221
*/
210
222
public Set <Integer > getSlots () {
211
- return Collections .unmodifiableSet (range );
223
+ if (range .isEmpty ()) {
224
+ return Collections .emptySet ();
225
+ }
226
+ LinkedHashSet <Integer > slots = new LinkedHashSet <>(Math .max (2 * range .cardinality (), 11 ));
227
+ for (int i = 0 ; i < range .length (); i ++) {
228
+ if (range .get (i )) {
229
+ slots .add (i );
230
+ }
231
+ }
232
+ return Collections .unmodifiableSet (slots );
212
233
}
213
234
214
235
public int [] getSlotsArray () {
215
-
216
- int [] slots = new int [range .size ()];
236
+ if (range .isEmpty ()) {
237
+ return new int [0 ];
238
+ }
239
+ int [] slots = new int [range .cardinality ()];
217
240
int pos = 0 ;
218
241
219
- for (Integer value : range ) {
220
- slots [pos ++] = value .intValue ();
242
+ for (int i = 0 ; i < ClusterSlotHashUtil .SLOT_COUNT ; i ++) {
243
+ if (this .range .get (i )) {
244
+ slots [pos ++] = i ;
245
+ }
221
246
}
222
247
223
248
return slots ;
0 commit comments