Search in sources :

Example 96 with StringSink

use of io.questdb.std.str.StringSink in project questdb by questdb.

the class TableReaderTailRecordCursorTest method testBusyPoll.

private void testBusyPoll(long timestampIncrement, int n, String createStatement) throws Exception {
    assertMemoryLeak(() -> {
        compiler.compile(createStatement, sqlExecutionContext);
        final AtomicInteger errorCount = new AtomicInteger();
        final CyclicBarrier barrier = new CyclicBarrier(2);
        final CountDownLatch latch = new CountDownLatch(2);
        new Thread(() -> {
            try (TableWriter writer = engine.getWriter(AllowAllCairoSecurityContext.INSTANCE, "xyz", "testing")) {
                barrier.await();
                long ts = 0;
                long addr = Unsafe.malloc(128, MemoryTag.NATIVE_DEFAULT);
                try {
                    Rnd rnd = new Rnd();
                    for (int i = 0; i < n; i++) {
                        if (errorCount.get() > 0) {
                            // Reader already failed
                            return;
                        }
                        TableWriter.Row row = writer.newRow(ts);
                        row.putInt(0, i);
                        for (int k = 0; k < 128; k++) {
                            Unsafe.getUnsafe().putByte(addr + k, rnd.nextByte());
                        }
                        row.putBin(1, addr, 128);
                        row.putLong(2, rnd.nextLong());
                        row.append();
                        writer.commit();
                        ts += timestampIncrement;
                    }
                } finally {
                    Unsafe.free(addr, 128, MemoryTag.NATIVE_DEFAULT);
                }
            } catch (Throwable e) {
                e.printStackTrace();
                errorCount.incrementAndGet();
            } finally {
                latch.countDown();
            }
        }).start();
        new Thread(() -> {
            try (TableReader reader = engine.getReader(AllowAllCairoSecurityContext.INSTANCE, "xyz", TableUtils.ANY_TABLE_ID, TableUtils.ANY_TABLE_VERSION)) {
                Rnd rnd = new Rnd();
                int count = 0;
                final TableReaderTailRecordCursor cursor = new TableReaderTailRecordCursor();
                cursor.of(reader);
                final Record record = cursor.getRecord();
                barrier.await();
                while (count < n) {
                    if (cursor.reload()) {
                        while (cursor.hasNext()) {
                            if (count != record.getInt(0)) {
                                errorCount.incrementAndGet();
                                StringSink ss = new StringSink();
                                ss.put("[");
                                for (int i = 0; i < reader.getPartitionCount(); i++) {
                                    ss.put(reader.getPartitionRowCount(i));
                                    ss.put(",");
                                }
                                ss.put("]:").put(reader.getTxn());
                                int val = record.getInt(0);
                                Assert.assertEquals(ss.toString(), count, record.getInt(0));
                            }
                            BinarySequence binarySequence = record.getBin(1);
                            for (int i = 0; i < 128; i++) {
                                Assert.assertEquals(rnd.nextByte(), binarySequence.byteAt(i));
                            }
                            Assert.assertEquals(rnd.nextLong(), record.getLong(2));
                            count++;
                        }
                    }
                }
            } catch (Throwable e) {
                e.printStackTrace();
                errorCount.incrementAndGet();
            } finally {
                latch.countDown();
            }
        }).start();
        Assert.assertTrue(latch.await(600, TimeUnit.SECONDS));
        Assert.assertEquals(0, errorCount.get());
        Thread.sleep(1000);
    });
}
Also used : BinarySequence(io.questdb.std.BinarySequence) Rnd(io.questdb.std.Rnd) StringSink(io.questdb.std.str.StringSink) CountDownLatch(java.util.concurrent.CountDownLatch) CyclicBarrier(java.util.concurrent.CyclicBarrier) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Record(io.questdb.cairo.sql.Record)

Example 97 with StringSink

use of io.questdb.std.str.StringSink in project questdb by questdb.

the class CastDoubleToStrFunctionFactory method newInstance.

