Search in sources :

Example 21 with RecordCursor

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

the class TableReaderTest method testReadLong256One.

@Test
public void testReadLong256One() {
    try (TableModel model = new TableModel(configuration, "w", PartitionBy.DAY).col("l", ColumnType.LONG256).timestamp()) {
        CairoTestUtils.create(model);
    }
    final int N = 1_000_000;
    final Rnd rnd = new Rnd();
    long timestamp = 0;
    try (TableWriter writer = new TableWriter(configuration, "w")) {
        for (int i = 0; i < N; i++) {
            TableWriter.Row row = writer.newRow(timestamp);
            row.putLong256(0, "0x" + padHexLong(rnd.nextLong()));
            row.append();
        }
        writer.commit();
    }
    rnd.reset();
    final StringSink sink = new StringSink();
    try (TableReader reader = new TableReader(configuration, "w")) {
        final RecordCursor cursor = reader.getCursor();
        final Record record = cursor.getRecord();
        int count = 0;
        while (cursor.hasNext()) {
            sink.clear();
            record.getLong256(0, sink);
            TestUtils.assertEquals("0x" + padHexLong(rnd.nextLong()), sink);
            count++;
        }
        Assert.assertEquals(N, count);
    }
}
Also used : RecordCursor(io.questdb.cairo.sql.RecordCursor) StringSink(io.questdb.std.str.StringSink) Record(io.questdb.cairo.sql.Record) Test(org.junit.Test)

Example 22 with RecordCursor

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

the class TableReaderTest method testReaderAndWriterRace.

