use of org.apache.commons.lang3.mutable.MutableLong in project neo4j by neo4j.
the class InternalTreeLogicTest method shouldMergeValueInRootLeaf.
@Test
public void shouldMergeValueInRootLeaf() throws Exception {
// GIVEN
initialize();
long key = 10;
long baseValue = 100;
insert(key, baseValue);
// WHEN
generationManager.checkpoint();
int toAdd = 5;
insert(key, toAdd, ADDER);
// THEN
goTo(readCursor, rootId);
int searchResult = KeySearch.search(readCursor, node, key(key), new MutableLong(), keyCount());
assertTrue(KeySearch.isHit(searchResult));
int pos = KeySearch.positionOf(searchResult);
assertEquals(0, pos);
assertEquals(key, keyAt(pos).longValue());
assertEquals(baseValue + toAdd, valueAt(pos).longValue());
}
use of org.apache.commons.lang3.mutable.MutableLong in project neo4j by neo4j.
the class GBPTreeIT method shouldStayCorrectAfterRandomModifications.
@Test
public void shouldStayCorrectAfterRandomModifications() throws Exception {
// GIVEN
GBPTree<MutableLong, MutableLong> index = createIndex(256);
Comparator<MutableLong> keyComparator = layout;
Map<MutableLong, MutableLong> data = new TreeMap<>(keyComparator);
int count = 100;
int totalNumberOfRounds = 10;
for (int i = 0; i < count; i++) {
data.put(randomKey(random.random()), randomKey(random.random()));
}
// WHEN
try (Writer<MutableLong, MutableLong> writer = index.writer()) {
for (Map.Entry<MutableLong, MutableLong> entry : data.entrySet()) {
writer.put(entry.getKey(), entry.getValue());
}
}
for (int round = 0; round < totalNumberOfRounds; round++) {
// THEN
for (int i = 0; i < count; i++) {
MutableLong first = randomKey(random.random());
MutableLong second = randomKey(random.random());
MutableLong from, to;
if (first.longValue() < second.longValue()) {
from = first;
to = second;
} else {
from = second;
to = first;
}
Map<MutableLong, MutableLong> expectedHits = expectedHits(data, from, to, keyComparator);
try (RawCursor<Hit<MutableLong, MutableLong>, IOException> result = index.seek(from, to)) {
while (result.next()) {
MutableLong key = result.get().key();
if (expectedHits.remove(key) == null) {
fail("Unexpected hit " + key + " when searching for " + from + " - " + to);
}
assertTrue(keyComparator.compare(key, from) >= 0);
assertTrue(keyComparator.compare(key, to) < 0);
}
if (!expectedHits.isEmpty()) {
fail("There were results which were expected to be returned, but weren't:" + expectedHits + " when searching range " + from + " - " + to);
}
}
}
index.checkpoint(IOLimiter.unlimited());
randomlyModifyIndex(index, data, random.random(), (double) round / totalNumberOfRounds);
}
}
use of org.apache.commons.lang3.mutable.MutableLong in project neo4j by neo4j.
the class GBPTreeIT method randomlyModifyIndex.
private static void randomlyModifyIndex(GBPTree<MutableLong, MutableLong> index, Map<MutableLong, MutableLong> data, Random random, double removeProbability) throws IOException {
int changeCount = random.nextInt(10) + 10;
try (Writer<MutableLong, MutableLong> writer = index.writer()) {
for (int i = 0; i < changeCount; i++) {
if (random.nextDouble() < removeProbability && data.size() > 0) {
// remove
MutableLong key = randomKey(data, random);
MutableLong value = data.remove(key);
MutableLong removedValue = writer.remove(key);
assertEquals("For " + key, value, removedValue);
} else {
// put
MutableLong key = randomKey(random);
MutableLong value = randomKey(random);
writer.put(key, value);
data.put(key, value);
}
}
}
}
use of org.apache.commons.lang3.mutable.MutableLong in project neo4j by neo4j.
the class KeySearchTest method shouldSearchAndFindOnRandomData.
@Test
public void shouldSearchAndFindOnRandomData() throws Exception {
// GIVEN a leaf node with random, although sorted (as of course it must be to binary-search), data
node.initializeLeaf(cursor, STABLE_GENERATION, UNSTABLE_GENERATION);
int internalMaxKeyCount = node.internalMaxKeyCount();
int half = internalMaxKeyCount / 2;
int keyCount = random.nextInt(half) + half;
long[] keys = new long[keyCount];
int currentKey = random.nextInt(10_000);
MutableLong key = layout.newKey();
for (int i = 0; i < keyCount; i++) {
keys[i] = currentKey;
key.setValue(currentKey);
node.insertKeyAt(cursor, key, i, i);
currentKey += random.nextInt(100) + 10;
}
node.setKeyCount(cursor, keyCount);
// WHEN searching for random keys within that general range
for (int i = 0; i < 1_000; i++) {
long searchKey = random.nextInt(currentKey + 10);
key.setValue(searchKey);
int searchResult = search(cursor, node, key, readKey, keyCount);
// THEN position should be as expected
boolean exists = contains(keys, searchKey);
int position = KeySearch.positionOf(searchResult);
assertEquals(exists, KeySearch.isHit(searchResult));
if (searchKey <= keys[0]) {
// Our search key was lower than any of our keys, expect 0
assertEquals(0, position);
} else {
// step backwards through our expected keys and see where it should fit, assert that fact
boolean found = false;
for (int j = keyCount - 1; j >= 0; j--) {
if (searchKey > keys[j]) {
assertEquals(j + 1, position);
found = true;
break;
}
}
assertTrue(found);
}
}
}
use of org.apache.commons.lang3.mutable.MutableLong in project neo4j by neo4j.
the class KeySearchTest method fullLeafWithUniqueKeys.
private void fullLeafWithUniqueKeys() {
// [2,4,8,16,32,64,128,512,1024,2048]
node.initializeLeaf(cursor, STABLE_GENERATION, UNSTABLE_GENERATION);
MutableLong key = layout.newKey();
for (int i = 0; i < KEY_COUNT; i++) {
key.setValue(key(i));
node.insertKeyAt(cursor, key, i, i);
}
node.setKeyCount(cursor, KEY_COUNT);
}
Aggregations