Search in sources :

Example 16 with RecordCursor

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

the class FastMapTest method testLong256AndCharAsKey.

@Test
public void testLong256AndCharAsKey() {
    Rnd rnd = new Rnd();
    ArrayColumnTypes keyTypes = new ArrayColumnTypes();
    ArrayColumnTypes valueTypes = new ArrayColumnTypes();
    keyTypes.add(ColumnType.LONG256);
    keyTypes.add(ColumnType.CHAR);
    valueTypes.add(ColumnType.DOUBLE);
    Long256Impl long256 = new Long256Impl();
    try (FastMap map = new FastMap(64, keyTypes, valueTypes, 64, 0.8, 24)) {
        final int N = 100000;
        for (int i = 0; i < N; i++) {
            MapKey key = map.withKey();
            long256.fromRnd(rnd);
            key.putLong256(long256);
            key.putChar(rnd.nextChar());
            MapValue value = key.createValue();
            Assert.assertTrue(value.isNew());
            value.putDouble(0, rnd.nextDouble());
        }
        rnd.reset();
        // assert that all values are good
        for (int i = 0; i < N; i++) {
            MapKey key = map.withKey();
            long256.fromRnd(rnd);
            key.putLong256(long256);
            key.putChar(rnd.nextChar());
            MapValue value = key.createValue();
            Assert.assertFalse(value.isNew());
            Assert.assertEquals(rnd.nextDouble(), value.getDouble(0), 0.000000001d);
        }
        try (RecordCursor cursor = map.getCursor()) {
            rnd.reset();
            assertCursorLong256(rnd, cursor, long256);
            rnd.reset();
            cursor.toTop();
            assertCursorLong256(rnd, cursor, long256);
        }
    }
}
Also used : RecordCursor(io.questdb.cairo.sql.RecordCursor) Test(org.junit.Test)

Example 17 with RecordCursor

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

the class FastMapTest method testValueAccess.

@Test
public void testValueAccess() throws Exception {
    TestUtils.assertMemoryLeak(() -> {
        final int N = 1000;
        final Rnd rnd = new Rnd();
        TestRecord.ArrayBinarySequence binarySequence = new TestRecord.ArrayBinarySequence();
        createTestTable(N, rnd, binarySequence);
        BytecodeAssembler asm = new BytecodeAssembler();
        try (TableReader reader = new TableReader(configuration, "x")) {
            EntityColumnFilter entityColumnFilter = new EntityColumnFilter();
            entityColumnFilter.of(reader.getMetadata().getColumnCount());
            try (FastMap map = new FastMap(Numbers.SIZE_1MB, new SymbolAsStrTypes(reader.getMetadata()), new ArrayColumnTypes().add(ColumnType.LONG).add(ColumnType.INT).add(ColumnType.SHORT).add(ColumnType.BYTE).add(ColumnType.FLOAT).add(ColumnType.DOUBLE).add(ColumnType.DATE).add(ColumnType.TIMESTAMP).add(ColumnType.BOOLEAN).add(ColumnType.getGeoHashTypeWithBits(5)).add(ColumnType.getGeoHashTypeWithBits(10)).add(ColumnType.getGeoHashTypeWithBits(20)).add(ColumnType.getGeoHashTypeWithBits(40)), N, 0.9f, 1)) {
                RecordSink sink = RecordSinkFactory.getInstance(asm, reader.getMetadata(), entityColumnFilter, true);
                // this random will be populating values
                Rnd rnd2 = new Rnd();
                RecordCursor cursor = reader.getCursor();
                Record record = cursor.getRecord();
                populateMapGeo(map, rnd2, cursor, sink);
                cursor.toTop();
                rnd2.reset();
                long c = 0;
                while (cursor.hasNext()) {
                    MapKey key = map.withKey();
                    key.put(record, sink);
                    MapValue value = key.findValue();
                    Assert.assertNotNull(value);
                    Assert.assertEquals(++c, value.getLong(0));
                    Assert.assertEquals(rnd2.nextInt(), value.getInt(1));
                    Assert.assertEquals(rnd2.nextShort(), value.getShort(2));
                    Assert.assertEquals(rnd2.nextByte(), value.getByte(3));
                    Assert.assertEquals(rnd2.nextFloat(), value.getFloat(4), 0.000001f);
                    Assert.assertEquals(rnd2.nextDouble(), value.getDouble(5), 0.000000001);
                    Assert.assertEquals(rnd2.nextLong(), value.getDate(6));
                    Assert.assertEquals(rnd2.nextLong(), value.getTimestamp(7));
                    Assert.assertEquals(rnd2.nextBoolean(), value.getBool(8));
                    Assert.assertEquals((byte) Math.abs(rnd2.nextByte()), value.getGeoByte(9));
                    Assert.assertEquals((short) Math.abs(rnd2.nextShort()), value.getGeoShort(10));
                    Assert.assertEquals(Math.abs(rnd2.nextInt()), value.getGeoInt(11));
                    Assert.assertEquals(Math.abs(rnd2.nextLong()), value.getGeoLong(12));
                }
            }
        }
    });
}
Also used : RecordCursor(io.questdb.cairo.sql.RecordCursor) Record(io.questdb.cairo.sql.Record) Test(org.junit.Test)

