mirror of https://github.com/apache/activemq.git
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>
(cherry picked from commit 8f88dcda09
)
This commit is contained in:
parent
77784061c5
commit
d2572ceaee
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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) {
|
||||
|
||||
|
|
|
@ -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();
|
||||
|
|
Loading…
Reference in New Issue