Search in sources :

Example 96 with RecordCursor

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

the class CompactMapTest method testGeoHashValueAccess.

@Test
public void testGeoHashValueAccess() throws Exception {
    TestUtils.assertMemoryLeak(() -> {
        int precisionBits = 10;
        int geohashType = ColumnType.getGeoHashTypeWithBits(precisionBits);
        try (TableModel model = new TableModel(configuration, "x", PartitionBy.NONE)) {
            model.col("a", ColumnType.LONG).col("b", geohashType);
            CairoTestUtils.create(model);
        }
        BytecodeAssembler asm = new BytecodeAssembler();
        final int N = 1000;
        final Rnd rnd = new Rnd();
        try (TableWriter writer = new TableWriter(configuration, "x")) {
            for (int i = 0; i < N; i++) {
                TableWriter.Row row = writer.newRow();
                long rndGeohash = GeoHashes.fromCoordinatesDeg(rnd.nextDouble() * 180 - 90, rnd.nextDouble() * 360 - 180, precisionBits);
                row.putLong(0, i);
                row.putGeoHash(1, rndGeohash);
                row.append();
            }
            writer.commit();
        }
        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), N, 0.9, 1, Integer.MAX_VALUE)) {
                RecordSink sink = RecordSinkFactory.getInstance(asm, reader.getMetadata(), entityColumnFilter, true);
                RecordCursor cursor = reader.getCursor();
                long counter = 0;
                final Record record = cursor.getRecord();
                while (cursor.hasNext()) {
                    MapKey key = map.withKey();
                    key.put(record, sink);
                    MapValue value = key.createValue();
                    Assert.assertTrue(value.isNew());
                    value.putLong(0, counter++);
                }
                cursor.toTop();
                int count = 0;
                while (cursor.hasNext()) {
                    MapKey key = map.withKey();
                    key.put(record, sink);
                    MapValue value = key.findValue();
                    Assert.assertNotNull(value);
                    Assert.assertEquals(count++, value.getLong(0));
                }
                Assert.assertEquals(N, count);
            }
        }
    });
}
Also used : RecordCursor(io.questdb.cairo.sql.RecordCursor) Record(io.questdb.cairo.sql.Record) Test(org.junit.Test)

Example 97 with RecordCursor

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

the class CompactMapTest method testRowIdAccess.

@Test
public void testRowIdAccess() throws Exception {
    TestUtils.assertMemoryLeak(() -> {
        ColumnTypes types = new SingleColumnType(ColumnType.INT);
        final int N = 10000;
        final Rnd rnd = new Rnd();
        try (CompactMap map = new CompactMap(Numbers.SIZE_1MB, types, types, 64, 0.5, 1, Integer.MAX_VALUE)) {
            for (int i = 0; i < N; i++) {
                MapKey key = map.withKey();
                key.putInt(rnd.nextInt());
                MapValue values = key.createValue();
                Assert.assertTrue(values.isNew());
                values.putInt(0, i + 1);
            }
            // reset random generator and iterate map to double the value
            rnd.reset();
            LongList list = new LongList();
            try (RecordCursor mapCursor = map.getCursor()) {
                final MapRecord record = (MapRecord) mapCursor.getRecord();
                while (mapCursor.hasNext()) {
                    list.add(record.getRowId());
                    Assert.assertEquals(rnd.nextInt(), record.getInt(1));
                    MapValue value = record.getValue();
                    value.putInt(0, value.getInt(0) * 2);
                }
                // access map by rowid now
                rnd.reset();
                Record rec = mapCursor.getRecordB();
                for (int i = 0, n = list.size(); i < n; i++) {
                    mapCursor.recordAt(rec, list.getQuick(i));
                    Assert.assertEquals((i + 1) * 2, rec.getInt(0));
                    Assert.assertEquals(rnd.nextInt(), rec.getInt(1));
                }
                rnd.reset();
                Assert.assertNotSame(rec, mapCursor.getRecord());
                for (int i = 0, n = list.size(); i < n; i++) {
                    mapCursor.recordAt(rec, list.getQuick(i));
                    Assert.assertEquals((i + 1) * 2, rec.getInt(0));
                    Assert.assertEquals(rnd.nextInt(), rec.getInt(1));
                }
            }
        }
    });
}
Also used : RecordCursor(io.questdb.cairo.sql.RecordCursor) Record(io.questdb.cairo.sql.Record) Test(org.junit.Test)

