Search in sources :

Example 61 with Rnd

use of com.questdb.std.Rnd in project questdb by bluestreak01.

the class SymbolMapTest method testRollback.

@Test
public void testRollback() throws Exception {
    TestUtils.assertMemoryLeak(() -> {
        int N = 1024;
        try (Path path = new Path().of(configuration.getRoot())) {
            create(path, "x", N, true);
            try (SymbolMapWriter writer = new SymbolMapWriter(configuration, path, "x", 0)) {
                Rnd rnd = new Rnd();
                long prev = -1L;
                for (int i = 0; i < N; i++) {
                    CharSequence cs = rnd.nextChars(10);
                    long key = writer.put(cs);
                    Assert.assertEquals(prev + 1, key);
                    Assert.assertEquals(key, writer.put(cs));
                    prev = key;
                }
                writer.rollback(N / 2);
                prev = N / 2 - 1;
                for (int i = 0; i < N; i++) {
                    CharSequence cs = rnd.nextChars(10);
                    long key = writer.put(cs);
                    Assert.assertEquals(prev + 1, key);
                    Assert.assertEquals(key, writer.put(cs));
                    prev = key;
                }
            }
        }
    });
}
Also used : Path(com.questdb.std.str.Path) Rnd(com.questdb.std.Rnd) Test(org.junit.Test)

Example 62 with Rnd

use of com.questdb.std.Rnd in project questdb by bluestreak01.

the class SymbolMapTest method testTransactionalRead.

@Test
public void testTransactionalRead() throws Exception {
    TestUtils.assertMemoryLeak(() -> {
        int N = 1000000;
        Rnd rnd = new Rnd();
        try (Path path = new Path().of(configuration.getRoot())) {
            create(path, "x", N, false);
            try (SymbolMapWriter writer = new SymbolMapWriter(configuration, path, "x", 0)) {
                long prev = -1L;
                for (int i = 0; i < N; i++) {
                    CharSequence cs = rnd.nextChars(10);
                    long key = writer.put(cs);
                    Assert.assertEquals(prev + 1, key);
                    prev = key;
                }
                rnd.reset();
                try (SymbolMapReaderImpl reader = new SymbolMapReaderImpl(configuration, path, "x", N)) {
                    for (int i = 0; i < N; i++) {
                        CharSequence cs = rnd.nextChars(10);
                        TestUtils.assertEquals(cs, reader.value(i));
                        Assert.assertEquals(i, reader.getQuick(cs));
                    }
                    Assert.assertNull(reader.value(N));
                    Assert.assertEquals(SymbolTable.VALUE_NOT_FOUND, reader.getQuick("hola"));
                    Assert.assertEquals(N, writer.put("XYZ"));
                    // must not be able to read new symbol
                    Assert.assertNull(reader.value(N));
                    Assert.assertEquals(SymbolTable.VALUE_NOT_FOUND, reader.getQuick("XYZ"));
                    reader.updateSymbolCount(N + 1);
                    TestUtils.assertEquals("XYZ", reader.value(N));
                    Assert.assertEquals(N, reader.getQuick("XYZ"));
                }
            }
        }
    });
}
Also used : Path(com.questdb.std.str.Path) Rnd(com.questdb.std.Rnd) Test(org.junit.Test)

Example 63 with Rnd

use of com.questdb.std.Rnd in project questdb by bluestreak01.

the class SymbolMapTest method testAppend.

@Test
public void testAppend() throws Exception {
    TestUtils.assertMemoryLeak(() -> {
        int N = 1000;
        try (Path path = new Path().of(configuration.getRoot())) {
            create(path, "x", N, true);
            Rnd rnd = new Rnd();
            try (SymbolMapWriter writer = new SymbolMapWriter(configuration, path, "x", 0)) {
                long prev = -1L;
                for (int i = 0; i < N; i++) {
                    CharSequence cs = rnd.nextChars(10);
                    long key = writer.put(cs);
                    Assert.assertEquals(prev + 1, key);
                    Assert.assertEquals(key, writer.put(cs));
                    prev = key;
                }
            }
            try (SymbolMapWriter writer = new SymbolMapWriter(configuration, path, "x", N)) {
                long prev = N - 1;
                // append second batch and check that symbol keys start with N
                for (int i = 0; i < N; i++) {
                    CharSequence cs = rnd.nextChars(10);
                    long key = writer.put(cs);
                    Assert.assertEquals(prev + 1, key);
                    Assert.assertEquals(key, writer.put(cs));
                    prev = key;
                }
                // try append first batch - this should return symbol keys starting with 0
                rnd.reset();
                prev = -1;
                for (int i = 0; i < N; i++) {
                    CharSequence cs = rnd.nextChars(10);
                    long key = writer.put(cs);
                    Assert.assertEquals(prev + 1, key);
                    prev = key;
                }
                Assert.assertEquals(SymbolTable.VALUE_IS_NULL, writer.put(null));
            }
        }
    });
}
Also used : Path(com.questdb.std.str.Path) Rnd(com.questdb.std.Rnd) Test(org.junit.Test)

Example 64 with Rnd

use of com.questdb.std.Rnd in project questdb by bluestreak01.

the class SymbolMapTest method testSimpleAdd.

