use of io.questdb.std.Rnd in project questdb by bluestreak01.
the class PGJobContextTest method testInsertAllTypes.
private void testInsertAllTypes(boolean binary) throws Exception {
assertMemoryLeak(() -> {
compiler.compile("create table xyz (" + "a byte," + "b char," + "c short," + "d int," + "e long," + "f float," + "g double," + "h string," + "i symbol," + "j boolean," + "k long256" + ")", sqlExecutionContext);
try (final PGWireServer ignored = createPGServer(2);
final Connection connection = getConnection(false, binary);
final PreparedStatement insert = connection.prepareStatement("insert into xyz values (?,?,?,?,?,?,?,?,?,?,?)")) {
final Rnd rnd = new Rnd();
connection.setAutoCommit(false);
for (int i = 0; i < 10_000; i++) {
if (rnd.nextInt() % 4 > 0) {
insert.setByte(1, rnd.nextByte());
} else {
insert.setNull(1, Types.SMALLINT);
}
if (rnd.nextInt() % 4 > 0) {
insert.setByte(2, (byte) rnd.nextChar());
} else {
insert.setNull(2, Types.SMALLINT);
}
if (rnd.nextInt() % 4 > 0) {
insert.setShort(3, rnd.nextShort());
} else {
insert.setNull(3, Types.SMALLINT);
}
if (rnd.nextInt() % 4 > 0) {
insert.setInt(4, rnd.nextInt());
} else {
insert.setNull(4, Types.INTEGER);
}
if (rnd.nextInt() % 4 > 0) {
insert.setLong(5, rnd.nextLong());
} else {
insert.setNull(5, Types.BIGINT);
}
if (rnd.nextInt() % 4 > 0) {
insert.setFloat(6, rnd.nextFloat());
} else {
insert.setNull(6, Types.REAL);
}
if (rnd.nextInt() % 4 > 0) {
insert.setDouble(7, rnd.nextDouble());
} else {
insert.setNull(7, Types.FLOAT);
}
if (rnd.nextInt() % 4 > 0) {
insert.setString(8, "hello21");
} else {
insert.setNull(8, Types.VARCHAR);
}
if (rnd.nextInt() % 4 > 0) {
insert.setString(9, "bus");
} else {
insert.setNull(9, Types.VARCHAR);
}
if (rnd.nextInt() % 4 > 0) {
insert.setBoolean(10, true);
} else {
insert.setNull(10, Types.BOOLEAN);
}
if (rnd.nextInt() % 4 > 0) {
insert.setString(11, "05a9796963abad00001e5f6bbdb38");
} else {
insert.setNull(11, Types.VARCHAR);
}
insert.execute();
Assert.assertEquals(1, insert.getUpdateCount());
}
connection.commit();
rnd.reset();
try (RecordCursorFactory factory = compiler.compile("xyz", sqlExecutionContext).getRecordCursorFactory()) {
try (RecordCursor cursor = factory.getCursor(sqlExecutionContext)) {
final Record record = cursor.getRecord();
int count = 0;
while (cursor.hasNext()) {
if (rnd.nextInt() % 4 > 0) {
Assert.assertEquals(rnd.nextByte(), record.getByte(0));
} else {
Assert.assertEquals(0, record.getByte(0));
}
if (rnd.nextInt() % 4 > 0) {
Assert.assertEquals(rnd.nextChar(), record.getChar(1));
} else {
Assert.assertEquals(0, record.getChar(1));
}
if (rnd.nextInt() % 4 > 0) {
Assert.assertEquals(rnd.nextShort(), record.getShort(2));
} else {
Assert.assertEquals(0, record.getShort(2));
}
if (rnd.nextInt() % 4 > 0) {
Assert.assertEquals(rnd.nextInt(), record.getInt(3));
} else {
Assert.assertEquals(Numbers.INT_NaN, record.getInt(3));
}
if (rnd.nextInt() % 4 > 0) {
Assert.assertEquals(rnd.nextLong(), record.getLong(4));
} else {
Assert.assertEquals(Numbers.LONG_NaN, record.getLong(4));
}
if (rnd.nextInt() % 4 > 0) {
Assert.assertEquals(rnd.nextFloat(), record.getFloat(5), 0.0001f);
} else {
Assert.assertTrue(record.getFloat(5) != record.getFloat(5));
}
if (rnd.nextInt() % 4 > 0) {
Assert.assertEquals(rnd.nextDouble(), record.getDouble(6), 0.000001);
} else {
Assert.assertTrue(record.getDouble(6) != record.getDouble(6));
}
if (rnd.nextInt() % 4 > 0) {
TestUtils.assertEquals("hello21", record.getStr(7));
} else {
Assert.assertNull(record.getStr(7));
}
if (rnd.nextInt() % 4 > 0) {
TestUtils.assertEquals("bus", record.getSym(8));
} else {
Assert.assertNull(record.getSym(8));
}
if (rnd.nextInt() % 4 > 0) {
Assert.assertTrue(record.getBool(9));
} else {
Assert.assertFalse(record.getBool(9));
}
sink.clear();
record.getLong256(10, sink);
if (rnd.nextInt() % 4 > 0) {
TestUtils.assertEquals("0x5a9796963abad00001e5f6bbdb38", sink);
} else {
Assert.assertEquals(0, sink.length());
}
count++;
}
Assert.assertEquals(10_000, count);
}
}
}
});
}
use of io.questdb.std.Rnd in project questdb by bluestreak01.
the class SymbolCacheTest method testConcurrency.
@Test
public void testConcurrency() throws Exception {
assertMemoryLeak(() -> {
final Rnd rndCache = new Rnd();
final int N = 1_000_000;
long ts = TimestampFormatUtils.parseTimestamp("2020-09-10T20:00:00.000000Z");
final long incrementUs = 10000;
final String constValue = "hello";
compiler.compile("create table x(a symbol, c int, b symbol capacity 10000000, ts timestamp) timestamp(ts) partition by DAY", sqlExecutionContext);
try (SymbolCache symbolCache = new SymbolCache(new DefaultLineTcpReceiverConfiguration());
Path path = new Path()) {
path.of(configuration.getRoot()).concat("x");
symbolCache.of(configuration, path, "b", 1);
final CyclicBarrier barrier = new CyclicBarrier(2);
final SOCountDownLatch haltLatch = new SOCountDownLatch(1);
final AtomicBoolean cacheInError = new AtomicBoolean(false);
RingQueue<Holder> wheel = new RingQueue<Holder>(Holder::new, 256);
SPSequence pubSeq = new SPSequence(wheel.getCycle());
SCSequence subSeq = new SCSequence();
pubSeq.then(subSeq).then(pubSeq);
new Thread(() -> {
try {
barrier.await();
for (int i = 0; i < N; i++) {
// All keys should not be found, but we keep looking them up because
// we pretend we don't know this upfront. The aim is to cause
// race condition between lookup and table writer
final CharSequence value2 = rndCache.nextString(5);
symbolCache.getSymbolKey(constValue);
symbolCache.getSymbolKey(value2);
final long cursor = pubSeq.nextBully();
final Holder h = wheel.get(cursor);
// publish the value2 to the table writer
h.value1 = constValue;
h.value2 = Chars.toString(value2);
pubSeq.done(cursor);
}
} catch (Throwable e) {
cacheInError.set(true);
e.printStackTrace();
} finally {
haltLatch.countDown();
}
}).start();
try (TableWriter w = engine.getWriter(sqlExecutionContext.getCairoSecurityContext(), "x", "test")) {
barrier.await();
OUT: for (int i = 0; i < N; i++) {
long cursor;
while (true) {
cursor = subSeq.next();
if (cursor < 0) {
// due to random generator producing duplicate strings
if (haltLatch.getCount() < 1) {
break OUT;
}
} else {
break;
}
}
Holder h = wheel.get(cursor);
TableWriter.Row r = w.newRow(ts);
r.putSym(0, h.value1);
r.putInt(1, 0);
r.putSym(2, h.value2);
r.append();
subSeq.done(cursor);
if (i % 256 == 0) {
w.commit();
}
ts += incrementUs;
}
w.commit();
} finally {
haltLatch.await();
}
Assert.assertFalse(cacheInError.get());
}
compiler.compile("drop table x", sqlExecutionContext);
});
}
use of io.questdb.std.Rnd in project questdb by bluestreak01.
the class TableReaderReloadTest method testReloadAfterTruncate.
private void testReloadAfterTruncate(int partitionBy, long increment) {
if (Os.type == Os.WINDOWS) {
return;
}
final Rnd rnd = new Rnd();
final int bufferSize = 1024;
long buffer = Unsafe.malloc(bufferSize, MemoryTag.NATIVE_DEFAULT);
try (TableModel model = CairoTestUtils.getAllTypesModel(configuration, partitionBy)) {
model.timestamp();
CairoTestUtils.create(model);
}
long timestamp = 0;
try (TableWriter writer = new TableWriter(configuration, "all")) {
try (TableReader reader = new TableReader(configuration, "all")) {
Assert.assertFalse(reader.reload());
}
populateTable(rnd, buffer, timestamp, increment, writer);
rnd.reset();
try (TableReader reader = new TableReader(configuration, "all")) {
RecordCursor cursor = reader.getCursor();
final Record record = cursor.getRecord();
assertTable(rnd, buffer, cursor, record);
writer.truncate();
Assert.assertTrue(reader.reload());
cursor = reader.getCursor();
Assert.assertFalse(cursor.hasNext());
rnd.reset();
populateTable(rnd, buffer, timestamp, increment, writer);
Assert.assertTrue(reader.reload());
rnd.reset();
cursor = reader.getCursor();
assertTable(rnd, buffer, cursor, record);
}
}
}
use of io.questdb.std.Rnd in project questdb by bluestreak01.
the class BitmapIndexBwdNullReaderTest method testCursor.
@Test
public void testCursor() {
final Rnd rnd = new Rnd();
for (int i = 0; i < 10; i++) {
int n = rnd.nextPositiveInt() % 1024;
int m = n;
RowCursor cursor = reader.getCursor(true, 0, 0, n);
while (cursor.hasNext()) {
Assert.assertEquals(m--, cursor.next());
}
Assert.assertEquals(-1, m);
}
}
use of io.questdb.std.Rnd in project questdb by bluestreak01.
the class IntervalFwdDataFrameCursorTest method testIntervals.
private void testIntervals(int partitionBy, long increment, int rowCount, CharSequence expected, long expectedCount) throws Exception {
TestUtils.assertMemoryLeak(() -> {
try (TableModel model = new TableModel(configuration, "x", partitionBy).col("a", ColumnType.SYMBOL).indexed(true, 4).col("b", ColumnType.SYMBOL).indexed(true, 4).timestamp()) {
CairoTestUtils.create(model);
}
final Rnd rnd = new Rnd();
long timestamp = TimestampFormatUtils.parseTimestamp("1980-01-01T00:00:00.000Z");
try (TableWriter writer = new TableWriter(configuration, "x")) {
for (int i = 0; i < rowCount; i++) {
TableWriter.Row row = writer.newRow(timestamp);
row.putSym(0, rnd.nextChars(4));
row.putSym(1, rnd.nextChars(4));
row.append();
timestamp += increment;
}
writer.commit();
timestamp = Timestamps.addYear(timestamp, 3);
for (int i = 0; i < rowCount; i++) {
TableWriter.Row row = writer.newRow(timestamp);
row.putSym(0, rnd.nextChars(4));
row.putSym(1, rnd.nextChars(4));
row.append();
timestamp += increment;
}
writer.commit();
}
try (TableReader reader = new TableReader(configuration, "x")) {
final TableReaderRecord record = new TableReaderRecord();
IntervalFwdDataFrameCursor cursor = new IntervalFwdDataFrameCursor(new RuntimeIntervalModel(IntervalFwdDataFrameCursorTest.intervals), reader.getMetadata().getTimestampIndex());
cursor.of(reader, null);
record.of(reader);
assertEquals(expected, record, cursor);
if (expected.length() > 0) {
cursor.toTop();
assertIndexRowsMatchSymbol(cursor, record, 0, expectedCount);
cursor.toTop();
assertIndexRowsMatchSymbol(cursor, record, 1, expectedCount);
}
cursor.toTop();
assertEquals(expected, record, cursor);
}
});
}
Aggregations