@Override
public Function newInstance(int position, ObjList<Function> args, IntList argPositions, CairoConfiguration configuration, SqlExecutionContext sqlExecutionContext) {
    Function intFunc = args.getQuick(0);
    if (intFunc.isConstant()) {
        final StringSink sink = Misc.getThreadLocalBuilder();
        sink.put(intFunc.getDouble(null), configuration.getDoubleToStrCastScale());
        return new StrConstant(Chars.toString(sink));
    }
    return new Func(args.getQuick(0), configuration.getDoubleToStrCastScale());
}
Also used : UnaryFunction(io.questdb.griffin.engine.functions.UnaryFunction) Function(io.questdb.cairo.sql.Function) StrFunction(io.questdb.griffin.engine.functions.StrFunction) StringSink(io.questdb.std.str.StringSink) StrConstant(io.questdb.griffin.engine.functions.constants.StrConstant)

Example 98 with StringSink

use of io.questdb.std.str.StringSink in project questdb by questdb.

the class CastDoubleToSymbolFunctionFactory method newInstance.

@Override
public Function newInstance(int position, ObjList<Function> args, IntList argPositions, CairoConfiguration configuration, SqlExecutionContext sqlExecutionContext) {
    final Function arg = args.getQuick(0);
    if (arg.isConstant()) {
        final StringSink sink = Misc.getThreadLocalBuilder();
        sink.put(arg.getDouble(null), configuration.getDoubleToStrCastScale());
        return SymbolConstant.newInstance(sink);
    }
    return new Func(arg, configuration.getDoubleToStrCastScale());
}
Also used : SymbolFunction(io.questdb.griffin.engine.functions.SymbolFunction) UnaryFunction(io.questdb.griffin.engine.functions.UnaryFunction) Function(io.questdb.cairo.sql.Function) StringSink(io.questdb.std.str.StringSink)

Example 99 with StringSink

use of io.questdb.std.str.StringSink in project questdb by questdb.

the class CastDateToStrFunctionFactory method newInstance.

@Override
public Function newInstance(int position, ObjList<Function> args, IntList argPositions, CairoConfiguration configuration, SqlExecutionContext sqlExecutionContext) {
    Function func = args.getQuick(0);
    if (func.isConstant()) {
        StringSink sink = Misc.getThreadLocalBuilder();
        sink.put(func.getDate(null));
        return new StrConstant(Chars.toString(sink));
    }
    return new Func(args.getQuick(0));
}
Also used : Function(io.questdb.cairo.sql.Function) StrFunction(io.questdb.griffin.engine.functions.StrFunction) UnaryFunction(io.questdb.griffin.engine.functions.UnaryFunction) StringSink(io.questdb.std.str.StringSink) StrConstant(io.questdb.griffin.engine.functions.constants.StrConstant)

Example 100 with StringSink

use of io.questdb.std.str.StringSink in project questdb by questdb.

the class CastCharToSymbolFunctionFactory method newInstance.

@Override
public Function newInstance(int position, ObjList<Function> args, IntList argPositions, CairoConfiguration configuration, SqlExecutionContext sqlExecutionContext) {
    final Function arg = args.getQuick(0);
    if (arg.isConstant()) {
        final char value = arg.getChar(null);
        if (value == 0) {
            return SymbolConstant.NULL;
        }
        final StringSink sink = Misc.getThreadLocalBuilder();
        sink.put(value);
        return SymbolConstant.newInstance(Chars.toString(sink));
    }
    return new Func(arg);
}
Also used : Function(io.questdb.cairo.sql.Function) StringSink(io.questdb.std.str.StringSink)

Aggregations

StringSink (io.questdb.std.str.StringSink)284 Test (org.junit.Test)167 Function (io.questdb.cairo.sql.Function)38 BaseConnection (org.postgresql.core.BaseConnection)36 UnaryFunction (io.questdb.griffin.engine.functions.UnaryFunction)28 StrConstant (io.questdb.griffin.engine.functions.constants.StrConstant)22 StrFunction (io.questdb.griffin.engine.functions.StrFunction)20 AbstractGriffinTest (io.questdb.griffin.AbstractGriffinTest)16 Path (io.questdb.std.str.Path)16 CountDownLatch (java.util.concurrent.CountDownLatch)15 CyclicBarrier (java.util.concurrent.CyclicBarrier)15 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)13 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)11 SqlCompiler (io.questdb.griffin.SqlCompiler)10 SqlExecutionContextImpl (io.questdb.griffin.SqlExecutionContextImpl)10 SqlException (io.questdb.griffin.SqlException)9 Metrics (io.questdb.Metrics)8 io.questdb.cairo (io.questdb.cairo)8 AllowAllCairoSecurityContext (io.questdb.cairo.security.AllowAllCairoSecurityContext)8 NetUtils (io.questdb.cutlass.NetUtils)8