Search in sources :

Example 6 with RowCursor

use of io.questdb.cairo.sql.RowCursor in project questdb by bluestreak01.

the class BitmapIndexTest method assertForwardCursorLimit.

private void assertForwardCursorLimit(BitmapIndexFwdReader reader, int min, int N, LongList tmp, int nExpectedResults) {
    tmp.clear();
    RowCursor cursor = reader.getCursor(true, 0, min, N - 1);
    while (cursor.hasNext()) {
        tmp.add(cursor.next());
    }
    Assert.assertEquals(nExpectedResults, tmp.size());
    int len = 0;
    for (int i = min; i < N; i++) {
        if (i % 3 == 0) {
            continue;
        }
        Assert.assertEquals(i, tmp.getQuick(len++));
        Assert.assertEquals(i, tmp.getQuick(len++));
        Assert.assertEquals(i, tmp.getQuick(len++));
    }
}
Also used : RowCursor(io.questdb.cairo.sql.RowCursor)

Example 7 with RowCursor

use of io.questdb.cairo.sql.RowCursor in project questdb by bluestreak01.

the class BitmapIndexTest method assertBackwardCursorLimit.

private void assertBackwardCursorLimit(BitmapIndexBwdReader reader, long max, LongList tmp) {
    tmp.clear();
    RowCursor cursor = reader.getCursor(true, 0, 0, max);
    while (cursor.hasNext()) {
        tmp.add(cursor.next());
    }
    int len = tmp.size();
    for (int i = 0; i < max; i++) {
        if (i % 3 == 0) {
            continue;
        }
        Assert.assertEquals(i, tmp.getQuick(--len));
        Assert.assertEquals(i, tmp.getQuick(--len));
        Assert.assertEquals(i, tmp.getQuick(--len));
    }
}
Also used : RowCursor(io.questdb.cairo.sql.RowCursor)

Example 8 with RowCursor

use of io.questdb.cairo.sql.RowCursor in project questdb by bluestreak01.

the class BitmapIndexTest method testEmptyCursor.

@Test
public void testEmptyCursor() {
    RowCursor cursor = new EmptyRowCursor();
    Assert.assertFalse(cursor.hasNext());
    Assert.assertEquals(0, cursor.next());
}
Also used : RowCursor(io.questdb.cairo.sql.RowCursor) Test(org.junit.Test)

Example 9 with RowCursor

use of io.questdb.cairo.sql.RowCursor in project questdb by bluestreak01.

the class BitmapIndexTest method testForwardReaderDoesNotUnmapPages.

@Test
public void testForwardReaderDoesNotUnmapPages() throws Exception {
    TestUtils.assertMemoryLeak(() -> {
        Rnd rnd = new Rnd();
        create(configuration, path.trimTo(plen), "x", 1024);
        class CountingFacade extends FilesFacadeImpl {

            private int count = 0;

            @Override
            public long getMapPageSize() {
                return 1024 * 1024;
            }

            @Override
            public void munmap(long address, long size, int memoryTag) {
                super.munmap(address, size, memoryTag);
                count++;
            }
        }
        final CountingFacade facade = new CountingFacade();
        CairoConfiguration configuration = new DefaultCairoConfiguration(root) {

            @Override
            public FilesFacade getFilesFacade() {
                return facade;
            }
        };
        try (BitmapIndexWriter writer = new BitmapIndexWriter(configuration, path.trimTo(plen), "x")) {
            try (BitmapIndexFwdReader reader = new BitmapIndexFwdReader(configuration, path.trimTo(plen), "x", 0)) {
                for (int i = 0; i < 100000; i++) {
                    int key = rnd.nextPositiveInt() % 1024;
                    long value = rnd.nextPositiveLong();
                    writer.add(key, value);
                    RowCursor cursor = reader.getCursor(true, key, 0, Long.MAX_VALUE);
                    boolean found = false;
                    while (cursor.hasNext()) {
                        if (value == cursor.next()) {
                            found = true;
                            break;
                        }
                    }
                    Assert.assertTrue(found);
                }
                Assert.assertEquals(0, facade.count);
            }
        }
    });
}
Also used : RowCursor(io.questdb.cairo.sql.RowCursor) Test(org.junit.Test)

