Search in sources :

Example 56 with StringSink

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

the class PGJobContextTest method testLargeBatchInsertMethod.

@Test
public void testLargeBatchInsertMethod() throws Exception {
    assertMemoryLeak(() -> {
        try (final PGWireServer ignored = createPGServer(4);
            final Connection connection = getConnection(false, true)) {
            try (Statement statement = connection.createStatement()) {
                statement.executeUpdate("create table test_large_batch(id long,val int)");
            }
            connection.setAutoCommit(false);
            try (PreparedStatement batchInsert = connection.prepareStatement("insert into test_large_batch(id,val) values(?,?)")) {
                for (int i = 0; i < 50_000; i++) {
                    batchInsert.clearParameters();
                    batchInsert.setLong(1, 0L);
                    batchInsert.setInt(2, 1);
                    batchInsert.addBatch();
                    batchInsert.clearParameters();
                    batchInsert.setLong(1, 1L);
                    batchInsert.setInt(2, 2);
                    batchInsert.addBatch();
                    batchInsert.clearParameters();
                    batchInsert.setLong(1, 2L);
                    batchInsert.setInt(2, 3);
                    batchInsert.addBatch();
                }
                batchInsert.executeBatch();
                connection.commit();
            }
            StringSink sink = new StringSink();
            String expected = "count[BIGINT]\n" + "150000\n";
            Statement statement = connection.createStatement();
            ResultSet rs = statement.executeQuery("select count(*) from test_large_batch");
            assertResultSet(expected, sink, rs);
        }
    });
}
Also used : BaseConnection(org.postgresql.core.BaseConnection) StringSink(io.questdb.std.str.StringSink) AbstractGriffinTest(io.questdb.griffin.AbstractGriffinTest) Test(org.junit.Test)

Example 57 with StringSink

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

the class PGJobContextTest method testGeoHashSelect.

private void testGeoHashSelect(boolean simple, boolean binary) throws Exception {
    TestUtils.assertMemoryLeak(() -> {
        try (final PGWireServer ignore = createPGServer(2);
            final Connection connection = getConnection(simple, binary)) {
            Statement statement = connection.createStatement();
            ResultSet rs = statement.executeQuery("select " + "rnd_geohash(1) hash1b, " + "rnd_geohash(2) hash2b, " + "rnd_geohash(3) hash3b, " + "rnd_geohash(5) hash1c, " + "rnd_geohash(10) hash2c, " + "rnd_geohash(20) hash4c, " + "rnd_geohash(40) hash8c " + "from long_sequence(10)");
            final String expected = "hash1b[VARCHAR],hash2b[VARCHAR],hash3b[VARCHAR],hash1c[VARCHAR],hash2c[VARCHAR],hash4c[VARCHAR],hash8c[VARCHAR]\n" + "0,00,100,z,hp,wh4b,s2z2fyds\n" + "0,10,001,f,q4,uzr0,jj53eufn\n" + "1,01,111,7,q0,s2vq,y5nbb1qj\n" + "1,10,111,r,5t,g5xx,kt2bujns\n" + "1,11,010,w,u7,qjuz,gyye1jqc\n" + "1,01,101,2,cs,vqnq,9yvqyf2r\n" + "1,10,001,0,be,4bw1,v676yupj\n" + "0,11,010,q,vg,g6mm,4tyruscu\n" + "1,01,011,u,wt,jgke,pw94gc64\n" + "0,01,101,8,y0,b2vj,b8182chp\n";
            StringSink sink = new StringSink();
            // dump metadata
            assertResultSet(expected, sink, rs);
        }
    });
}
Also used : BaseConnection(org.postgresql.core.BaseConnection) StringSink(io.questdb.std.str.StringSink)

Example 58 with StringSink

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

the class TextUtilTest method testQuotedTextParsing.

@Test
public void testQuotedTextParsing() throws Utf8Exception {
    StringSink query = new StringSink();
    String text = "select count(*) from \"file.csv\" abcd";
    copyToSinkWithTextUtil(query, text, false);
    Assert.assertEquals(text, query.toString());
}
Also used : StringSink(io.questdb.std.str.StringSink) Test(org.junit.Test)

Example 59 with StringSink

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

the class TextUtilTest method testDoubleQuotedTextParsing.

@Test
public void testDoubleQuotedTextParsing() throws Utf8Exception {
    StringSink query = new StringSink();
    String text = "select count(*) from \"\"file.csv\"\" abcd";
    copyToSinkWithTextUtil(query, text, true);
    Assert.assertEquals(text.replace("\"\"", "\""), query.toString());
}
Also used : StringSink(io.questdb.std.str.StringSink) Test(org.junit.Test)

Example 60 with StringSink

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

the class LineTcpReceiverTest method test.

