Search in sources :

Example 21 with Record

use of com.questdb.common.Record in project questdb by bluestreak01.

the class TableReadFailTest method testReloadTimeout.

@Test
public void testReloadTimeout() throws Exception {
    TestUtils.assertMemoryLeak(() -> {
        try (TableModel model = new TableModel(configuration, "x", PartitionBy.NONE).col("a", ColumnType.INT).col("b", ColumnType.LONG).timestamp()) {
            CairoTestUtils.create(model);
        }
        try (Path path = new Path();
            TableReader reader = new TableReader(configuration, "x");
            ReadWriteMemory mem = new ReadWriteMemory()) {
            final Rnd rnd = new Rnd();
            final int N = 1000;
            // home path at txn file
            path.of(configuration.getRoot()).concat("x").concat(TableUtils.TXN_FILE_NAME).$();
            try (TableWriter w = new TableWriter(configuration, "x")) {
                for (int i = 0; i < N; i++) {
                    TableWriter.Row r = w.newRow(0);
                    r.putInt(0, rnd.nextInt());
                    r.putLong(1, rnd.nextLong());
                    r.append();
                }
                w.commit();
            }
            Assert.assertTrue(reader.reload());
            RecordCursor cursor = reader.getCursor();
            rnd.reset();
            int count = 0;
            while (cursor.hasNext()) {
                Record r = cursor.next();
                Assert.assertEquals(rnd.nextInt(), r.getInt(0));
                Assert.assertEquals(rnd.nextLong(), r.getLong(1));
                count++;
            }
            Assert.assertEquals(N, count);
            mem.of(configuration.getFilesFacade(), path, configuration.getFilesFacade().getPageSize());
            // keep txn file parameters
            long offset = configuration.getFilesFacade().length(mem.getFd());
            long txn = mem.getLong(TableUtils.TX_OFFSET_TXN);
            // corrupt the txn file
            mem.jumpTo(TableUtils.TX_OFFSET_TXN);
            mem.putLong(123);
            mem.jumpTo(offset);
            mem.close();
            // this should time out
            try {
                reader.reload();
                Assert.fail();
            } catch (CairoException e) {
                TestUtils.assertContains(e.getMessage(), "timeout");
            }
            // restore txn file to its former glory
            mem.of(configuration.getFilesFacade(), path, configuration.getFilesFacade().getPageSize());
            mem.jumpTo(TableUtils.TX_OFFSET_TXN);
            mem.putLong(txn);
            mem.jumpTo(offset);
            mem.close();
            mem.close();
            // make sure reload functions correctly
            Assert.assertFalse(reader.reload());
            try (TableWriter w = new TableWriter(configuration, "x")) {
                // add more data
                for (int i = 0; i < N; i++) {
                    TableWriter.Row r = w.newRow(0);
                    r.putInt(0, rnd.nextInt());
                    r.putLong(1, rnd.nextLong());
                    r.append();
                }
                w.commit();
            }
            // does positive reload work?
            Assert.assertTrue(reader.reload());
            // can reader still see correct data?
            cursor = reader.getCursor();
            rnd.reset();
            count = 0;
            while (cursor.hasNext()) {
                Record r = cursor.next();
                Assert.assertEquals(rnd.nextInt(), r.getInt(0));
                Assert.assertEquals(rnd.nextLong(), r.getLong(1));
                count++;
            }
            Assert.assertEquals(2 * N, count);
        }
    });
}
Also used : Path(com.questdb.std.str.Path) RecordCursor(com.questdb.common.RecordCursor) Rnd(com.questdb.std.Rnd) Record(com.questdb.common.Record) Test(org.junit.Test)

Example 22 with Record

use of com.questdb.common.Record 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)

Example 23 with Record

use of com.questdb.common.Record in project questdb by bluestreak01.

the class AbstractTest method assertSymbol.

public void assertSymbol(String query) throws ParserException {
    try (RecordSource src = compiler.compile(getFactory(), query)) {
        RecordCursor cursor = src.prepareCursor(getFactory());
        try {
            SymbolTable tab = cursor.getStorageFacade().getSymbolTable(0);
            while (cursor.hasNext()) {
                Record r = cursor.next();
                TestUtils.assertEquals(r.getSym(0), tab.value(r.getInt(0)));
            }
        } finally {
            cursor.releaseCursor();
        }
    }
}
Also used : RecordSource(com.questdb.ql.RecordSource) RecordCursor(com.questdb.common.RecordCursor) SymbolTable(com.questdb.common.SymbolTable) Record(com.questdb.common.Record)

Example 24 with Record

use of com.questdb.common.Record in project questdb by bluestreak01.

the class Generator method fetchCursor.

public static int fetchCursor(RecordCursor cursor) {
    int count = 0;
    while (cursor.hasNext()) {
        Record r = cursor.next();
        r.getInt(0);
        r.getFlyweightStr(1);
        r.getDate(2);
        count++;
    }
    return count;
}
Also used : Record(com.questdb.common.Record)

Example 25 with Record

use of com.questdb.common.Record in project questdb by bluestreak01.

the class AvgAggregator method computeAvg.

private void computeAvg(DirectMapValues values) {
    long ref = values.getLong(oListHead);
    if (ref == -1) {
        // no partial sums found, compute average of what we have accumulated so far
        values.putDouble(oAvg, values.getDouble(oSum) / (double) values.getLong(oLocalTotal));
    } else {
        // there are partial sums
        // compute average of last partial sum (which hasn't been added to record list)
        double total = (double) values.getLong(oTotal);
        double count = (double) values.getLong(oLocalTotal);
        double avg = (count / total) * (values.getDouble(oSum) / count);
        // add partial sums together using weight of each sum
        // weight = partial count / total count
        records.of(ref);
        while (records.hasNext()) {
            Record r = records.next();
            count = r.getLong(1);
            avg += (count / total) * (r.getDouble(0) / count);
        }
        values.putDouble(oAvg, avg);
    }
}
Also used : Record(com.questdb.common.Record)

Aggregations

Record (com.questdb.common.Record)29 RecordCursor (com.questdb.common.RecordCursor)19 Test (org.junit.Test)13 RecordSource (com.questdb.ql.RecordSource)10 JournalWriter (com.questdb.store.JournalWriter)5 AbstractTest (com.questdb.test.tools.AbstractTest)5 FakeRecord (com.questdb.ql.join.hash.FakeRecord)4 JournalEntryWriter (com.questdb.store.JournalEntryWriter)4 NullableRecord (com.questdb.ql.NullableRecord)3 Rnd (com.questdb.std.Rnd)3 SymbolTable (com.questdb.common.SymbolTable)2 AbstractOptimiserTest (com.questdb.parser.sql.AbstractOptimiserTest)2 LongList (com.questdb.std.LongList)2 RedBlackTree (com.questdb.std.RedBlackTree)2 ArrayList (java.util.ArrayList)2 RecordCursorFactory (com.questdb.cairo.sql.RecordCursorFactory)1 QueryCompiler (com.questdb.parser.sql.QueryCompiler)1 RecordSourcePrinter (com.questdb.ql.RecordSourcePrinter)1 AsOfPartitionedJoinRecordSource (com.questdb.ql.join.AsOfPartitionedJoinRecordSource)1 BytecodeAssembler (com.questdb.std.BytecodeAssembler)1