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