Search in sources :

Example 66 with Rnd

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

the class TableReaderRecordCursorFactoryTest method testFactory.

@Test
public void testFactory() throws Exception {
    TestUtils.assertMemoryLeak(() -> {
        final int N = 100;
        // separate two symbol columns with primitive. It will make problems apparent if index does not shift correctly
        try (TableModel model = new TableModel(configuration, "x", PartitionBy.DAY).col("a", ColumnType.STRING).col("b", ColumnType.SYMBOL).indexed(true, N / 4).col("i", ColumnType.INT).col("c", ColumnType.SYMBOL).indexed(true, N / 4).timestamp()) {
            CairoTestUtils.create(model);
        }
        final Rnd rnd = new Rnd();
        final String[] symbols = new String[N];
        final int M = 1000;
        final long increment = 1000000 * 60L * 10;
        for (int i = 0; i < N; i++) {
            symbols[i] = rnd.nextChars(8).toString();
        }
        rnd.reset();
        // prepare the data
        long timestamp = 0;
        try (TableWriter writer = new TableWriter(configuration, "x")) {
            for (int i = 0; i < M; i++) {
                TableWriter.Row row = writer.newRow(timestamp += increment);
                row.putStr(0, rnd.nextChars(20));
                row.putSym(1, symbols[rnd.nextPositiveInt() % N]);
                row.putInt(2, rnd.nextInt());
                row.putSym(3, symbols[rnd.nextPositiveInt() % N]);
                row.append();
            }
            writer.commit();
        }
        try (Engine engine = new Engine(configuration)) {
            RecordCursorFactory factory = new TableReaderRecordCursorFactory(engine, "x");
            long count = 0;
            RecordCursor cursor = factory.getCursor();
            try {
                rnd.reset();
                while (cursor.hasNext()) {
                    Record record = cursor.next();
                    TestUtils.assertEquals(rnd.nextChars(20), record.getFlyweightStr(0));
                    TestUtils.assertEquals(symbols[rnd.nextPositiveInt() % N], record.getSym(1));
                    Assert.assertEquals(rnd.nextInt(), record.getInt(2));
                    TestUtils.assertEquals(symbols[rnd.nextPositiveInt() % N], record.getSym(3));
                    count++;
                }
            } finally {
                cursor.releaseCursor();
            }
            Assert.assertEquals(0, engine.getBusyReaderCount());
            Assert.assertEquals(M, count);
        }
    });
}
Also used : RecordCursor(com.questdb.common.RecordCursor) Rnd(com.questdb.std.Rnd) RecordCursorFactory(com.questdb.cairo.sql.RecordCursorFactory) Record(com.questdb.common.Record) Test(org.junit.Test)

Example 67 with Rnd

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

the class RedBlackTreeTest method testAddAndGet.

@Test
public void testAddAndGet() {
    Rnd rnd = new Rnd();
    TreeSet<Long> control = new TreeSet<>();
    try (RedBlackTree tree = new RedBlackTree(new RedBlackTree.LongComparator() {

        private long left;

        @Override
        public int compare(long y) {
            return Long.compare(left, y);
        }

        @Override
        public void setLeft(long left) {
            this.left = left;
        }
    }, 1024)) {
        long l;
        for (int i = 0; i < 10000; i++) {
            tree.add(l = rnd.nextLong());
            control.add(l);
        }
        Iterator<Long> controlIterator = control.iterator();
        RedBlackTree.LongIterator iterator = tree.iterator();
        while (iterator.hasNext()) {
            Assert.assertTrue(controlIterator.hasNext());
            Assert.assertEquals(controlIterator.next().longValue(), iterator.next());
        }
        tree.clear();
        Assert.assertFalse(tree.iterator().hasNext());
    }
}
Also used : RedBlackTree(com.questdb.std.RedBlackTree) TreeSet(java.util.TreeSet) Rnd(com.questdb.std.Rnd) Test(org.junit.Test)

Example 68 with Rnd

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

the class RBTreeSortedRecordSourceTest method setUp.