Example 18 with RecordCursor

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

the class FastMapTest method testRecordAsKey.

@Test
public void testRecordAsKey() throws Exception {
    TestUtils.assertMemoryLeak(() -> {
        final int N = 5000;
        final Rnd rnd = new Rnd();
        TestRecord.ArrayBinarySequence binarySequence = new TestRecord.ArrayBinarySequence();
        createTestTable(N, rnd, binarySequence);
        BytecodeAssembler asm = new BytecodeAssembler();
        try (TableReader reader = new TableReader(configuration, "x")) {
            EntityColumnFilter entityColumnFilter = new EntityColumnFilter();
            entityColumnFilter.of(reader.getMetadata().getColumnCount());
            try (FastMap map = new FastMap(Numbers.SIZE_1MB, new SymbolAsStrTypes(reader.getMetadata()), new ArrayColumnTypes().add(ColumnType.LONG).add(ColumnType.INT).add(ColumnType.SHORT).add(ColumnType.BYTE).add(ColumnType.FLOAT).add(ColumnType.DOUBLE).add(ColumnType.DATE).add(ColumnType.TIMESTAMP).add(ColumnType.BOOLEAN), N, 0.9f, 1)) {
                RecordSink sink = RecordSinkFactory.getInstance(asm, reader.getMetadata(), entityColumnFilter, true);
                final int keyColumnOffset = map.getValueColumnCount();
                // this random will be populating values
                Rnd rnd2 = new Rnd();
                RecordCursor cursor = reader.getCursor();
                populateMap(map, rnd2, cursor, sink);
                try (RecordCursor mapCursor = map.getCursor()) {
                    assertCursor2(rnd, binarySequence, keyColumnOffset, rnd2, mapCursor);
                    mapCursor.toTop();
                    assertCursor2(rnd, binarySequence, keyColumnOffset, rnd2, mapCursor);
                }
            }
        }
    });
}
Also used : RecordCursor(io.questdb.cairo.sql.RecordCursor) Test(org.junit.Test)

Example 19 with RecordCursor

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

the class TableReaderTest method testConcurrentReloadMultiplePartitions.

