AMQ-7055 - Optimization on SequenceSet to prevent iterating through the whole set when a value bigger than the last value is added

Signed-off-by: gtully <gary.tully@gmail.com>
This commit is contained in:
Alan Protasio 2018-09-19 12:20:59 -07:00 committed by gtully
parent 2f5e5efec5
commit 8f88dcda09
3 changed files with 42 additions and 0 deletions

View File

@ -38,6 +38,10 @@ public class Sequence extends LinkedNode<Sequence> {
return last + 1 == value;
}
public boolean isBiggerButNotAdjacentToLast(long value) {
return last + 1 < value;
}
public boolean isAdjacentToFirst(long value) {
return first - 1 == value;
}

View File

@ -114,6 +114,13 @@ public class SequenceSet extends LinkedNodeList<Sequence> implements Iterable<Lo
return true;
}
// check if the value is greater than the bigger sequence value and if it's not adjacent to it
// in this case, we are sure that the value should be add to the tail of the sequence.
if (sequence.isBiggerButNotAdjacentToLast(value)) {
addLast(new Sequence(value));
return true;
}
sequence = getHead();
while (sequence != null) {

View File

@ -85,6 +85,37 @@ public class SequenceSetTest {
assertFalse(set.contains(11));
}
@Test
public void testAddValuesToTail() {
SequenceSet set = new SequenceSet();
set.add(new Sequence(0, 10));
set.add(new Sequence(21, 42));
set.add(new Sequence(142, 512));
set.add(513);
for (int i = 600; i < 650; i++) {
set.add(i);
}
for (int i = 0; i < 10; i++) {
set.add(i * 100);
}
assertTrue(set.contains(0));
assertTrue(set.contains(25));
assertTrue(set.contains(513));
assertTrue(!set.contains(514));
assertFalse(set.contains(599));
assertTrue(set.contains(625));
assertFalse(set.contains(651));
for (int i = 0; i < 10; i++) {
assertTrue(set.contains(i * 100));
}
}
@Test
public void testRemove() {
SequenceSet set = new SequenceSet();