Elffuss BraidHub

kikocis/java-driver public

Handle ranges that wrap the ring in TokenRange.intersectWith.
Changeseta9ff4e181f5250793cf96db258ca6d5c2ab43897a17ed65bf05db88e88085079
AuthorAndrew Tolbert <andrew.tolbert@datastax.com> · 2015-03-06 15:45 -06:00
CommitterAndrew Tolbert <andrew.tolbert@datastax.com> · 2015-03-06 15:46 -06:00
Parents398dffe73a15

Browse the files at this changeset

2 files changed

modified driver-core/src/main/java/com/datastax/driver/core/TokenRange.java +34 −6
@@ -16,10 +16,12 @@
1616 package com.datastax.driver.core;
1717
1818 import java.util.ArrayList;
19+import java.util.Iterator;
1920 import java.util.List;
2021
2122 import com.google.common.base.Objects;
2223 import com.google.common.collect.ImmutableList;
24+import com.google.common.collect.Lists;
2325
2426 /**
2527 * A range of tokens on the Cassandra ring.
@@ -166,21 +168,47 @@
166168 /**
167169 * Computes the intersection of this range with another one.
168170 * <p>
171+ * If either of these ranges overlap the the ring, they are unwrapped and the unwrapped
172+ * tokens are compared with one another.
173+ * <p>
169174 * This call will fail if the two ranges do not intersect, you must check by calling
170175 * {@link #intersects(TokenRange)} beforehand.
171176 *
172177 * @param that the other range.
173- * @return the range resulting from the intersection.
178+ * @return the range(s) resulting from the intersection.
174179 * @throws IllegalArgumentException if the ranges do not intersect.
175180 */
176- public TokenRange intersectWith(TokenRange that) {
181+ public List<TokenRange> intersectWith(TokenRange that) {
177182 if (!this.intersects(that))
178183 throw new IllegalArgumentException("The two ranges do not intersect, use intersects() before calling this method");
179184
180- return new TokenRange(
181- (this.contains(that.start, true)) ? that.start : this.start,
182- (this.contains(that.end, false)) ? that.end : this.end,
183- factory);
185+ List<TokenRange> intersected = Lists.newArrayList();
186+
187+ // Compare the unwrapped ranges to one another.
188+ List<TokenRange> unwrappedForThis = this.unwrap();
189+ List<TokenRange> unwrappedForThat = that.unwrap();
190+ for(TokenRange t1 : unwrappedForThis) {
191+ for(TokenRange t2 : unwrappedForThat) {
192+ if(t1.intersects(t2)) {
193+ intersected.add(new TokenRange(
194+ (t1.contains(t2.start, true)) ? t2.start : t1.start,
195+ (t1.contains(t2.end, false)) ? t2.end : t1.end,
196+ factory));
197+ }
198+ }
199+ }
200+
201+ // If two intersecting ranges were produced, merge them if they are adjacent.
202+ // This could happen in the case that two wrapped ranges intersected.
203+ if(intersected.size() == 2) {
204+ TokenRange t1 = intersected.get(0);
205+ TokenRange t2 = intersected.get(1);
206+ if (t1.end.equals(t2.start) || t2.end.equals(t1.start)) {
207+ return ImmutableList.of(t1.mergeWith(t2));
208+ }
209+ }
210+
211+ return intersected;
184212 }
185213
186214 // isStart handles the case where the token is the start of another range, for example:
modified driver-core/src/test/java/com/datastax/driver/core/TokenRangeTest.java +21 −5
@@ -2,6 +2,7 @@
22
33 import java.util.List;
44
5+import com.google.common.collect.ImmutableList;
56 import org.testng.annotations.Test;
67
78 import static org.testng.Assert.fail;
@@ -76,15 +77,30 @@
7677 @Test(groups = "unit")
7778 public void should_compute_intersection() {
7879 assertThat(tokenRange(3, 9).intersectWith(tokenRange(2, 4)))
79- .isEqualTo(tokenRange(3, 4));
80+ .isEqualTo(ImmutableList.of(tokenRange(3, 4)));
8081 assertThat(tokenRange(3, 9).intersectWith(tokenRange(3, 5)))
81- .isEqualTo(tokenRange(3, 5));
82+ .isEqualTo(ImmutableList.of(tokenRange(3, 5)));
8283 assertThat(tokenRange(3, 9).intersectWith(tokenRange(4, 6)))
83- .isEqualTo(tokenRange(4, 6));
84+ .isEqualTo(ImmutableList.of(tokenRange(4, 6)));
8485 assertThat(tokenRange(3, 9).intersectWith(tokenRange(7, 9)))
85- .isEqualTo(tokenRange(7, 9));
86+ .isEqualTo(ImmutableList.of(tokenRange(7, 9)));
8687 assertThat(tokenRange(3, 9).intersectWith(tokenRange(8, 10)))
87- .isEqualTo(tokenRange(8, 9));
88+ .isEqualTo(ImmutableList.of(tokenRange(8, 9)));
89+ }
90+
91+ @Test(groups = "unit")
92+ public void should_could_intersection_with_ranges_around_ring() {
93+ // If a range wraps the ring like 10, -10 does this will produce two separate
94+ // intersected ranges.
95+ assertThat(tokenRange(10, -10).intersectWith(tokenRange(-20, 20)))
96+ .isEqualTo(ImmutableList.of(tokenRange(10, 20), tokenRange(-20, -10)));
97+ assertThat(tokenRange(-20, 20).intersectWith(tokenRange(10, -10)))
98+ .isEqualTo(ImmutableList.of(tokenRange(10, 20), tokenRange(-20, -10)));
99+
100+ // If both ranges wrap the ring, they should be merged together wrapping across
101+ // the range.
102+ assertThat(tokenRange(10, -30).intersectWith(tokenRange(20, -20)))
103+ .isEqualTo(ImmutableList.of(tokenRange(20, -30)));
88104 }
89105
90106 @Test(groups = "unit", expectedExceptions = IllegalArgumentException.class)