Search in sources :

Example 6 with RowCursor

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

the class BitmapIndexTest method testSimpleRollback.

@Test
public void testSimpleRollback() throws Exception {
    TestUtils.assertMemoryLeak(() -> {
        Rnd modelRnd = new Rnd();
        final int maxKeys = 1024;
        final int N = 1000000;
        final int CUTOFF = 60000;
        // noinspection ConstantConditions
        assert CUTOFF < N;
        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 = modelRnd.nextPositiveInt() % maxKeys;
            LongList list = lists.get(key);
            if (list == null) {
                lists.put(key, list = new LongList());
                keys.add(key);
            }
            if (i > CUTOFF) {
                continue;
            }
            list.add(i);
        }
        Rnd rnd = new Rnd();
        create(configuration, path.trimTo(plen), "x", 1024);
        try (BitmapIndexWriter writer = new BitmapIndexWriter(configuration, path.trimTo(plen), "x")) {
            for (int i = 0; i < N; i++) {
                writer.add(rnd.nextPositiveInt() % maxKeys, i);
            }
            writer.rollbackValues(CUTOFF);
        }
        try (BitmapIndexBackwardReader reader = new BitmapIndexBackwardReader(configuration, path.trimTo(plen), "x", 0)) {
            for (int i = 0, n = keys.size(); i < n; i++) {
                int key = keys.getQuick(i);
                // do not limit reader, we have to read everything index has
                RowCursor cursor = reader.getCursor(key, Long.MAX_VALUE);
                LongList list = lists.get(key);
                int v = list.size();
                while (cursor.hasNext()) {
                    Assert.assertEquals(list.getQuick(--v), cursor.next());
                }
                Assert.assertEquals(0, v);
            }
        }
        // add more data to model
        for (int i = 0; i < N; i++) {
            int key = modelRnd.nextPositiveInt() % maxKeys;
            LongList list = lists.get(key);
            if (list == null) {
                lists.put(key, list = new LongList());
                keys.add(key);
            }
            list.add(i + N);
        }
        // add more date to index
        try (BitmapIndexWriter writer = new BitmapIndexWriter(configuration, path.trimTo(plen), "x")) {
            for (int i = 0; i < N; i++) {
                writer.add(rnd.nextPositiveInt() % maxKeys, i + N);
            }
        }
        // assert against model again
        try (BitmapIndexBackwardReader reader = new BitmapIndexBackwardReader(configuration, path.trimTo(plen), "x", 0)) {
            for (int i = 0, n = keys.size(); i < n; i++) {
                int key = keys.getQuick(i);
                // do not limit reader, we have to read everything index has
                RowCursor cursor = reader.getCursor(key, Long.MAX_VALUE);
                LongList list = lists.get(key);
                int v = list.size();
                while (cursor.hasNext()) {
                    Assert.assertEquals(list.getQuick(--v), cursor.next());
                }
                Assert.assertEquals(0, v);
            }
        }
    });
}
Also used : RowCursor(com.questdb.common.RowCursor) Test(org.junit.Test)

Example 7 with RowCursor

use of com.questdb.common.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(com.questdb.common.RowCursor) Test(org.junit.Test)

Example 8 with RowCursor

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

the class BitmapIndexTest method testAdd1MValues.

@Test
public void testAdd1MValues() throws Exception {
    TestUtils.assertMemoryLeak(() -> {
        Rnd rnd = new Rnd();
        int maxKeys = 1024;
        int N = 1000000;
        IntList keys = new IntList();
        IntObjHashMap<LongList> lists = new IntObjHashMap<>();
        // we need to have a conventional structure, which will unfortunately be memory-inefficient, to
        // assert that index is populated correctly
        create(configuration, path.trimTo(plen), "x", 128);
        try (BitmapIndexWriter writer = new BitmapIndexWriter(configuration, path, "x")) {
            for (int i = 0; i < N; i++) {
                int key = i % maxKeys;
                long value = rnd.nextLong();
                writer.add(key, value);
                LongList list = lists.get(key);
                if (list == null) {
                    lists.put(key, list = new LongList());
                    keys.add(key);
                }
                list.add(value);
            }
        }
        // read values and compare to the structure index is expected to be holding
        try (BitmapIndexBackwardReader reader = new BitmapIndexBackwardReader(configuration, path.trimTo(plen), "x", 0)) {
            for (int i = 0, n = keys.size(); i < n; i++) {
                LongList list = lists.get(keys.getQuick(i));
                Assert.assertNotNull(list);
                RowCursor cursor = reader.getCursor(keys.getQuick(i), Long.MAX_VALUE);
                int z = list.size();
                while (cursor.hasNext()) {
                    Assert.assertTrue(z > -1);
                    Assert.assertEquals(list.getQuick(z - 1), cursor.next());
                    z--;
                }
                // makes sure entire list is processed
                Assert.assertEquals(0, z);
            }
        }
    });
}
Also used : RowCursor(com.questdb.common.RowCursor) Test(org.junit.Test)

Example 9 with RowCursor

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

the class BitmapIndexTest method testReaderDoesNotUnmapPages.

@Test
public void testReaderDoesNotUnmapPages() 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) {
                super.munmap(address, size);
                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 (BitmapIndexBackwardReader reader = new BitmapIndexBackwardReader(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(key, 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(com.questdb.common.RowCursor) Test(org.junit.Test)

Example 10 with RowCursor

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

the class BitmapIndexTest method testBackwardReaderKeyUpdateFail.

@Test
public void testBackwardReaderKeyUpdateFail() {
    create(configuration, path.trimTo(plen), "x", 1024);
    try (BitmapIndexWriter writer = new BitmapIndexWriter(configuration, path, "x")) {
        writer.add(0, 1000);
    }
    try (BitmapIndexBackwardReader reader = new BitmapIndexBackwardReader(configuration, path.trimTo(plen), "x", 0)) {
        // should have single value in cursor
        RowCursor cursor = reader.getCursor(0, Long.MAX_VALUE);
        Assert.assertTrue(cursor.hasNext());
        Assert.assertEquals(1000, cursor.next());
        Assert.assertFalse(cursor.hasNext());
        try (Path path = new Path();
            ReadWriteMemory mem = new ReadWriteMemory(configuration.getFilesFacade(), path.of(root).concat("x").put(".k").$(), configuration.getFilesFacade().getPageSize())) {
            // change sequence but not sequence check
            long seq = mem.getLong(BitmapIndexUtils.KEY_RESERVED_OFFSET_SEQUENCE);
            mem.jumpTo(BitmapIndexUtils.KEY_RESERVED_OFFSET_SEQUENCE);
            mem.putLong(22);
            try {
                reader.getCursor(10, Long.MAX_VALUE);
            } catch (CairoException e) {
                Assert.assertTrue(Chars.contains(e.getMessage(), "failed to consistently"));
            }
            try {
                reader.getCursor(0, Long.MAX_VALUE);
            } catch (CairoException e) {
                Assert.assertTrue(Chars.contains(e.getMessage(), "failed to consistently"));
            }
            mem.jumpTo(BitmapIndexUtils.KEY_RESERVED_OFFSET_SEQUENCE);
            mem.putLong(seq);
            // test that index recovers
            cursor = reader.getCursor(0, Long.MAX_VALUE);
            Assert.assertTrue(cursor.hasNext());
            Assert.assertEquals(1000, cursor.next());
            Assert.assertFalse(cursor.hasNext());
        }
    }
}
Also used : Path(com.questdb.std.str.Path) RowCursor(com.questdb.common.RowCursor) Test(org.junit.Test)

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