Search in sources :

Example 1 with Rnd

use of io.questdb.std.Rnd 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 Rnd

use of io.questdb.std.Rnd 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 Rnd

use of io.questdb.std.Rnd 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 Rnd

use of io.questdb.std.Rnd in project questdb by bluestreak01.

the class IntervalBwdDataFrameCursorTest method testReload.

public void testReload(int partitionBy, long increment, LongList intervals, int rowCount, CharSequence expected1, CharSequence expected2) throws Exception {
    assertMemoryLeak(() -> {
        try (TableModel model = new TableModel(configuration, "x", partitionBy).col("a", ColumnType.SYMBOL).indexed(true, 4).col("b", ColumnType.SYMBOL).indexed(true, 4).timestamp()) {
            CairoTestUtils.create(model);
        }
        final Rnd rnd = new Rnd();
        long timestamp = TimestampFormatUtils.parseTimestamp("1980-01-01T00:00:00.000Z");
        final int timestampIndex;
        try (TableReader reader = engine.getReader(AllowAllCairoSecurityContext.INSTANCE, "x")) {
            timestampIndex = reader.getMetadata().getTimestampIndex();
        }
        final TableReaderRecord record = new TableReaderRecord();
        final IntervalBwdDataFrameCursorFactory factory = new IntervalBwdDataFrameCursorFactory(engine, "x", -1, 0, new RuntimeIntervalModel(intervals), timestampIndex);
        try (DataFrameCursor cursor = factory.getCursor(AllowAllSqlSecurityContext.INSTANCE)) {
            // assert that there is nothing to start with
            record.of(cursor.getTableReader());
            assertEquals("", record, cursor);
            try (TableWriter writer = new TableWriter(configuration, "x")) {
                for (int i = 0; i < rowCount; i++) {
                    TableWriter.Row row = writer.newRow(timestamp);
                    row.putSym(0, rnd.nextChars(4));
                    row.putSym(1, rnd.nextChars(4));
                    row.append();
                    timestamp += increment;
                }
                writer.commit();
                Assert.assertTrue(cursor.reload());
                assertEquals(expected1, record, cursor);
                timestamp = Timestamps.addYear(timestamp, 3);
                for (int i = 0; i < rowCount; i++) {
                    TableWriter.Row row = writer.newRow(timestamp);
                    row.putSym(0, rnd.nextChars(4));
                    row.putSym(1, rnd.nextChars(4));
                    row.append();
                    timestamp += increment;
                }
                writer.commit();
                Assert.assertTrue(cursor.reload());
                if (expected2 != null) {
                    assertEquals(expected2, record, cursor);
                } else {
                    assertEquals(expected1, record, cursor);
                }
                Assert.assertFalse(cursor.reload());
            }
        }
        try (TableWriter writer = engine.getWriter(AllowAllCairoSecurityContext.INSTANCE, "x", "testing")) {
            writer.removeColumn("b");
        }
        try {
            factory.getCursor(AllowAllSqlSecurityContext.INSTANCE);
            Assert.fail();
        } catch (ReaderOutOfDateException ignored) {
        }
    });
}
Also used : Rnd(io.questdb.std.Rnd) RuntimeIntervalModel(io.questdb.griffin.model.RuntimeIntervalModel)

Example 5 with Rnd

use of io.questdb.std.Rnd in project questdb by bluestreak01.

the class SymbolMapTest method testSimpleAdd.

@Test
public void testSimpleAdd() throws Exception {
    TestUtils.assertMemoryLeak(() -> {
        int N = 1000000;
        try (Path path = new Path().of(configuration.getRoot())) {
            create(path, "x", N, false);
            try (SymbolMapWriter writer = new SymbolMapWriter(configuration, path, "x", 0, -1, NOOP_COLLECTOR)) {
                Rnd rnd = new Rnd();
                long prev = -1L;
                for (int i = 0; i < N; i++) {
                    CharSequence cs = rnd.nextChars(10);
                    long key = writer.put(cs);
                    Assert.assertEquals(prev + 1, key);
                    Assert.assertEquals(key, writer.put(cs));
                    prev = key;
                }
            }
        }
    });
}
Also used : Path(io.questdb.std.str.Path) Rnd(io.questdb.std.Rnd) Test(org.junit.Test)

Aggregations

Rnd (io.questdb.std.Rnd)76 Test (org.junit.Test)49 Record (io.questdb.cairo.sql.Record)32 RecordCursor (io.questdb.cairo.sql.RecordCursor)29 AbstractGriffinTest (io.questdb.griffin.AbstractGriffinTest)28 RecordCursorFactory (io.questdb.cairo.sql.RecordCursorFactory)24 TableWriter (io.questdb.cairo.TableWriter)23 Path (io.questdb.std.str.Path)10 RuntimeIntervalModel (io.questdb.griffin.model.RuntimeIntervalModel)4 CyclicBarrier (java.util.concurrent.CyclicBarrier)3 DataFrameCursor (io.questdb.cairo.sql.DataFrameCursor)2 ReaderOutOfDateException (io.questdb.cairo.sql.ReaderOutOfDateException)2 RowCursor (io.questdb.cairo.sql.RowCursor)2 StaticSymbolTable (io.questdb.cairo.sql.StaticSymbolTable)2 LineTcpSender (io.questdb.cutlass.line.LineTcpSender)2 SqlExecutionContextImpl (io.questdb.griffin.SqlExecutionContextImpl)2 AbstractFunctionFactoryTest (io.questdb.griffin.engine.AbstractFunctionFactoryTest)2 BinarySequence (io.questdb.std.BinarySequence)2 BytecodeAssembler (io.questdb.std.BytecodeAssembler)2 FilesFacade (io.questdb.std.FilesFacade)2