use of com.questdb.std.str.StringSink in project questdb by bluestreak01.
the class LexerTest method testUnicode.
@Test
@Ignore
public void testUnicode() throws Exception {
Lexer lex = new Lexer();
lex.defineSymbol("+");
lex.defineSymbol("++");
lex.defineSymbol("*");
String s = "'авг'";
byte[] bb = s.getBytes("UTF8");
System.out.println(new String(bb));
long mem = Unsafe.malloc(bb.length);
for (int i = 0; i < bb.length; i++) {
Unsafe.getUnsafe().putByte(mem + i, bb[i]);
}
DirectByteCharSequence cs = new DirectByteCharSequence();
cs.of(mem, mem + bb.length);
lex.setContent(cs);
StringSink sink = new StringSink();
while (lex.hasNext()) {
sink.put(lex.optionTok());
}
TestUtils.assertEquals("a+'b'*abc", sink);
}
use of com.questdb.std.str.StringSink in project questdb by bluestreak01.
the class FullTableFrameCursorTest method assertMetadataEquals.
private void assertMetadataEquals(RecordMetadata a, RecordMetadata b) {
StringSink sinkA = new StringSink();
StringSink sinkB = new StringSink();
a.toJson(sinkA);
b.toJson(sinkB);
TestUtils.assertEquals(sinkA, sinkB);
}
use of com.questdb.std.str.StringSink in project questdb by bluestreak01.
the class ReaderPoolTest method testLockBusyReader.
@Test
public void testLockBusyReader() throws Exception {
final int readerCount = 5;
int threadCount = 2;
final int iterations = 10000;
Rnd dataRnd = new Rnd();
StringSink sink = new StringSink();
RecordSourcePrinter printer = new RecordSourcePrinter(sink);
final String[] names = new String[readerCount];
final String[] expectedRows = new String[readerCount];
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();
}
LOG.info().$("testLockBusyReader BEGIN").$();
assertWithPool(pool -> {
final CyclicBarrier barrier = new CyclicBarrier(threadCount);
final CountDownLatch halt = new CountDownLatch(threadCount);
final AtomicInteger errors = new AtomicInteger();
final LongList lockTimes = new LongList();
final LongList workerTimes = new LongList();
new Thread(() -> {
Rnd rnd = new Rnd();
try {
barrier.await();
String name;
for (int i = 0; i < iterations; i++) {
name = names[rnd.nextPositiveInt() % readerCount];
while (true) {
if (pool.lock(name)) {
lockTimes.add(System.currentTimeMillis());
LockSupport.parkNanos(10L);
pool.unlock(name);
break;
}
}
}
} catch (Exception e) {
errors.incrementAndGet();
e.printStackTrace();
} finally {
halt.countDown();
}
}).start();
new Thread(() -> {
Rnd rnd = new Rnd();
try {
workerTimes.add(System.currentTimeMillis());
for (int i = 0; i < iterations; i++) {
int index = rnd.nextPositiveInt() % readerCount;
String name = names[index];
try (TableReader r = pool.get(name)) {
RecordCursor cursor = r.getCursor();
sink.clear();
printer.print(cursor, true, r.getMetadata());
TestUtils.assertEquals(expectedRows[index], sink);
if (name.equals(names[readerCount - 1]) && barrier.getNumberWaiting() > 0) {
barrier.await();
}
LockSupport.parkNanos(10L);
} catch (EntryLockedException | EntryUnavailableException ignored) {
} catch (Exception e) {
errors.incrementAndGet();
e.printStackTrace();
break;
}
}
workerTimes.add(System.currentTimeMillis());
} finally {
halt.countDown();
}
}).start();
halt.await();
Assert.assertEquals(0, halt.getCount());
Assert.assertEquals(0, errors.get());
// check that there are lock times between worker times
int count = 0;
// ensure that we have worker times
Assert.assertEquals(2, workerTimes.size());
long lo = workerTimes.get(0);
long hi = workerTimes.get(1);
Assert.assertTrue(lockTimes.size() > 0);
for (int i = 0, n = lockTimes.size(); i < n; i++) {
long t = lockTimes.getQuick(i);
if (t > lo && t < hi) {
count++;
}
}
Assert.assertTrue(count > 0);
LOG.info().$("testLockBusyReader END").$();
});
}
use of com.questdb.std.str.StringSink in project questdb by bluestreak01.
the class LinuxLineProtoReceiverTest method assertReceive.
private void assertReceive(ReceiverConfiguration receiverCfg, ReceiverFactory factory) throws Exception {
TestUtils.assertMemoryLeak(() -> {
final String expected = "colour\tshape\tsize\ttimestamp\n" + "blue\tsquare\t3.400000000000\t1970-01-01T00:01:40.000Z\n" + "blue\tsquare\t3.400000000000\t1970-01-01T00:01:40.000Z\n" + "blue\tsquare\t3.400000000000\t1970-01-01T00:01:40.000Z\n" + "blue\tsquare\t3.400000000000\t1970-01-01T00:01:40.000Z\n" + "blue\tsquare\t3.400000000000\t1970-01-01T00:01:40.000Z\n" + "blue\tsquare\t3.400000000000\t1970-01-01T00:01:40.000Z\n" + "blue\tsquare\t3.400000000000\t1970-01-01T00:01:40.000Z\n" + "blue\tsquare\t3.400000000000\t1970-01-01T00:01:40.000Z\n" + "blue\tsquare\t3.400000000000\t1970-01-01T00:01:40.000Z\n" + "blue\tsquare\t3.400000000000\t1970-01-01T00:01:40.000Z\n";
CairoConfiguration cairoCfg = new DefaultCairoConfiguration(root);
try (WriterPool pool = new WriterPool(cairoCfg)) {
Job receiver = factory.createReceiver(receiverCfg, cairoCfg, pool);
try {
CountDownLatch workerHaltLatch = new CountDownLatch(1);
try (TableModel model = new TableModel(configuration, "tab", PartitionBy.NONE).col("colour", ColumnType.SYMBOL).col("shape", ColumnType.SYMBOL).col("size", ColumnType.DOUBLE).timestamp()) {
CairoTestUtils.create(model);
}
// warm writer up
try (TableWriter w = pool.get("tab")) {
w.warmUp();
}
ObjHashSet<Job> jobs = new ObjHashSet<>();
jobs.add(receiver);
Worker worker = new Worker(jobs, workerHaltLatch);
worker.start();
try (LineProtoSender sender = new LineProtoSender("127.0.0.1", receiverCfg.getPort(), 1400)) {
for (int i = 0; i < 10; i++) {
sender.metric("tab").tag("colour", "blue").tag("shape", "square").field("size", 3.4, 4).$(100000000);
}
sender.flush();
}
try (TableReader reader = new TableReader(cairoCfg, "tab")) {
int count = 1000000;
while (true) {
if (count-- > 0 && reader.size() < 10) {
reader.reload();
LockSupport.parkNanos(1);
} else {
break;
}
}
Assert.assertTrue(count > 0);
worker.halt();
Assert.assertTrue(workerHaltLatch.await(3, TimeUnit.SECONDS));
StringSink sink = new StringSink();
RecordSourcePrinter printer = new RecordSourcePrinter(sink);
printer.print(reader.getCursor(), true, reader.getMetadata());
TestUtils.assertEquals(expected, sink);
}
} finally {
Misc.free(receiver);
}
}
});
}
use of com.questdb.std.str.StringSink in project questdb by bluestreak01.
the class NumbersTest method testLong.
@Test
public void testLong() throws Exception {
Rnd rnd = new Rnd();
StringSink sink = new StringSink();
for (int i = 0; i < 100; i++) {
long l1 = rnd.nextLong();
long l2 = rnd.nextLong();
sink.clear();
Numbers.append(sink, l1);
int p = sink.length();
Numbers.append(sink, l2);
Assert.assertEquals(l1, Numbers.parseLong(sink, 0, p));
Assert.assertEquals(l2, Numbers.parseLong(sink, p, sink.length()));
}
}
Aggregations