use of com.questdb.ql.RecordSourcePrinter in project questdb by bluestreak01.
the class HttpServerTest method testImportNumberPrefixedColumn.
@Test
public void testImportNumberPrefixedColumn() throws Exception {
final ServerConfiguration configuration = new ServerConfiguration();
BootstrapEnv env = new BootstrapEnv();
env.configuration = configuration;
env.factory = getFactory();
env.typeProbeCollection = TYPE_PROBE_COLLECTION;
env.matcher = new SimpleUrlMatcher() {
{
put("/imp", new ImportHandler(env));
}
};
HttpServer server = new HttpServer(env);
server.start();
try {
Assert.assertEquals(200, HttpTestUtils.upload("/csv/test-import-num-prefix.csv", "http://localhost:9000/imp?fmt=json", null, null));
StringSink sink = new StringSink();
RecordSourcePrinter printer = new RecordSourcePrinter(sink);
QueryCompiler qc = new QueryCompiler(env);
try (RecordSource rs = qc.compile(env.factory, "select count(StrSym), count(IntSym), count(_1IntCol), count(long), count() from 'test-import-num-prefix.csv'")) {
printer.print(rs, env.factory);
}
TestUtils.assertEquals("126\t126\t128\t129\t129\n", sink);
} finally {
server.halt();
}
}
use of com.questdb.ql.RecordSourcePrinter in project questdb by bluestreak01.
the class ReaderPoolTest method testConcurrentRead.
@Test
public void testConcurrentRead() throws Exception {
final int readerCount = 5;
int threadCount = 2;
final int iterations = 1000000;
Rnd dataRnd = new Rnd();
final String[] names = new String[readerCount];
final String[] expectedRows = new String[readerCount];
final CharSequenceObjHashMap<String> expectedRowMap = new CharSequenceObjHashMap<>();
for (int i = 0; i < readerCount; i++) {
names[i] = "x" + i;
try (TableModel model = new TableModel(configuration, names[i], PartitionBy.NONE).col("ts", ColumnType.DATE)) {
CairoTestUtils.create(model);
}
try (TableWriter w = new TableWriter(configuration, names[i])) {
for (int k = 0; k < 10; k++) {
TableWriter.Row r = w.newRow(0);
r.putDate(0, dataRnd.nextLong());
r.append();
}
w.commit();
}
sink.clear();
try (TableReader r = new TableReader(configuration, names[i])) {
printer.print(r.getCursor(), true, r.getMetadata());
}
expectedRows[i] = sink.toString();
expectedRowMap.put(names[i], expectedRows[i]);
}
assertWithPool((ReaderPool pool) -> {
final CyclicBarrier barrier = new CyclicBarrier(threadCount);
final CountDownLatch halt = new CountDownLatch(threadCount);
final AtomicInteger errors = new AtomicInteger();
for (int k = 0; k < threadCount; k++) {
new Thread(new Runnable() {
final ObjHashSet<TableReader> readers = new ObjHashSet<>();
final StringSink sink = new StringSink();
final RecordSourcePrinter printer = new RecordSourcePrinter(sink);
@Override
public void run() {
Rnd rnd = new Rnd();
try {
barrier.await();
String name;
// 3. it will close of of readers if has opened
for (int i = 0; i < iterations; i++) {
if (readers.size() == 0 || (readers.size() < 40 && rnd.nextPositiveInt() % 4 == 0)) {
name = names[rnd.nextPositiveInt() % readerCount];
try {
Assert.assertTrue(readers.add(pool.get(name)));
} catch (EntryUnavailableException ignore) {
}
}
Thread.yield();
if (readers.size() == 0) {
continue;
}
int index = rnd.nextPositiveInt() % readers.size();
TableReader reader = readers.get(index);
Assert.assertTrue(reader.isOpen());
// read rows
RecordCursor cursor = reader.getCursor();
sink.clear();
printer.print(cursor, true, reader.getMetadata());
TestUtils.assertEquals(expectedRowMap.get(reader.getTableName()), sink);
Thread.yield();
if (readers.size() > 0 && rnd.nextPositiveInt() % 4 == 0) {
TableReader r2 = readers.get(rnd.nextPositiveInt() % readers.size());
Assert.assertTrue(r2.isOpen());
r2.close();
Assert.assertTrue(readers.remove(r2));
}
Thread.yield();
}
} catch (Exception e) {
errors.incrementAndGet();
e.printStackTrace();
} finally {
for (int i = 0; i < readers.size(); i++) {
readers.get(i).close();
}
halt.countDown();
}
}
}).start();
}
halt.await();
Assert.assertEquals(0, halt.getCount());
Assert.assertEquals(0, errors.get());
});
}
Aggregations