private void test(String authKeyId, PrivateKey authPrivateKey, int msgBufferSize, final int nRows, boolean expectDisconnect) throws Exception {
    this.authKeyId = authKeyId;
    this.msgBufferSize = msgBufferSize;
    assertMemoryLeak(() -> {
        final String[] locations = { "x london", "paris", "rome" };
        final CharSequenceHashSet tables = new CharSequenceHashSet();
        tables.add("weather1");
        tables.add("weather2");
        tables.add("weather3");
        SOCountDownLatch tablesCreated = new SOCountDownLatch();
        tablesCreated.setCount(tables.size());
        final Rnd rand = new Rnd();
        final StringBuilder[] expectedSbs = new StringBuilder[tables.size()];
        engine.setPoolListener((factoryType, thread, name, event, segment, position) -> {
            if (factoryType == PoolListener.SRC_WRITER && event == PoolListener.EV_RETURN) {
                if (tables.contains(name)) {
                    tablesCreated.countDown();
                }
            }
        });
        minIdleMsBeforeWriterRelease = 100;
        try (LineTcpReceiver ignored = LineTcpReceiver.create(lineConfiguration, sharedWorkerPool, LOG, engine)) {
            long startEpochMs = System.currentTimeMillis();
            sharedWorkerPool.assignCleaner(Path.CLEANER);
            sharedWorkerPool.start(LOG);
            try {
                final AbstractLineSender[] senders = new AbstractLineSender[tables.size()];
                for (int n = 0; n < senders.length; n++) {
                    if (null != authKeyId) {
                        AuthenticatedLineTcpSender sender = new AuthenticatedLineTcpSender(authKeyId, authPrivateKey, Net.parseIPv4("127.0.0.1"), bindPort, 4096);
                        sender.authenticate();
                        senders[n] = sender;
                    } else {
                        senders[n] = new LineTcpSender(Net.parseIPv4("127.0.0.1"), bindPort, 4096);
                    }
                    StringBuilder sb = new StringBuilder((nRows + 1) * lineConfiguration.getMaxMeasurementSize());
                    sb.append("location\ttemp\ttimestamp\n");
                    expectedSbs[n] = sb;
                }
                long ts = Os.currentTimeMicros();
                StringSink tsSink = new StringSink();
                for (int nRow = 0; nRow < nRows; nRow++) {
                    int nTable = nRow < tables.size() ? nRow : rand.nextInt(tables.size());
                    AbstractLineSender sender = senders[nTable];
                    StringBuilder sb = expectedSbs[nTable];
                    CharSequence tableName = tables.get(nTable);
                    sender.metric(tableName);
                    String location = locations[rand.nextInt(locations.length)];
                    sb.append(location);
                    sb.append('\t');
                    sender.tag("location", location);
                    int temp = rand.nextInt(100);
                    sb.append(temp);
                    sb.append('\t');
                    sender.field("temp", temp);
                    tsSink.clear();
                    TimestampFormatUtils.appendDateTimeUSec(tsSink, ts);
                    sb.append(tsSink);
                    sb.append('\n');
                    sender.$(ts * 1000);
                    sender.flush();
                    if (expectDisconnect) {
                        // To prevent all data being buffered before the expected disconnect slow sending
                        Os.sleep(100);
                    }
                    ts += rand.nextInt(1000);
                }
                for (int n = 0; n < senders.length; n++) {
                    AbstractLineSender sender = senders[n];
                    sender.close();
                }
                Assert.assertFalse(expectDisconnect);
                boolean ready = tablesCreated.await(TimeUnit.MINUTES.toNanos(1));
                if (!ready) {
                    throw new IllegalStateException("Timeout waiting for tables to be created");
                }
                int nRowsWritten;
                do {
                    nRowsWritten = 0;
                    long timeTakenMs = System.currentTimeMillis() - startEpochMs;
                    if (timeTakenMs > TEST_TIMEOUT_IN_MS) {
                        LOG.error().$("after ").$(timeTakenMs).$("ms tables only had ").$(nRowsWritten).$(" rows out of ").$(nRows).$();
                        break;
                    }
                    Thread.yield();
                    for (int n = 0; n < tables.size(); n++) {
                        CharSequence tableName = tables.get(n);
                        while (true) {
                            try (TableReader reader = engine.getReader(AllowAllCairoSecurityContext.INSTANCE, tableName)) {
                                TableReaderRecordCursor cursor = reader.getCursor();
                                while (cursor.hasNext()) {
                                    nRowsWritten++;
                                }
                                break;
                            } catch (EntryLockedException ex) {
                                LOG.info().$("retrying read for ").$(tableName).$();
                                LockSupport.parkNanos(1);
                            }
                        }
                    }
                } while (nRowsWritten < nRows);
                LOG.info().$(nRowsWritten).$(" rows written").$();
            } finally {
                sharedWorkerPool.halt();
            }
        } finally {
            engine.setPoolListener(null);
        }
        for (int n = 0; n < tables.size(); n++) {
            CharSequence tableName = tables.get(n);
            LOG.info().$("checking table ").$(tableName).$();
            assertTable(expectedSbs[n], tableName);
        }
    });
}
Also used : LineTcpSender(io.questdb.cutlass.line.LineTcpSender) AuthenticatedLineTcpSender(io.questdb.cutlass.line.AuthenticatedLineTcpSender) StringSink(io.questdb.std.str.StringSink) SOCountDownLatch(io.questdb.mp.SOCountDownLatch) AbstractLineSender(io.questdb.cutlass.line.AbstractLineSender) AuthenticatedLineTcpSender(io.questdb.cutlass.line.AuthenticatedLineTcpSender) EntryLockedException(io.questdb.cairo.pool.ex.EntryLockedException)

Aggregations

StringSink (io.questdb.std.str.StringSink)304 Test (org.junit.Test)173 Function (io.questdb.cairo.sql.Function)38 BaseConnection (org.postgresql.core.BaseConnection)38 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 Path (io.questdb.std.str.Path)18 AbstractGriffinTest (io.questdb.griffin.AbstractGriffinTest)16 CountDownLatch (java.util.concurrent.CountDownLatch)15 CyclicBarrier (java.util.concurrent.CyclicBarrier)15 Record (io.questdb.cairo.sql.Record)14 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)13 SqlCompiler (io.questdb.griffin.SqlCompiler)12 SqlExecutionContextImpl (io.questdb.griffin.SqlExecutionContextImpl)12 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)11 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