Search in sources :

Example 1 with BinarySequence

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

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

the class PGJobContextTest method testBinaryInsert.

private void testBinaryInsert(int maxLength, boolean binaryProtocol) throws Exception {
    assertMemoryLeak(() -> {
        compiler.compile("create table xyz (" + "a binary" + ")", sqlExecutionContext);
        try (final PGWireServer ignored = createPGServer(1);
            final Connection connection = getConnection(false, binaryProtocol);
            final PreparedStatement insert = connection.prepareStatement("insert into xyz values (?)")) {
            connection.setAutoCommit(false);
            try (InputStream str = new InputStream() {

                int value = 0;

                @Override
                public int read() {
                    if (maxLength == value)
                        return -1;
                    return value++ % 255;
                }
            }) {
                int totalCount = 1;
                for (int i = 0; i < totalCount; i++) {
                    insert.setBinaryStream(1, str);
                    insert.execute();
                }
                connection.commit();
                try (RecordCursorFactory factory = compiler.compile("xyz", sqlExecutionContext).getRecordCursorFactory()) {
                    try (RecordCursor cursor = factory.getCursor(sqlExecutionContext)) {
                        final Record record = cursor.getRecord();
                        int count = 0;
                        while (cursor.hasNext()) {
                            Assert.assertEquals(maxLength, record.getBinLen(0));
                            BinarySequence bs = record.getBin(0);
                            for (int i = 0; i < maxLength; i++) {
                                Assert.assertEquals(i % 255, // Convert byte to unsigned int
                                bs.byteAt(i) & 0xff);
                            }
                            count++;
                        }
                        Assert.assertEquals(totalCount, count);
                    }
                }
            }
        }
    });
}
Also used : BinarySequence(io.questdb.std.BinarySequence) RecordCursorFactory(io.questdb.cairo.sql.RecordCursorFactory) RecordCursor(io.questdb.cairo.sql.RecordCursor) InputStream(java.io.InputStream) BaseConnection(org.postgresql.core.BaseConnection) Record(io.questdb.cairo.sql.Record)

Example 3 with BinarySequence

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

the class InsertTest method testBindVariableInsert.

