Search in sources :

Example 1 with DataFrameCursor

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

the class FullFwdDataFrameCursorFactoryTest method testFactory.

@Test
public void testFactory() throws Exception {
    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();
        }
        FullFwdDataFrameCursorFactory factory = new FullFwdDataFrameCursorFactory(engine, "x", TableUtils.ANY_TABLE_ID, 0);
        long count = 0;
        try (DataFrameCursor cursor = factory.getCursor(AllowAllSqlSecurityContext.INSTANCE)) {
            DataFrame frame;
            while ((frame = cursor.next()) != null) {
                count += frame.getRowHi() - frame.getRowLo();
            }
        }
        Assert.assertEquals(0, engine.getBusyReaderCount());
        Assert.assertEquals(M, count);
        try (TableWriter writer = engine.getWriter(AllowAllCairoSecurityContext.INSTANCE, "x", "testing")) {
            writer.removeColumn("b");
        }
        try {
            factory.getCursor(AllowAllSqlSecurityContext.INSTANCE);
            Assert.fail();
        } catch (ReaderOutOfDateException ignored) {
        }
    });
}
Also used : DataFrameCursor(io.questdb.cairo.sql.DataFrameCursor) Rnd(io.questdb.std.Rnd) DataFrame(io.questdb.cairo.sql.DataFrame) ReaderOutOfDateException(io.questdb.cairo.sql.ReaderOutOfDateException) Test(org.junit.Test)

Example 2 with DataFrameCursor

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

the class FullBwdDataFrameCursorTest method testReload.

@Test
public void testReload() throws Exception {
    final String expected = "-409854405\t339631474\t1970-01-04T00:00:00.000000Z\n" + "1569490116\t1573662097\t1970-01-03T16:00:00.000000Z\n" + "806715481\t1545253512\t1970-01-03T08:00:00.000000Z\n" + "-1436881714\t-1575378703\t1970-01-03T00:00:00.000000Z\n" + "-1191262516\t-2041844972\t1970-01-02T16:00:00.000000Z\n" + "1868723706\t-847531048\t1970-01-02T08:00:00.000000Z\n" + "1326447242\t592859671\t1970-01-02T00:00:00.000000Z\n" + "73575701\t-948263339\t1970-01-01T16:00:00.000000Z\n" + "1548800833\t-727724771\t1970-01-01T08:00:00.000000Z\n" + "-1148479920\t315515118\t1970-01-01T00:00:00.000000Z\n";
    final String expectedNext = "-1975183723\t-1252906348\t1975-01-04T00:00:00.000000Z\n" + "-1125169127\t1631244228\t1975-01-03T16:00:00.000000Z\n" + "1404198\t-1715058769\t1975-01-03T08:00:00.000000Z\n" + "-1101822104\t-1153445279\t1975-01-03T00:00:00.000000Z\n" + "-1844391305\t-1520872171\t1975-01-02T16:00:00.000000Z\n" + "-85170055\t-1792928964\t1975-01-02T08:00:00.000000Z\n" + "-1432278050\t426455968\t1975-01-02T00:00:00.000000Z\n" + "1125579207\t-1849627000\t1975-01-01T16:00:00.000000Z\n" + "-1532328444\t-1458132197\t1975-01-01T08:00:00.000000Z\n" + "1530831067\t1904508147\t1975-01-01T00:00:00.000000Z\n";
    assertMemoryLeak(() -> {
        try (TableModel model = new TableModel(configuration, "x", PartitionBy.DAY).col("a", ColumnType.INT).col("b", ColumnType.INT).timestamp()) {
            CairoTestUtils.create(model);
        }
        Rnd rnd = new Rnd();
        long timestamp = 0;
        long increment = 3600000000L * 8;
        int N = 10;
        try (TableWriter w = new TableWriter(configuration, "x")) {
            for (int i = 0; i < N; i++) {
                TableWriter.Row row = w.newRow(timestamp);
                row.putInt(0, rnd.nextInt());
                row.putInt(1, rnd.nextInt());
                row.append();
                timestamp += increment;
            }
            w.commit();
            Assert.assertEquals(N, w.size());
            FullBwdDataFrameCursorFactory factory = new FullBwdDataFrameCursorFactory(engine, "x", TableUtils.ANY_TABLE_ID, 0);
            final TableReaderRecord record = new TableReaderRecord();
            try (final DataFrameCursor cursor = factory.getCursor(AllowAllSqlSecurityContext.INSTANCE)) {
                printCursor(record, cursor);
                TestUtils.assertEquals(expected, sink);
                // now add some more rows
                timestamp = TimestampFormatUtils.parseTimestamp("1975-01-01T00:00:00.000Z");
                for (int i = 0; i < N; i++) {
                    TableWriter.Row row = w.newRow(timestamp);
                    row.putInt(0, rnd.nextInt());
                    row.putInt(1, rnd.nextInt());
                    row.append();
                    timestamp += increment;
                }
                w.commit();
                Assert.assertTrue(cursor.reload());
                printCursor(record, cursor);
                TestUtils.assertEquals(expectedNext + expected, sink);
            }
            w.removeColumn("a");
            try {
                factory.getCursor(AllowAllSqlSecurityContext.INSTANCE);
                Assert.fail();
            } catch (ReaderOutOfDateException ignored) {
            }
        }
    });
}
Also used : DataFrameCursor(io.questdb.cairo.sql.DataFrameCursor) Rnd(io.questdb.std.Rnd) ReaderOutOfDateException(io.questdb.cairo.sql.ReaderOutOfDateException) Test(org.junit.Test)

Aggregations

DataFrameCursor (io.questdb.cairo.sql.DataFrameCursor)2 ReaderOutOfDateException (io.questdb.cairo.sql.ReaderOutOfDateException)2 Rnd (io.questdb.std.Rnd)2 Test (org.junit.Test)2 DataFrame (io.questdb.cairo.sql.DataFrame)1