Search in sources :

Example 1 with IndexCursor

use of com.questdb.store.IndexCursor 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)

Example 2 with IndexCursor

use of com.questdb.store.IndexCursor in project questdb by bluestreak01.

the class KvIndexIntLambdaHeadRowSource method prepareCursor.

@Override
public RowCursor prepareCursor(PartitionSlice slice) {
    try {
        Partition partition = rec.partition = slice.partition.open();
        KVIndex index = partition.getIndexForColumn(columnIndex);
        FixedColumn col = partition.fixCol(columnIndex);
        long lo = slice.lo - 1;
        long hi = slice.calcHi ? partition.size() : slice.hi + 1;
        rows.clear();
        for (int i = 0, n = keys.size(); i < n; i++) {
            IndexCursor c = index.cursor(keys.get(i) & buckets);
            while (c.hasNext()) {
                long r = rec.rowid = c.next();
                if (r > lo && r < hi && col.getInt(r) == keys.get(i) && (filter == null || filter.getBool(rec))) {
                    rows.add(r);
                    break;
                }
            }
        }
        rows.sort();
        cursor = 0;
        return this;
    } catch (JournalException e) {
        throw new JournalRuntimeException(e);
    }
}
Also used : Partition(com.questdb.store.Partition) JournalException(com.questdb.std.ex.JournalException) IndexCursor(com.questdb.store.IndexCursor) KVIndex(com.questdb.store.KVIndex) FixedColumn(com.questdb.store.FixedColumn)

Example 3 with IndexCursor

use of com.questdb.store.IndexCursor in project questdb by bluestreak01.

the class KvIndexIntListHeadRowSource method prepareCursor.

@Override
public RowCursor prepareCursor(PartitionSlice slice) {
    try {
        Partition partition = rec.partition = slice.partition.open();
        KVIndex index = partition.getIndexForColumn(columnIndex);
        FixedColumn col = partition.fixCol(columnIndex);
        long lo = slice.lo - 1;
        long hi = slice.calcHi ? partition.size() : slice.hi + 1;
        rows.clear();
        for (int i = 0, n = values.size(); i < n; i++) {
            IndexCursor c = index.cursor(values.get(i) & buckets);
            long r = -1;
            boolean found = false;
            while (c.hasNext()) {
                r = rec.rowid = c.next();
                if (r > lo && r < hi && col.getInt(r) == values.get(i) && (filter == null || filter.getBool(rec))) {
                    found = true;
                    break;
                }
            }
            if (found) {
                rows.add(r);
            }
        }
        rows.sort();
        keyIndex = 0;
        return this;
    } catch (JournalException e) {
        throw new JournalRuntimeException(e);
    }
}
Also used : Partition(com.questdb.store.Partition) JournalException(com.questdb.std.ex.JournalException) IndexCursor(com.questdb.store.IndexCursor) JournalRuntimeException(com.questdb.common.JournalRuntimeException) KVIndex(com.questdb.store.KVIndex) FixedColumn(com.questdb.store.FixedColumn)

Example 4 with IndexCursor

use of com.questdb.store.IndexCursor in project questdb by bluestreak01.

the class KvIndexLongListHeadRowSource method prepareCursor.

@Override
public RowCursor prepareCursor(PartitionSlice slice) {
    try {
        Partition partition = rec.partition = slice.partition.open();
        KVIndex index = partition.getIndexForColumn(columnIndex);
        FixedColumn col = partition.fixCol(columnIndex);
        long lo = slice.lo - 1;
        long hi = slice.calcHi ? partition.size() : slice.hi + 1;
        rows.clear();
        for (int i = 0, n = values.size(); i < n; i++) {
            IndexCursor c = index.cursor((int) (values.get(i) & buckets));
            long r = -1;
            boolean found = false;
            while (c.hasNext()) {
                r = rec.rowid = c.next();
                if (r > lo && r < hi && col.getLong(r) == values.get(i) && (filter == null || filter.getBool(rec))) {
                    found = true;
                    break;
                }
            }
            if (found) {
                rows.add(r);
            }
        }
        rows.sort();
        keyIndex = 0;
        return this;
    } catch (JournalException e) {
        throw new JournalRuntimeException(e);
    }
}
Also used : Partition(com.questdb.store.Partition) JournalException(com.questdb.std.ex.JournalException) IndexCursor(com.questdb.store.IndexCursor) JournalRuntimeException(com.questdb.common.JournalRuntimeException) KVIndex(com.questdb.store.KVIndex) FixedColumn(com.questdb.store.FixedColumn)

Example 5 with IndexCursor

use of com.questdb.store.IndexCursor in project questdb by bluestreak01.

the class QueryAllResultSetBuilder method read.

@Override
public void read(long lo, long hi) {
    for (int i = 0, sz = symbolKeys.size(); i < sz; i++) {
        int symbolKey = symbolKeys.getQuick(i);
        if (index.contains(symbolKey)) {
            if (searchIndices.length > 0) {
                for (int k = 0; k < searchIndices.length; k++) {
                    if (searchIndices[k].contains(filterSymbolKeys.getQuick(k))) {
                        LongList searchLocalRowIDs = searchIndices[k].getValues(filterSymbolKeys.getQuick(k));
                        IndexCursor cursor = index.cursor(symbolKey);
                        while (cursor.hasNext()) {
                            long localRowID = cursor.next();
                            if (localRowID < lo) {
                                break;
                            }
                            if (localRowID <= hi && searchLocalRowIDs.binarySearch(localRowID) >= 0) {
                                result.add(Rows.toRowID(partition.getPartitionIndex(), localRowID));
                            }
                        }
                    }
                }
            } else {
                IndexCursor cursor = index.cursor(symbolKey);
                result.ensureCapacity((int) cursor.size());
                while (cursor.hasNext()) {
                    long localRowID = cursor.next();
                    if (localRowID >= lo && localRowID <= hi) {
                        result.add(Rows.toRowID(partition.getPartitionIndex(), localRowID));
                    }
                }
            }
        }
    }
}
Also used : IndexCursor(com.questdb.store.IndexCursor) LongList(com.questdb.std.LongList)

Aggregations

IndexCursor (com.questdb.store.IndexCursor)10 KVIndex (com.questdb.store.KVIndex)9 JournalException (com.questdb.std.ex.JournalException)8 Partition (com.questdb.store.Partition)8 JournalRuntimeException (com.questdb.common.JournalRuntimeException)5 FixedColumn (com.questdb.store.FixedColumn)3 VariableColumn (com.questdb.store.VariableColumn)2 IntLongPriorityQueue (com.questdb.std.IntLongPriorityQueue)1 LongList (com.questdb.std.LongList)1 Rnd (com.questdb.std.Rnd)1 AbstractTest (com.questdb.test.tools.AbstractTest)1 Test (org.junit.Test)1