Search in sources :

Example 11 with RecordSource

use of com.questdb.ql.RecordSource in project questdb by bluestreak01.

the class SQLParameters method main.

public static void main(String[] args) throws JournalException, ParserException, IOException {
    if (args.length < 1) {
        System.out.println("Usage: SQLParameters <path>");
        System.exit(1);
    }
    JournalConfiguration configuration = new JournalConfigurationBuilder().build(args[0]);
    try (Factory factory = new Factory(configuration, 1000, 1, 0)) {
        // import movies data to query
        ImportManager.importFile(factory, SQLParameters.class.getResource("/movies.csv").getFile(), ',', null, false);
        // Create SQL engine instance.
        QueryCompiler compiler = new QueryCompiler();
        try (RecordSource rs = compiler.compile(factory, "'movies.csv' where movieId = :id")) {
            RecordSourcePrinter printer = new RecordSourcePrinter(new StdoutSink());
            // use of parameters avoids expensive query compilation
            rs.getParam(":id").set(62198);
            printer.print(rs, factory);
            System.out.println("----------------");
            rs.getParam(":id").set(125);
            printer.print(rs, factory);
        }
    }
}
Also used : JournalConfiguration(com.questdb.store.factory.configuration.JournalConfiguration) RecordSource(com.questdb.ql.RecordSource) StdoutSink(com.questdb.std.str.StdoutSink) RecordSourcePrinter(com.questdb.ql.RecordSourcePrinter) Factory(com.questdb.store.factory.Factory) JournalConfigurationBuilder(com.questdb.store.factory.configuration.JournalConfigurationBuilder) QueryCompiler(com.questdb.parser.sql.QueryCompiler)

Example 12 with RecordSource

use of com.questdb.ql.RecordSource in project questdb by bluestreak01.

the class GenericInteropTest method testObjectWriteGenericRead.

@Test
public void testObjectWriteGenericRead() throws Exception {
    try (JournalWriter<Data> writer = getFactory().writer(new JournalMetadataBuilder<Data>(Data.class, "test") {

        {
            $sym("sym").index();
            $int("id").index();
            $str("rateId").index();
        }
    })) {
        Data d = new Data();
        d.sym = "GBPUSD";
        d.created = 30000;
        d.bid = 0.65;
        d.ask = 0.66;
        d.bidSize = 1000;
        d.askSize = 1100;
        d.id = 1;
        d.status = "OK";
        d.user = "system";
        d.rateId = "GBPUSD:GLOBAL";
        d.active = true;
        d.nullable = null;
        d.ticks = 12345678;
        d.modulo = 425;
        writer.append(d);
        writer.commit();
    }
    try (RecordSource rs = compile("test")) {
        RecordCursor cursor = rs.prepareCursor(getFactory());
        try {
            Record e;
            Assert.assertTrue(cursor.hasNext());
            Assert.assertNotNull(e = cursor.next());
            Assert.assertEquals("GBPUSD", e.getSym(0));
            Assert.assertEquals(30000, e.getDate(1));
            Assert.assertEquals(0.65, e.getDouble(2), 0.000001);
            Assert.assertEquals(0.66, e.getDouble(3), 0.000001);
            Assert.assertEquals(1000, e.getInt(4));
            Assert.assertEquals(1100, e.getInt(5));
            Assert.assertEquals(1, e.getByte(6));
            TestUtils.assertEquals("OK", e.getFlyweightStr(7));
            TestUtils.assertEquals("system", e.getFlyweightStr(8));
            TestUtils.assertEquals("GBPUSD:GLOBAL", e.getFlyweightStr(9));
            Assert.assertTrue(e.getBool(10));
            Assert.assertNull(e.getFlyweightStr(11));
            Assert.assertEquals(12345678, e.getLong(12));
            Assert.assertEquals(425, e.getShort(13));
            Assert.assertFalse(cursor.hasNext());
        } finally {
            cursor.releaseCursor();
        }
    }
}
Also used : RecordSource(com.questdb.ql.RecordSource) RecordCursor(com.questdb.common.RecordCursor) Record(com.questdb.common.Record) AbstractTest(com.questdb.test.tools.AbstractTest) Test(org.junit.Test)

Example 13 with RecordSource

use of com.questdb.ql.RecordSource in project questdb by bluestreak01.

the class PerformanceTest method testAllBySymbolValueOverIntervalNew.