@Test
public void testSimpleAdd() throws Exception {
    TestUtils.assertMemoryLeak(() -> {
        int N = 1000000;
        try (Path path = new Path().of(configuration.getRoot())) {
            create(path, "x", N, false);
            try (SymbolMapWriter writer = new SymbolMapWriter(configuration, path, "x", 0)) {
                Rnd rnd = new Rnd();
                long prev = -1L;
                for (int i = 0; i < N; i++) {
                    CharSequence cs = rnd.nextChars(10);
                    long key = writer.put(cs);
                    Assert.assertEquals(prev + 1, key);
                    Assert.assertEquals(key, writer.put(cs));
                    prev = key;
                }
            }
        }
    });
}
Also used : Path(com.questdb.std.str.Path) Rnd(com.questdb.std.Rnd) Test(org.junit.Test)

Example 65 with Rnd

use of com.questdb.std.Rnd in project questdb by bluestreak01.

the class TableReadFailTest method testReloadTimeout.

@Test
public void testReloadTimeout() throws Exception {
    TestUtils.assertMemoryLeak(() -> {
        try (TableModel model = new TableModel(configuration, "x", PartitionBy.NONE).col("a", ColumnType.INT).col("b", ColumnType.LONG).timestamp()) {
            CairoTestUtils.create(model);
        }
        try (Path path = new Path();
            TableReader reader = new TableReader(configuration, "x");
            ReadWriteMemory mem = new ReadWriteMemory()) {
            final Rnd rnd = new Rnd();
            final int N = 1000;
            // home path at txn file
            path.of(configuration.getRoot()).concat("x").concat(TableUtils.TXN_FILE_NAME).$();
            try (TableWriter w = new TableWriter(configuration, "x")) {
                for (int i = 0; i < N; i++) {
                    TableWriter.Row r = w.newRow(0);
                    r.putInt(0, rnd.nextInt());
                    r.putLong(1, rnd.nextLong());
                    r.append();
                }
                w.commit();
            }
            Assert.assertTrue(reader.reload());
            RecordCursor cursor = reader.getCursor();
            rnd.reset();
            int count = 0;
            while (cursor.hasNext()) {
                Record r = cursor.next();
                Assert.assertEquals(rnd.nextInt(), r.getInt(0));
                Assert.assertEquals(rnd.nextLong(), r.getLong(1));
                count++;
            }
            Assert.assertEquals(N, count);
            mem.of(configuration.getFilesFacade(), path, configuration.getFilesFacade().getPageSize());
            // keep txn file parameters
            long offset = configuration.getFilesFacade().length(mem.getFd());
            long txn = mem.getLong(TableUtils.TX_OFFSET_TXN);
            // corrupt the txn file
            mem.jumpTo(TableUtils.TX_OFFSET_TXN);
            mem.putLong(123);
            mem.jumpTo(offset);
            mem.close();
            // this should time out
            try {
                reader.reload();
                Assert.fail();
            } catch (CairoException e) {
                TestUtils.assertContains(e.getMessage(), "timeout");
            }
            // restore txn file to its former glory
            mem.of(configuration.getFilesFacade(), path, configuration.getFilesFacade().getPageSize());
            mem.jumpTo(TableUtils.TX_OFFSET_TXN);
            mem.putLong(txn);
            mem.jumpTo(offset);
            mem.close();
            mem.close();
            // make sure reload functions correctly
            Assert.assertFalse(reader.reload());
            try (TableWriter w = new TableWriter(configuration, "x")) {
                // add more data
                for (int i = 0; i < N; i++) {
                    TableWriter.Row r = w.newRow(0);
                    r.putInt(0, rnd.nextInt());
                    r.putLong(1, rnd.nextLong());
                    r.append();
                }
                w.commit();
            }
            // does positive reload work?
            Assert.assertTrue(reader.reload());
            // can reader still see correct data?
            cursor = reader.getCursor();
            rnd.reset();
            count = 0;
            while (cursor.hasNext()) {
                Record r = cursor.next();
                Assert.assertEquals(rnd.nextInt(), r.getInt(0));
                Assert.assertEquals(rnd.nextLong(), r.getLong(1));
                count++;
            }
            Assert.assertEquals(2 * N, count);
        }
    });
}
Also used : Path(com.questdb.std.str.Path) RecordCursor(com.questdb.common.RecordCursor) Rnd(com.questdb.std.Rnd) Record(com.questdb.common.Record) Test(org.junit.Test)

Aggregations

Rnd (com.questdb.std.Rnd)82 Test (org.junit.Test)50 JournalEntryWriter (com.questdb.store.JournalEntryWriter)43 JournalStructure (com.questdb.store.factory.configuration.JournalStructure)43 JournalWriter (com.questdb.store.JournalWriter)42 AbstractTest (com.questdb.test.tools.AbstractTest)30 BeforeClass (org.junit.BeforeClass)8 Path (com.questdb.std.str.Path)7 JournalException (com.questdb.std.ex.JournalException)6 StringSink (com.questdb.std.str.StringSink)5 ObjList (com.questdb.std.ObjList)4 Factory (com.questdb.store.factory.Factory)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 NumericException (com.questdb.common.NumericException)3 Record (com.questdb.common.Record)3 RecordCursor (com.questdb.common.RecordCursor)3 Quote (com.questdb.model.Quote)3 ObjHashSet (com.questdb.std.ObjHashSet)3 File (java.io.File)3 BootstrapEnv (com.questdb.BootstrapEnv)2