Search in sources :

Example 1 with RecordCursorFactory

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);
        }
    });
}
Also used : RecordCursor(com.questdb.common.RecordCursor) Rnd(com.questdb.std.Rnd) RecordCursorFactory(com.questdb.cairo.sql.RecordCursorFactory) Record(com.questdb.common.Record) Test(org.junit.Test)

Aggregations

RecordCursorFactory (com.questdb.cairo.sql.RecordCursorFactory)1 Record (com.questdb.common.Record)1 RecordCursor (com.questdb.common.RecordCursor)1 Rnd (com.questdb.std.Rnd)1 Test (org.junit.Test)1