Search in sources :

Example 6 with RecordCursor

use of io.questdb.cairo.sql.RecordCursor in project questdb by bluestreak01.

the class TableReaderTest method testReadLong256Four.

@Test
public void testReadLong256Four() {
    try (TableModel model = new TableModel(configuration, "w", PartitionBy.DAY).col("l", ColumnType.LONG256).timestamp()) {
        CairoTestUtils.create(model);
    }
    final int N = 1_000_000;
    final Rnd rnd = new Rnd();
    long timestamp = 0;
    try (TableWriter writer = new TableWriter(configuration, "w")) {
        for (int i = 0; i < N; i++) {
            TableWriter.Row row = writer.newRow(timestamp);
            row.putLong256(0, "0x" + padHexLong(rnd.nextLong()) + padHexLong(rnd.nextLong()) + padHexLong(rnd.nextLong()) + padHexLong(rnd.nextLong()));
            row.append();
        }
        writer.commit();
    }
    rnd.reset();
    final StringSink sink = new StringSink();
    try (TableReader reader = new TableReader(configuration, "w")) {
        final RecordCursor cursor = reader.getCursor();
        final Record record = cursor.getRecord();
        int count = 0;
        while (cursor.hasNext()) {
            sink.clear();
            record.getLong256(0, sink);
            TestUtils.assertEquals("0x" + padHexLong(rnd.nextLong()) + padHexLong(rnd.nextLong()) + padHexLong(rnd.nextLong()) + padHexLong(rnd.nextLong()), sink);
            count++;
        }
        Assert.assertEquals(N, count);
    }
}
Also used : RecordCursor(io.questdb.cairo.sql.RecordCursor) StringSink(io.questdb.std.str.StringSink) Record(io.questdb.cairo.sql.Record) Test(org.junit.Test)

Example 7 with RecordCursor

use of io.questdb.cairo.sql.RecordCursor in project questdb by bluestreak01.

the class TableReaderTest method testRemovePartition.

private void testRemovePartition(int partitionBy, CharSequence partitionNameToDelete, int affectedBand, NextPartitionTimestampProvider provider) throws Exception {
    TestUtils.assertMemoryLeak(() -> {
        int N = 100;
        int N_PARTITIONS = 5;
        long timestampUs = TimestampFormatUtils.parseTimestamp("2017-12-11T10:00:00.000Z");
        long stride = 100;
        int bandStride = 1000;
        int totalCount = 0;
        // model table
        try (TableModel model = new TableModel(configuration, "w", partitionBy).col("l", ColumnType.LONG).timestamp()) {
            CairoTestUtils.create(model);
        }
        try (TableWriter writer = new TableWriter(configuration, "w")) {
            for (int k = 0; k < N_PARTITIONS; k++) {
                long band = k * bandStride;
                for (int i = 0; i < N; i++) {
                    TableWriter.Row row = writer.newRow(timestampUs);
                    row.putLong(0, band + i);
                    row.append();
                    writer.commit();
                    timestampUs += stride;
                }
                timestampUs = provider.getNext(timestampUs);
            }
            Assert.assertEquals(N * N_PARTITIONS, writer.size());
            DateFormat fmt = TableWriter.selectPartitionDirFmt(partitionBy);
            assert fmt != null;
            final long timestamp = fmt.parse(partitionNameToDelete, null);
            Assert.assertTrue(writer.removePartition(timestamp));
            Assert.assertFalse(writer.removePartition(timestamp));
            Assert.assertEquals(N * (N_PARTITIONS - 1), writer.size());
        }
        // now open table reader having partition gap
        try (TableReader reader = new TableReader(configuration, "w")) {
            Assert.assertEquals(N * (N_PARTITIONS - 1), reader.size());
            int previousBand = -1;
            int bandCount = 0;
            RecordCursor cursor = reader.getCursor();
            final Record record = cursor.getRecord();
            while (cursor.hasNext()) {
                long value = record.getLong(0);
                int band = (int) ((value / bandStride) * bandStride);
                if (band != previousBand) {
                    // make sure we don#t pick up deleted partition
                    Assert.assertNotEquals(affectedBand, band);
                    if (previousBand != -1) {
                        Assert.assertEquals(N, bandCount);
                    }
                    previousBand = band;
                    bandCount = 0;
                }
                bandCount++;
                totalCount++;
            }
            Assert.assertEquals(N, bandCount);
        }
        Assert.assertEquals(N * (N_PARTITIONS - 1), totalCount);
    });
}
Also used : RecordCursor(io.questdb.cairo.sql.RecordCursor) DateFormat(io.questdb.std.datetime.DateFormat) Record(io.questdb.cairo.sql.Record)

