use of com.questdb.cairo.sql.RecordCursorFactory in project questdb by bluestreak01.
the class TableReaderRecordCursorFactoryTest method testFactory.
@Test
public void testFactory() throws Exception {
TestUtils.assertMemoryLeak(() -> {
final int N = 100;
// separate two symbol columns with primitive. It will make problems apparent if index does not shift correctly
try (TableModel model = new TableModel(configuration, "x", PartitionBy.DAY).col("a", ColumnType.STRING).col("b", ColumnType.SYMBOL).indexed(true, N / 4).col("i", ColumnType.INT).col("c", ColumnType.SYMBOL).indexed(true, N / 4).timestamp()) {
CairoTestUtils.create(model);
}
final Rnd rnd = new Rnd();
final String[] symbols = new String[N];
final int M = 1000;
final long increment = 1000000 * 60L * 10;
for (int i = 0; i < N; i++) {
symbols[i] = rnd.nextChars(8).toString();
}
rnd.reset();
// prepare the data
long timestamp = 0;
try (TableWriter writer = new TableWriter(configuration, "x")) {
for (int i = 0; i < M; i++) {
TableWriter.Row row = writer.newRow(timestamp += increment);
row.putStr(0, rnd.nextChars(20));
row.putSym(1, symbols[rnd.nextPositiveInt() % N]);
row.putInt(2, rnd.nextInt());
row.putSym(3, symbols[rnd.nextPositiveInt() % N]);
row.append();
}
writer.commit();
}
try (Engine engine = new Engine(configuration)) {
RecordCursorFactory factory = new TableReaderRecordCursorFactory(engine, "x");
long count = 0;
RecordCursor cursor = factory.getCursor();
try {
rnd.reset();
while (cursor.hasNext()) {
Record record = cursor.next();
TestUtils.assertEquals(rnd.nextChars(20), record.getFlyweightStr(0));
TestUtils.assertEquals(symbols[rnd.nextPositiveInt() % N], record.getSym(1));
Assert.assertEquals(rnd.nextInt(), record.getInt(2));
TestUtils.assertEquals(symbols[rnd.nextPositiveInt() % N], record.getSym(3));
count++;
}
} finally {
cursor.releaseCursor();
}
Assert.assertEquals(0, engine.getBusyReaderCount());
Assert.assertEquals(M, count);
}
});
}
Aggregations