private void testBindVariableInsert(int partitionBy, TimestampFunction timestampFunction, boolean initBindVariables, boolean columnSet) throws Exception {
    assertMemoryLeak(() -> {
        CairoTestUtils.createAllTableWithNewTypes(configuration, partitionBy);
        // this is BLOB
        byte[] blob = new byte[500];
        TestBinarySequence bs = new TestBinarySequence();
        bs.of(blob);
        Rnd rnd = new Rnd();
        if (initBindVariables) {
            // this is type declaration to have query compile correctly
            bindVariableService.setInt(0, 0);
            bindVariableService.setShort(1, (short) 10);
            bindVariableService.setByte(2, (byte) 91);
            bindVariableService.setDouble(3, 9.2);
            bindVariableService.setFloat(4, 5.6f);
            bindVariableService.setLong(5, 99901);
            bindVariableService.setStr(6, "hello kitty");
            bindVariableService.setStr(7, "sym?");
            bindVariableService.setBoolean(8, true);
            bindVariableService.setBin(9, bs);
            bindVariableService.setDate(10, 1234L);
            bindVariableService.setLong256(11, 1, 2, 3, 4);
            bindVariableService.setChar(12, 'A');
            bindVariableService.setTimestamp(13, timestampFunction.getTimestamp());
        }
        final String sql;
        if (columnSet) {
            sql = "insert into all2 (" + "int, " + "short, " + "byte, " + "double, " + "float, " + "long, " + "str, " + "sym, " + "bool, " + "bin, " + "date, " + "long256, " + "chr, " + "timestamp" + ") values (" + "$1, " + "$2, " + "$3, " + "$4, " + "$5, " + "$6, " + "$7, " + "$8, " + "$9, " + "$10, " + "$11, " + "$12, " + "$13, " + "$14)";
        } else {
            sql = "insert into all2 values (" + "$1, " + "$2, " + "$3, " + "$4, " + "$5, " + "$6, " + "$7, " + "$8, " + "$9, " + "$10, " + "$11, " + "$12, " + "$13, " + "$14)";
        }
        final CompiledQuery cq = compiler.compile(sql, sqlExecutionContext);
        Assert.assertEquals(CompiledQuery.INSERT, cq.getType());
        InsertStatement insert = cq.getInsertStatement();
        try (InsertMethod method = insert.createMethod(sqlExecutionContext)) {
            for (int i = 0; i < 10_000; i++) {
                bindVariableService.setInt(0, rnd.nextInt());
                bindVariableService.setShort(1, rnd.nextShort());
                bindVariableService.setByte(2, rnd.nextByte());
                bindVariableService.setDouble(3, rnd.nextDouble());
                bindVariableService.setFloat(4, rnd.nextFloat());
                bindVariableService.setLong(5, rnd.nextLong());
                bindVariableService.setStr(6, rnd.nextChars(6));
                bindVariableService.setStr(7, rnd.nextChars(1));
                bindVariableService.setBoolean(8, rnd.nextBoolean());
                rnd.nextBytes(blob);
                bindVariableService.setBin(9, bs);
                bindVariableService.setDate(10, rnd.nextLong());
                bindVariableService.setLong256(11, rnd.nextLong(), rnd.nextLong(), rnd.nextLong(), rnd.nextLong());
                bindVariableService.setChar(12, rnd.nextChar());
                bindVariableService.setTimestamp(13, timestampFunction.getTimestamp());
                method.execute();
            }
            method.commit();
        }
        rnd.reset();
        try (TableReader reader = engine.getReader(sqlExecutionContext.getCairoSecurityContext(), "all2")) {
            final TableReaderRecordCursor cursor = reader.getCursor();
            final Record record = cursor.getRecord();
            while (cursor.hasNext()) {
                Assert.assertEquals(rnd.nextInt(), record.getInt(0));
                Assert.assertEquals(rnd.nextShort(), record.getShort(1));
                Assert.assertEquals(rnd.nextByte(), record.getByte(2));
                Assert.assertEquals(rnd.nextDouble(), record.getDouble(3), 0.0001);
                Assert.assertEquals(rnd.nextFloat(), record.getFloat(4), 0.000001);
                Assert.assertEquals(rnd.nextLong(), record.getLong(5));
                TestUtils.assertEquals(rnd.nextChars(6), record.getStr(6));
                TestUtils.assertEquals(rnd.nextChars(1), record.getSym(7));
                Assert.assertEquals(rnd.nextBoolean(), record.getBool(8));
                rnd.nextBytes(blob);
                BinarySequence binarySequence = record.getBin(9);
                Assert.assertEquals(blob.length, binarySequence.length());
                for (int j = 0, m = blob.length; j < m; j++) {
                    Assert.assertEquals(blob[j], binarySequence.byteAt(j));
                }
                Assert.assertEquals(rnd.nextLong(), record.getDate(10));
                Long256 long256 = record.getLong256A(11);
                Assert.assertEquals(rnd.nextLong(), long256.getLong0());
                Assert.assertEquals(rnd.nextLong(), long256.getLong1());
                Assert.assertEquals(rnd.nextLong(), long256.getLong2());
                Assert.assertEquals(rnd.nextLong(), long256.getLong3());
                Assert.assertEquals(rnd.nextChar(), record.getChar(12));
            }
        }
    });
}
Also used : TestBinarySequence(io.questdb.griffin.engine.TestBinarySequence) BinarySequence(io.questdb.std.BinarySequence) TestBinarySequence(io.questdb.griffin.engine.TestBinarySequence) Rnd(io.questdb.std.Rnd) Long256(io.questdb.std.Long256)

Aggregations

BinarySequence (io.questdb.std.BinarySequence)3 Record (io.questdb.cairo.sql.Record)2 Rnd (io.questdb.std.Rnd)2 RecordCursor (io.questdb.cairo.sql.RecordCursor)1 RecordCursorFactory (io.questdb.cairo.sql.RecordCursorFactory)1 TestBinarySequence (io.questdb.griffin.engine.TestBinarySequence)1 Long256 (io.questdb.std.Long256)1 InputStream (java.io.InputStream)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 CyclicBarrier (java.util.concurrent.CyclicBarrier)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 BaseConnection (org.postgresql.core.BaseConnection)1