Search in sources :

Example 26 with LPSZ

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

the class LineTcpConnectionContextTest method testCairoExceptionOnCreateTable.

@Test
public void testCairoExceptionOnCreateTable() throws Exception {
    String table = "cairoEx";
    runInContext(new FilesFacadeImpl() {

        @Override
        public long openRW(LPSZ name) {
            if (Chars.endsWith(name, "broken.d")) {
                return -1;
            }
            return super.openRW(name);
        }
    }, () -> {
        recvBuffer = table + ",location=us-eastcoast temperature=81,broken=23 1465839830101400200\n" + table + ",location=us-midwest temperature=82 1465839830100400200\n" + table + ",location=us-midwest temperature=83 1465839830100500200\n" + table + ",location=us-midwest temperature=85 1465839830102300200\n" + table + ",location=us-eastcoast temperature=89 1465839830102400200\n" + table + ",location=us-eastcoast temperature=80 1465839830102400200\n" + table + ",location=us-westcost temperature=82 1465839830102500200\n";
        do {
            handleContextIO();
            Assert.assertFalse(disconnected);
        } while (recvBuffer.length() > 0);
        closeContext();
        String expected = "location\ttemperature\tbroken\ttimestamp\n";
        assertTable(expected, table);
    }, null, null);
}
Also used : LPSZ(io.questdb.std.str.LPSZ) FilesFacadeImpl(io.questdb.std.FilesFacadeImpl) Test(org.junit.Test)

Example 27 with LPSZ

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

the class TableReaderTest method testUnsuccessfulRemoveAndReloadSymTwice.

@Test
public void testUnsuccessfulRemoveAndReloadSymTwice() 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();
                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");
                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)

Example 28 with LPSZ

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

the class TableReaderTest method testUnsuccessfulFileRemove.