Example 8 with RecordCursor

use of io.questdb.cairo.sql.RecordCursor in project questdb by bluestreak01.

the class TableReaderTest method testRemovePartitionReload.

private void testRemovePartitionReload(int partitionBy, CharSequence partitionNameToDelete, int affectedBand, NextPartitionTimestampProvider provider) throws Exception {
    TestUtils.assertMemoryLeak(() -> {
        int N = 100;
        int N_PARTITIONS = 5;
        long timestampUs = TimestampFormatUtils.parseTimestamp("2017-12-11T00:00:00.000Z");
        long stride = 100;
        int bandStride = 1000;
        int totalCount = 0;
        // model table
        try (TableModel model = new TableModel(configuration, "w", partitionBy).col("l", ColumnType.LONG).timestamp()) {
            CairoTestUtils.create(model);
        }
        try (TableWriter writer = new TableWriter(configuration, "w")) {
            for (int k = 0; k < N_PARTITIONS; k++) {
                long band = k * bandStride;
                for (int i = 0; i < N; i++) {
                    TableWriter.Row row = writer.newRow(timestampUs);
                    row.putLong(0, band + i);
                    row.append();
                    writer.commit();
                    timestampUs += stride;
                }
                timestampUs = provider.getNext(timestampUs);
            }
            Assert.assertEquals(N * N_PARTITIONS, writer.size());
            // now open table reader having partition gap
            try (TableReader reader = new TableReader(configuration, "w")) {
                Assert.assertEquals(N * N_PARTITIONS, reader.size());
                RecordCursor cursor = reader.getCursor();
                Record record = cursor.getRecord();
                while (cursor.hasNext()) {
                    record.getLong(0);
                    totalCount++;
                }
                Assert.assertEquals(N * N_PARTITIONS, totalCount);
                DateFormat fmt = TableWriter.selectPartitionDirFmt(partitionBy);
                assert fmt != null;
                Assert.assertTrue(writer.removePartition(fmt.parse(partitionNameToDelete, null)));
                Assert.assertEquals(N * (N_PARTITIONS - 1), writer.size());
                reader.reload();
                totalCount = 0;
                Assert.assertEquals(N * (N_PARTITIONS - 1), reader.size());
                int previousBand = -1;
                int bandCount = 0;
                cursor = reader.getCursor();
                record = cursor.getRecord();
                while (cursor.hasNext()) {
                    long value = record.getLong(0);
                    int band = (int) ((value / bandStride) * bandStride);
                    if (band != previousBand) {
                        // make sure we don#t pick up deleted partition
                        Assert.assertNotEquals(affectedBand, band);
                        if (previousBand != -1) {
                            Assert.assertEquals(N, bandCount);
                        }
                        previousBand = band;
                        bandCount = 0;
                    }
                    bandCount++;
                    totalCount++;
                }
                Assert.assertEquals(N, bandCount);
            }
        }
        Assert.assertEquals(N * (N_PARTITIONS - 1), totalCount);
    });
}
Also used : RecordCursor(io.questdb.cairo.sql.RecordCursor) DateFormat(io.questdb.std.datetime.DateFormat) Record(io.questdb.cairo.sql.Record)

Example 9 with RecordCursor

use of io.questdb.cairo.sql.RecordCursor in project questdb by bluestreak01.

the class TableReaderTest method testCharAsString.

@Test
public void testCharAsString() throws Exception {
    TestUtils.assertMemoryLeak(() -> {
        try (TableModel model = new TableModel(configuration, "char_test", PartitionBy.NONE).col("cc", ColumnType.STRING)) {
            CairoTestUtils.create(model);
        }
        char[] data = { 'a', 'b', 'f', 'g' };
        try (TableWriter writer = new TableWriter(configuration, "char_test")) {
            for (int i = 0, n = data.length; i < n; i++) {
                TableWriter.Row r = writer.newRow();
                r.putStr(0, data[i]);
                r.append();
            }
            writer.commit();
        }
        try (TableReader reader = new TableReader(configuration, "char_test")) {
            final RecordCursor cursor = reader.getCursor();
            final Record record = cursor.getRecord();
            int index = 0;
            while (cursor.hasNext()) {
                Assert.assertTrue(index < data.length);
                CharSequence value = record.getStr(0);
                Assert.assertEquals(1, value.length());
                Assert.assertEquals(data[index], value.charAt(0));
                index++;
            }
        }
    });
}
Also used : RecordCursor(io.questdb.cairo.sql.RecordCursor) Record(io.questdb.cairo.sql.Record) Test(org.junit.Test)

Example 10 with RecordCursor

use of io.questdb.cairo.sql.RecordCursor in project questdb by bluestreak01.

