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"));
}
}
});
}
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));
}
}
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());
}
}
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();
}
});
}
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);
}
}
Aggregations