Search in sources :

Example 1 with IntLongPriorityQueue

use of com.questdb.std.IntLongPriorityQueue in project questdb by bluestreak01.

the class IntLongPriorityQueueTest method testIndexSort.

@Test
public void testIndexSort() throws Exception {
    final int nStreams = 16;
    Rnd rnd = new Rnd();
    int totalLen = 0;
    try (KVIndex index = new KVIndex(indexFile, totalKeys, totalValues, 1, JournalMode.APPEND, 0, false)) {
        for (int i = 0; i < nStreams; i++) {
            long[] values = new long[rnd.nextPositiveInt() % 1000];
            totalLen += values.length;
            for (int j = 0; j < values.length; j++) {
                values[j] = rnd.nextPositiveLong() % 100;
            }
            Arrays.sort(values);
            for (int j = 0; j < values.length; j++) {
                index.add(i, values[j]);
            }
            index.commit();
        }
        long[] expected = new long[totalLen];
        int p = 0;
        for (int i = 0; i < nStreams; i++) {
            IndexCursor c = index.fwdCursor(i);
            while (c.hasNext()) {
                expected[p++] = c.next();
            }
        }
        Arrays.sort(expected);
        IntLongPriorityQueue heap = new IntLongPriorityQueue(nStreams);
        IndexCursor[] cursors = new IndexCursor[nStreams];
        for (int i = 0; i < nStreams; i++) {
            cursors[i] = index.newFwdCursor(i);
            if (cursors[i].hasNext()) {
                heap.add(i, cursors[i].next());
            }
        }
        p = 0;
        while (heap.hasNext()) {
            int idx = heap.popIndex();
            long v;
            if (cursors[idx].hasNext()) {
                v = heap.popAndReplace(idx, cursors[idx].next());
            } else {
                v = heap.popValue();
            }
            Assert.assertEquals(expected[p++], v);
        }
    }
}
Also used : IndexCursor(com.questdb.store.IndexCursor) Rnd(com.questdb.std.Rnd) IntLongPriorityQueue(com.questdb.std.IntLongPriorityQueue) KVIndex(com.questdb.store.KVIndex) AbstractTest(com.questdb.test.tools.AbstractTest) Test(org.junit.Test)

Aggregations

IntLongPriorityQueue (com.questdb.std.IntLongPriorityQueue)1 Rnd (com.questdb.std.Rnd)1 IndexCursor (com.questdb.store.IndexCursor)1 KVIndex (com.questdb.store.KVIndex)1 AbstractTest (com.questdb.test.tools.AbstractTest)1 Test (org.junit.Test)1