@Test
public void testUnsuccessfulFileRemove() throws Exception {
    TestUtils.assertMemoryLeak(() -> {
        // create table with two string columns
        try (TableModel model = new TableModel(configuration, "x", PartitionBy.NONE).col("a", ColumnType.STRING).col("b", ColumnType.STRING)) {
            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 = 0;

            @Override
            public boolean remove(LPSZ name) {
                if (Chars.endsWith(name, "b.i") || Chars.endsWith(name, "b.d")) {
                    counter++;
                    return false;
                }
                return super.remove(name);
            }

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

            @Override
            public FilesFacade getFilesFacade() {
                return ff;
            }
        };
        // populate table and delete column
        try (TableWriter writer = new TableWriter(configuration, "x")) {
            for (int i = 0; i < N; i++) {
                TableWriter.Row row = writer.newRow();
                row.putStr(0, rnd.nextChars(10));
                row.putStr(1, rnd.nextChars(15));
                row.append();
            }
            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.getStr(0));
                    Assert.assertEquals(rnd.nextChars(15), record.getStr(1));
                    counter++;
                }
                Assert.assertEquals(N, counter);
                // this should write metadata without column "b" but will ignore
                // file delete failures
                writer.removeColumn("b");
                // this must fail because we cannot delete foreign files
                try {
                    writer.addColumn("b", ColumnType.STRING);
                    Assert.fail();
                } catch (CairoException e) {
                    TestUtils.assertContains(e.getFlyweightMessage(), "Cannot remove");
                }
                // now assert what reader sees
                Assert.assertTrue(reader.reload());
                Assert.assertEquals(N, reader.size());
                rnd.reset();
                cursor.toTop();
                while (cursor.hasNext()) {
                    Assert.assertEquals(rnd.nextChars(10), record.getStr(0));
                    // roll random generator to make sure it returns same values
                    rnd.nextChars(15);
                    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)

Example 29 with LPSZ

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

the class CairoMemoryTest method testReadWriteCannotOpenFile.

@Test
public void testReadWriteCannotOpenFile() {
    long used = Unsafe.getMemUsed();
    class X extends FilesFacadeImpl {

        @Override
        public long openRW(LPSZ name) {
            int n = name.length();
            if (n > 5 && Chars.equals(".fail", name, n - 5, n)) {
                return -1;
            }
            return super.openRW(name);
        }
    }
    X ff = new X();
    long openFileCount = ff.getOpenFileCount();
    int successCount = 0;
    int failCount = 0;
    try (Path path = new Path()) {
        path.of(temp.getRoot().getAbsolutePath());
        int prefixLen = path.length();
        try (MemoryCMARW mem = Vm.getCMARWInstance()) {
            Rnd rnd = new Rnd();
            for (int k = 0; k < 10; k++) {
                path.trimTo(prefixLen).concat(rnd.nextString(10));
                boolean fail = rnd.nextBoolean();
                if (fail) {
                    path.put(".fail").$();
                    failCount++;
                } else {
                    path.put(".data").$();
                    successCount++;
                }
                if (fail) {
                    try {
                        mem.of(ff, path, 2 * ff.getPageSize(), -1, MemoryTag.MMAP_DEFAULT);
                        Assert.fail();
                    } catch (CairoException ignored) {
                    }
                } else {
                    mem.of(ff, path, 2 * ff.getPageSize(), -1, MemoryTag.MMAP_DEFAULT);
                    for (int i = 0; i < N; i++) {
                        mem.putLong(i);
                    }
                    Assert.assertEquals(N * 8, mem.getAppendOffset());
                }
            }
        }
    }
    Assert.assertEquals(used, Unsafe.getMemUsed());
    Assert.assertEquals(openFileCount, ff.getOpenFileCount());
    Assert.assertTrue(failCount > 0);
    Assert.assertTrue(successCount > 0);
}
Also used : Path(io.questdb.std.str.Path) MemoryCMARW(io.questdb.cairo.vm.api.MemoryCMARW) LPSZ(io.questdb.std.str.LPSZ) Test(org.junit.Test)

Example 30 with LPSZ

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

the class TableReaderTest method testUnsuccessfulFileRename.

@Test
public void testUnsuccessfulFileRename() throws Exception {
    TestUtils.assertMemoryLeak(() -> {
        // create table with two string columns
        try (TableModel model = new TableModel(configuration, "x", PartitionBy.NONE).col("a", ColumnType.STRING).col("b", ColumnType.STRING)) {
            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 = 0;

            @Override
            public boolean remove(LPSZ name) {
                if (Chars.endsWith(name, "b.i") || Chars.endsWith(name, "b.d")) {
                    counter++;
                    return false;
                }
                return super.remove(name);
            }

            @Override
            public boolean rename(LPSZ name, LPSZ to) {
                if (Chars.endsWith(name, "b.i") || Chars.endsWith(name, "b.d")) {
                    counter++;
                    return false;
                }
                return super.rename(name, to);
            }

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

            @Override
            public FilesFacade getFilesFacade() {
                return ff;
            }
        };
        // populate table and delete column
        try (TableWriter writer = new TableWriter(configuration, "x")) {
            for (int i = 0; i < N; i++) {
                TableWriter.Row row = writer.newRow();
                row.putStr(0, rnd.nextChars(10));
                row.putStr(1, rnd.nextChars(15));
                row.append();
            }
            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.getStr(0));
                    Assert.assertEquals(rnd.nextChars(15), record.getStr(1));
                    counter++;
                }
                Assert.assertEquals(N, counter);
                // this should write metadata without column "b" but will ignore
                // file delete failures
                writer.renameColumn("b", "bb");
                // this must fail because we cannot delete foreign files
                try {
                    writer.addColumn("b", ColumnType.STRING);
                    Assert.fail();
                } catch (CairoException e) {
                    TestUtils.assertContains(e.getFlyweightMessage(), "Cannot remove");
                }
                // now assert what reader sees
                Assert.assertTrue(reader.reload());
                Assert.assertEquals(N, reader.size());
                rnd.reset();
                cursor.toTop();
                while (cursor.hasNext()) {
                    Assert.assertEquals(rnd.nextChars(10), record.getStr(0));
                    // roll random generator to make sure it returns same values
                    rnd.nextChars(15);
                    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

LPSZ (io.questdb.std.str.LPSZ)50 Test (org.junit.Test)49 FilesFacadeImpl (io.questdb.std.FilesFacadeImpl)35 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)11 RecordCursor (io.questdb.cairo.sql.RecordCursor)8 Record (io.questdb.cairo.sql.Record)7 Path (io.questdb.std.str.Path)5 FilesFacade (io.questdb.std.FilesFacade)3 RecordCursorFactory (io.questdb.cairo.sql.RecordCursorFactory)1 MemoryCMARW (io.questdb.cairo.vm.api.MemoryCMARW)1 MemoryMA (io.questdb.cairo.vm.api.MemoryMA)1 TestMicroClock (io.questdb.test.tools.TestMicroClock)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1