Search in sources :

Example 1 with RedBlackTree

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

the class RedBlackTreeTest method testNonUnique.

@Test
public void testNonUnique() {
    try (RedBlackTree set = new RedBlackTree(new RedBlackTree.LongComparator() {

        private long left;

        @Override
        public int compare(long y) {
            return Long.compare(left, y);
        }

        @Override
        public void setLeft(long left) {
            this.left = left;
        }
    }, 1024)) {
        set.add(200);
        set.add(200);
        set.add(100);
        set.add(100);
        RedBlackTree.LongIterator iterator = set.iterator();
        StringBuilder b = new StringBuilder();
        while (iterator.hasNext()) {
            b.append(iterator.next()).append(',');
        }
        TestUtils.assertEquals("100,100,200,200,", b);
    }
}
Also used : RedBlackTree(com.questdb.std.RedBlackTree) Test(org.junit.Test)

Example 2 with RedBlackTree

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

the class RedBlackTreeTest method testAddAndGet.

@Test
public void testAddAndGet() {
    Rnd rnd = new Rnd();
    TreeSet<Long> control = new TreeSet<>();
    try (RedBlackTree tree = new RedBlackTree(new RedBlackTree.LongComparator() {

        private long left;

        @Override
        public int compare(long y) {
            return Long.compare(left, y);
        }

        @Override
        public void setLeft(long left) {
            this.left = left;
        }
    }, 1024)) {
        long l;
        for (int i = 0; i < 10000; i++) {
            tree.add(l = rnd.nextLong());
            control.add(l);
        }
        Iterator<Long> controlIterator = control.iterator();
        RedBlackTree.LongIterator iterator = tree.iterator();
        while (iterator.hasNext()) {
            Assert.assertTrue(controlIterator.hasNext());
            Assert.assertEquals(controlIterator.next().longValue(), iterator.next());
        }
        tree.clear();
        Assert.assertFalse(tree.iterator().hasNext());
    }
}
Also used : RedBlackTree(com.questdb.std.RedBlackTree) TreeSet(java.util.TreeSet) Rnd(com.questdb.std.Rnd) Test(org.junit.Test)

Example 3 with RedBlackTree

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

the class CachedAnalyticRecordSource 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 = recordSource.prepareCursor(factory, cancellationHandler);
    try {
        this.storageFacade.prepare(cursor.getStorageFacade());
        // 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();
            rowid = recordList.append(record, rowid);
            if (orderGroupCount > 0) {
                for (int i = 0; i < orderGroupCount; i++) {
                    RedBlackTree tree = orderedSources.getQuick(i);
                    if (tree != null) {
                        tree.add(rowid);
                    }
                }
            }
        }
        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 = recordList.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(recordList.next());
                        }
                    }
                }
            }
        }
    } finally {
        cursor.releaseCursor();
    }
    recordList.toTop();
    setCursorAndPrepareFunctions();
    return this;
}
Also used : RedBlackTree(com.questdb.std.RedBlackTree) RecordCursor(com.questdb.common.RecordCursor) Record(com.questdb.common.Record)

Example 4 with RedBlackTree

use of com.questdb.std.RedBlackTree 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

RedBlackTree (com.questdb.std.RedBlackTree)4 Record (com.questdb.common.Record)2 RecordCursor (com.questdb.common.RecordCursor)2 Test (org.junit.Test)2 FakeRecord (com.questdb.ql.join.hash.FakeRecord)1 Rnd (com.questdb.std.Rnd)1 TreeSet (java.util.TreeSet)1