Search in sources :

Example 1 with Record

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

the class TableReaderTailRecordCursorTest method testBusyPoll.

private void testBusyPoll(long timestampIncrement, int n, String createStatement) throws Exception {
    assertMemoryLeak(() -> {
        compiler.compile(createStatement, sqlExecutionContext);
        final AtomicInteger errorCount = new AtomicInteger();
        final CyclicBarrier barrier = new CyclicBarrier(2);
        final CountDownLatch latch = new CountDownLatch(2);
        new Thread(() -> {
            try (TableWriter writer = engine.getWriter(AllowAllCairoSecurityContext.INSTANCE, "xyz", "testing")) {
                barrier.await();
                long ts = 0;
                long addr = Unsafe.malloc(128, MemoryTag.NATIVE_DEFAULT);
                try {
                    Rnd rnd = new Rnd();
                    for (int i = 0; i < n; i++) {
                        TableWriter.Row row = writer.newRow(ts);
                        row.putInt(0, i);
                        for (int k = 0; k < 128; k++) {
                            Unsafe.getUnsafe().putByte(addr + k, rnd.nextByte());
                        }
                        row.putBin(1, addr, 128);
                        row.putLong(2, rnd.nextLong());
                        row.append();
                        writer.commit();
                        ts += timestampIncrement;
                    }
                } finally {
                    Unsafe.free(addr, 128, MemoryTag.NATIVE_DEFAULT);
                }
            } catch (Throwable e) {
                e.printStackTrace();
                errorCount.incrementAndGet();
            } finally {
                latch.countDown();
            }
        }).start();
        new Thread(() -> {
            try (TableReader reader = engine.getReader(AllowAllCairoSecurityContext.INSTANCE, "xyz", TableUtils.ANY_TABLE_ID, TableUtils.ANY_TABLE_VERSION)) {
                Rnd rnd = new Rnd();
                int count = 0;
                final TableReaderTailRecordCursor cursor = new TableReaderTailRecordCursor();
                cursor.of(reader);
                final Record record = cursor.getRecord();
                barrier.await();
                while (count < n) {
                    if (cursor.reload()) {
                        while (cursor.hasNext()) {
                            Assert.assertEquals(count, record.getInt(0));
                            BinarySequence binarySequence = record.getBin(1);
                            for (int i = 0; i < 128; i++) {
                                Assert.assertEquals(rnd.nextByte(), binarySequence.byteAt(i));
                            }
                            Assert.assertEquals(rnd.nextLong(), record.getLong(2));
                            count++;
                        }
                    }
                }
            } catch (Throwable e) {
                e.printStackTrace();
                errorCount.incrementAndGet();
            } finally {
                latch.countDown();
            }
        }).start();
        Assert.assertTrue(latch.await(600, TimeUnit.SECONDS));
        Assert.assertEquals(0, errorCount.get());
    });
}
Also used : BinarySequence(io.questdb.std.BinarySequence) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Rnd(io.questdb.std.Rnd) Record(io.questdb.cairo.sql.Record) CountDownLatch(java.util.concurrent.CountDownLatch) CyclicBarrier(java.util.concurrent.CyclicBarrier)

Example 2 with Record

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

the class TableReaderTailRecordCursorTest method testBusyPollFromMidTable.

