Search in sources :

Example 1 with DataFrameCursor

use of com.questdb.cairo.sql.DataFrameCursor in project questdb by bluestreak01.

the class FullTableFrameCursorFactoryTest 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();
        }
        // 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)) {
            FullTableFrameCursorFactory factory = new FullTableFrameCursorFactory(engine, "x");
            long count = 0;
            try (DataFrameCursor cursor = factory.getCursor()) {
                while (cursor.hasNext()) {
                    DataFrame frame = cursor.next();
                    count += frame.getRowHi() - frame.getRowLo();
                }
            }
            Assert.assertEquals(0, engine.getBusyReaderCount());
            Assert.assertEquals(M, count);
        }
    });
}
Also used : DataFrameCursor(com.questdb.cairo.sql.DataFrameCursor) Rnd(com.questdb.std.Rnd) DataFrame(com.questdb.cairo.sql.DataFrame) Test(org.junit.Test)

Example 2 with DataFrameCursor

use of com.questdb.cairo.sql.DataFrameCursor in project questdb by bluestreak01.

the class IntervalFrameCursorTest method testReload.

public void testReload(int partitionBy, long increment, LongList intervals, int rowCount, CharSequence expected1, CharSequence expected2) throws Exception {
    TestUtils.assertMemoryLeak(() -> {
        try (TableModel model = new TableModel(configuration, "x", partitionBy).col("a", ColumnType.SYMBOL).indexed(true, 4).col("b", ColumnType.SYMBOL).indexed(true, 4).timestamp()) {
            CairoTestUtils.create(model);
        }
        final Rnd rnd = new Rnd();
        long timestamp = DateFormatUtils.parseDateTime("1980-01-01T00:00:00.000Z");
        try (CairoEngine engine = new Engine(configuration)) {
            final TableReaderRecord record = new TableReaderRecord();
            final IntervalFrameCursorFactory factory = new IntervalFrameCursorFactory(engine, "x", intervals);
            try (DataFrameCursor cursor = factory.getCursor()) {
                // assert that there is nothing to start with
                record.of(cursor.getReader());
                assertEquals("", record, cursor);
                try (TableWriter writer = new TableWriter(configuration, "x")) {
                    for (int i = 0; i < rowCount; i++) {
                        TableWriter.Row row = writer.newRow(timestamp);
                        row.putSym(0, rnd.nextChars(4));
                        row.putSym(1, rnd.nextChars(4));
                        row.append();
                        timestamp += increment;
                    }
                    writer.commit();
                    Assert.assertTrue(cursor.reload());
                    assertEquals(expected1, record, cursor);
                    timestamp = Dates.addYear(timestamp, 3);
                    for (int i = 0; i < rowCount; i++) {
                        TableWriter.Row row = writer.newRow(timestamp);
                        row.putSym(0, rnd.nextChars(4));
                        row.putSym(1, rnd.nextChars(4));
                        row.append();
                        timestamp += increment;
                    }
                    writer.commit();
                    Assert.assertTrue(cursor.reload());
                    if (expected2 != null) {
                        assertEquals(expected2, record, cursor);
                    } else {
                        assertEquals(expected1, record, cursor);
                    }
                    Assert.assertFalse(cursor.reload());
                }
            }
        }
    });
}
Also used : DataFrameCursor(com.questdb.cairo.sql.DataFrameCursor) Rnd(com.questdb.std.Rnd) CairoEngine(com.questdb.cairo.sql.CairoEngine) CairoEngine(com.questdb.cairo.sql.CairoEngine)

Aggregations

DataFrameCursor (com.questdb.cairo.sql.DataFrameCursor)2 Rnd (com.questdb.std.Rnd)2 CairoEngine (com.questdb.cairo.sql.CairoEngine)1 DataFrame (com.questdb.cairo.sql.DataFrame)1 Test (org.junit.Test)1