use of io.questdb.cairo.sql.RecordCursor in project questdb by bluestreak01.
the class TableWriterTest method assertGeoStr.
private void assertGeoStr(String hash, int tableBits, long expected) {
final String tableName = "geo1";
try (TableModel model = new TableModel(configuration, tableName, PartitionBy.NONE)) {
model.col("g", ColumnType.getGeoHashTypeWithBits(tableBits));
CairoTestUtils.createTable(model);
}
try (TableWriter writer = new TableWriter(configuration, tableName)) {
TableWriter.Row r = writer.newRow();
r.putGeoStr(0, hash);
r.append();
writer.commit();
}
try (TableReader r = new TableReader(configuration, tableName)) {
final RecordCursor cursor = r.getCursor();
final Record record = cursor.getRecord();
final int type = r.getMetadata().getColumnType(0);
Assert.assertTrue(cursor.hasNext());
final long actual;
switch(ColumnType.tagOf(type)) {
case ColumnType.GEOBYTE:
actual = record.getGeoByte(0);
break;
case ColumnType.GEOSHORT:
actual = record.getGeoShort(0);
break;
case ColumnType.GEOINT:
actual = record.getGeoInt(0);
break;
default:
actual = record.getGeoLong(0);
break;
}
Assert.assertEquals(expected, actual);
}
}
use of io.questdb.cairo.sql.RecordCursor in project questdb by bluestreak01.
the class TableWriterTest method testO3WithCancelRow.
@Test
public void testO3WithCancelRow() throws Exception {
TestUtils.assertMemoryLeak(() -> {
try (TableModel model = new TableModel(configuration, "weather", PartitionBy.DAY).col("windspeed", ColumnType.DOUBLE).timestamp()) {
CairoTestUtils.create(model);
}
long[] tss = new long[] { 631150000000000L, 631152000000000L, 631160000000000L };
try (TableWriter writer = new TableWriter(configuration, "weather")) {
TableWriter.Row r = writer.newRow(tss[1]);
r.putDouble(0, 1.0);
r.append();
// Out of order
r = writer.newRow(tss[0]);
r.putDouble(0, 2.0);
r.append();
r = writer.newRow(tss[2]);
r.putDouble(0, 3.0);
r.cancel();
// Implicit commit
writer.addColumn("timetocycle", ColumnType.DOUBLE);
writer.newRow(tss[2]);
r.putDouble(0, 3.0);
r.putDouble(2, -1.0);
r.append();
writer.commit();
}
try (TableReader reader = new TableReader(configuration, "weather")) {
int col = reader.getMetadata().getColumnIndex("timestamp");
RecordCursor cursor = reader.getCursor();
final Record r = cursor.getRecord();
int i = 0;
while (cursor.hasNext()) {
Assert.assertEquals("Row " + i, tss[i++], r.getTimestamp(col));
}
Assert.assertEquals(tss.length, i);
}
});
}
use of io.questdb.cairo.sql.RecordCursor in project questdb by bluestreak01.
the class TableReaderTest method testReload.
private void testReload(int partitionBy, int count, long inct, final int testPartitionSwitch) throws Exception {
final long increment = inct * 1000;
CairoTestUtils.createAllTable(configuration, partitionBy);
TestUtils.assertMemoryLeak(() -> {
Rnd rnd = new Rnd();
long ts = TimestampFormatUtils.parseTimestamp("2013-03-04T00:00:00.000Z");
long blob = allocBlob();
try {
try (TableReader reader = new TableReader(configuration, "all")) {
// can we reload empty table?
Assert.assertFalse(reader.reload());
// reader can see all the rows ? Meaning none?
assertCursor(reader.getCursor(), ts, increment, blob, 0, null);
}
try (TableReader reader = new TableReader(configuration, "all")) {
RecordCursor cursor = reader.getCursor();
// this combination of reload/iterate/reload is deliberate
// we make sure that reload() behavior is not affected by
// iterating empty result set
Assert.assertFalse(reader.reload());
assertCursor(cursor, ts, increment, blob, 0, null);
Assert.assertFalse(reader.reload());
// create table with first batch populating all columns (there could be null values too)
long nextTs = testAppend(rnd, configuration, ts, count, increment, blob, 0);
// can we reload from empty to first batch?
Assert.assertTrue(reader.reload());
// make sure we can see first batch right after table is open
assertCursor(cursor, ts, increment, blob, count, BATCH1_ASSERTER);
// create another reader to make sure it can load data from constructor
try (TableReader reader2 = new TableReader(configuration, "all")) {
// make sure we can see first batch right after table is open
assertCursor(reader2.getCursor(), ts, increment, blob, count, BATCH1_ASSERTER);
}
// try to reload when table hasn't changed
Assert.assertFalse(reader.reload());
// add second batch to test if reload of open table will pick it up
nextTs = testAppend(rnd, configuration, nextTs, count, increment, blob, testPartitionSwitch);
// if we don't reload reader it should still see first batch
// reader can see all the rows ?
cursor.toTop();
assertPartialCursor(cursor, new Rnd(), ts, increment, blob, count / 4, BATCH1_ASSERTER);
// reload should be successful because we have new data in the table
Assert.assertTrue(reader.reload());
// check if we can see second batch after reader was reloaded
assertCursor(cursor, ts, increment, blob, 2L * count, BATCH1_ASSERTER);
// reader must be able to cope with that
try (TableWriter writer = new TableWriter(configuration, "all")) {
// this is a bit of paranoid check, but make sure our reader doesn't flinch when new writer is open
assertCursor(cursor, ts, increment, blob, 2L * count, BATCH1_ASSERTER);
// also make sure that there is nothing to reload, we've not done anything to data after all
Assert.assertFalse(reader.reload());
// check that we can still see two batches after no-op reload
// we rule out possibility of reload() corrupting table state
assertCursor(cursor, ts, increment, blob, 2L * count, BATCH1_ASSERTER);
// just for no reason add third batch
nextTs = testAppend(writer, rnd, nextTs, count, increment, blob, 0, BATCH1_GENERATOR);
// table must be able to reload now
Assert.assertTrue(reader.reload());
// and we should see three batches of data
assertCursor(cursor, ts, increment, blob, 3L * count, BATCH1_ASSERTER);
// this is where things get interesting
// add single column
writer.addColumn("str2", ColumnType.STRING);
// populate table with fourth batch, this time we also populate new column
// we expect that values of new column will be NULL for first three batches and non-NULL for fourth
nextTs = testAppend(writer, rnd, nextTs, count, increment, blob, 0, BATCH2_GENERATOR);
// reload table, check if it was positive effort
Assert.assertTrue(reader.reload());
// two-step assert checks 3/4 rows checking that new column is NUL
// the last 1/3 is checked including new column
// this is why we need to use same random state and timestamp
assertBatch2(count, increment, ts, blob, reader);
// good job we got as far as this
// lets now add another column and populate fifth batch, including new column
// reading this table will ensure tops are preserved
writer.addColumn("int2", ColumnType.INT);
nextTs = testAppend(writer, rnd, nextTs, count, increment, blob, 0, BATCH3_GENERATOR);
Assert.assertTrue(reader.reload());
assertBatch3(count, increment, ts, blob, reader);
// now append more columns that would overflow column buffer and force table to use different
// algo when retaining resources
writer.addColumn("short2", ColumnType.SHORT);
writer.addColumn("bool2", ColumnType.BOOLEAN);
writer.addColumn("byte2", ColumnType.BYTE);
writer.addColumn("float2", ColumnType.FLOAT);
writer.addColumn("double2", ColumnType.DOUBLE);
writer.addColumn("sym2", ColumnType.SYMBOL);
writer.addColumn("long2", ColumnType.LONG);
writer.addColumn("date2", ColumnType.DATE);
writer.addColumn("bin2", ColumnType.BINARY);
// populate new columns and start asserting batches, which would assert that new columns are
// retrospectively "null" in existing records
nextTs = testAppend(writer, rnd, nextTs, count, increment, blob, 0, BATCH4_GENERATOR);
Assert.assertTrue(reader.reload());
assertBatch4(count, increment, ts, blob, reader);
if (configuration.getFilesFacade().isRestrictedFileSystem()) {
reader.closeColumnForRemove("bin2");
}
writer.removeColumn("bin2");
Assert.assertTrue(reader.reload());
// and assert that all columns that have not been deleted contain correct values
assertBatch5(count, increment, ts, blob, cursor, new Rnd());
// append all columns excluding the one we just deleted
nextTs = testAppend(writer, rnd, nextTs, count, increment, blob, 0, BATCH6_GENERATOR);
Assert.assertTrue(reader.reload());
// and assert that all columns that have not been deleted contain correct values
assertBatch6(count, increment, ts, blob, cursor);
if (configuration.getFilesFacade().isRestrictedFileSystem()) {
reader.closeColumnForRemove("int");
}
// remove first column and add new column by same name
writer.removeColumn("int");
writer.addColumn("int", ColumnType.INT);
Assert.assertTrue(reader.reload());
assertBatch7(count, increment, ts, blob, cursor);
Assert.assertFalse(reader.reload());
nextTs = testAppend(writer, rnd, nextTs, count, increment, blob, 0, BATCH8_GENERATOR);
Assert.assertTrue(reader.reload());
assertBatch8(count, increment, ts, blob, cursor);
if (configuration.getFilesFacade().isRestrictedFileSystem()) {
reader.closeColumnForRemove("sym");
}
writer.removeColumn("sym");
writer.addColumn("sym", ColumnType.SYMBOL);
Assert.assertTrue(reader.reload());
testAppend(writer, rnd, nextTs, count, increment, blob, 0, BATCH9_GENERATOR);
Assert.assertTrue(reader.reload());
assertBatch9(count, increment, ts, blob, cursor);
}
}
} finally {
freeBlob(blob);
}
});
}
use of io.questdb.cairo.sql.RecordCursor in project questdb by bluestreak01.
the class TableReaderTest method testOver2GFile.
@Test
public void testOver2GFile() throws Exception {
TestUtils.assertMemoryLeak(() -> {
try (TableModel model = new TableModel(configuration, "x", PartitionBy.NONE).col("a", ColumnType.LONG)) {
CairoTestUtils.create(model);
}
long N = 280000000;
Rnd rnd = new Rnd();
try (TableWriter writer = new TableWriter(configuration, "x")) {
for (int i = 0; i < N; i++) {
TableWriter.Row r = writer.newRow();
r.putLong(0, rnd.nextLong());
r.append();
}
writer.commit();
}
try (TableReader reader = new TableReader(configuration, "x")) {
int count = 0;
rnd.reset();
RecordCursor cursor = reader.getCursor();
final Record record = cursor.getRecord();
while (cursor.hasNext()) {
Assert.assertEquals(rnd.nextLong(), record.getLong(0));
count++;
}
Assert.assertEquals(N, count);
}
});
}
use of io.questdb.cairo.sql.RecordCursor in project questdb by bluestreak01.
the class TableReaderTest method assertBatch3.
private void assertBatch3(int count, long increment, long ts, long blob, TableReader reader) {
Rnd exp = new Rnd();
long ts2;
RecordCursor cursor = reader.getCursor();
ts2 = assertPartialCursor(cursor, exp, ts, increment, blob, 3L * count, (r, rnd1, ts1, blob1) -> {
BATCH1_ASSERTER.assertRecord(r, rnd1, ts1, blob1);
BATCH2_BEFORE_ASSERTER.assertRecord(r, rnd1, ts1, blob1);
BATCH3_BEFORE_ASSERTER.assertRecord(r, rnd1, ts1, blob1);
});
ts2 = assertPartialCursor(cursor, exp, ts2, increment, blob, count, (r, rnd12, ts12, blob12) -> {
BATCH2_ASSERTER.assertRecord(r, rnd12, ts12, blob12);
BATCH3_BEFORE_ASSERTER.assertRecord(r, rnd12, ts12, blob12);
});
assertPartialCursor(cursor, exp, ts2, increment, blob, count, BATCH3_ASSERTER);
}
Aggregations