Example 98 with RecordCursor

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

the class CompactMapTest 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();
        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), N, 0.9, 1, Integer.MAX_VALUE)) {
                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()) {
                    assertMap2(rnd, binarySequence, keyColumnOffset, rnd2, mapCursor);
                    mapCursor.toTop();
                    assertMap2(rnd, binarySequence, keyColumnOffset, rnd2, mapCursor);
                }
            }
        }
    });
}
Also used : RecordCursor(io.questdb.cairo.sql.RecordCursor) Test(org.junit.Test)

Example 99 with RecordCursor

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

the class CompactMapTest method testValueRandomWrite.

@Test
public void testValueRandomWrite() 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 SymbolAsIntTypes().of(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.9, 1, Integer.MAX_VALUE)) {
                RecordSink sink = RecordSinkFactory.getInstance(asm, reader.getMetadata(), entityColumnFilter, false);
                // this random will be populating values
                Rnd rnd2 = new Rnd();
                RecordCursor cursor = reader.getCursor();
                Record record = cursor.getRecord();
                long counter = 0;
                while (cursor.hasNext()) {
                    MapKey key = map.withKey();
                    key.put(record, sink);
                    MapValue value1 = key.createValue();
                    Assert.assertTrue(value1.isNew());
                    value1.putFloat(4, rnd2.nextFloat());
                    value1.putDouble(5, rnd2.nextDouble());
                    value1.putDate(6, rnd2.nextLong());
                    value1.putTimestamp(7, rnd2.nextLong());
                    value1.putBool(8, rnd2.nextBoolean());
                    value1.putLong(0, ++counter);
                    value1.putInt(1, rnd2.nextInt());
                    value1.putShort(2, rnd2.nextShort());
                    value1.putByte(3, rnd2.nextByte());
                }
                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(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(++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));
                }
            }
        }
    });
}
Also used : RecordCursor(io.questdb.cairo.sql.RecordCursor) Record(io.questdb.cairo.sql.Record) Test(org.junit.Test)

Example 100 with RecordCursor

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

the class TableWriterTest method testSymbolCacheFlag.

private void testSymbolCacheFlag(boolean cacheFlag) {
    try (TableModel model = new TableModel(configuration, "x", PartitionBy.NONE).col("a", ColumnType.SYMBOL).cached(cacheFlag).col("b", ColumnType.STRING).col("c", ColumnType.SYMBOL).cached(!cacheFlag).timestamp()) {
        CairoTestUtils.create(model);
    }
    int N = 1000;
    Rnd rnd = new Rnd();
    try (TableWriter writer = new TableWriter(configuration, "x")) {
        Assert.assertEquals(cacheFlag, writer.isSymbolMapWriterCached(0));
        Assert.assertNotEquals(cacheFlag, writer.isSymbolMapWriterCached(2));
        for (int i = 0; i < N; i++) {
            TableWriter.Row r = writer.newRow();
            r.putSym(0, rnd.nextChars(5));
            r.putStr(1, rnd.nextChars(10));
            r.append();
        }
        writer.commit();
    }
    try (TableReader reader = new TableReader(configuration, "x")) {
        rnd.reset();
        int count = 0;
        Assert.assertEquals(cacheFlag, reader.isColumnCached(0));
        Assert.assertNotEquals(cacheFlag, reader.isColumnCached(2));
        RecordCursor cursor = reader.getCursor();
        final Record record = cursor.getRecord();
        while (cursor.hasNext()) {
            TestUtils.assertEquals(rnd.nextChars(5), record.getSym(0));
            TestUtils.assertEquals(rnd.nextChars(10), record.getStr(1));
            count++;
        }
        Assert.assertEquals(N, count);
    }
}
Also used : RecordCursor(io.questdb.cairo.sql.RecordCursor) Record(io.questdb.cairo.sql.Record)

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