Search in sources :

Example 26 with StringSink

use of com.questdb.std.str.StringSink in project questdb by bluestreak01.

the class LexerTest method testUnicode.

@Test
@Ignore
public void testUnicode() throws Exception {
    Lexer lex = new Lexer();
    lex.defineSymbol("+");
    lex.defineSymbol("++");
    lex.defineSymbol("*");
    String s = "'авг'";
    byte[] bb = s.getBytes("UTF8");
    System.out.println(new String(bb));
    long mem = Unsafe.malloc(bb.length);
    for (int i = 0; i < bb.length; i++) {
        Unsafe.getUnsafe().putByte(mem + i, bb[i]);
    }
    DirectByteCharSequence cs = new DirectByteCharSequence();
    cs.of(mem, mem + bb.length);
    lex.setContent(cs);
    StringSink sink = new StringSink();
    while (lex.hasNext()) {
        sink.put(lex.optionTok());
    }
    TestUtils.assertEquals("a+'b'*abc", sink);
}
Also used : DirectByteCharSequence(com.questdb.std.str.DirectByteCharSequence) StringSink(com.questdb.std.str.StringSink) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 27 with StringSink

use of com.questdb.std.str.StringSink in project questdb by bluestreak01.

the class FullTableFrameCursorTest method assertMetadataEquals.

private void assertMetadataEquals(RecordMetadata a, RecordMetadata b) {
    StringSink sinkA = new StringSink();
    StringSink sinkB = new StringSink();
    a.toJson(sinkA);
    b.toJson(sinkB);
    TestUtils.assertEquals(sinkA, sinkB);
}
Also used : StringSink(com.questdb.std.str.StringSink)

Example 28 with StringSink

use of com.questdb.std.str.StringSink in project questdb by bluestreak01.

the class ReaderPoolTest method testLockBusyReader.

@Test
public void testLockBusyReader() throws Exception {
    final int readerCount = 5;
    int threadCount = 2;
    final int iterations = 10000;
    Rnd dataRnd = new Rnd();
    StringSink sink = new StringSink();
    RecordSourcePrinter printer = new RecordSourcePrinter(sink);
    final String[] names = new String[readerCount];
    final String[] expectedRows = new String[readerCount];
    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();
    }
    LOG.info().$("testLockBusyReader BEGIN").$();
    assertWithPool(pool -> {
        final CyclicBarrier barrier = new CyclicBarrier(threadCount);
        final CountDownLatch halt = new CountDownLatch(threadCount);
        final AtomicInteger errors = new AtomicInteger();
        final LongList lockTimes = new LongList();
        final LongList workerTimes = new LongList();
        new Thread(() -> {
            Rnd rnd = new Rnd();
            try {
                barrier.await();
                String name;
                for (int i = 0; i < iterations; i++) {
                    name = names[rnd.nextPositiveInt() % readerCount];
                    while (true) {
                        if (pool.lock(name)) {
                            lockTimes.add(System.currentTimeMillis());
                            LockSupport.parkNanos(10L);
                            pool.unlock(name);
                            break;
                        }
                    }
                }
            } catch (Exception e) {
                errors.incrementAndGet();
                e.printStackTrace();
            } finally {
                halt.countDown();
            }
        }).start();
        new Thread(() -> {
            Rnd rnd = new Rnd();
            try {
                workerTimes.add(System.currentTimeMillis());
                for (int i = 0; i < iterations; i++) {
                    int index = rnd.nextPositiveInt() % readerCount;
                    String name = names[index];
                    try (TableReader r = pool.get(name)) {
                        RecordCursor cursor = r.getCursor();
                        sink.clear();
                        printer.print(cursor, true, r.getMetadata());
                        TestUtils.assertEquals(expectedRows[index], sink);
                        if (name.equals(names[readerCount - 1]) && barrier.getNumberWaiting() > 0) {
                            barrier.await();
                        }
                        LockSupport.parkNanos(10L);
                    } catch (EntryLockedException | EntryUnavailableException ignored) {
                    } catch (Exception e) {
                        errors.incrementAndGet();
                        e.printStackTrace();
                        break;
                    }
                }
                workerTimes.add(System.currentTimeMillis());
            } finally {
                halt.countDown();
            }
        }).start();
        halt.await();
        Assert.assertEquals(0, halt.getCount());
        Assert.assertEquals(0, errors.get());
        // check that there are lock times between worker times
        int count = 0;
        // ensure that we have worker times
        Assert.assertEquals(2, workerTimes.size());
        long lo = workerTimes.get(0);
        long hi = workerTimes.get(1);
        Assert.assertTrue(lockTimes.size() > 0);
        for (int i = 0, n = lockTimes.size(); i < n; i++) {
            long t = lockTimes.getQuick(i);
            if (t > lo && t < hi) {
                count++;
            }
        }
        Assert.assertTrue(count > 0);
        LOG.info().$("testLockBusyReader END").$();
    });
}
Also used : RecordCursor(com.questdb.common.RecordCursor) RecordSourcePrinter(com.questdb.ql.RecordSourcePrinter) StringSink(com.questdb.std.str.StringSink) 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)

Example 29 with StringSink

use of com.questdb.std.str.StringSink in project questdb by bluestreak01.

the class LinuxLineProtoReceiverTest method assertReceive.

