Search in sources :

Example 6 with RecordCursor

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

the class DDLTests method testCreateAsSelectAll.

@Test
public void testCreateAsSelectAll() throws Exception {
    int N = 50;
    try (JournalWriter w = compiler.createWriter(getFactory(), "create table x (a INT, b BYTE, c SHORT, d LONG, e FLOAT, f DOUBLE, g DATE, h BINARY, t DATE, x SYMBOL, z STRING, y BOOLEAN) timestamp(t) record hint 100")) {
        Rnd rnd = new Rnd();
        long t = DateFormatUtils.parseDateTime("2016-01-10T00:00:00.000Z");
        for (int i = 0; i < N; i++) {
            JournalEntryWriter ew = w.entryWriter(t += Dates.DAY_MILLIS);
            ew.putInt(0, i);
            ew.put(1, (byte) rnd.nextInt());
            ew.putShort(2, (short) rnd.nextInt());
            ew.putLong(3, rnd.nextLong());
            ew.putFloat(4, rnd.nextFloat());
            ew.putDouble(5, rnd.nextDouble());
            ew.putDate(6, rnd.nextLong());
            ew.putNull(7);
            ew.putSym(9, rnd.nextChars(1));
            ew.putStr(10, rnd.nextChars(10));
            ew.putBool(11, rnd.nextBoolean());
            ew.append();
        }
        w.commit();
    }
    exec("create table y as (x) partition by MONTH");
    try (Journal r = getFactory().reader("y")) {
        Assert.assertEquals(2, r.getPartitionCount());
    }
    int count = 0;
    try (RecordSource rs = compiler.compile(getFactory(), "y")) {
        RecordCursor cursor = rs.prepareCursor(getFactory());
        try {
            Rnd rnd = new Rnd();
            while (cursor.hasNext()) {
                Record rec = cursor.next();
                Assert.assertEquals(count, rec.getInt(0));
                Assert.assertTrue((byte) rnd.nextInt() == rec.getByte(1));
                Assert.assertEquals((short) rnd.nextInt(), rec.getShort(2));
                Assert.assertEquals(rnd.nextLong(), rec.getLong(3));
                Assert.assertEquals(rnd.nextFloat(), rec.getFloat(4), 0.00001f);
                Assert.assertEquals(rnd.nextDouble(), rec.getDouble(5), 0.00000000001);
                Assert.assertEquals(rnd.nextLong(), rec.getDate(6));
                Assert.assertNull(rec.getBin(7));
                TestUtils.assertEquals(rnd.nextChars(1), rec.getSym(9));
                TestUtils.assertEquals(rnd.nextChars(10), rec.getFlyweightStr(10));
                Assert.assertEquals(rnd.nextBoolean(), rec.getBool(11));
                count++;
            }
        } finally {
            cursor.releaseCursor();
        }
    }
}
Also used : JournalWriter(com.questdb.store.JournalWriter) RecordCursor(com.questdb.common.RecordCursor) Journal(com.questdb.store.Journal) Record(com.questdb.common.Record) JournalEntryWriter(com.questdb.store.JournalEntryWriter) AbstractTest(com.questdb.test.tools.AbstractTest) Test(org.junit.Test)

Example 7 with RecordCursor

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

the class RecordKeyCopierCompilerTest method testCompiler.