@SuppressWarnings("StatementWithEmptyBody")
@Test
public void testAllBySymbolValueOverIntervalNew() throws JournalException, ParserException, NumericException {
    try (JournalWriter<Quote> w = getFactory().writer(Quote.class, "quote", TEST_DATA_SIZE)) {
        TestUtils.generateQuoteData(w, TEST_DATA_SIZE, DateFormatUtils.parseDateTime("2013-10-05T10:00:00.000Z"), 1000);
        w.commit();
    }
    QueryCompiler compiler = new QueryCompiler();
    Factory factory = getFactory();
    try (RecordSource src = compiler.compile(factory, "quote where timestamp = '2013-10-05T10:00:00.000Z;10d' and sym = 'LLOY.L'")) {
        int count = 1000;
        long t = 0;
        for (int i = -count; i < count; i++) {
            if (i == 0) {
                t = System.nanoTime();
            }
            RecordCursor c = src.prepareCursor(factory);
            try {
                for (; c.hasNext(); ) {
                    c.next();
                }
            } finally {
                c.releaseCursor();
            }
        }
        LOG.info().$("NEW journal.query().all().withKeys(\"LLOY.L\").slice(interval) (query only) latency: ").$((System.nanoTime() - t) / count / 1000).$("μs").$();
    }
}
Also used : Quote(com.questdb.model.Quote) RecordSource(com.questdb.ql.RecordSource) Factory(com.questdb.store.factory.Factory) LogFactory(com.questdb.log.LogFactory) ReaderFactory(com.questdb.store.factory.ReaderFactory) QueryCompiler(com.questdb.parser.sql.QueryCompiler) AbstractTest(com.questdb.test.tools.AbstractTest) Test(org.junit.Test)

Example 14 with RecordSource

use of com.questdb.ql.RecordSource in project questdb by bluestreak01.

the class PerformanceTest method testJournalAppendAndReadSpeed.

@Test
public void testJournalAppendAndReadSpeed() throws JournalException, ParserException, NumericException {
    int count = 10;
    long t = 0;
    long result;
    try (JournalWriter<Quote> w = getFactory().writer(Quote.class, "quote", TEST_DATA_SIZE)) {
        for (int i = -count; i < count; i++) {
            w.truncate();
            if (i == 0) {
                t = System.nanoTime();
            }
            TestUtils.generateQuoteData(w, TEST_DATA_SIZE, DateFormatUtils.parseDateTime("2013-10-05T10:00:00.000Z"), 1000);
            w.commit();
        }
        result = System.nanoTime() - t;
        LOG.info().$("append (1M): ").$(TimeUnit.NANOSECONDS.toMillis(result / count)).$("ms").$();
        if (enabled) {
            Assert.assertTrue("Append speed must be under 400ms (" + TimeUnit.NANOSECONDS.toMillis(result) + ")", TimeUnit.NANOSECONDS.toMillis(result) < 400);
        }
        for (int i = -count; i < count; i++) {
            if (i == 0) {
                t = System.nanoTime();
            }
            Iterator<Quote> iterator = JournalIterators.bufferedIterator(w);
            int cnt = 0;
            while (iterator.hasNext()) {
                iterator.next();
                cnt++;
            }
            Assert.assertEquals(TEST_DATA_SIZE, cnt);
        }
        result = System.nanoTime() - t;
        LOG.info().$("read (1M): ").$(TimeUnit.NANOSECONDS.toMillis(result / count)).$("ms").$();
        if (enabled) {
            Assert.assertTrue("Read speed must be under 120ms (" + TimeUnit.NANOSECONDS.toMillis(result) + ")", TimeUnit.NANOSECONDS.toMillis(result) < 120);
        }
    }
    ReaderFactory readerFactory = getFactory();
    try (RecordSource rs = compile("quote")) {
        for (int i = -count; i < count; i++) {
            if (i == 0) {
                t = System.nanoTime();
            }
            RecordCursor s = rs.prepareCursor(readerFactory);
            try {
                int cnt = 0;
                for (Record r : s) {
                    r.getLong(0);
                    r.getSym(1);
                    r.getDouble(2);
                    r.getDouble(3);
                    r.getInt(4);
                    r.getInt(5);
                    r.getSym(6);
                    r.getSym(7);
                    cnt++;
                }
                Assert.assertEquals(TEST_DATA_SIZE, cnt);
            } finally {
                s.releaseCursor();
            }
        }
    }
    result = System.nanoTime() - t;
    LOG.info().$("generic read (1M): ").$(TimeUnit.NANOSECONDS.toMillis(result / count)).$("ms").$();
    if (enabled) {
        Assert.assertTrue("Read speed must be under 60ms (" + TimeUnit.NANOSECONDS.toMillis(result) + ")", TimeUnit.NANOSECONDS.toMillis(result) < 60);
    }
}
Also used : Quote(com.questdb.model.Quote) RecordSource(com.questdb.ql.RecordSource) ReaderFactory(com.questdb.store.factory.ReaderFactory) AbstractTest(com.questdb.test.tools.AbstractTest) Test(org.junit.Test)

