Search in sources :

Example 26 with RecordCursor

use of com.questdb.common.RecordCursor in project questdb by bluestreak01.

the class CachedRowAnalyticRecordSource method prepareCursor.

@Override
public RecordCursor prepareCursor(ReaderFactory factory, CancellationHandler cancellationHandler) {
    recordList.clear();
    for (int i = 0; i < orderGroupCount; i++) {
        RedBlackTree tree = orderedSources.getQuick(i);
        if (tree != null) {
            tree.clear();
        }
    }
    for (int i = 0, n = functions.size(); i < n; i++) {
        functions.getQuick(i).reset();
    }
    final RecordCursor cursor = delegate.prepareCursor(factory, cancellationHandler);
    this.parentCursor = cursor;
    this.storageFacade.prepare(cursor.getStorageFacade());
    // red&black trees, one for each comparator where comparator is not null
    for (int i = 0; i < orderGroupCount; i++) {
        RedBlackTree tree = orderedSources.getQuick(i);
        if (tree != null) {
            ((MyComparator) tree.getComparator()).setCursor(cursor);
        }
    }
    // step #1: store source cursor in record list
    // - add record list' row ids to all trees, which will put these row ids in necessary order
    // for this we will be using out comparator, which helps tree compare long values
    // based on record these values are addressing
    long rowid = -1;
    while (cursor.hasNext()) {
        cancellationHandler.check();
        Record record = cursor.next();
        long row = record.getRowId();
        rowid = recordList.append(fakeRecord.of(row), rowid);
        if (orderGroupCount > 0) {
            for (int i = 0; i < orderGroupCount; i++) {
                RedBlackTree tree = orderedSources.getQuick(i);
                if (tree != null) {
                    tree.add(row);
                }
            }
        }
    }
    for (int i = 0; i < orderGroupCount; i++) {
        RedBlackTree tree = orderedSources.getQuick(i);
        ObjList<AnalyticFunction> functions = functionGroups.getQuick(i);
        if (tree != null) {
            // step #2: populate all analytic functions with records in order of respective tree
            RedBlackTree.LongIterator iterator = tree.iterator();
            while (iterator.hasNext()) {
                cancellationHandler.check();
                Record record = cursor.recordAt(iterator.next());
                for (int j = 0, n = functions.size(); j < n; j++) {
                    functions.getQuick(j).add(record);
                }
            }
        } else {
            // step #2: alternatively run record list through two-pass functions
            for (int j = 0, n = functions.size(); j < n; j++) {
                AnalyticFunction f = functions.getQuick(j);
                if (f.getType() != AnalyticFunction.STREAM) {
                    recordList.toTop();
                    while (recordList.hasNext()) {
                        f.add(cursor.recordAt(recordList.next().getLong(0)));
                    }
                }
            }
        }
    }
    recordList.toTop();
    for (int i = 0, n = functions.size(); i < n; i++) {
        functions.getQuick(i).prepare(cursor);
    }
    return this;
}
Also used : RedBlackTree(com.questdb.std.RedBlackTree) RecordCursor(com.questdb.common.RecordCursor) Record(com.questdb.common.Record) FakeRecord(com.questdb.ql.join.hash.FakeRecord)

Aggregations

RecordCursor (com.questdb.common.RecordCursor)26 Record (com.questdb.common.Record)19 Test (org.junit.Test)14 RecordSource (com.questdb.ql.RecordSource)11 AbstractTest (com.questdb.test.tools.AbstractTest)7 JournalWriter (com.questdb.store.JournalWriter)6 JournalEntryWriter (com.questdb.store.JournalEntryWriter)5 RecordSourcePrinter (com.questdb.ql.RecordSourcePrinter)3 Rnd (com.questdb.std.Rnd)3 StringSink (com.questdb.std.str.StringSink)3 EntryLockedException (com.questdb.cairo.pool.ex.EntryLockedException)2 EntryUnavailableException (com.questdb.cairo.pool.ex.EntryUnavailableException)2 PoolClosedException (com.questdb.cairo.pool.ex.PoolClosedException)2 SymbolTable (com.questdb.common.SymbolTable)2 Quote (com.questdb.model.Quote)2 AbstractOptimiserTest (com.questdb.parser.sql.AbstractOptimiserTest)2 HeapMergingRowSource (com.questdb.ql.latest.HeapMergingRowSource)2 KvIndexSymLookupRowSource (com.questdb.ql.latest.KvIndexSymLookupRowSource)2 MergingRowSource (com.questdb.ql.latest.MergingRowSource)2 RedBlackTree (com.questdb.std.RedBlackTree)2