@BeforeClass
public static void setUp() throws Exception {
    try (JournalWriter w = FACTORY_CONTAINER.getFactory().writer(new JournalStructure("xyz").$int("i").$str("str").$())) {
        int n = 100;
        Rnd rnd = new Rnd();
        for (int i = 0; i < n; i++) {
            JournalEntryWriter ew = w.entryWriter();
            ew.putInt(0, rnd.nextInt());
            ew.putStr(1, rnd.nextChars(2));
            ew.append();
        }
        w.commit();
    }
    try (JournalWriter w = FACTORY_CONTAINER.getFactory().writer(new JournalStructure("dupes").$int("x").$())) {
        for (int i = 0; i < 10; i++) {
            JournalEntryWriter ew = w.entryWriter();
            ew.putInt(0, i % 2 == 0 ? 10 : 20);
            ew.append();
        }
        JournalEntryWriter ew = w.entryWriter();
        ew.putInt(0, 30);
        ew.append();
        w.commit();
    }
    try (JournalWriter w = FACTORY_CONTAINER.getFactory().writer(new JournalStructure("timeseries").$double("d").$ts().$())) {
        Rnd rnd = new Rnd();
        long ts = Dates.toMillis(2016, 3, 12, 0, 0);
        for (int i = 0; i < 1000; i++) {
            JournalEntryWriter ew = w.entryWriter();
            ew.putDouble(0, rnd.nextDouble());
            ew.putDate(1, ts + (rnd.nextPositiveInt() % Dates.DAY_MILLIS));
            ew.append();
        }
        w.commit();
    }
}
Also used : JournalWriter(com.questdb.store.JournalWriter) JournalStructure(com.questdb.store.factory.configuration.JournalStructure) Rnd(com.questdb.std.Rnd) JournalEntryWriter(com.questdb.store.JournalEntryWriter) BeforeClass(org.junit.BeforeClass)

Example 69 with Rnd

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

the class BinaryTest method testBinaryPerformance.

@Test
public void testBinaryPerformance() throws Exception {
    try (JournalWriter<Band> writer = getFactory().writer(Band.class)) {
        final int count = 20000;
        Rnd r = new Rnd(System.currentTimeMillis(), System.currentTimeMillis());
        byte[] bytes = r.nextBytes(10240);
        String[] types = new String[] { "jazz", "rap", "pop", "rock", "soul" };
        String[] bands = new String[1200];
        for (int i = 0; i < bands.length; i++) {
            bands[i] = r.nextString(10);
        }
        long t = System.nanoTime();
        Band band = new Band();
        for (int i = 0; i < count; i++) {
            band.setName(bands[Math.abs(r.nextInt() % bands.length)]);
            band.setType(types[Math.abs(r.nextInt() % types.length)]);
            band.setImage(bytes);
            writer.append(band);
        }
        writer.commit();
        LOGGER.info().$("Appended ").$(count).$(" 10k blobs in ").$(TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - t)).$("ms.").$();
    }
}
Also used : Rnd(com.questdb.std.Rnd) Band(com.questdb.model.Band) AbstractTest(com.questdb.test.tools.AbstractTest) Test(org.junit.Test)

Example 70 with Rnd

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

the class ColumnTest method testTwoByteEdges.

@Test
public void testTwoByteEdges() throws JournalException {
    Rnd r = new Rnd();
    String s1 = r.nextString(65000);
    String s2 = r.nextString(65000);
    MemoryFile df1 = new MemoryFile(dataFile, 22, JournalMode.APPEND, false);
    MemoryFile idxFile1 = new MemoryFile(indexFile, 22, JournalMode.APPEND, false);
    try (VariableColumn varchar1 = new VariableColumn(df1, idxFile1)) {
        varchar1.putStr(s1);
        varchar1.commit();
        varchar1.putStr(s2);
        varchar1.commit();
    }
    MemoryFile df2 = new MemoryFile(dataFile, 22, JournalMode.READ, false);
    MemoryFile idxFile2 = new MemoryFile(indexFile, 22, JournalMode.READ, false);
    try (VariableColumn varchar2 = new VariableColumn(df2, idxFile2)) {
        Assert.assertEquals(s1, varchar2.getStr(0));
        Assert.assertEquals(s2, varchar2.getStr(1));
    }
}
Also used : Rnd(com.questdb.std.Rnd)

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