private void testBusyPollFromMidTable(int partitionBy, long timestampIncrement) throws Exception {
    final int blobSize = 1024;
    final int n = 1000;
    assertMemoryLeak(() -> {
        compiler.compile("create table xyz (sequence INT, event BINARY, ts LONG, stamp TIMESTAMP) timestamp(stamp) partition by " + PartitionBy.toString(partitionBy), sqlExecutionContext);
        try (TableWriter writer = engine.getWriter(AllowAllCairoSecurityContext.INSTANCE, "xyz", "testing")) {
            long ts = 0;
            long addr = Unsafe.malloc(blobSize, MemoryTag.NATIVE_DEFAULT);
            try {
                Rnd rnd = new Rnd();
                appendRecords(0, n, timestampIncrement, writer, ts, addr, rnd);
                ts = n * timestampIncrement;
                try (TableReader reader = engine.getReader(AllowAllCairoSecurityContext.INSTANCE, "xyz", TableUtils.ANY_TABLE_ID, TableUtils.ANY_TABLE_VERSION);
                    TableReaderTailRecordCursor cursor = new TableReaderTailRecordCursor()) {
                    cursor.of(reader);
                    Assert.assertTrue(cursor.reload());
                    int count = 0;
                    Record record = cursor.getRecord();
                    while (cursor.hasNext()) {
                        Assert.assertEquals(n - count, record.getLong(2));
                        count++;
                    }
                    Assert.assertFalse(cursor.reload());
                    Assert.assertFalse(cursor.hasNext());
                    appendRecords(n, n, timestampIncrement, writer, ts, addr, rnd);
                    Assert.assertTrue(cursor.reload());
                    count = 0;
                    while (cursor.hasNext()) {
                        Assert.assertEquals(n + n - count, record.getLong(2));
                        count++;
                    }
                    writer.truncate();
                    Assert.assertTrue(cursor.reload());
                    Assert.assertFalse(cursor.hasNext());
                    appendRecords(n * 2, n / 2, timestampIncrement, writer, ts, addr, rnd);
                    Assert.assertTrue(cursor.reload());
                    count = 0;
                    while (cursor.hasNext()) {
                        Assert.assertEquals(n * 2 + n / 2 - count, record.getLong(2));
                        count++;
                    }
                    Assert.assertEquals(n / 2, count);
                }
            } finally {
                Unsafe.free(addr, blobSize, MemoryTag.NATIVE_DEFAULT);
            }
        }
    });
}
Also used : Rnd(io.questdb.std.Rnd) Record(io.questdb.cairo.sql.Record)

Example 3 with Record

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

the class TableReaderTailRecordCursorTest method testBusyPollFromBottomOfTable.

private void testBusyPollFromBottomOfTable(int partitionBy, long timestampIncrement) throws Exception {
    final int blobSize = 1024;
    final int n = 1000;
    assertMemoryLeak(() -> {
        compiler.compile("create table xyz (sequence INT, event BINARY, ts LONG, stamp TIMESTAMP) timestamp(stamp) partition by " + PartitionBy.toString(partitionBy), sqlExecutionContext);
        try (TableWriter writer = engine.getWriter(AllowAllCairoSecurityContext.INSTANCE, "xyz", "testing")) {
            long ts = 0;
            long addr = Unsafe.malloc(blobSize, MemoryTag.NATIVE_DEFAULT);
            try {
                Rnd rnd = new Rnd();
                appendRecords(0, n, timestampIncrement, writer, ts, addr, rnd);
                ts = n * timestampIncrement;
                try (TableReader reader = engine.getReader(AllowAllCairoSecurityContext.INSTANCE, "xyz", TableUtils.ANY_TABLE_ID, TableUtils.ANY_TABLE_VERSION);
                    TableReaderTailRecordCursor cursor = new TableReaderTailRecordCursor()) {
                    cursor.of(reader);
                    cursor.toBottom();
                    Assert.assertFalse(cursor.reload());
                    Assert.assertFalse(cursor.hasNext());
                    appendRecords(n, n, timestampIncrement, writer, ts, addr, rnd);
                    Assert.assertTrue(cursor.reload());
                    int count = 0;
                    final Record record = cursor.getRecord();
                    while (cursor.hasNext()) {
                        Assert.assertEquals(n + n - count, record.getLong(2));
                        count++;
                    }
                    writer.truncate();
                    Assert.assertTrue(cursor.reload());
                    Assert.assertFalse(cursor.hasNext());
                    appendRecords(n * 2, n / 2, timestampIncrement, writer, ts, addr, rnd);
                    Assert.assertTrue(cursor.reload());
                    count = 0;
                    while (cursor.hasNext()) {
                        Assert.assertEquals(n * 2 + n / 2 - count, record.getLong(2));
                        count++;
                    }
                    Assert.assertEquals(n / 2, count);
                }
            } finally {
                Unsafe.free(addr, blobSize, MemoryTag.NATIVE_DEFAULT);
            }
        }
    });
}
Also used : Rnd(io.questdb.std.Rnd) Record(io.questdb.cairo.sql.Record)

Example 4 with Record

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

the class TableWriterTest method assertGeoStr.