Example 15 with RecordSource

use of com.questdb.ql.RecordSource in project questdb by bluestreak01.

the class RecordKeyCopierCompilerTest method testCompiler.

@Test
public void testCompiler() throws Exception {
    try (JournalWriter w = compiler.createWriter(FACTORY_CONTAINER.getFactory(), "create table x (a INT, b BYTE, c SHORT, d LONG, e FLOAT, f DOUBLE, g DATE, h BINARY, t DATE, x SYMBOL, z STRING, y BOOLEAN) timestamp(t) partition by MONTH record hint 100")) {
        JournalEntryWriter ew = w.entryWriter();
        IntList keyColumns = new IntList();
        ew.putInt(0, 12345);
        keyColumns.add(0);
        ew.put(1, (byte) 128);
        keyColumns.add(1);
        ew.putShort(2, (short) 6500);
        keyColumns.add(2);
        ew.putLong(3, 123456789);
        keyColumns.add(3);
        ew.putFloat(4, 0.345f);
        keyColumns.add(4);
        ew.putDouble(5, 0.123456789);
        keyColumns.add(5);
        ew.putDate(6, 10000000000L);
        keyColumns.add(6);
        ew.putSym(9, "xyz");
        keyColumns.add(9);
        ew.putStr(10, "abc");
        keyColumns.add(10);
        ew.putBool(11, true);
        keyColumns.add(11);
        ew.append();
        w.commit();
        try (RecordSource src = compileSource("x")) {
            RecordKeyCopierCompiler cc = new RecordKeyCopierCompiler(new BytecodeAssembler());
            RecordKeyCopier copier = cc.compile(src.getMetadata(), keyColumns);
            IntList valueTypes = new IntList();
            valueTypes.add(ColumnType.DOUBLE);
            MetadataTypeResolver metadataTypeResolver = new MetadataTypeResolver();
            TypeListResolver typeListResolver = new TypeListResolver();
            try (DirectMap map = new DirectMap(1024, metadataTypeResolver.of(src.getMetadata(), keyColumns), typeListResolver.of(valueTypes))) {
                RecordCursor cursor = src.prepareCursor(FACTORY_CONTAINER.getFactory());
                try {
                    while (cursor.hasNext()) {
                        Record r = cursor.next();
                        DirectMap.KeyWriter kw = map.keyWriter();
                        copier.copy(r, kw);
                        DirectMapValues val = map.getOrCreateValues();
                        val.putDouble(0, 5000.01);
                    }
                    cursor.toTop();
                    while (cursor.hasNext()) {
                        Record r = cursor.next();
                        DirectMap.KeyWriter kw = map.keyWriter();
                        copier.copy(r, kw);
                        Assert.assertEquals(map.getValues().getDouble(0), 5000.01, 0.00000001);
                    }
                } finally {
                    cursor.releaseCursor();
                }
            }
        }
    }
}
Also used : JournalWriter(com.questdb.store.JournalWriter) RecordCursor(com.questdb.common.RecordCursor) BytecodeAssembler(com.questdb.std.BytecodeAssembler) IntList(com.questdb.std.IntList) RecordSource(com.questdb.ql.RecordSource) Record(com.questdb.common.Record) JournalEntryWriter(com.questdb.store.JournalEntryWriter) Test(org.junit.Test) AbstractOptimiserTest(com.questdb.parser.sql.AbstractOptimiserTest)

Aggregations

RecordSource (com.questdb.ql.RecordSource)30 Test (org.junit.Test)18 AbstractTest (com.questdb.test.tools.AbstractTest)12 RecordCursor (com.questdb.common.RecordCursor)11 Record (com.questdb.common.Record)10 QueryCompiler (com.questdb.parser.sql.QueryCompiler)7 RecordSourcePrinter (com.questdb.ql.RecordSourcePrinter)7 StringSink (com.questdb.std.str.StringSink)5 JournalWriter (com.questdb.store.JournalWriter)5 JournalEntryWriter (com.questdb.store.JournalEntryWriter)4 Factory (com.questdb.store.factory.Factory)4 BootstrapEnv (com.questdb.BootstrapEnv)3 ServerConfiguration (com.questdb.ServerConfiguration)3 Quote (com.questdb.model.Quote)3 AbstractJournalTest (com.questdb.net.ha.AbstractJournalTest)3 ImportHandler (com.questdb.net.http.handlers.ImportHandler)3 File (java.io.File)3 SymbolTable (com.questdb.common.SymbolTable)2 ClientConfig (com.questdb.net.ha.config.ClientConfig)2 ServerConfig (com.questdb.net.ha.config.ServerConfig)2