Search in sources :

Example 36 with StringSink

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

the class CastShortToSymbolFunctionFactory 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.getShort(null));
        return SymbolConstant.newInstance(sink);
    }
    return new Func(arg);
}
Also used : Function(io.questdb.cairo.sql.Function) StringSink(io.questdb.std.str.StringSink)

Example 37 with StringSink

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

the class CastDateToSymbolFunctionFactory 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.getDate(null));
        return SymbolConstant.newInstance(sink);
    }
    return new Func(arg);
}
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 38 with StringSink

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

the class IODispatcherTest method testHttpLong256AndCharImportLimitColumns.

@Test
public void testHttpLong256AndCharImportLimitColumns() {
    // this script uploads text file:
    // 0x5c504ed432cb51138bcf09aa5e8a410dd4a1e204ef84bfed1be16dfba1b22060,a
    // 0x19f1df2c7ee6b464720ad28e903aeda1a5ad8780afc22f0b960827bd4fcf656d,b
    // 0x9e6e19637bb625a8ff3d052b7c2fe57dc78c55a15d258d77c43d5a9c160b0384,p
    // 0xcb9378977089c773c074045b20ede2cdcc3a6ff562f4e64b51b20c5205234525,w
    // 0xd23ae9b2e5c68caf2c5663af5ba27679dc3b3cb781c4dc698abbd17d63e32e9f,t
    final String uploadScript = ">504f5354202f696d703f666d743d6a736f6e266f76657277726974653d7472756520485454502f312e310d0a486f73743a206c6f63616c686f73743a393030300d0a436f6e6e656374696f6e3a206b6565702d616c6976650d0a436f6e74656e742d4c656e6774683a203534380d0a4163636570743a202a2f2a0d0a582d5265717565737465642d576974683a20584d4c48747470526571756573740d0a557365722d4167656e743a204d6f7a696c6c612f352e30202857696e646f7773204e542031302e303b2057696e36343b2078363429204170706c655765624b69742f3533372e333620284b48544d4c2c206c696b65204765636b6f29204368726f6d652f37362e302e333830392e313030205361666172692f3533372e33360d0a5365632d46657463682d4d6f64653a20636f72730d0a436f6e74656e742d547970653a206d756c7469706172742f666f726d2d646174613b20626f756e646172793d2d2d2d2d5765624b6974466f726d426f756e64617279386c75374239696e37567a5767614a4f0d0a4f726967696e3a20687474703a2f2f6c6f63616c686f73743a393030300d0a5365632d46657463682d536974653a2073616d652d6f726967696e0d0a526566657265723a20687474703a2f2f6c6f63616c686f73743a393030302f696e6465782e68746d6c0d0a4163636570742d456e636f64696e673a20677a69702c206465666c6174652c2062720d0a4163636570742d4c616e67756167653a20656e2d47422c656e2d55533b713d302e392c656e3b713d302e380d0a0d0a\n" + ">2d2d2d2d2d2d5765624b6974466f726d426f756e64617279386c75374239696e37567a5767614a4f0d0a436f6e74656e742d446973706f736974696f6e3a20666f726d2d646174613b206e616d653d2264617461223b2066696c656e616d653d2273616d706c652e637376220d0a436f6e74656e742d547970653a206170706c69636174696f6e2f766e642e6d732d657863656c0d0a0d0a3078356335303465643433326362353131333862636630396161356538613431306464346131653230346566383462666564316265313664666261316232323036302c610d0a3078313966316466326337656536623436343732306164323865393033616564613161356164383738306166633232663062393630383237626434666366363536642c620d0a3078396536653139363337626236323561386666336430353262376332666535376463373863353561313564323538643737633433643561396331363062303338342c700d0a3078636239333738393737303839633737336330373430343562323065646532636463633361366666353632663465363462353162323063353230353233343532352c770d0a3078643233616539623265356336386361663263353636336166356261323736373964633362336362373831633464633639386162626431376436336533326539662c740d0a0d0a2d2d2d2d2d2d5765624b6974466f726d426f756e64617279386c75374239696e37567a5767614a4f2d2d0d0a\n" + "<485454502f312e3120323030204f4b0d0a5365727665723a20717565737444422f312e300d0a446174653a205468752c2031204a616e20313937302030303a30303a303020474d540d0a5472616e736665722d456e636f64696e673a206368756e6b65640d0a436f6e74656e742d547970653a206170706c69636174696f6e2f6a736f6e3b20636861727365743d7574662d380d0a\n" + "<0d0a63380d0a\n" + "<7b22737461747573223a224f4b222c226c6f636174696f6e223a2273616d706c652e637376222c22726f777352656a6563746564223a302c22726f7773496d706f72746564223a352c22686561646572223a66616c73652c22636f6c756d6e73223a5b7b226e616d65223a226630222c2274797065223a224c4f4e47323536222c2273697a65223a33322c226572726f7273223a307d2c7b226e616d65223a226631222c2274797065223a2243484152222c2273697a65223a322c226572726f7273223a307d5d7d\n" + "<0d0a30300d0a\n" + "<0d0a\n";
    final String expectedTableMetadata = "{\"columnCount\":2,\"columns\":[{\"index\":0,\"name\":\"f0\",\"type\":\"LONG256\"},{\"index\":1,\"name\":\"f1\",\"type\":\"CHAR\"}],\"timestampIndex\":-1}";
    final String baseDir = temp.getRoot().getAbsolutePath();
    final CairoConfiguration configuration = new DefaultCairoConfiguration(baseDir);
    try (CairoEngine cairoEngine = new CairoEngine(configuration);
        HttpServer ignored = HttpServer.create(new DefaultHttpServerConfiguration(new DefaultHttpContextConfiguration() {

            @Override
            public MillisecondClock getClock() {
                return StationaryMillisClock.INSTANCE;
            }
        }), null, LOG, cairoEngine, metrics)) {
        // upload file
        NetUtils.playScript(NetworkFacadeImpl.INSTANCE, uploadScript, "127.0.0.1", 9001);
        try (TableReader reader = cairoEngine.getReader(AllowAllCairoSecurityContext.INSTANCE, "sample.csv", TableUtils.ANY_TABLE_ID, TableUtils.ANY_TABLE_VERSION)) {
            StringSink sink = new StringSink();
            reader.getMetadata().toJson(sink);
            TestUtils.assertEquals(expectedTableMetadata, sink);
        }
        final String selectAsJsonScript = ">474554202f657865633f71756572793d25323773616d706c652e63737625323726636f756e743d66616c736526636f6c733d66302532436631267372633d76697320485454502f312e310d0a486f73743a206c6f63616c686f73743a393030300d0a436f6e6e656374696f6e3a206b6565702d616c6976650d0a4163636570743a202a2f2a0d0a582d5265717565737465642d576974683a20584d4c48747470526571756573740d0a557365722d4167656e743a204d6f7a696c6c612f352e30202857696e646f7773204e542031302e303b2057696e36343b2078363429204170706c655765624b69742f3533372e333620284b48544d4c2c206c696b65204765636b6f29204368726f6d652f37392e302e333934352e313330205361666172692f3533372e33360d0a5365632d46657463682d536974653a2073616d652d6f726967696e0d0a5365632d46657463682d4d6f64653a20636f72730d0a526566657265723a20687474703a2f2f6c6f63616c686f73743a393030302f696e6465782e68746d6c0d0a4163636570742d456e636f64696e673a20677a69702c206465666c6174652c2062720d0a4163636570742d4c616e67756167653a20656e2d47422c656e2d55533b713d302e392c656e3b713d302e380d0a436f6f6b69653a205f67613d4741312e312e323132343933323030312e313537333832343636393b205f6769643d4741312e312e3339323836373839362e313538303132333336350d0a0d0a\n" + "<485454502f312e3120323030204f4b0d0a5365727665723a20717565737444422f312e300d0a446174653a205468752c2031204a616e20313937302030303a30303a303020474d540d0a5472616e736665722d456e636f64696e673a206368756e6b65640d0a436f6e74656e742d547970653a206170706c69636174696f6e2f6a736f6e3b20636861727365743d7574662d380d0a4b6565702d416c6976653a2074696d656f75743d352c206d61783d31303030300d0a\n" + "<0d0a303165630d0a\n";
        // select * from 'sample.csv' and limit columns to f0,f1
        NetUtils.playScript(NetworkFacadeImpl.INSTANCE, selectAsJsonScript, "127.0.0.1", 9001);
    }
}
Also used : StringSink(io.questdb.std.str.StringSink)