private void testConcurrentReloadMultiplePartitions(int partitionBy, long stride) throws Exception {
    TestUtils.assertMemoryLeak(() -> {
        final int N = 1024_0000;
        // model table
        try (TableModel model = new TableModel(configuration, "w", partitionBy).col("l", ColumnType.LONG).timestamp()) {
            CairoTestUtils.create(model);
        }
        final int threads = 2;
        final CyclicBarrier startBarrier = new CyclicBarrier(threads);
        final CountDownLatch stopLatch = new CountDownLatch(threads);
        final AtomicInteger errors = new AtomicInteger(0);
        // start writer
        new Thread(() -> {
            try {
                startBarrier.await();
                long timestampUs = TimestampFormatUtils.parseTimestamp("2017-12-11T00:00:00.000Z");
                try (TableWriter writer = new TableWriter(configuration, "w")) {
                    for (int i = 0; i < N; i++) {
                        TableWriter.Row row = writer.newRow(timestampUs);
                        row.putLong(0, i);
                        row.append();
                        writer.commit();
                        timestampUs += stride;
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
                errors.incrementAndGet();
            } finally {
                stopLatch.countDown();
            }
        }).start();
        // start reader
        new Thread(() -> {
            try {
                startBarrier.await();
                try (TableReader reader = new TableReader(configuration, "w")) {
                    RecordCursor cursor = reader.getCursor();
                    final Record record = cursor.getRecord();
                    sink.clear();
                    ((Sinkable) record).toSink(sink);
                    TestUtils.assertEquals("TableReaderRecord [columnBase=0, recordIndex=-1]", sink);
                    do {
                        // we deliberately ignore result of reload()
                        // to create more race conditions
                        reader.reload();
                        cursor.toTop();
                        int count = 0;
                        while (cursor.hasNext()) {
                            if (count++ != record.getLong(0)) {
                                sink.clear();
                                sink.put("Test [count=").put(count--).put(", rec=").put(record.getLong(0)).put(']').put(',');
                                ((Sinkable) record).toSink(sink);
                                Assert.fail(sink.toString());
                            }
                        }
                        if (count == N) {
                            break;
                        }
                    } while (true);
                }
            } catch (Throwable e) {
                e.printStackTrace();
                errors.incrementAndGet();
            } finally {
                stopLatch.countDown();
            }
        }).start();
        Assert.assertTrue(stopLatch.await(120, TimeUnit.SECONDS));
        Assert.assertEquals(0, errors.get());
        // check that we had multiple partitions created during the test
        try (TableReader reader = new TableReader(configuration, "w")) {
            Assert.assertTrue(reader.getPartitionCount() > 10);
        }
    });
}
Also used : RecordCursor(io.questdb.cairo.sql.RecordCursor) CountDownLatch(java.util.concurrent.CountDownLatch) CyclicBarrier(java.util.concurrent.CyclicBarrier) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Record(io.questdb.cairo.sql.Record)

Example 20 with RecordCursor

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

the class TableReaderTest method testRemoveDefaultPartition.

@Test
public void testRemoveDefaultPartition() throws Exception {
    TestUtils.assertMemoryLeak(() -> {
        int N = 100;
        int N_PARTITIONS = 5;
        long timestampUs = TimestampFormatUtils.parseTimestamp("2017-12-11T00:00:00.000Z");
        long stride = 100;
        int bandStride = 1000;
        int totalCount = 0;
        // model table
        try (TableModel model = new TableModel(configuration, "w", PartitionBy.NONE).col("l", ColumnType.LONG).timestamp()) {
            CairoTestUtils.create(model);
        }
        try (TableWriter writer = new TableWriter(configuration, "w")) {
            for (int k = 0; k < N_PARTITIONS; k++) {
                long band = k * bandStride;
                for (int i = 0; i < N; i++) {
                    TableWriter.Row row = writer.newRow(timestampUs);
                    row.putLong(0, band + i);
                    row.append();
                    writer.commit();
                    timestampUs += stride;
                }
                timestampUs = Timestamps.addDays(Timestamps.floorDD(timestampUs), 1);
            }
            Assert.assertEquals(N * N_PARTITIONS, writer.size());
            Assert.assertFalse(writer.removePartition(0));
            Assert.assertEquals(N * N_PARTITIONS, writer.size());
        }
        // now open table reader having partition gap
        try (TableReader reader = new TableReader(configuration, "w")) {
            Assert.assertEquals(N * N_PARTITIONS, reader.size());
            RecordCursor cursor = reader.getCursor();
            final Record record = cursor.getRecord();
            while (cursor.hasNext()) {
                record.getLong(0);
                totalCount++;
            }
        }
        Assert.assertEquals(N * N_PARTITIONS, totalCount);
    });
}
Also used : RecordCursor(io.questdb.cairo.sql.RecordCursor) Record(io.questdb.cairo.sql.Record) Test(org.junit.Test)

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