Search in sources :

Example 16 with RecordSourcePrinter

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

the class HttpServerTest method testImportNumberPrefixedColumn.

@Test
public void testImportNumberPrefixedColumn() throws Exception {
    final ServerConfiguration configuration = new ServerConfiguration();
    BootstrapEnv env = new BootstrapEnv();
    env.configuration = configuration;
    env.factory = getFactory();
    env.typeProbeCollection = TYPE_PROBE_COLLECTION;
    env.matcher = new SimpleUrlMatcher() {

        {
            put("/imp", new ImportHandler(env));
        }
    };
    HttpServer server = new HttpServer(env);
    server.start();
    try {
        Assert.assertEquals(200, HttpTestUtils.upload("/csv/test-import-num-prefix.csv", "http://localhost:9000/imp?fmt=json", null, null));
        StringSink sink = new StringSink();
        RecordSourcePrinter printer = new RecordSourcePrinter(sink);
        QueryCompiler qc = new QueryCompiler(env);
        try (RecordSource rs = qc.compile(env.factory, "select count(StrSym), count(IntSym), count(_1IntCol), count(long), count() from 'test-import-num-prefix.csv'")) {
            printer.print(rs, env.factory);
        }
        TestUtils.assertEquals("126\t126\t128\t129\t129\n", sink);
    } finally {
        server.halt();
    }
}
Also used : BootstrapEnv(com.questdb.BootstrapEnv) RecordSource(com.questdb.ql.RecordSource) ServerConfiguration(com.questdb.ServerConfiguration) RecordSourcePrinter(com.questdb.ql.RecordSourcePrinter) ImportHandler(com.questdb.net.http.handlers.ImportHandler) StringSink(com.questdb.std.str.StringSink) QueryCompiler(com.questdb.parser.sql.QueryCompiler) AbstractJournalTest(com.questdb.net.ha.AbstractJournalTest) Test(org.junit.Test)

Example 17 with RecordSourcePrinter

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

the class ReaderPoolTest method testConcurrentRead.

@Test
public void testConcurrentRead() throws Exception {
    final int readerCount = 5;
    int threadCount = 2;
    final int iterations = 1000000;
    Rnd dataRnd = new Rnd();
    final String[] names = new String[readerCount];
    final String[] expectedRows = new String[readerCount];
    final CharSequenceObjHashMap<String> expectedRowMap = new CharSequenceObjHashMap<>();
    for (int i = 0; i < readerCount; i++) {
        names[i] = "x" + i;
        try (TableModel model = new TableModel(configuration, names[i], PartitionBy.NONE).col("ts", ColumnType.DATE)) {
            CairoTestUtils.create(model);
        }
        try (TableWriter w = new TableWriter(configuration, names[i])) {
            for (int k = 0; k < 10; k++) {
                TableWriter.Row r = w.newRow(0);
                r.putDate(0, dataRnd.nextLong());
                r.append();
            }
            w.commit();
        }
        sink.clear();
        try (TableReader r = new TableReader(configuration, names[i])) {
            printer.print(r.getCursor(), true, r.getMetadata());
        }
        expectedRows[i] = sink.toString();
        expectedRowMap.put(names[i], expectedRows[i]);
    }
    assertWithPool((ReaderPool pool) -> {
        final CyclicBarrier barrier = new CyclicBarrier(threadCount);
        final CountDownLatch halt = new CountDownLatch(threadCount);
        final AtomicInteger errors = new AtomicInteger();
        for (int k = 0; k < threadCount; k++) {
            new Thread(new Runnable() {

                final ObjHashSet<TableReader> readers = new ObjHashSet<>();

                final StringSink sink = new StringSink();

                final RecordSourcePrinter printer = new RecordSourcePrinter(sink);

                @Override
                public void run() {
                    Rnd rnd = new Rnd();
                    try {
                        barrier.await();
                        String name;
                        // 3. it will close of of readers if has opened
                        for (int i = 0; i < iterations; i++) {
                            if (readers.size() == 0 || (readers.size() < 40 && rnd.nextPositiveInt() % 4 == 0)) {
                                name = names[rnd.nextPositiveInt() % readerCount];
                                try {
                                    Assert.assertTrue(readers.add(pool.get(name)));
                                } catch (EntryUnavailableException ignore) {
                                }
                            }
                            Thread.yield();
                            if (readers.size() == 0) {
                                continue;
                            }
                            int index = rnd.nextPositiveInt() % readers.size();
                            TableReader reader = readers.get(index);
                            Assert.assertTrue(reader.isOpen());
                            // read rows
                            RecordCursor cursor = reader.getCursor();
                            sink.clear();
                            printer.print(cursor, true, reader.getMetadata());
                            TestUtils.assertEquals(expectedRowMap.get(reader.getTableName()), sink);
                            Thread.yield();
                            if (readers.size() > 0 && rnd.nextPositiveInt() % 4 == 0) {
                                TableReader r2 = readers.get(rnd.nextPositiveInt() % readers.size());
                                Assert.assertTrue(r2.isOpen());
                                r2.close();
                                Assert.assertTrue(readers.remove(r2));
                            }
                            Thread.yield();
                        }
                    } catch (Exception e) {
                        errors.incrementAndGet();
                        e.printStackTrace();
                    } finally {
                        for (int i = 0; i < readers.size(); i++) {
                            readers.get(i).close();
                        }
                        halt.countDown();
                    }
                }
            }).start();
        }
        halt.await();
        Assert.assertEquals(0, halt.getCount());
        Assert.assertEquals(0, errors.get());
    });
}
Also used : RecordCursor(com.questdb.common.RecordCursor) RecordSourcePrinter(com.questdb.ql.RecordSourcePrinter) StringSink(com.questdb.std.str.StringSink) EntryUnavailableException(com.questdb.cairo.pool.ex.EntryUnavailableException) CountDownLatch(java.util.concurrent.CountDownLatch) EntryLockedException(com.questdb.cairo.pool.ex.EntryLockedException) EntryUnavailableException(com.questdb.cairo.pool.ex.EntryUnavailableException) PoolClosedException(com.questdb.cairo.pool.ex.PoolClosedException) CyclicBarrier(java.util.concurrent.CyclicBarrier) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.Test)

Aggregations

RecordSourcePrinter (com.questdb.ql.RecordSourcePrinter)17 StringSink (com.questdb.std.str.StringSink)14 Test (org.junit.Test)13 QueryCompiler (com.questdb.parser.sql.QueryCompiler)11 BootstrapEnv (com.questdb.BootstrapEnv)9 ServerConfiguration (com.questdb.ServerConfiguration)9 AbstractJournalTest (com.questdb.net.ha.AbstractJournalTest)9 ImportHandler (com.questdb.net.http.handlers.ImportHandler)9 RecordSource (com.questdb.ql.RecordSource)7 CountDownLatch (java.util.concurrent.CountDownLatch)5 RecordCursor (com.questdb.common.RecordCursor)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 EntryLockedException (com.questdb.cairo.pool.ex.EntryLockedException)2 EntryUnavailableException (com.questdb.cairo.pool.ex.EntryUnavailableException)2 PoolClosedException (com.questdb.cairo.pool.ex.PoolClosedException)2 ClientConfig (com.questdb.net.ha.config.ClientConfig)2 ServerConfig (com.questdb.net.ha.config.ServerConfig)2 ServerNode (com.questdb.net.ha.config.ServerNode)2 StdoutSink (com.questdb.std.str.StdoutSink)2 JournalKey (com.questdb.store.JournalKey)2