Example 39 with StringSink

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

the class IODispatcherTest method testSendHttpGetAndSimpleResponse.

@Test
public void testSendHttpGetAndSimpleResponse() throws Exception {
    LOG.info().$("started testSendHttpGetAndSimpleResponse").$();
    final String request = "GET /status?x=1&a=%26b&c&d=x HTTP/1.1\r\n" + "Host: localhost:9000\r\n" + "Connection: keep-alive\r\n" + "Cache-Control: max-age=0\r\n" + "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8\r\n" + "User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.48 Safari/537.36\r\n" + "Accept-Encoding: gzip,deflate,sdch\r\n" + "Accept-Language: en-US,en;q=0.8\r\n" + "Cookie: textwrapon=false; textautoformat=false; wysiwyg=textarea\r\n" + "\r\n";
    // the difference between request and expected is url encoding (and ':' padding, which can easily be fixed)
    final String expected = "GET /status?x=1&a=&b&c&d=x HTTP/1.1\r\n" + "host:localhost:9000\r\n" + "connection:keep-alive\r\n" + "cache-control:max-age=0\r\n" + "accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8\r\n" + "user-agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.48 Safari/537.36\r\n" + "accept-encoding:gzip,deflate,sdch\r\n" + "accept-language:en-US,en;q=0.8\r\n" + "cookie:textwrapon=false; textautoformat=false; wysiwyg=textarea\r\n" + "\r\n";
    final String expectedResponse = "HTTP/1.1 200 OK\r\n" + "Server: questDB/1.0\r\n" + "Date: Thu, 1 Jan 1970 00:00:00 GMT\r\n" + "Transfer-Encoding: chunked\r\n" + "Content-Type: text/plain; charset=utf-8\r\n" + "\r\n" + "04\r\n" + "OK\r\n" + "\r\n" + "00\r\n" + "\r\n";
    assertMemoryLeak(() -> {
        HttpServerConfiguration httpServerConfiguration = new DefaultHttpServerConfiguration(new DefaultHttpContextConfiguration() {

            @Override
            public MillisecondClock getClock() {
                return () -> 0;
            }
        });
        SOCountDownLatch connectLatch = new SOCountDownLatch(1);
        SOCountDownLatch contextClosedLatch = new SOCountDownLatch(1);
        AtomicInteger closeCount = new AtomicInteger(0);
        try (IODispatcher<HttpConnectionContext> dispatcher = IODispatchers.create(new DefaultIODispatcherConfiguration(), new IOContextFactory<HttpConnectionContext>() {

            @Override
            public HttpConnectionContext newInstance(long fd, IODispatcher<HttpConnectionContext> dispatcher1) {
                connectLatch.countDown();
                return new HttpConnectionContext(httpServerConfiguration.getHttpContextConfiguration()) {

                    @Override
                    public void close() {
                        // context is closed
                        if (closeCount.incrementAndGet() == 1) {
                            super.close();
                            contextClosedLatch.countDown();
                        }
                    }
                }.of(fd, dispatcher1);
            }
        })) {
            StringSink sink = new StringSink();
            final HttpRequestProcessorSelector selector = new HttpRequestProcessorSelector() {

                @Override
                public HttpRequestProcessor select(CharSequence url) {
                    return null;
                }

                @Override
                public HttpRequestProcessor getDefaultProcessor() {
                    return new HttpRequestProcessor() {

                        @Override
                        public void onHeadersReady(HttpConnectionContext context) {
                            HttpRequestHeader headers = context.getRequestHeader();
                            sink.put(headers.getMethodLine());
                            sink.put("\r\n");
                            ObjList<CharSequence> headerNames = headers.getHeaderNames();
                            for (int i = 0, n = headerNames.size(); i < n; i++) {
                                sink.put(headerNames.getQuick(i)).put(':');
                                sink.put(headers.getHeader(headerNames.getQuick(i)));
                                sink.put("\r\n");
                            }
                            sink.put("\r\n");
                        }

                        @Override
                        public void onRequestComplete(HttpConnectionContext context) throws PeerDisconnectedException, PeerIsSlowToReadException {
                            context.simpleResponse().sendStatusWithDefaultMessage(200);
                        }
                    };
                }

                @Override
                public void close() {
                }
            };
            AtomicBoolean serverRunning = new AtomicBoolean(true);
            SOCountDownLatch serverHaltLatch = new SOCountDownLatch(1);
            new Thread(() -> {
                while (serverRunning.get()) {
                    dispatcher.run(0);
                    dispatcher.processIOQueue((operation, context) -> context.handleClientOperation(operation, selector, EmptyRescheduleContext));
                }
                serverHaltLatch.countDown();
            }).start();
            long fd = Net.socketTcp(true);
            try {
                long sockAddr = Net.sockaddr("127.0.0.1", 9001);
                try {
                    TestUtils.assertConnect(fd, sockAddr);
                    connectLatch.await();
                    int len = request.length();
                    long buffer = TestUtils.toMemory(request);
                    try {
                        Assert.assertEquals(len, Net.send(fd, buffer, len));
                        // read response we expect
                        StringSink sink2 = new StringSink();
                        final int expectedLen = expectedResponse.length();
                        int read = 0;
                        while (read < expectedLen) {
                            int n = Net.recv(fd, buffer, len);
                            Assert.assertTrue(n > 0);
                            for (int i = 0; i < n; i++) {
                                sink2.put((char) Unsafe.getUnsafe().getByte(buffer + i));
                            }
                            // copy response bytes to sink
                            read += n;
                        }
                        TestUtils.assertEquals(expectedResponse, sink2);
                    } finally {
                        Unsafe.free(buffer, len, MemoryTag.NATIVE_DEFAULT);
                    }
                    Assert.assertEquals(0, Net.close(fd));
                    LOG.info().$("closed [fd=").$(fd).$(']').$();
                    fd = -1;
                    contextClosedLatch.await();
                    serverRunning.set(false);
                    serverHaltLatch.await();
                    Assert.assertEquals(0, dispatcher.getConnectionCount());
                    TestUtils.assertEquals(expected, sink);
                } finally {
                    Net.freeSockAddr(sockAddr);
                }
            } finally {
                if (fd != -1) {
                    Net.close(fd);
                }
            }
            Assert.assertEquals(1, closeCount.get());
        }
    });
}
Also used : IOOperation(io.questdb.network.IOOperation) DefaultIODispatcherConfiguration(io.questdb.network.DefaultIODispatcherConfiguration) Log(io.questdb.log.Log) WorkerPoolConfiguration(io.questdb.mp.WorkerPoolConfiguration) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TestUtils.assertMemoryLeak(io.questdb.test.tools.TestUtils.assertMemoryLeak) WorkerPool(io.questdb.mp.WorkerPool) Timestamps(io.questdb.std.datetime.microtime.Timestamps) TestLatchedCounterFunctionFactory(io.questdb.griffin.engine.functions.test.TestLatchedCounterFunctionFactory) QueryCache(io.questdb.cutlass.http.processors.QueryCache) io.questdb.std(io.questdb.std) IODispatchers(io.questdb.network.IODispatchers) CyclicBarrier(java.util.concurrent.CyclicBarrier) SOCountDownLatch(io.questdb.mp.SOCountDownLatch) LockSupport(java.util.concurrent.locks.LockSupport) StaticContentProcessor(io.questdb.cutlass.http.processors.StaticContentProcessor) CountDownLatch(java.util.concurrent.CountDownLatch) Path(io.questdb.std.str.Path) io.questdb.cairo(io.questdb.cairo) AllowAllCairoSecurityContext(io.questdb.cairo.security.AllowAllCairoSecurityContext) IORequestProcessor(io.questdb.network.IORequestProcessor) NotNull(org.jetbrains.annotations.NotNull) JsonQueryProcessor(io.questdb.cutlass.http.processors.JsonQueryProcessor) TextImportProcessor(io.questdb.cutlass.http.processors.TextImportProcessor) IODispatcher(io.questdb.network.IODispatcher) AbstractCharSequence(io.questdb.std.str.AbstractCharSequence) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MPSequence(io.questdb.mp.MPSequence) IOContextFactory(io.questdb.network.IOContextFactory) NetUtils(io.questdb.cutlass.NetUtils) SqlException(io.questdb.griffin.SqlException) SCSequence(io.questdb.mp.SCSequence) IOContext(io.questdb.network.IOContext) TestUtils(io.questdb.test.tools.TestUtils) SqlCompiler(io.questdb.griffin.SqlCompiler) LogFactory(io.questdb.log.LogFactory) Metrics(io.questdb.Metrics) HealthCheckProcessor(io.questdb.cutlass.http.processors.HealthCheckProcessor) NetworkFacadeImpl(io.questdb.network.NetworkFacadeImpl) SqlExecutionContextImpl(io.questdb.griffin.SqlExecutionContextImpl) NetworkFacade(io.questdb.network.NetworkFacade) RingQueue(io.questdb.mp.RingQueue) PeerDisconnectedException(io.questdb.network.PeerDisconnectedException) TimeUnit(java.util.concurrent.TimeUnit) SharedRandom(io.questdb.griffin.engine.functions.rnd.SharedRandom) AtomicLong(java.util.concurrent.atomic.AtomicLong) PeerIsSlowToReadException(io.questdb.network.PeerIsSlowToReadException) StringSink(io.questdb.std.str.StringSink) Net(io.questdb.network.Net) MillisecondClock(io.questdb.std.datetime.millitime.MillisecondClock) IODispatcherConfiguration(io.questdb.network.IODispatcherConfiguration) org.junit(org.junit) SqlExecutionContext(io.questdb.griffin.SqlExecutionContext) TemporaryFolder(org.junit.rules.TemporaryFolder) ByteSequence(io.questdb.std.str.ByteSequence) InputStream(java.io.InputStream) AbstractCharSequence(io.questdb.std.str.AbstractCharSequence) StringSink(io.questdb.std.str.StringSink) SOCountDownLatch(io.questdb.mp.SOCountDownLatch) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DefaultIODispatcherConfiguration(io.questdb.network.DefaultIODispatcherConfiguration) MillisecondClock(io.questdb.std.datetime.millitime.MillisecondClock)

Example 40 with StringSink

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

the class IODispatcherTest method assertMetadataAndData.

private void assertMetadataAndData(String tableName, long expectedCommitLag, int expectedMaxUncommittedRows, int expectedImportedRows, String expectedData) {
    final String baseDir = temp.getRoot().getAbsolutePath();
    DefaultCairoConfiguration configuration = new DefaultCairoConfiguration(baseDir);
    try (TableReader reader = new TableReader(configuration, tableName)) {
        Assert.assertEquals(expectedCommitLag, reader.getCommitLag());
        Assert.assertEquals(expectedMaxUncommittedRows, reader.getMaxUncommittedRows());
        Assert.assertEquals(expectedImportedRows, reader.size());
        Assert.assertEquals(0, expectedImportedRows - reader.size());
        StringSink sink = new StringSink();
        TestUtils.assertCursor(expectedData, reader.getCursor(), reader.getMetadata(), false, sink);
    }
}
Also used : 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