@Test
public void testReaderAndWriterRace() throws Exception {
    TestUtils.assertMemoryLeak(() -> {
        try (TableModel model = new TableModel(configuration, "x", PartitionBy.NONE)) {
            CairoTestUtils.create(model.timestamp());
        }
        CountDownLatch stopLatch = new CountDownLatch(2);
        CyclicBarrier barrier = new CyclicBarrier(2);
        int count = 1000000;
        AtomicInteger reloadCount = new AtomicInteger(0);
        try (TableWriter writer = new TableWriter(configuration, "x");
            TableReader reader = new TableReader(configuration, "x")) {
            new Thread(() -> {
                try {
                    barrier.await();
                    for (int i = 0; i < count; i++) {
                        TableWriter.Row row = writer.newRow(i);
                        row.append();
                        writer.commit();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    stopLatch.countDown();
                }
            }).start();
            new Thread(() -> {
                try {
                    barrier.await();
                    int max = 0;
                    RecordCursor cursor = reader.getCursor();
                    while (max < count) {
                        if (reader.reload()) {
                            reloadCount.incrementAndGet();
                            cursor.toTop();
                            int localCount = 0;
                            while (cursor.hasNext()) {
                                localCount++;
                            }
                            if (localCount > max) {
                                max = localCount;
                            }
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    stopLatch.countDown();
                }
            }).start();
            stopLatch.await();
            Assert.assertTrue(reloadCount.get() > 0);
        }
    });
}
Also used : RecordCursor(io.questdb.cairo.sql.RecordCursor) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CountDownLatch(java.util.concurrent.CountDownLatch) CyclicBarrier(java.util.concurrent.CyclicBarrier) Test(org.junit.Test)

Example 23 with RecordCursor

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

the class TableReaderTest method testTableCursor.

private void testTableCursor(long inc) throws NumericException {
    Rnd rnd = new Rnd();
    int N = 100;
    long ts = TimestampFormatUtils.parseTimestamp("2013-03-04T00:00:00.000Z") / 1000;
    long blob = allocBlob();
    try {
        testAppend(rnd, configuration, ts, N, inc, blob, 0);
        final LongList rows = new LongList();
        try (TableReader reader = new TableReader(configuration, "all")) {
            Assert.assertEquals(N, reader.size());
            RecordCursor cursor = reader.getCursor();
            final Record record = cursor.getRecord();
            assertCursor(cursor, ts, inc, blob, N, BATCH1_ASSERTER);
            cursor.toTop();
            while (cursor.hasNext()) {
                rows.add(record.getRowId());
            }
            Rnd exp = new Rnd();
            final Record rec = cursor.getRecordB();
            for (int i = 0, n = rows.size(); i < n; i++) {
                cursor.recordAt(rec, rows.getQuick(i));
                BATCH1_ASSERTER.assertRecord(rec, exp, ts += inc, blob);
            }
        }
    } finally {
        freeBlob(blob);
    }
}
Also used : RecordCursor(io.questdb.cairo.sql.RecordCursor) Record(io.questdb.cairo.sql.Record)

Example 24 with RecordCursor

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

the class TruncateTest method testTruncateOpenReader.

@Test
public void testTruncateOpenReader() throws Exception {
    assertMemoryLeak(() -> {
        createX(1_000_000);
        assertQuery("count\n" + "1000000\n", "select count() from x", null, false, true);
        try (RecordCursorFactory factory = compiler.compile("select * from x", sqlExecutionContext).getRecordCursorFactory()) {
            try (RecordCursor cursor = factory.getCursor(sqlExecutionContext)) {
                final Record record = cursor.getRecord();
                while (cursor.hasNext()) {
                    record.getInt(0);
                    record.getSym(1);
                    record.getDouble(2);
                }
            }
        }
        compiler.compile("truncate table 'x'", sqlExecutionContext);
    });
}
Also used : RecordCursorFactory(io.questdb.cairo.sql.RecordCursorFactory) RecordCursor(io.questdb.cairo.sql.RecordCursor) Record(io.questdb.cairo.sql.Record) Test(org.junit.Test)

Example 25 with RecordCursor

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

the class TruncateTest method testDropTableWithCachedPlan.

private void testDropTableWithCachedPlan(String query) throws Exception {
    assertMemoryLeak(() -> {
        compiler.compile("create table y as (" + "select timestamp_sequence(0, 1000000000) timestamp," + " rnd_symbol('a','b',null) symbol1 " + " from long_sequence(10)" + ") timestamp (timestamp)", sqlExecutionContext);
        try (RecordCursorFactory factory = compiler.compile(query, sqlExecutionContext).getRecordCursorFactory()) {
            try (RecordCursor cursor = factory.getCursor(sqlExecutionContext)) {
                sink.clear();
                TestUtils.printCursor(cursor, factory.getMetadata(), true, sink, printer);
            }
            compiler.compile("drop table y", sqlExecutionContext);
            compiler.compile("create table y as ( " + " select " + " timestamp_sequence('1970-01-01T02:30:00.000000Z', 1000000000L) timestamp " + " ,rnd_str('a','b','c', 'd', 'e', 'f',null) symbol2" + " ,rnd_str('a','b',null) symbol1" + " from long_sequence(10)" + ")", sqlExecutionContext);
            try (RecordCursor cursor = factory.getCursor(sqlExecutionContext)) {
                TestUtils.printCursor(cursor, factory.getMetadata(), true, sink, printer);
                Assert.fail();
            } catch (ReaderOutOfDateException e) {
                TestUtils.assertContains(e.getFlyweightMessage(), "cannot be used because table schema has changed [table='y']");
            }
        }
    });
}
Also used : RecordCursorFactory(io.questdb.cairo.sql.RecordCursorFactory) RecordCursor(io.questdb.cairo.sql.RecordCursor) ReaderOutOfDateException(io.questdb.cairo.sql.ReaderOutOfDateException)

Aggregations

RecordCursor (io.questdb.cairo.sql.RecordCursor)174 Test (org.junit.Test)137 Record (io.questdb.cairo.sql.Record)123 RecordCursorFactory (io.questdb.cairo.sql.RecordCursorFactory)108 AbstractGriffinTest (io.questdb.griffin.AbstractGriffinTest)87 TableWriter (io.questdb.cairo.TableWriter)71 Rnd (io.questdb.std.Rnd)29 LPSZ (io.questdb.std.str.LPSZ)10 SqlExecutionContextImpl (io.questdb.griffin.SqlExecutionContextImpl)7 SqlCompiler (io.questdb.griffin.SqlCompiler)6 DateFormat (io.questdb.std.datetime.DateFormat)6 Path (io.questdb.std.str.Path)6 StringSink (io.questdb.std.str.StringSink)6 CountDownLatch (java.util.concurrent.CountDownLatch)5 CyclicBarrier (java.util.concurrent.CyclicBarrier)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 SqlException (io.questdb.griffin.SqlException)4 BaseConnection (org.postgresql.core.BaseConnection)4 LoopInterruptedCheck (de.invesdwin.util.concurrent.loop.LoopInterruptedCheck)3 Instant (de.invesdwin.util.time.Instant)3