@Test
public void testCompiler() throws Exception {
    try (JournalWriter w = compiler.createWriter(FACTORY_CONTAINER.getFactory(), "create table x (a INT, b BYTE, c SHORT, d LONG, e FLOAT, f DOUBLE, g DATE, h BINARY, t DATE, x SYMBOL, z STRING, y BOOLEAN) timestamp(t) partition by MONTH record hint 100")) {
        JournalEntryWriter ew = w.entryWriter();
        IntList keyColumns = new IntList();
        ew.putInt(0, 12345);
        keyColumns.add(0);
        ew.put(1, (byte) 128);
        keyColumns.add(1);
        ew.putShort(2, (short) 6500);
        keyColumns.add(2);
        ew.putLong(3, 123456789);
        keyColumns.add(3);
        ew.putFloat(4, 0.345f);
        keyColumns.add(4);
        ew.putDouble(5, 0.123456789);
        keyColumns.add(5);
        ew.putDate(6, 10000000000L);
        keyColumns.add(6);
        ew.putSym(9, "xyz");
        keyColumns.add(9);
        ew.putStr(10, "abc");
        keyColumns.add(10);
        ew.putBool(11, true);
        keyColumns.add(11);
        ew.append();
        w.commit();
        try (RecordSource src = compileSource("x")) {
            RecordKeyCopierCompiler cc = new RecordKeyCopierCompiler(new BytecodeAssembler());
            RecordKeyCopier copier = cc.compile(src.getMetadata(), keyColumns);
            IntList valueTypes = new IntList();
            valueTypes.add(ColumnType.DOUBLE);
            MetadataTypeResolver metadataTypeResolver = new MetadataTypeResolver();
            TypeListResolver typeListResolver = new TypeListResolver();
            try (DirectMap map = new DirectMap(1024, metadataTypeResolver.of(src.getMetadata(), keyColumns), typeListResolver.of(valueTypes))) {
                RecordCursor cursor = src.prepareCursor(FACTORY_CONTAINER.getFactory());
                try {
                    while (cursor.hasNext()) {
                        Record r = cursor.next();
                        DirectMap.KeyWriter kw = map.keyWriter();
                        copier.copy(r, kw);
                        DirectMapValues val = map.getOrCreateValues();
                        val.putDouble(0, 5000.01);
                    }
                    cursor.toTop();
                    while (cursor.hasNext()) {
                        Record r = cursor.next();
                        DirectMap.KeyWriter kw = map.keyWriter();
                        copier.copy(r, kw);
                        Assert.assertEquals(map.getValues().getDouble(0), 5000.01, 0.00000001);
                    }
                } finally {
                    cursor.releaseCursor();
                }
            }
        }
    }
}
Also used : JournalWriter(com.questdb.store.JournalWriter) RecordCursor(com.questdb.common.RecordCursor) BytecodeAssembler(com.questdb.std.BytecodeAssembler) IntList(com.questdb.std.IntList) RecordSource(com.questdb.ql.RecordSource) Record(com.questdb.common.Record) JournalEntryWriter(com.questdb.store.JournalEntryWriter) Test(org.junit.Test) AbstractOptimiserTest(com.questdb.parser.sql.AbstractOptimiserTest)

Example 8 with RecordCursor

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

the class ReaderPoolTest method testLockBusyReader.

@Test
public void testLockBusyReader() throws Exception {
    final int readerCount = 5;
    int threadCount = 2;
    final int iterations = 10000;
    Rnd dataRnd = new Rnd();
    StringSink sink = new StringSink();
    RecordSourcePrinter printer = new RecordSourcePrinter(sink);
    final String[] names = new String[readerCount];
    final String[] expectedRows = new String[readerCount];
    for (int i = 0; i < readerCount; i++) {
        names[i] = "x" + i;
        try (TableModel model = new TableModel(configuration, names[i], PartitionBy.NONE).col("ts", ColumnType.DATE)) {
            CairoTestUtils.create(model);
        }
        try (TableWriter w = new TableWriter(configuration, names[i])) {
            for (int k = 0; k < 10; k++) {
                TableWriter.Row r = w.newRow(0);
                r.putDate(0, dataRnd.nextLong());
                r.append();
            }
            w.commit();
        }
        sink.clear();
        try (TableReader r = new TableReader(configuration, names[i])) {
            printer.print(r.getCursor(), true, r.getMetadata());
        }
        expectedRows[i] = sink.toString();
    }
    LOG.info().$("testLockBusyReader BEGIN").$();
    assertWithPool(pool -> {
        final CyclicBarrier barrier = new CyclicBarrier(threadCount);
        final CountDownLatch halt = new CountDownLatch(threadCount);
        final AtomicInteger errors = new AtomicInteger();
        final LongList lockTimes = new LongList();
        final LongList workerTimes = new LongList();
        new Thread(() -> {
            Rnd rnd = new Rnd();
            try {
                barrier.await();
                String name;
                for (int i = 0; i < iterations; i++) {
                    name = names[rnd.nextPositiveInt() % readerCount];
                    while (true) {
                        if (pool.lock(name)) {
                            lockTimes.add(System.currentTimeMillis());
                            LockSupport.parkNanos(10L);
                            pool.unlock(name);
                            break;
                        }
                    }
                }
            } catch (Exception e) {
                errors.incrementAndGet();
                e.printStackTrace();
            } finally {
                halt.countDown();
            }
        }).start();
        new Thread(() -> {
            Rnd rnd = new Rnd();
            try {
                workerTimes.add(System.currentTimeMillis());
                for (int i = 0; i < iterations; i++) {
                    int index = rnd.nextPositiveInt() % readerCount;
                    String name = names[index];
                    try (TableReader r = pool.get(name)) {
                        RecordCursor cursor = r.getCursor();
                        sink.clear();
                        printer.print(cursor, true, r.getMetadata());
                        TestUtils.assertEquals(expectedRows[index], sink);
                        if (name.equals(names[readerCount - 1]) && barrier.getNumberWaiting() > 0) {
                            barrier.await();
                        }
                        LockSupport.parkNanos(10L);
                    } catch (EntryLockedException | EntryUnavailableException ignored) {
                    } catch (Exception e) {
                        errors.incrementAndGet();
                        e.printStackTrace();
                        break;
                    }
                }
                workerTimes.add(System.currentTimeMillis());
            } finally {
                halt.countDown();
            }
        }).start();
        halt.await();
        Assert.assertEquals(0, halt.getCount());
        Assert.assertEquals(0, errors.get());
        // check that there are lock times between worker times
        int count = 0;
        // ensure that we have worker times
        Assert.assertEquals(2, workerTimes.size());
        long lo = workerTimes.get(0);
        long hi = workerTimes.get(1);
        Assert.assertTrue(lockTimes.size() > 0);
        for (int i = 0, n = lockTimes.size(); i < n; i++) {
            long t = lockTimes.getQuick(i);
            if (t > lo && t < hi) {
                count++;
            }
        }
        Assert.assertTrue(count > 0);
        LOG.info().$("testLockBusyReader END").$();
    });
}
Also used : RecordCursor(com.questdb.common.RecordCursor) RecordSourcePrinter(com.questdb.ql.RecordSourcePrinter) StringSink(com.questdb.std.str.StringSink) CountDownLatch(java.util.concurrent.CountDownLatch) EntryLockedException(com.questdb.cairo.pool.ex.EntryLockedException) EntryUnavailableException(com.questdb.cairo.pool.ex.EntryUnavailableException) PoolClosedException(com.questdb.cairo.pool.ex.PoolClosedException) CyclicBarrier(java.util.concurrent.CyclicBarrier) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.Test)

