use of io.questdb.std.Rnd in project questdb by bluestreak01.
the class IntervalFwdDataFrameCursorTest method testReload.
public void testReload(int partitionBy, long increment, LongList intervals, int rowCount, CharSequence expected1, CharSequence expected2) throws Exception {
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");
final int timestampIndex;
try (TableReader reader = engine.getReader(AllowAllCairoSecurityContext.INSTANCE, "x")) {
timestampIndex = reader.getMetadata().getTimestampIndex();
}
final TableReaderRecord record = new TableReaderRecord();
final IntervalFwdDataFrameCursorFactory factory = new IntervalFwdDataFrameCursorFactory(engine, "x", -1, 0, new RuntimeIntervalModel(intervals), timestampIndex);
try (DataFrameCursor cursor = factory.getCursor(AllowAllSqlSecurityContext.INSTANCE)) {
// assert that there is nothing to start with
record.of(cursor.getTableReader());
assertEquals("", record, cursor);
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();
Assert.assertTrue(cursor.reload());
assertEquals(expected1, record, cursor);
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();
Assert.assertTrue(cursor.reload());
if (expected2 != null) {
assertEquals(expected2, record, cursor);
} else {
assertEquals(expected1, record, cursor);
}
Assert.assertFalse(cursor.reload());
}
}
try (TableWriter writer = engine.getWriter(AllowAllCairoSecurityContext.INSTANCE, "x", "testing")) {
writer.removeColumn("a");
}
try {
factory.getCursor(AllowAllSqlSecurityContext.INSTANCE);
Assert.fail();
} catch (ReaderOutOfDateException ignored) {
}
});
}
use of io.questdb.std.Rnd in project questdb by bluestreak01.
the class FullFwdDataFrameCursorFactoryTest method testFactory.
@Test
public void testFactory() throws Exception {
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();
}
// 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();
}
FullFwdDataFrameCursorFactory factory = new FullFwdDataFrameCursorFactory(engine, "x", TableUtils.ANY_TABLE_ID, 0);
long count = 0;
try (DataFrameCursor cursor = factory.getCursor(AllowAllSqlSecurityContext.INSTANCE)) {
DataFrame frame;
while ((frame = cursor.next()) != null) {
count += frame.getRowHi() - frame.getRowLo();
}
}
Assert.assertEquals(0, engine.getBusyReaderCount());
Assert.assertEquals(M, count);
try (TableWriter writer = engine.getWriter(AllowAllCairoSecurityContext.INSTANCE, "x", "testing")) {
writer.removeColumn("b");
}
try {
factory.getCursor(AllowAllSqlSecurityContext.INSTANCE);
Assert.fail();
} catch (ReaderOutOfDateException ignored) {
}
});
}
use of io.questdb.std.Rnd in project questdb by bluestreak01.
the class FullBwdDataFrameCursorTest method testReload.
@Test
public void testReload() throws Exception {
final String expected = "-409854405\t339631474\t1970-01-04T00:00:00.000000Z\n" + "1569490116\t1573662097\t1970-01-03T16:00:00.000000Z\n" + "806715481\t1545253512\t1970-01-03T08:00:00.000000Z\n" + "-1436881714\t-1575378703\t1970-01-03T00:00:00.000000Z\n" + "-1191262516\t-2041844972\t1970-01-02T16:00:00.000000Z\n" + "1868723706\t-847531048\t1970-01-02T08:00:00.000000Z\n" + "1326447242\t592859671\t1970-01-02T00:00:00.000000Z\n" + "73575701\t-948263339\t1970-01-01T16:00:00.000000Z\n" + "1548800833\t-727724771\t1970-01-01T08:00:00.000000Z\n" + "-1148479920\t315515118\t1970-01-01T00:00:00.000000Z\n";
final String expectedNext = "-1975183723\t-1252906348\t1975-01-04T00:00:00.000000Z\n" + "-1125169127\t1631244228\t1975-01-03T16:00:00.000000Z\n" + "1404198\t-1715058769\t1975-01-03T08:00:00.000000Z\n" + "-1101822104\t-1153445279\t1975-01-03T00:00:00.000000Z\n" + "-1844391305\t-1520872171\t1975-01-02T16:00:00.000000Z\n" + "-85170055\t-1792928964\t1975-01-02T08:00:00.000000Z\n" + "-1432278050\t426455968\t1975-01-02T00:00:00.000000Z\n" + "1125579207\t-1849627000\t1975-01-01T16:00:00.000000Z\n" + "-1532328444\t-1458132197\t1975-01-01T08:00:00.000000Z\n" + "1530831067\t1904508147\t1975-01-01T00:00:00.000000Z\n";
assertMemoryLeak(() -> {
try (TableModel model = new TableModel(configuration, "x", PartitionBy.DAY).col("a", ColumnType.INT).col("b", ColumnType.INT).timestamp()) {
CairoTestUtils.create(model);
}
Rnd rnd = new Rnd();
long timestamp = 0;
long increment = 3600000000L * 8;
int N = 10;
try (TableWriter w = new TableWriter(configuration, "x")) {
for (int i = 0; i < N; i++) {
TableWriter.Row row = w.newRow(timestamp);
row.putInt(0, rnd.nextInt());
row.putInt(1, rnd.nextInt());
row.append();
timestamp += increment;
}
w.commit();
Assert.assertEquals(N, w.size());
FullBwdDataFrameCursorFactory factory = new FullBwdDataFrameCursorFactory(engine, "x", TableUtils.ANY_TABLE_ID, 0);
final TableReaderRecord record = new TableReaderRecord();
try (final DataFrameCursor cursor = factory.getCursor(AllowAllSqlSecurityContext.INSTANCE)) {
printCursor(record, cursor);
TestUtils.assertEquals(expected, sink);
// now add some more rows
timestamp = TimestampFormatUtils.parseTimestamp("1975-01-01T00:00:00.000Z");
for (int i = 0; i < N; i++) {
TableWriter.Row row = w.newRow(timestamp);
row.putInt(0, rnd.nextInt());
row.putInt(1, rnd.nextInt());
row.append();
timestamp += increment;
}
w.commit();
Assert.assertTrue(cursor.reload());
printCursor(record, cursor);
TestUtils.assertEquals(expectedNext + expected, sink);
}
w.removeColumn("a");
try {
factory.getCursor(AllowAllSqlSecurityContext.INSTANCE);
Assert.fail();
} catch (ReaderOutOfDateException ignored) {
}
}
});
}
use of io.questdb.std.Rnd in project questdb by bluestreak01.
the class IntervalBwdDataFrameCursorTest 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();
IntervalBwdDataFrameCursor cursor = new IntervalBwdDataFrameCursor(new RuntimeIntervalModel(IntervalBwdDataFrameCursorTest.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);
}
});
}
use of io.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, -1, NOOP_COLLECTOR)) {
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.valueOf(i));
Assert.assertEquals(i, reader.keyOf(cs));
}
Assert.assertEquals(N, reader.size());
Assert.assertNull(reader.valueOf(-1));
Assert.assertNull(reader.valueOf(N));
Assert.assertEquals(SymbolTable.VALUE_NOT_FOUND, reader.keyOf("hola"));
}
}
});
}
Aggregations