use of org.apache.commons.lang3.mutable.MutableLong in project neo4j by neo4j.
the class TreeNodeTest method shouldRemoveValue.
@Test
public void shouldRemoveValue() throws Exception {
// GIVEN
node.initializeLeaf(cursor, STABLE_GENERATION, UNSTABLE_GENERATION);
MutableLong value = layout.newKey();
long firstValue = 123456789;
value.setValue(firstValue);
node.insertValueAt(cursor, value, 0, 0);
long otherValue = 987654321;
value.setValue(otherValue);
node.insertValueAt(cursor, value, 1, 1);
long thirdValue = 49756;
value.setValue(thirdValue);
node.insertValueAt(cursor, value, 2, 2);
// WHEN
node.removeValueAt(cursor, 1, 3);
// THEN
assertEquals(firstValue, node.valueAt(cursor, value, 0).longValue());
assertEquals(thirdValue, node.valueAt(cursor, value, 1).longValue());
}
use of org.apache.commons.lang3.mutable.MutableLong in project neo4j by neo4j.
the class TreeNodeTest method shouldReadAndInsertValues.
@Test
public void shouldReadAndInsertValues() throws Exception {
// GIVEN
node.initializeLeaf(cursor, STABLE_GENERATION, UNSTABLE_GENERATION);
MutableLong value = layout.newKey();
value.setValue(1);
node.insertValueAt(cursor, value, 0, 0);
value.setValue(3);
node.insertValueAt(cursor, value, 1, 1);
// WHEN
value.setValue(2);
node.insertValueAt(cursor, value, 1, 2);
// THEN
assertEquals(1, node.valueAt(cursor, value, 0).longValue());
assertEquals(2, node.valueAt(cursor, value, 1).longValue());
assertEquals(3, node.valueAt(cursor, value, 2).longValue());
}
use of org.apache.commons.lang3.mutable.MutableLong in project neo4j by neo4j.
the class TreeNodeTest method shouldInsertAndRemoveRandomKeysAndValues.
@Test
public void shouldInsertAndRemoveRandomKeysAndValues() throws Exception {
// This test doesn't care about sorting, that's an aspect that lies outside of TreeNode, really
// GIVEN
node.initializeLeaf(cursor, STABLE_GENERATION, UNSTABLE_GENERATION);
int maxKeyCount = node.leafMaxKeyCount();
// add +1 to these to simplify some array logic in the test itself
long[] expectedKeys = new long[maxKeyCount + 1];
long[] expectedValues = new long[maxKeyCount + 1];
int expectedKeyCount = 0;
MutableLong key = layout.newKey();
MutableLong value = layout.newValue();
// WHEN/THEN
for (int i = 0; i < 1000; i++) {
if (random.nextFloat() < 0.7) {
// 70% insert
if (expectedKeyCount < maxKeyCount) {
// there's room
int position = expectedKeyCount == 0 ? 0 : random.nextInt(expectedKeyCount);
// ensure unique
do {
key.setValue(random.nextLong());
} while (contains(expectedKeys, 0, expectedKeyCount, key.longValue()));
node.insertKeyAt(cursor, key, position, expectedKeyCount);
insert(expectedKeys, expectedKeyCount, key.longValue(), position);
value.setValue(random.nextLong());
node.insertValueAt(cursor, value, position, expectedKeyCount);
insert(expectedValues, expectedKeyCount, value.longValue(), position);
node.setKeyCount(cursor, ++expectedKeyCount);
}
} else {
// 30% remove
if (expectedKeyCount > 0) {
// there are things to remove
int position = random.nextInt(expectedKeyCount);
node.keyAt(cursor, key, position);
node.removeKeyAt(cursor, position, expectedKeyCount);
long expectedKey = remove(expectedKeys, expectedKeyCount, position);
assertEquals(expectedKey, key.longValue());
node.valueAt(cursor, value, position);
node.removeValueAt(cursor, position, expectedKeyCount);
long expectedValue = remove(expectedValues, expectedKeyCount, position);
assertEquals(expectedValue, value.longValue());
node.setKeyCount(cursor, --expectedKeyCount);
}
}
}
// THEN
assertEquals(expectedKeyCount, node.keyCount(cursor));
for (int i = 0; i < expectedKeyCount; i++) {
long expectedKey = expectedKeys[i];
node.keyAt(cursor, key, i);
assertEquals(expectedKey, key.longValue());
long expectedValue = expectedValues[i];
node.valueAt(cursor, value, i);
assertEquals("For key " + key.longValue(), expectedValue, value.longValue());
}
}
use of org.apache.commons.lang3.mutable.MutableLong in project neo4j by neo4j.
the class ConsistencyCheckerTest method shouldThrowDescriptiveExceptionOnBrokenGSPP.
@Test
public void shouldThrowDescriptiveExceptionOnBrokenGSPP() throws Exception {
// GIVEN
int pageSize = 256;
PageCursor cursor = new PageAwareByteArrayCursor(pageSize);
Layout<MutableLong, MutableLong> layout = new SimpleLongLayout();
TreeNode<MutableLong, MutableLong> treeNode = new TreeNode<>(pageSize, layout);
long stableGeneration = MIN_GENERATION;
long crashGeneration = stableGeneration + 1;
long unstableGeneration = stableGeneration + 2;
String pointerFieldName = "abc";
long pointer = 123;
cursor.next(0);
treeNode.initializeInternal(cursor, stableGeneration, crashGeneration);
treeNode.setSuccessor(cursor, pointer, stableGeneration, crashGeneration);
// WHEN
try {
assertNoCrashOrBrokenPointerInGSPP(cursor, stableGeneration, unstableGeneration, pointerFieldName, TreeNode.BYTE_POS_SUCCESSOR, treeNode);
cursor.checkAndClearCursorException();
fail("Should have failed");
} catch (CursorException e) {
// THEN
assertThat(e.getMessage(), containsString(pointerFieldName));
assertThat(e.getMessage(), containsString(pointerFieldName));
assertThat(e.getMessage(), containsString("state=CRASH"));
assertThat(e.getMessage(), containsString("state=EMPTY"));
assertThat(e.getMessage(), containsString(String.valueOf(pointer)));
}
}
use of org.apache.commons.lang3.mutable.MutableLong in project neo4j by neo4j.
the class ConsistencyCheckerTest method shouldDetectUnusedPages.
@Test
public void shouldDetectUnusedPages() throws Exception {
// GIVEN
int pageSize = 256;
Layout<MutableLong, MutableLong> layout = new SimpleLongLayout();
TreeNode<MutableLong, MutableLong> node = new TreeNode<>(pageSize, layout);
long stableGeneration = GenerationSafePointer.MIN_GENERATION;
long unstableGeneration = stableGeneration + 1;
SimpleIdProvider idProvider = new SimpleIdProvider();
InternalTreeLogic<MutableLong, MutableLong> logic = new InternalTreeLogic<>(idProvider, node, layout);
PageCursor cursor = new PageAwareByteArrayCursor(pageSize);
cursor.next(idProvider.acquireNewId(stableGeneration, unstableGeneration));
node.initializeLeaf(cursor, stableGeneration, unstableGeneration);
logic.initialize(cursor);
StructurePropagation<MutableLong> structure = new StructurePropagation<>(layout.newKey(), layout.newKey(), layout.newKey());
MutableLong key = layout.newKey();
for (int g = 0, k = 0; g < 3; g++) {
for (int i = 0; i < 100; i++, k++) {
key.setValue(k);
logic.insert(cursor, structure, key, key, ValueMergers.overwrite(), stableGeneration, unstableGeneration);
if (structure.hasRightKeyInsert) {
goTo(cursor, "new root", idProvider.acquireNewId(stableGeneration, unstableGeneration));
node.initializeInternal(cursor, stableGeneration, unstableGeneration);
node.insertKeyAt(cursor, structure.rightKey, 0, 0);
node.setKeyCount(cursor, 1);
node.setChildAt(cursor, structure.midChild, 0, stableGeneration, unstableGeneration);
node.setChildAt(cursor, structure.rightChild, 1, stableGeneration, unstableGeneration);
logic.initialize(cursor);
}
if (structure.hasMidChildUpdate) {
logic.initialize(cursor);
}
structure.clear();
}
stableGeneration = unstableGeneration;
unstableGeneration++;
}
// WHEN
ConsistencyChecker<MutableLong> cc = new ConsistencyChecker<>(node, layout, stableGeneration, unstableGeneration);
try {
cc.checkSpace(cursor, idProvider.lastId(), PrimitiveLongCollections.emptyIterator());
fail("Should have failed");
} catch (RuntimeException e) {
// THEN good
assertThat(e.getMessage(), containsString("unused pages"));
}
}
Aggregations