Search in sources :

Example 11 with TableWriter

use of io.questdb.cairo.TableWriter in project questdb by bluestreak01.

the class MemoryLeakTest method populateUsersTable.

private void populateUsersTable(CairoEngine engine, int n) throws SqlException {
    try (final SqlCompiler compiler = new SqlCompiler(engine);
        final SqlExecutionContextImpl executionContext = new SqlExecutionContextImpl(engine, 1).with(AllowAllCairoSecurityContext.INSTANCE, new BindVariableServiceImpl(configuration), null)) {
        compiler.compile("create table users (sequence long, event binary, timestamp timestamp, id long) timestamp(timestamp)", executionContext);
        long buffer = Unsafe.malloc(1024, MemoryTag.NATIVE_DEFAULT);
        try {
            try (TableWriter writer = engine.getWriter(executionContext.getCairoSecurityContext(), "users", "testing")) {
                // time can go backwards if asked too quickly, add I to offset the chance (on mac M1 at least)
                // call_j can yield a lower value than call_i thus resulting in an unordered
                long baseTimestamp = Os.currentTimeMicros();
                for (int i = 0; i < n; i++) {
                    // table, so we add i to make sure the timestamps are ordered
                    long sequence = 20 + i * 2L;
                    TableWriter.Row row = writer.newRow(baseTimestamp + i);
                    row.putLong(0, sequence);
                    row.putBin(1, buffer, 1024);
                    row.putLong(3, i);
                    row.append();
                }
                writer.commit();
            }
        } finally {
            Unsafe.free(buffer, 1024, MemoryTag.NATIVE_DEFAULT);
        }
    }
}
Also used : TableWriter(io.questdb.cairo.TableWriter) BindVariableServiceImpl(io.questdb.griffin.engine.functions.bind.BindVariableServiceImpl)

Example 12 with TableWriter

use of io.questdb.cairo.TableWriter in project questdb by bluestreak01.

the class DropTableTest method testDropBusyWriter.

@Test
public void testDropBusyWriter() throws Exception {
    assertMemoryLeak(() -> {
        CompiledQuery cc = compiler.compile("create table 'large table' (a int)", sqlExecutionContext);
        Assert.assertEquals(CompiledQuery.CREATE_TABLE, cc.getType());
        try (TableWriter ignored = engine.getWriter(sqlExecutionContext.getCairoSecurityContext(), "large table", "testing")) {
            compiler.compile("drop table 'large table'", sqlExecutionContext);
        } catch (CairoException e) {
            TestUtils.assertContains(e.getFlyweightMessage(), "Could not lock");
        }
    });
}
Also used : TableWriter(io.questdb.cairo.TableWriter) CairoException(io.questdb.cairo.CairoException) Test(org.junit.Test)

Example 13 with TableWriter

use of io.questdb.cairo.TableWriter in project questdb by bluestreak01.

the class FirstDateGroupByFunctionFactoryTest method testFirstNull.

@Test
public void testFirstNull() throws SqlException {
    compiler.compile("create table tab (f date)", sqlExecutionContext);
    final Rnd rnd = new Rnd();
    try (TableWriter w = engine.getWriter(sqlExecutionContext.getCairoSecurityContext(), "tab", "testing")) {
        TableWriter.Row r = w.newRow();
        r.append();
        for (int i = 100; i > 10; i--) {
            r = w.newRow();
            r.putLong(0, rnd.nextLong());
            r.append();
        }
        w.commit();
    }
    try (RecordCursorFactory factory = compiler.compile("select first(f) from tab", sqlExecutionContext).getRecordCursorFactory()) {
        try (RecordCursor cursor = factory.getCursor(sqlExecutionContext)) {
            Record record = cursor.getRecord();
            Assert.assertEquals(1, cursor.size());
            Assert.assertTrue(cursor.hasNext());
            Assert.assertEquals(Numbers.LONG_NaN, record.getLong(0));
        }
    }
}
Also used : TableWriter(io.questdb.cairo.TableWriter) RecordCursorFactory(io.questdb.cairo.sql.RecordCursorFactory) RecordCursor(io.questdb.cairo.sql.RecordCursor) Rnd(io.questdb.std.Rnd) Record(io.questdb.cairo.sql.Record) AbstractGriffinTest(io.questdb.griffin.AbstractGriffinTest) Test(org.junit.Test)