private void assertReceive(ReceiverConfiguration receiverCfg, ReceiverFactory factory) throws Exception {
    TestUtils.assertMemoryLeak(() -> {
        final String expected = "colour\tshape\tsize\ttimestamp\n" + "blue\tsquare\t3.400000000000\t1970-01-01T00:01:40.000Z\n" + "blue\tsquare\t3.400000000000\t1970-01-01T00:01:40.000Z\n" + "blue\tsquare\t3.400000000000\t1970-01-01T00:01:40.000Z\n" + "blue\tsquare\t3.400000000000\t1970-01-01T00:01:40.000Z\n" + "blue\tsquare\t3.400000000000\t1970-01-01T00:01:40.000Z\n" + "blue\tsquare\t3.400000000000\t1970-01-01T00:01:40.000Z\n" + "blue\tsquare\t3.400000000000\t1970-01-01T00:01:40.000Z\n" + "blue\tsquare\t3.400000000000\t1970-01-01T00:01:40.000Z\n" + "blue\tsquare\t3.400000000000\t1970-01-01T00:01:40.000Z\n" + "blue\tsquare\t3.400000000000\t1970-01-01T00:01:40.000Z\n";
        CairoConfiguration cairoCfg = new DefaultCairoConfiguration(root);
        try (WriterPool pool = new WriterPool(cairoCfg)) {
            Job receiver = factory.createReceiver(receiverCfg, cairoCfg, pool);
            try {
                CountDownLatch workerHaltLatch = new CountDownLatch(1);
                try (TableModel model = new TableModel(configuration, "tab", PartitionBy.NONE).col("colour", ColumnType.SYMBOL).col("shape", ColumnType.SYMBOL).col("size", ColumnType.DOUBLE).timestamp()) {
                    CairoTestUtils.create(model);
                }
                // warm writer up
                try (TableWriter w = pool.get("tab")) {
                    w.warmUp();
                }
                ObjHashSet<Job> jobs = new ObjHashSet<>();
                jobs.add(receiver);
                Worker worker = new Worker(jobs, workerHaltLatch);
                worker.start();
                try (LineProtoSender sender = new LineProtoSender("127.0.0.1", receiverCfg.getPort(), 1400)) {
                    for (int i = 0; i < 10; i++) {
                        sender.metric("tab").tag("colour", "blue").tag("shape", "square").field("size", 3.4, 4).$(100000000);
                    }
                    sender.flush();
                }
                try (TableReader reader = new TableReader(cairoCfg, "tab")) {
                    int count = 1000000;
                    while (true) {
                        if (count-- > 0 && reader.size() < 10) {
                            reader.reload();
                            LockSupport.parkNanos(1);
                        } else {
                            break;
                        }
                    }
                    Assert.assertTrue(count > 0);
                    worker.halt();
                    Assert.assertTrue(workerHaltLatch.await(3, TimeUnit.SECONDS));
                    StringSink sink = new StringSink();
                    RecordSourcePrinter printer = new RecordSourcePrinter(sink);
                    printer.print(reader.getCursor(), true, reader.getMetadata());
                    TestUtils.assertEquals(expected, sink);
                }
            } finally {
                Misc.free(receiver);
            }
        }
    });
}
Also used : RecordSourcePrinter(com.questdb.ql.RecordSourcePrinter) StringSink(com.questdb.std.str.StringSink) WriterPool(com.questdb.cairo.pool.WriterPool) CountDownLatch(java.util.concurrent.CountDownLatch) Worker(com.questdb.mp.Worker) Job(com.questdb.mp.Job) LineProtoSender(com.questdb.cutlass.client.LineProtoSender)

Example 30 with StringSink

use of com.questdb.std.str.StringSink in project questdb by bluestreak01.

the class NumbersTest method testLong.

@Test
public void testLong() throws Exception {
    Rnd rnd = new Rnd();
    StringSink sink = new StringSink();
    for (int i = 0; i < 100; i++) {
        long l1 = rnd.nextLong();
        long l2 = rnd.nextLong();
        sink.clear();
        Numbers.append(sink, l1);
        int p = sink.length();
        Numbers.append(sink, l2);
        Assert.assertEquals(l1, Numbers.parseLong(sink, 0, p));
        Assert.assertEquals(l2, Numbers.parseLong(sink, p, sink.length()));
    }
}
Also used : Rnd(com.questdb.std.Rnd) StringSink(com.questdb.std.str.StringSink) Test(org.junit.Test)

Aggregations

StringSink (com.questdb.std.str.StringSink)52 Test (org.junit.Test)35 RecordSourcePrinter (com.questdb.ql.RecordSourcePrinter)14 BootstrapEnv (com.questdb.BootstrapEnv)11 ServerConfiguration (com.questdb.ServerConfiguration)11 DirectByteCharSequence (com.questdb.std.str.DirectByteCharSequence)10 AbstractJournalTest (com.questdb.net.ha.AbstractJournalTest)9 ImportHandler (com.questdb.net.http.handlers.ImportHandler)9 QueryCompiler (com.questdb.parser.sql.QueryCompiler)9 AbstractTest (com.questdb.test.tools.AbstractTest)7 RecordSource (com.questdb.ql.RecordSource)5 Rnd (com.questdb.std.Rnd)5 CountDownLatch (java.util.concurrent.CountDownLatch)5 Album (com.questdb.model.Album)4 Band (com.questdb.model.Band)4 SelectedColumnsRecordSource (com.questdb.ql.select.SelectedColumnsRecordSource)4 JournalEntryWriter (com.questdb.store.JournalEntryWriter)4 RecordColumnMetadata (com.questdb.common.RecordColumnMetadata)3 RecordCursor (com.questdb.common.RecordCursor)3 Quote (com.questdb.model.Quote)3