the class TableReaderTest method testUnsuccessfulRemoveAndReloadSym.

@Test
public void testUnsuccessfulRemoveAndReloadSym() throws Exception {
    TestUtils.assertMemoryLeak(() -> {
        // create table with two string columns
        try (TableModel model = new TableModel(configuration, "x", PartitionBy.NONE).col("a", ColumnType.SYMBOL).col("b", ColumnType.SYMBOL)) {
            CairoTestUtils.create(model);
        }
        Rnd rnd = new Rnd();
        final int N = 1000;
        // make sure we forbid deleting column "b" files
        TestFilesFacade ff = new TestFilesFacade() {

            int counter = 5;

            @Override
            public boolean remove(LPSZ name) {
                if (counter > 0 && ((Chars.endsWith(name, "b.i") || Chars.endsWith(name, "b.d") || Chars.endsWith(name, "b.o") || Chars.endsWith(name, "b.k") || Chars.endsWith(name, "b.c") || Chars.endsWith(name, "b.v")))) {
                    counter--;
                    return false;
                }
                return super.remove(name);
            }

            @Override
            public boolean wasCalled() {
                return counter < 1;
            }
        };
        CairoConfiguration configuration = new DefaultCairoConfiguration(root) {

            @Override
            public FilesFacade getFilesFacade() {
                return ff;
            }
        };
        // populate table and delete column
        try (TableWriter writer = new TableWriter(configuration, "x")) {
            appendTwoSymbols(writer, rnd);
            writer.commit();
            try (TableReader reader = new TableReader(configuration, "x")) {
                long counter = 0;
                rnd.reset();
                RecordCursor cursor = reader.getCursor();
                final Record record = cursor.getRecord();
                while (cursor.hasNext()) {
                    Assert.assertEquals(rnd.nextChars(10), record.getSym(0));
                    Assert.assertEquals(rnd.nextChars(15), record.getSym(1));
                    counter++;
                }
                Assert.assertEquals(N, counter);
                // this should write metadata without column "b" but will ignore
                // file delete failures
                writer.removeColumn("b");
                if (configuration.getFilesFacade().isRestrictedFileSystem()) {
                    reader.closeColumnForRemove("b");
                }
                // reader.reload();
                // now when we add new column by same name it must not pick up files we failed to delete previously
                writer.addColumn("b", ColumnType.SYMBOL);
                // SymbolMap must be cleared when we try to do add values to new column
                appendTwoSymbols(writer, rnd);
                writer.commit();
                // now assert what reader sees
                Assert.assertTrue(reader.reload());
                Assert.assertEquals(N * 2, reader.size());
                rnd.reset();
                cursor.toTop();
                counter = 0;
                while (cursor.hasNext()) {
                    Assert.assertEquals(rnd.nextChars(10), record.getSym(0));
                    if (counter < N) {
                        // roll random generator to make sure it returns same values
                        rnd.nextChars(15);
                        Assert.assertNull(record.getSym(1));
                    } else {
                        Assert.assertEquals(rnd.nextChars(15), record.getSym(1));
                    }
                    counter++;
                }
                Assert.assertEquals(N * 2, counter);
            }
        }
        Assert.assertTrue(ff.wasCalled());
    });
}
Also used : RecordCursor(io.questdb.cairo.sql.RecordCursor) LPSZ(io.questdb.std.str.LPSZ) Record(io.questdb.cairo.sql.Record) Test(org.junit.Test)

Aggregations

RecordCursor (io.questdb.cairo.sql.RecordCursor)174 Test (org.junit.Test)137 Record (io.questdb.cairo.sql.Record)123 RecordCursorFactory (io.questdb.cairo.sql.RecordCursorFactory)108 AbstractGriffinTest (io.questdb.griffin.AbstractGriffinTest)87 TableWriter (io.questdb.cairo.TableWriter)71 Rnd (io.questdb.std.Rnd)29 LPSZ (io.questdb.std.str.LPSZ)10 SqlExecutionContextImpl (io.questdb.griffin.SqlExecutionContextImpl)7 SqlCompiler (io.questdb.griffin.SqlCompiler)6 DateFormat (io.questdb.std.datetime.DateFormat)6 Path (io.questdb.std.str.Path)6 StringSink (io.questdb.std.str.StringSink)6 CountDownLatch (java.util.concurrent.CountDownLatch)5 CyclicBarrier (java.util.concurrent.CyclicBarrier)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 SqlException (io.questdb.griffin.SqlException)4 BaseConnection (org.postgresql.core.BaseConnection)4 LoopInterruptedCheck (de.invesdwin.util.concurrent.loop.LoopInterruptedCheck)3 Instant (de.invesdwin.util.time.Instant)3