Search in sources :

Example 36 with Rnd

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

the class SymbolMapTest method testSimpleRead.

@Test
public void testSimpleRead() 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.assertEquals(N, reader.size());
                Assert.assertNull(reader.value(-1));
                Assert.assertNull(reader.value(N));
                Assert.assertEquals(SymbolTable.VALUE_NOT_FOUND, reader.getQuick("hola"));
            }
        }
    });
}
Also used : Path(com.questdb.std.str.Path) Rnd(com.questdb.std.Rnd) Test(org.junit.Test)

Example 37 with Rnd

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

the class ObjListTest method testAdd.

@Test
public void testAdd() {
    Rnd rnd = new Rnd();
    ObjList<String> list = new ObjList<>();
    for (int i = 0; i < 100; i++) {
        list.add(rnd.nextString(10));
    }
    Assert.assertEquals(100, list.size());
    rnd.reset();
    for (int i = 0; i < list.size(); i++) {
        Assert.assertEquals(rnd.nextString(10), list.getQuick(i));
    }
}
Also used : ObjList(com.questdb.std.ObjList) Rnd(com.questdb.std.Rnd) Test(org.junit.Test)

Example 38 with Rnd

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

the class CachingReaderFactoryTest method testConcurrentOpenAndClose.

@Test
public void testConcurrentOpenAndClose() throws Exception {
    final int readerCount = 5;
    int threadCount = 2;
    final int iterations = 1000;
    // create journals to read
    final JournalMetadata<?>[] meta = new JournalMetadata[readerCount];
    for (int i = 0; i < readerCount; i++) {
        final JournalMetadata<?> m = new JournalStructure("x" + i).$date("ts").$().build();
        ((WriterFactory) getFactory()).writer(m).close();
        meta[i] = m;
    }
    try (final CachingReaderFactory rf = new CachingReaderFactory(factoryContainer.getConfiguration(), 1000, 2)) {
        final CyclicBarrier barrier = new CyclicBarrier(threadCount);
        final CountDownLatch halt = new CountDownLatch(threadCount);
        final AtomicInteger errors = new AtomicInteger();
        for (int i = 0; i < threadCount; i++) {
            final int x = i;
            new Thread(() -> {
                Rnd rnd = new Rnd(x, -x);
                try {
                    barrier.await();
                    for (int i1 = 0; i1 < iterations; i1++) {
                        JournalMetadata<?> m = meta[rnd.nextPositiveInt() % readerCount];
                        try (Journal ignored = rf.reader(m)) {
                            LockSupport.parkNanos(100);
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    errors.incrementAndGet();
                } finally {
                    halt.countDown();
                }
            }).start();
        }
        halt.await();
        Assert.assertEquals(0, errors.get());
    }
}
Also used : Rnd(com.questdb.std.Rnd) Journal(com.questdb.store.Journal) CountDownLatch(java.util.concurrent.CountDownLatch) JournalLockedException(com.questdb.ex.JournalLockedException) JournalException(com.questdb.std.ex.JournalException) FactoryFullException(com.questdb.ex.FactoryFullException) RetryLockException(com.questdb.ex.RetryLockException) CyclicBarrier(java.util.concurrent.CyclicBarrier) JournalMetadata(com.questdb.store.factory.configuration.JournalMetadata) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) JournalStructure(com.questdb.store.factory.configuration.JournalStructure) AbstractTest(com.questdb.test.tools.AbstractTest) Test(org.junit.Test)

Example 39 with Rnd

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

the class FilteredTableRecordCursorFactoryTest 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 * 4;
        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)) {
            String value = symbols[N - 10];
            SymbolIndexRowCursorFactory symbolIndexRowCursorFactory = new SymbolIndexRowCursorFactory(engine, "x", "b", value);
            FullTableFrameCursorFactory dataFrameFactory = new FullTableFrameCursorFactory(engine, "x");
            FilteredTableRecordCursorFactory factory = new FilteredTableRecordCursorFactory(dataFrameFactory, symbolIndexRowCursorFactory);
            RecordCursor cursor = factory.getCursor();
            while (cursor.hasNext()) {
                Record record = cursor.next();
                TestUtils.assertEquals(value, record.getSym(1));
            }
            cursor.releaseCursor();
        }
    });
}
Also used : RecordCursor(com.questdb.common.RecordCursor) Rnd(com.questdb.std.Rnd) Record(com.questdb.common.Record) Test(org.junit.Test)

Example 40 with Rnd

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

the class NumbersTest method testRoundDown.

@Test
public void testRoundDown() throws Exception {
    Rnd rnd = new Rnd();
    for (int i = 0; i < 1000; i++) {
        double d = rnd.nextDouble();
        double n = Numbers.roundDown(d, 8);
        Assert.assertTrue(d + " " + n + " " + (d - n - 1E-8), d - n - 1E-8 < Numbers.TOLERANCE);
    }
}
Also used : Rnd(com.questdb.std.Rnd) 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