Example 9 with RecordCursor

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

the class AbstractTest method assertThat.

protected void assertThat(String expected, String query, boolean header) throws ParserException, IOException {
    long memUsed = Unsafe.getMemUsed();
    try (RecordSource src = compiler.compile(getFactory(), query)) {
        RecordCursor cursor = src.prepareCursor(getFactory());
        try {
            sink.clear();
            printer.print(cursor, header, src.getMetadata());
            TestUtils.assertEquals(expected, sink);
            cursor.toTop();
            sink.clear();
            printer.print(cursor, header, src.getMetadata());
            TestUtils.assertEquals(expected, sink);
        } finally {
            cursor.releaseCursor();
        }
        TestUtils.assertStrings(src, getFactory());
    } catch (ParserException e) {
        System.out.println(QueryError.getMessage());
        System.out.println(QueryError.getPosition());
        throw e;
    }
    Assert.assertEquals(memUsed, Unsafe.getMemUsed());
}
Also used : ParserException(com.questdb.ex.ParserException) RecordSource(com.questdb.ql.RecordSource) RecordCursor(com.questdb.common.RecordCursor)

Example 10 with RecordCursor

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

the class FilteredTableRecordCursorFactoryTest method testFactory.

@Test
public void testFactory() throws Exception {
    TestUtils.assertMemoryLeak(() -> {
        final int N = 100;
        // separate two symbol columns with primitive. It will make problems apparent if index does not shift correctly
        try (TableModel model = new TableModel(configuration, "x", PartitionBy.DAY).col("a", ColumnType.STRING).col("b", ColumnType.SYMBOL).indexed(true, N / 4).col("i", ColumnType.INT).col("c", ColumnType.SYMBOL).indexed(true, N / 4).timestamp()) {
            CairoTestUtils.create(model);
        }
        final Rnd rnd = new Rnd();
        final String[] symbols = new String[N];
        final int M = 1000;
        final long increment = 1000000 * 60L * 4;
        for (int i = 0; i < N; i++) {
            symbols[i] = rnd.nextChars(8).toString();
        }
        rnd.reset();
        // prepare the data
        long timestamp = 0;
        try (TableWriter writer = new TableWriter(configuration, "x")) {
            for (int i = 0; i < M; i++) {
                TableWriter.Row row = writer.newRow(timestamp += increment);
                row.putStr(0, rnd.nextChars(20));
                row.putSym(1, symbols[rnd.nextPositiveInt() % N]);
                row.putInt(2, rnd.nextInt());
                row.putSym(3, symbols[rnd.nextPositiveInt() % N]);
                row.append();
            }
            writer.commit();
        }
        try (Engine engine = new Engine(configuration)) {
            String value = symbols[N - 10];
            SymbolIndexRowCursorFactory symbolIndexRowCursorFactory = new SymbolIndexRowCursorFactory(engine, "x", "b", value);
            FullTableFrameCursorFactory dataFrameFactory = new FullTableFrameCursorFactory(engine, "x");
            FilteredTableRecordCursorFactory factory = new FilteredTableRecordCursorFactory(dataFrameFactory, symbolIndexRowCursorFactory);
            RecordCursor cursor = factory.getCursor();
            while (cursor.hasNext()) {
                Record record = cursor.next();
                TestUtils.assertEquals(value, record.getSym(1));
            }
            cursor.releaseCursor();
        }
    });
}
Also used : RecordCursor(com.questdb.common.RecordCursor) Rnd(com.questdb.std.Rnd) Record(com.questdb.common.Record) Test(org.junit.Test)

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