use of io.questdb.cairo.sql.Record in project questdb by bluestreak01.
the class RecordValueSinkFactoryTest method testAllSupportedTypes.
@Test
public void testAllSupportedTypes() {
SingleColumnType keyTypes = new SingleColumnType(ColumnType.INT);
try (TableModel model = new TableModel(configuration, "all", PartitionBy.NONE).col("int", ColumnType.INT).col("short", ColumnType.SHORT).col("byte", ColumnType.BYTE).col("double", ColumnType.DOUBLE).col("float", ColumnType.FLOAT).col("long", ColumnType.LONG).col("sym", ColumnType.SYMBOL).symbolCapacity(64).col("bool", ColumnType.BOOLEAN).col("date", ColumnType.DATE).col("ts", ColumnType.TIMESTAMP)) {
CairoTestUtils.create(model);
}
final int N = 1024;
final Rnd rnd = new Rnd();
try (TableWriter writer = new TableWriter(configuration, "all")) {
for (int i = 0; i < N; i++) {
TableWriter.Row row = writer.newRow();
row.putInt(0, rnd.nextInt());
row.putShort(1, rnd.nextShort());
row.putByte(2, rnd.nextByte());
row.putDouble(3, rnd.nextDouble());
row.putFloat(4, rnd.nextFloat());
row.putLong(5, rnd.nextLong());
row.putSym(6, rnd.nextChars(10));
row.putBool(7, rnd.nextBoolean());
row.putDate(8, rnd.nextLong());
row.putTimestamp(9, rnd.nextLong());
row.append();
}
writer.commit();
}
try (TableReader reader = new TableReader(configuration, "all")) {
final SymbolAsIntTypes valueTypes = new SymbolAsIntTypes().of(reader.getMetadata());
try (final Map map = new FastMap(Numbers.SIZE_1MB, keyTypes, valueTypes, N, 0.5, 100)) {
EntityColumnFilter columnFilter = new EntityColumnFilter();
columnFilter.of(reader.getMetadata().getColumnCount());
RecordValueSink sink = RecordValueSinkFactory.getInstance(new BytecodeAssembler(), reader.getMetadata(), columnFilter);
RecordCursor cursor = reader.getCursor();
final Record record = cursor.getRecord();
int index = 0;
while (cursor.hasNext()) {
MapKey key = map.withKey();
key.putInt(index++);
MapValue value = key.createValue();
sink.copy(record, value);
}
Assert.assertEquals(N, index);
rnd.reset();
StaticSymbolTable symbolTable = reader.getSymbolMapReader(6);
for (int i = 0; i < N; i++) {
MapKey key = map.withKey();
key.putInt(i);
MapValue value = key.findValue();
Assert.assertNotNull(value);
Assert.assertEquals(rnd.nextInt(), value.getInt(0));
Assert.assertEquals(rnd.nextShort(), value.getShort(1));
Assert.assertEquals(rnd.nextByte(), value.getByte(2));
Assert.assertEquals(rnd.nextDouble(), value.getDouble(3), 0.000001);
Assert.assertEquals(rnd.nextFloat(), value.getFloat(4), 0.000001f);
Assert.assertEquals(rnd.nextLong(), value.getLong(5));
Assert.assertEquals(symbolTable.keyOf(rnd.nextChars(10)), value.getInt(6));
Assert.assertEquals(rnd.nextBoolean(), value.getBool(7));
Assert.assertEquals(rnd.nextLong(), value.getDate(8));
Assert.assertEquals(rnd.nextLong(), value.getTimestamp(9));
}
}
}
}
use of io.questdb.cairo.sql.Record in project questdb by bluestreak01.
the class CompactMapTest method populateMap.
private void populateMap(CompactMap map, Rnd rnd2, RecordCursor cursor, RecordSink sink) {
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);
value.putInt(1, rnd2.nextInt());
value.putShort(2, rnd2.nextShort());
value.putByte(3, rnd2.nextByte());
value.putFloat(4, rnd2.nextFloat());
value.putDouble(5, rnd2.nextDouble());
value.putDate(6, rnd2.nextLong());
value.putTimestamp(7, rnd2.nextLong());
value.putBool(8, rnd2.nextBoolean());
}
}
use of io.questdb.cairo.sql.Record in project questdb by bluestreak01.
the class CompactMapTest method populateMapGeo.
private void populateMapGeo(CompactMap map, Rnd rnd2, RecordCursor cursor, RecordSink sink) {
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);
value.putInt(1, rnd2.nextInt());
value.putShort(2, rnd2.nextShort());
value.putByte(3, rnd2.nextByte());
value.putFloat(4, rnd2.nextFloat());
value.putDouble(5, rnd2.nextDouble());
value.putDate(6, rnd2.nextLong());
value.putTimestamp(7, rnd2.nextLong());
value.putBool(8, rnd2.nextBoolean());
value.putByte(9, (byte) Math.abs(rnd2.nextByte()));
value.putShort(10, (short) Math.abs(rnd2.nextShort()));
value.putInt(11, Math.abs(rnd2.nextInt()));
value.putLong(12, Math.abs(rnd2.nextLong()));
}
}
use of io.questdb.cairo.sql.Record 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);
}
}
});
}
use of io.questdb.cairo.sql.Record 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));
}
}
}
});
}
Aggregations