Example 14 with TableWriter

use of io.questdb.cairo.TableWriter in project questdb by bluestreak01.

the class FirstDateGroupByFunctionFactoryTest method testNonNull.

@Test
public void testNonNull() throws SqlException {
    compiler.compile("create table tab (f date)", sqlExecutionContext);
    final Rnd rnd = new Rnd();
    try (TableWriter w = engine.getWriter(sqlExecutionContext.getCairoSecurityContext(), "tab", "testing")) {
        for (int i = 100; i > 10; i--) {
            TableWriter.Row r = w.newRow();
            r.putLong(0, rnd.nextLong());
            r.append();
        }
        w.commit();
    }
    try (RecordCursorFactory factory = compiler.compile("select first(f) from tab", sqlExecutionContext).getRecordCursorFactory()) {
        try (RecordCursor cursor = factory.getCursor(sqlExecutionContext)) {
            Record record = cursor.getRecord();
            Assert.assertEquals(1, cursor.size());
            Assert.assertTrue(cursor.hasNext());
            Assert.assertEquals(4689592037643856L, record.getLong(0));
        }
    }
}
Also used : TableWriter(io.questdb.cairo.TableWriter) RecordCursorFactory(io.questdb.cairo.sql.RecordCursorFactory) RecordCursor(io.questdb.cairo.sql.RecordCursor) Rnd(io.questdb.std.Rnd) Record(io.questdb.cairo.sql.Record) AbstractGriffinTest(io.questdb.griffin.AbstractGriffinTest) Test(org.junit.Test)

Example 15 with TableWriter

use of io.questdb.cairo.TableWriter in project questdb by bluestreak01.

the class FirstIntGroupByFunctionFactoryTest method testSomeNull.

@Test
public void testSomeNull() throws SqlException {
    compiler.compile("create table tab (f int)", sqlExecutionContext);
    try (TableWriter w = engine.getWriter(sqlExecutionContext.getCairoSecurityContext(), "tab", "testing")) {
        for (int i = 100; i > 10; i--) {
            TableWriter.Row r = w.newRow();
            if (i % 4 == 0) {
                r.putInt(0, i);
            }
            r.append();
        }
        w.commit();
    }
    try (RecordCursorFactory factory = compiler.compile("select first(f) from tab", sqlExecutionContext).getRecordCursorFactory()) {
        try (RecordCursor cursor = factory.getCursor(sqlExecutionContext)) {
            Record record = cursor.getRecord();
            Assert.assertEquals(1, cursor.size());
            Assert.assertTrue(cursor.hasNext());
            Assert.assertEquals(100, record.getInt(0));
        }
    }
}
Also used : TableWriter(io.questdb.cairo.TableWriter) RecordCursorFactory(io.questdb.cairo.sql.RecordCursorFactory) RecordCursor(io.questdb.cairo.sql.RecordCursor) Record(io.questdb.cairo.sql.Record) AbstractGriffinTest(io.questdb.griffin.AbstractGriffinTest) Test(org.junit.Test)

Aggregations

TableWriter (io.questdb.cairo.TableWriter)103 Test (org.junit.Test)88 RecordCursor (io.questdb.cairo.sql.RecordCursor)71 AbstractGriffinTest (io.questdb.griffin.AbstractGriffinTest)71 Record (io.questdb.cairo.sql.Record)70 RecordCursorFactory (io.questdb.cairo.sql.RecordCursorFactory)68 Rnd (io.questdb.std.Rnd)23 TableReader (io.questdb.cairo.TableReader)9 CountDownLatch (java.util.concurrent.CountDownLatch)9 EntryUnavailableException (io.questdb.cairo.EntryUnavailableException)7 ServerDisconnectException (io.questdb.network.ServerDisconnectException)7 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)7 CairoConfiguration (io.questdb.cairo.CairoConfiguration)3 CairoEngine (io.questdb.cairo.CairoEngine)3 DefaultCairoConfiguration (io.questdb.cairo.DefaultCairoConfiguration)3 SqlCompiler (io.questdb.griffin.SqlCompiler)3 SqlExecutionContextImpl (io.questdb.griffin.SqlExecutionContextImpl)3 RecordCursorPrinter (io.questdb.cairo.RecordCursorPrinter)2 Path (io.questdb.std.str.Path)2 CyclicBarrier (java.util.concurrent.CyclicBarrier)2