kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | a9ff4e181f5250793cf96db258ca6d5c2ab43897a17ed65bf05db88e88085079 |
|---|---|
| Author | Andrew Tolbert <andrew.tolbert@datastax.com> · 2015-03-06 15:45 -06:00 |
| Committer | Andrew Tolbert <andrew.tolbert@datastax.com> · 2015-03-06 15:46 -06:00 |
| Parents | 398dffe73a15 |
2 files changed
modified driver-core/src/main/java/com/datastax/driver/core/TokenRange.java +34 −6
| @@ -16,10 +16,12 @@ | ||
| 16 | 16 | package com.datastax.driver.core; |
| 17 | 17 | |
| 18 | 18 | import java.util.ArrayList; |
| 19 | +import java.util.Iterator; | |
| 19 | 20 | import java.util.List; |
| 20 | 21 | |
| 21 | 22 | import com.google.common.base.Objects; |
| 22 | 23 | import com.google.common.collect.ImmutableList; |
| 24 | +import com.google.common.collect.Lists; | |
| 23 | 25 | |
| 24 | 26 | /** |
| 25 | 27 | * A range of tokens on the Cassandra ring. |
| @@ -166,21 +168,47 @@ | ||
| 166 | 168 | /** |
| 167 | 169 | * Computes the intersection of this range with another one. |
| 168 | 170 | * <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> | |
| 169 | 174 | * This call will fail if the two ranges do not intersect, you must check by calling |
| 170 | 175 | * {@link #intersects(TokenRange)} beforehand. |
| 171 | 176 | * |
| 172 | 177 | * @param that the other range. |
| 173 | - * @return the range resulting from the intersection. | |
| 178 | + * @return the range(s) resulting from the intersection. | |
| 174 | 179 | * @throws IllegalArgumentException if the ranges do not intersect. |
| 175 | 180 | */ |
| 176 | - public TokenRange intersectWith(TokenRange that) { | |
| 181 | + public List<TokenRange> intersectWith(TokenRange that) { | |
| 177 | 182 | if (!this.intersects(that)) |
| 178 | 183 | throw new IllegalArgumentException("The two ranges do not intersect, use intersects() before calling this method"); |
| 179 | 184 | |
| 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; | |
| 184 | 212 | } |
| 185 | 213 | |
| 186 | 214 | // 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 @@ | ||
| 2 | 2 | |
| 3 | 3 | import java.util.List; |
| 4 | 4 | |
| 5 | +import com.google.common.collect.ImmutableList; | |
| 5 | 6 | import org.testng.annotations.Test; |
| 6 | 7 | |
| 7 | 8 | import static org.testng.Assert.fail; |
| @@ -76,15 +77,30 @@ | ||
| 76 | 77 | @Test(groups = "unit") |
| 77 | 78 | public void should_compute_intersection() { |
| 78 | 79 | assertThat(tokenRange(3, 9).intersectWith(tokenRange(2, 4))) |
| 79 | - .isEqualTo(tokenRange(3, 4)); | |
| 80 | + .isEqualTo(ImmutableList.of(tokenRange(3, 4))); | |
| 80 | 81 | assertThat(tokenRange(3, 9).intersectWith(tokenRange(3, 5))) |
| 81 | - .isEqualTo(tokenRange(3, 5)); | |
| 82 | + .isEqualTo(ImmutableList.of(tokenRange(3, 5))); | |
| 82 | 83 | assertThat(tokenRange(3, 9).intersectWith(tokenRange(4, 6))) |
| 83 | - .isEqualTo(tokenRange(4, 6)); | |
| 84 | + .isEqualTo(ImmutableList.of(tokenRange(4, 6))); | |
| 84 | 85 | assertThat(tokenRange(3, 9).intersectWith(tokenRange(7, 9))) |
| 85 | - .isEqualTo(tokenRange(7, 9)); | |
| 86 | + .isEqualTo(ImmutableList.of(tokenRange(7, 9))); | |
| 86 | 87 | 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))); | |
| 88 | 104 | } |
| 89 | 105 | |
| 90 | 106 | @Test(groups = "unit", expectedExceptions = IllegalArgumentException.class) |