Search in sources :

Example 16 with MutableLong

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

Example 17 with MutableLong

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

Example 18 with MutableLong

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

Example 19 with MutableLong

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

Example 20 with MutableLong

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

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