private void assertGeoStr(String hash, int tableBits, long expected) {
    final String tableName = "geo1";
    try (TableModel model = new TableModel(configuration, tableName, PartitionBy.NONE)) {
        model.col("g", ColumnType.getGeoHashTypeWithBits(tableBits));
        CairoTestUtils.createTable(model);
    }
    try (TableWriter writer = new TableWriter(configuration, tableName)) {
        TableWriter.Row r = writer.newRow();
        r.putGeoStr(0, hash);
        r.append();
        writer.commit();
    }
    try (TableReader r = new TableReader(configuration, tableName)) {
        final RecordCursor cursor = r.getCursor();
        final Record record = cursor.getRecord();
        final int type = r.getMetadata().getColumnType(0);
        Assert.assertTrue(cursor.hasNext());
        final long actual;
        switch(ColumnType.tagOf(type)) {
            case ColumnType.GEOBYTE:
                actual = record.getGeoByte(0);
                break;
            case ColumnType.GEOSHORT:
                actual = record.getGeoShort(0);
                break;
            case ColumnType.GEOINT:
                actual = record.getGeoInt(0);
                break;
            default:
                actual = record.getGeoLong(0);
                break;
        }
        Assert.assertEquals(expected, actual);
    }
}
Also used : RecordCursor(io.questdb.cairo.sql.RecordCursor) Record(io.questdb.cairo.sql.Record)

Example 5 with Record

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

the class TableWriterTest method testO3WithCancelRow.

@Test
public void testO3WithCancelRow() throws Exception {
    TestUtils.assertMemoryLeak(() -> {
        try (TableModel model = new TableModel(configuration, "weather", PartitionBy.DAY).col("windspeed", ColumnType.DOUBLE).timestamp()) {
            CairoTestUtils.create(model);
        }
        long[] tss = new long[] { 631150000000000L, 631152000000000L, 631160000000000L };
        try (TableWriter writer = new TableWriter(configuration, "weather")) {
            TableWriter.Row r = writer.newRow(tss[1]);
            r.putDouble(0, 1.0);
            r.append();
            // Out of order
            r = writer.newRow(tss[0]);
            r.putDouble(0, 2.0);
            r.append();
            r = writer.newRow(tss[2]);
            r.putDouble(0, 3.0);
            r.cancel();
            // Implicit commit
            writer.addColumn("timetocycle", ColumnType.DOUBLE);
            writer.newRow(tss[2]);
            r.putDouble(0, 3.0);
            r.putDouble(2, -1.0);
            r.append();
            writer.commit();
        }
        try (TableReader reader = new TableReader(configuration, "weather")) {
            int col = reader.getMetadata().getColumnIndex("timestamp");
            RecordCursor cursor = reader.getCursor();
            final Record r = cursor.getRecord();
            int i = 0;
            while (cursor.hasNext()) {
                Assert.assertEquals("Row " + i, tss[i++], r.getTimestamp(col));
            }
            Assert.assertEquals(tss.length, i);
        }
    });
}
Also used : RecordCursor(io.questdb.cairo.sql.RecordCursor) Record(io.questdb.cairo.sql.Record) Test(org.junit.Test)

Aggregations

Record (io.questdb.cairo.sql.Record)171 Test (org.junit.Test)130 RecordCursor (io.questdb.cairo.sql.RecordCursor)121 RecordCursorFactory (io.questdb.cairo.sql.RecordCursorFactory)79 TableWriter (io.questdb.cairo.TableWriter)70 AbstractGriffinTest (io.questdb.griffin.AbstractGriffinTest)66 Rnd (io.questdb.std.Rnd)32 Function (io.questdb.cairo.sql.Function)28 InStrFunctionFactory (io.questdb.griffin.engine.functions.bool.InStrFunctionFactory)16 NotFunctionFactory (io.questdb.griffin.engine.functions.bool.NotFunctionFactory)15 OrFunctionFactory (io.questdb.griffin.engine.functions.bool.OrFunctionFactory)15 IntList (io.questdb.std.IntList)15 CastStrToGeoHashFunctionFactory (io.questdb.griffin.engine.functions.cast.CastStrToGeoHashFunctionFactory)14 ToStrDateFunctionFactory (io.questdb.griffin.engine.functions.date.ToStrDateFunctionFactory)14 ToStrTimestampFunctionFactory (io.questdb.griffin.engine.functions.date.ToStrTimestampFunctionFactory)14 LengthStrFunctionFactory (io.questdb.griffin.engine.functions.str.LengthStrFunctionFactory)14 LengthSymbolFunctionFactory (io.questdb.griffin.engine.functions.str.LengthSymbolFunctionFactory)14 ToCharBinFunctionFactory (io.questdb.griffin.engine.functions.str.ToCharBinFunctionFactory)14 CursorDereferenceFunctionFactory (io.questdb.griffin.engine.functions.catalogue.CursorDereferenceFunctionFactory)13 SwitchFunctionFactory (io.questdb.griffin.engine.functions.conditional.SwitchFunctionFactory)13