Example 10 with RowCursor

use of io.questdb.cairo.sql.RowCursor in project questdb by bluestreak01.

the class BitmapIndexTest method testConcurrentForwardRW.

private void testConcurrentForwardRW(int N, int maxKeys) throws Exception {
    TestUtils.assertMemoryLeak(() -> {
        Rnd rnd = new Rnd();
        IntList keys = new IntList();
        IntObjHashMap<LongList> lists = new IntObjHashMap<>();
        // populate model for both reader and writer
        for (int i = 0; i < N; i++) {
            int key = rnd.nextPositiveInt() % maxKeys;
            LongList list = lists.get(key);
            if (list == null) {
                lists.put(key, list = new LongList());
                keys.add(key);
            }
            list.add(i);
        }
        final int threadCount = 3;
        CountDownLatch stopLatch = new CountDownLatch(threadCount);
        CyclicBarrier startBarrier = new CyclicBarrier(threadCount);
        AtomicInteger errors = new AtomicInteger();
        // create empty index
        create(configuration, path.trimTo(plen), "x", 1024);
        new Thread(() -> {
            try {
                startBarrier.await();
                try (Path path = new Path().of(configuration.getRoot())) {
                    try (BitmapIndexWriter writer = new BitmapIndexWriter(configuration, path, "x")) {
                        int pass = 0;
                        while (true) {
                            boolean added = false;
                            for (int i = 0, n = keys.size(); i < n; i++) {
                                int key = keys.getQuick(i);
                                LongList values = lists.get(key);
                                if (pass < values.size()) {
                                    writer.add(key, values.getQuick(pass));
                                    added = true;
                                }
                            }
                            pass++;
                            if (!added) {
                                break;
                            }
                        }
                    }
                }
            } catch (Throwable e) {
                e.printStackTrace();
                errors.incrementAndGet();
            } finally {
                stopLatch.countDown();
            }
        }).start();
        class MyReader implements Runnable {

            @Override
            public void run() {
                try {
                    startBarrier.await();
                    try (Path path = new Path().of(configuration.getRoot())) {
                        try (BitmapIndexFwdReader reader1 = new BitmapIndexFwdReader(configuration, path, "x", 0)) {
                            LongList tmp = new LongList();
                            while (true) {
                                boolean keepGoing = false;
                                for (int i = keys.size() - 1; i > -1; i--) {
                                    int key = keys.getQuick(i);
                                    LongList values = lists.get(key);
                                    RowCursor cursor = reader1.getCursor(true, key, 0, Long.MAX_VALUE);
                                    tmp.clear();
                                    while (cursor.hasNext()) {
                                        tmp.add(cursor.next());
                                    }
                                    int sz = tmp.size();
                                    for (int k = 0; k < sz; k++) {
                                        Assert.assertEquals(values.getQuick(k), tmp.getQuick(k));
                                    }
                                    if (sz < values.size()) {
                                        keepGoing = true;
                                    }
                                }
                                if (!keepGoing) {
                                    break;
                                }
                            }
                        }
                    }
                } catch (Throwable e) {
                    errors.incrementAndGet();
                    e.printStackTrace();
                } finally {
                    stopLatch.countDown();
                }
            }
        }
        new Thread(new MyReader()).start();
        new Thread(new MyReader()).start();
        Assert.assertTrue(stopLatch.await(20000, TimeUnit.SECONDS));
        Assert.assertEquals(0, errors.get());
    });
}
Also used : Path(io.questdb.std.str.Path) CountDownLatch(java.util.concurrent.CountDownLatch) CyclicBarrier(java.util.concurrent.CyclicBarrier) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RowCursor(io.questdb.cairo.sql.RowCursor)

Aggregations

RowCursor (io.questdb.cairo.sql.RowCursor)24 Test (org.junit.Test)11 Path (io.questdb.std.str.Path)4 BitmapIndexReader (io.questdb.cairo.BitmapIndexReader)3 DataFrame (io.questdb.cairo.sql.DataFrame)3 Rnd (io.questdb.std.Rnd)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 CyclicBarrier (java.util.concurrent.CyclicBarrier)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 NullIndexFrameCursor (io.questdb.NullIndexFrameCursor)1