Search in sources :

Example 6 with MutableLong

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());
}
Also used : MutableLong(org.apache.commons.lang3.mutable.MutableLong) Test(org.junit.Test)

Example 7 with MutableLong

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());
}
Also used : MutableLong(org.apache.commons.lang3.mutable.MutableLong) Test(org.junit.Test)

Example 8 with MutableLong

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());
    }
}
Also used : MutableLong(org.apache.commons.lang3.mutable.MutableLong) Test(org.junit.Test)

Example 9 with MutableLong

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)));
    }
}
Also used : MutableLong(org.apache.commons.lang3.mutable.MutableLong) CursorException(org.neo4j.io.pagecache.CursorException) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) PageCursor(org.neo4j.io.pagecache.PageCursor) Test(org.junit.Test)

Example 10 with MutableLong

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"));
    }
}
Also used : PageCursor(org.neo4j.io.pagecache.PageCursor) MutableLong(org.apache.commons.lang3.mutable.MutableLong) Test(org.junit.Test)

Aggregations

MutableLong (org.apache.commons.lang3.mutable.MutableLong)66 Test (org.junit.Test)45 IOException (java.io.IOException)11 Query (org.apache.apex.malhar.lib.appdata.schemas.Query)6 CollectorTestSink (org.apache.apex.malhar.lib.testbench.CollectorTestSink)5 PageCache (org.neo4j.io.pagecache.PageCache)4 File (java.io.File)3 Map (java.util.Map)3 KeyedWindowedOperatorImpl (org.apache.apex.malhar.lib.window.impl.KeyedWindowedOperatorImpl)3 MutableBoolean (org.apache.commons.lang3.mutable.MutableBoolean)3 ParseException (java.text.ParseException)2 ArrayList (java.util.ArrayList)2 TreeMap (java.util.TreeMap)2 ConsoleOutputOperator (org.apache.apex.malhar.lib.io.ConsoleOutputOperator)2 SpillableComplexComponentImpl (org.apache.apex.malhar.lib.state.spillable.SpillableComplexComponentImpl)2 Tuple (org.apache.apex.malhar.lib.window.Tuple)2 WindowOption (org.apache.apex.malhar.lib.window.WindowOption)2 WindowState (org.apache.apex.malhar.lib.window.WindowState)2 SumLong (org.apache.apex.malhar.lib.window.accumulation.SumLong)2 WindowedOperatorImpl (org.apache.apex.malhar.lib.window.impl.WindowedOperatorImpl)2