Search in sources :

Example 41 with StringSink

use of com.questdb.std.str.StringSink 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)

Example 42 with StringSink

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

the class DirectMapTest method testAllKeysAndCursor.

@Test
public void testAllKeysAndCursor() {
    // Objective of this test is to create DirectMap with all
    // possible types in both key and value. Simultaneously create
    // regular hash map, where key is hex encoded bytes of direct map
    // and value is object, which holds same primitive values we
    // put into direct map.
    Rnd rnd = new Rnd();
    final int addressSize = 2 * 1024 * 1024;
    long address = Unsafe.malloc(addressSize);
    final int tmpSize = 140;
    long tmp = Unsafe.malloc(tmpSize);
    DirectMap map = new DirectMap(1024, KEY_RESOLVER, VALUE_RESOLVER);
    StringSink sink = new StringSink();
    HashMap<String, MapValue> hashMap = new HashMap<>();
    for (int i = 0; i < 1000; i++) {
        sink.clear();
        DirectMap.KeyWriter w = map.keyWriter();
        long p = address;
        CharSequence cs = rnd.nextChars(rnd.nextInt() % 64);
        long l = rnd.nextLong();
        boolean b = rnd.nextBoolean();
        int ii = rnd.nextInt();
        short s = (short) rnd.nextInt();
        byte by = (byte) rnd.nextInt();
        double d = rnd.nextDouble();
        float f = rnd.nextFloat();
        CharSequence s2 = rnd.nextBoolean() ? null : cs;
        w.put(tmp, Chars.strcpyw(cs, tmp));
        w.putLong(l);
        w.putBool(b);
        w.putInt(ii);
        w.putShort(s);
        w.putByte(by);
        w.putDouble(d);
        w.putFloat(f);
        w.putLong(l);
        w.putStr(s2);
        w.putByte(by);
        // write same string to base64 buffer
        p = put(p, cs);
        p = put(p, l);
        p = put(p, b);
        p = put(p, ii);
        p = put(p, s);
        p = put(p, by);
        p = put(p, d);
        p = put(p, f);
        p = put(p, l);
        p = put(p, s2);
        p = put(p, by);
        MapValue v = new MapValue();
        DirectMapValues values = map.getOrCreateValues();
        values.putInt(0, v.i = rnd.nextPositiveInt());
        values.putLong(1, v.l = rnd.nextPositiveLong());
        values.putShort(2, v.s = (short) rnd.nextInt());
        values.putByte(3, v.bt = (byte) rnd.nextInt());
        values.putDouble(4, v.d = rnd.nextDouble());
        values.putFloat(5, v.f = rnd.nextFloat());
        hashMap.put(toStr(sink, address, p), v);
    }
    Assert.assertEquals(hashMap.size(), map.size());
    HashMap<Long, MapValue> rowidMap = new HashMap<>();
    for (DirectMapEntry e : map) {
        long p = address;
        // check that A and B return same sequence
        CharSequence csA = e.getFlyweightStr(6);
        CharSequence csB = e.getFlyweightStrB(6);
        TestUtils.assertEquals(csA, csB);
        p = put(p, csA);
        p = put(p, e.getLong(7));
        p = put(p, e.getBool(8));
        p = put(p, e.getInt(9));
        p = put(p, e.getShort(10));
        p = put(p, e.get(11));
        p = put(p, e.getDouble(12));
        p = put(p, e.getFloat(13));
        p = put(p, e.getDate(14));
        String s = e.getStr(15);
        p = put(p, s);
        sink.clear();
        e.getStr(15, sink);
        if (s == null) {
            Assert.assertEquals(0, sink.length());
            Assert.assertEquals(-1, e.getStrLen(15));
        } else {
            TestUtils.assertEquals(s, sink);
            Assert.assertEquals(s.length(), e.getStrLen(15));
        }
        p = put(p, e.get(16));
        sink.clear();
        MapValue v = hashMap.get(toStr(sink, address, p));
        Assert.assertEquals(v.i, e.getInt(0));
        Assert.assertEquals(v.l, e.getLong(1));
        Assert.assertEquals(v.s, e.getShort(2));
        Assert.assertEquals(v.bt, e.get(3));
        Assert.assertEquals(v.d, e.getDouble(4), 0.000000001);
        Assert.assertEquals(v.f, e.getFloat(5), 0.0000000001f);
        rowidMap.put(e.getRowId(), v);
    }
    for (Map.Entry<Long, MapValue> me : rowidMap.entrySet()) {
        DirectMapEntry e = map.entryAt(me.getKey());
        MapValue v = me.getValue();
        Assert.assertEquals(v.i, e.getInt(0));
        Assert.assertEquals(v.l, e.getLong(1));
        Assert.assertEquals(v.s, e.getShort(2));
        Assert.assertEquals(v.bt, e.get(3));
        Assert.assertEquals(v.d, e.getDouble(4), 0.000000001);
        Assert.assertEquals(v.f, e.getFloat(5), 0.0000000001f);
    }
    map.clear();
    Assert.assertEquals(0, map.size());
    int count = 0;
    for (DirectMapEntry ignored : map) {
        count++;
    }
    Assert.assertEquals(0, count);
    map.close();
    Unsafe.free(address, addressSize);
    Unsafe.free(tmp, tmpSize);
}
Also used : HashMap(java.util.HashMap) StringSink(com.questdb.std.str.StringSink) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 43 with StringSink

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

the class Lexer2Test method testLineComment.

@Test
public void testLineComment() {
    Lexer2 lex = new Lexer2();
    lex.defineSymbol("+");
    lex.defineSymbol("++");
    lex.defineSymbol("*");
    lex.defineSymbol("/*");
    lex.defineSymbol("*/");
    lex.defineSymbol("--");
    lex.setContent("a + -- ok, this is a comment \n 'b' * abc");
    StringSink sink = new StringSink();
    while (lex.hasNext()) {
        sink.put(lex.optionTok());
    }
    TestUtils.assertEquals("a+'b'*abc", sink);
}
Also used : StringSink(com.questdb.std.str.StringSink) Test(org.junit.Test)

Example 44 with StringSink

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

the class Lexer2Test method testSingleQuotedToken.

@Test
public void testSingleQuotedToken() {
    Lexer2 ts = new Lexer2();
    ts.defineSymbol("+");
    ts.defineSymbol("++");
    ts.defineSymbol("*");
    ts.setContent("a+'b'*abc");
    StringSink sink = new StringSink();
    for (CharSequence cs : ts) {
        sink.put(cs);
    }
    TestUtils.assertEquals("a+'b'*abc", sink);
}
Also used : DirectByteCharSequence(com.questdb.std.str.DirectByteCharSequence) StringSink(com.questdb.std.str.StringSink) Test(org.junit.Test)

Example 45 with StringSink

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

the class Lexer2Test method testEdgeSymbol.

@Test
public void testEdgeSymbol() {
    Lexer2 ts = new Lexer2();
    ts.defineSymbol(" ");
    ts.defineSymbol("+");
    ts.defineSymbol("(");
    ts.defineSymbol(")");
    ts.defineSymbol(",");
    CharSequence content;
    ts.setContent(content = "create journal xyz(a int, b int)");
    StringSink sink = new StringSink();
    for (CharSequence cs : ts) {
        sink.put(cs);
    }
    TestUtils.assertEquals(content, sink);
}
Also used : DirectByteCharSequence(com.questdb.std.str.DirectByteCharSequence) 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