Search in sources :

Example 16 with Record

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

the class CompactMapTest 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();
        CairoTestUtils.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 (CompactMap map = new CompactMap(1024 * 1024, 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.9, 1, Integer.MAX_VALUE)) {
                RecordSink sink = RecordSinkFactory.getInstance(asm, reader.getMetadata(), entityColumnFilter, true);
                // this random will be populating values
                Rnd rnd2 = new Rnd();
                RecordCursor cursor = reader.getCursor();
                final 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 17 with Record

use of io.questdb.cairo.sql.Record 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 Record

use of io.questdb.cairo.sql.Record 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 19 with Record

use of io.questdb.cairo.sql.Record 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)

Example 20 with Record

use of io.questdb.cairo.sql.Record 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)

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