Search in sources :

Example 1 with RowCursor

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

the class SymbolMapReaderImpl method getQuick.

@Override
public int getQuick(CharSequence symbol) {
    if (symbol == null) {
        return SymbolTable.VALUE_IS_NULL;
    }
    int hash = Hash.boundedHash(symbol, maxHash);
    RowCursor cursor = indexReader.getCursor(hash, maxOffset);
    while (cursor.hasNext()) {
        long offsetOffset = cursor.next();
        if (Chars.equals(symbol, charMem.getStr(offsetMem.getLong(offsetOffset)))) {
            return SymbolMapWriter.offsetToKey(offsetOffset);
        }
    }
    return SymbolTable.VALUE_NOT_FOUND;
}
Also used : RowCursor(com.questdb.common.RowCursor)

Example 2 with RowCursor

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

the class SymbolMapWriter method lookupAndPut.

private int lookupAndPut(CharSequence symbol) {
    int hash = Hash.boundedHash(symbol, maxHash);
    RowCursor cursor = indexWriter.getCursor(hash);
    while (cursor.hasNext()) {
        long offsetOffset = cursor.next();
        if (Chars.equals(symbol, charMem.getStr(offsetMem.getLong(offsetOffset)))) {
            return offsetToKey(offsetOffset);
        }
    }
    return put0(symbol, hash);
}
Also used : RowCursor(com.questdb.common.RowCursor)

Example 3 with RowCursor

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

the class BitmapIndexNullReaderTest method testCursor.

@Test
public void testCursor() {
    final Rnd rnd = new Rnd();
    for (int i = 0; i < 10; i++) {
        int n = rnd.nextPositiveInt() % 1024;
        int m = n;
        RowCursor cursor = reader.getCursor(0, n);
        while (cursor.hasNext()) {
            Assert.assertEquals(m--, cursor.next());
        }
        Assert.assertEquals(-1, m);
    }
}
Also used : Rnd(com.questdb.std.Rnd) RowCursor(com.questdb.common.RowCursor) Test(org.junit.Test)

Example 4 with RowCursor

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

the class BitmapIndexTest method assertCursorLimit.

private void assertCursorLimit(BitmapIndexBackwardReader reader, long max, LongList tmp) {
    tmp.clear();
    RowCursor cursor = reader.getCursor(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(com.questdb.common.RowCursor)

Example 5 with RowCursor

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

the class BitmapIndexTest method testConcurrentRW.

private void testConcurrentRW(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 (BitmapIndexBackwardReader reader1 = new BitmapIndexBackwardReader(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(key, 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(sz - k - 1), 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(com.questdb.std.str.Path) CountDownLatch(java.util.concurrent.CountDownLatch) CyclicBarrier(java.util.concurrent.CyclicBarrier) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RowCursor(com.questdb.common.RowCursor)

Aggregations

RowCursor (com.questdb.common.RowCursor)13 Test (org.junit.Test)6 Path (com.questdb.std.str.Path)2 DataFrame (com.questdb.cairo.sql.DataFrame)1 SymbolTable (com.questdb.common.SymbolTable)1 Rnd (com.questdb.std.Rnd)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 CyclicBarrier (java.util.concurrent.CyclicBarrier)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1