use of com.questdb.common.RecordCursor in project questdb by bluestreak01.
the class AbstractOptimiserTest method assertThat.
protected void assertThat(String expected, RecordSource rs, boolean header) throws IOException {
RecordCursor cursor = rs.prepareCursor(FACTORY_CONTAINER.getFactory());
try {
assertThat(expected, cursor, rs.getMetadata(), header);
} finally {
cursor.releaseCursor();
}
TestUtils.assertStrings(rs, FACTORY_CONTAINER.getFactory());
}
use of com.questdb.common.RecordCursor in project questdb by bluestreak01.
the class AbstractOptimiserTest method assertRowId.
protected static void assertRowId(String query, String longColumn) throws ParserException {
RecordSource src = compiler.compile(FACTORY_CONTAINER.getFactory(), query);
try {
RecordCursor cursor = src.prepareCursor(FACTORY_CONTAINER.getFactory());
try {
int dateIndex = src.getMetadata().getColumnIndex(longColumn);
HashMap<Long, Long> map = new HashMap<>();
long count = 0;
while (cursor.hasNext()) {
Record record = cursor.next();
map.put(record.getRowId(), record.getLong(dateIndex));
count++;
}
Assert.assertTrue(count > 0);
Record record = cursor.newRecord();
for (Map.Entry<Long, Long> e : map.entrySet()) {
Assert.assertEquals((long) e.getValue(), cursor.recordAt(e.getKey()).getLong(dateIndex));
cursor.recordAt(record, e.getKey());
Assert.assertEquals((long) e.getValue(), record.getLong(dateIndex));
}
} finally {
cursor.releaseCursor();
}
} finally {
Misc.free(src);
}
}
use of com.questdb.common.RecordCursor in project questdb by bluestreak01.
the class SQLExamples method main.
public static void main(String[] args) throws JournalException, ParserException, IOException {
if (args.length < 1) {
System.out.println("Usage: SQLExamples <path>");
System.exit(1);
}
try (Factory factory = new Factory(args[0], 1000, 1, 0)) {
// import movies data to query
ImportManager.importFile(factory, SQLExamples.class.getResource("/movies.csv").getFile(), ',', null, false);
// Create SQL engine instance.
QueryCompiler compiler = new QueryCompiler();
try (RecordSource rs = compiler.compile(factory, "'movies.csv'")) {
// Execute query and fetch results
RecordCursor cursor = rs.prepareCursor(factory);
try {
while (cursor.hasNext()) {
Record record = cursor.next();
}
} finally {
cursor.releaseCursor();
}
}
// to simplify query demonstration we have generic record source printer
RecordSourcePrinter printer = new RecordSourcePrinter(new StdoutSink());
printer.print(compiler.compile(factory, "'movies.csv'"), factory);
System.out.println("---------");
// find movie by ID
printer.print(compiler.compile(factory, "'movies.csv' where movieId = 62198"), factory);
System.out.println("---------");
// extract year from movie title
printer.print(compiler.compile(factory, "select title, pluck('\\(([0-9]*?)\\)', title) year from 'movies.csv' where movieId = 62198"), factory);
System.out.println("---------");
// order by movie year descending
printer.print(compiler.compile(factory, "select title, pluck('\\(([0-9]*?)\\)', title) year from 'movies.csv' order by year desc"), factory);
System.out.println("---------");
// count titles by year
printer.print(compiler.compile(factory, "select year, count() from (select title, pluck('\\(([0-9]*?)\\)', title) year from 'movies.csv' order by year desc)"), factory);
}
}
use of com.questdb.common.RecordCursor in project questdb by bluestreak01.
the class GenericInteropTest method testObjectWriteGenericRead.
@Test
public void testObjectWriteGenericRead() throws Exception {
try (JournalWriter<Data> writer = getFactory().writer(new JournalMetadataBuilder<Data>(Data.class, "test") {
{
$sym("sym").index();
$int("id").index();
$str("rateId").index();
}
})) {
Data d = new Data();
d.sym = "GBPUSD";
d.created = 30000;
d.bid = 0.65;
d.ask = 0.66;
d.bidSize = 1000;
d.askSize = 1100;
d.id = 1;
d.status = "OK";
d.user = "system";
d.rateId = "GBPUSD:GLOBAL";
d.active = true;
d.nullable = null;
d.ticks = 12345678;
d.modulo = 425;
writer.append(d);
writer.commit();
}
try (RecordSource rs = compile("test")) {
RecordCursor cursor = rs.prepareCursor(getFactory());
try {
Record e;
Assert.assertTrue(cursor.hasNext());
Assert.assertNotNull(e = cursor.next());
Assert.assertEquals("GBPUSD", e.getSym(0));
Assert.assertEquals(30000, e.getDate(1));
Assert.assertEquals(0.65, e.getDouble(2), 0.000001);
Assert.assertEquals(0.66, e.getDouble(3), 0.000001);
Assert.assertEquals(1000, e.getInt(4));
Assert.assertEquals(1100, e.getInt(5));
Assert.assertEquals(1, e.getByte(6));
TestUtils.assertEquals("OK", e.getFlyweightStr(7));
TestUtils.assertEquals("system", e.getFlyweightStr(8));
TestUtils.assertEquals("GBPUSD:GLOBAL", e.getFlyweightStr(9));
Assert.assertTrue(e.getBool(10));
Assert.assertNull(e.getFlyweightStr(11));
Assert.assertEquals(12345678, e.getLong(12));
Assert.assertEquals(425, e.getShort(13));
Assert.assertFalse(cursor.hasNext());
} finally {
cursor.releaseCursor();
}
}
}
use of com.questdb.common.RecordCursor in project questdb by bluestreak01.
the class DDLTests method testCreateAsSelectBin.
@Test
public void testCreateAsSelectBin() throws Exception {
int N = 10000;
int SZ = 4096;
ByteBuffer buf = ByteBuffer.allocateDirect(SZ);
try {
long addr = ByteBuffers.getAddress(buf);
try (JournalWriter w = compiler.createWriter(getFactory(), "create table x (a INT, b BINARY)")) {
Rnd rnd = new Rnd();
for (int i = 0; i < N; i++) {
long p = addr;
int n = (rnd.nextPositiveInt() % (SZ - 1)) / 8;
for (int j = 0; j < n; j++) {
Unsafe.getUnsafe().putLong(p, rnd.nextLong());
p += 8;
}
buf.limit(n * 8);
JournalEntryWriter ew = w.entryWriter();
ew.putInt(0, i);
ew.putBin(1, buf);
ew.append();
buf.clear();
}
w.commit();
}
exec("create table y as (x)");
int count = 0;
try (RecordSource rs = compiler.compile(getFactory(), "y")) {
RecordCursor cursor = rs.prepareCursor(getFactory());
try {
Rnd rnd = new Rnd();
while (cursor.hasNext()) {
Record rec = cursor.next();
Assert.assertEquals(count, rec.getInt(0));
long len = rec.getBinLen(1);
DirectInputStream is = rec.getBin(1);
is.copyTo(addr, 0, len);
long p = addr;
int n = (rnd.nextPositiveInt() % (SZ - 1)) / 8;
for (int j = 0; j < n; j++) {
Assert.assertEquals(rnd.nextLong(), Unsafe.getUnsafe().getLong(p));
p += 8;
}
count++;
}
} finally {
cursor.releaseCursor();
}
}
Assert.assertEquals(N, count);
} finally {
ByteBuffers.release(buf);
}
}
Aggregations