use of com.questdb.std.Rnd in project questdb by bluestreak01.
the class VariableColumnTest method testBin1.
@Test
public void testBin1() throws Exception {
int N = 1000;
int SZ = 4096;
ByteBuffer buf = ByteBuffer.allocateDirect(SZ);
long addr = ByteBuffers.getAddress(buf);
try {
Rnd rnd = new Rnd();
try (MemoryFile smallFile = new MemoryFile(new File(temporaryFolder.getRoot(), "small.d"), 8, JournalMode.APPEND, false)) {
try (VariableColumn col = new VariableColumn(smallFile, indexFile)) {
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);
col.putBin(buf);
col.commit();
buf.clear();
}
Assert.assertEquals(N, col.size());
rnd = new Rnd();
for (int i = 0; i < N; i++) {
long len = col.getBinLen(i);
DirectInputStream is = col.getBin(i);
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;
}
}
}
}
} finally {
ByteBuffers.release(buf);
}
}
use of com.questdb.std.Rnd in project questdb by bluestreak01.
the class VariableColumnTest method testReadBinaryColumnData.
@Test
public void testReadBinaryColumnData() throws Exception {
int bitHint = 8;
try (MemoryFile smallFile = new MemoryFile(new File(temporaryFolder.getRoot(), "small.d"), bitHint, JournalMode.APPEND, false)) {
VariableColumn col1 = new VariableColumn(smallFile, indexFile);
int max = (int) Math.pow(2, bitHint) * 10 + 1;
Rnd rnd = new Rnd();
OutputStream writeStream = col1.putBin();
for (int i = 0; i < max; i++) {
writeStream.write(rnd.nextInt());
}
writeStream.flush();
col1.commit();
DirectInputStream readStream = col1.getBin(0);
byte b;
int count = 0;
Rnd exp = new Rnd();
while ((b = (byte) readStream.read()) != -1) {
Assert.assertEquals(String.format("difference at index %d", count), (byte) exp.nextInt(), b);
count++;
}
}
}
use of com.questdb.std.Rnd in project questdb by bluestreak01.
the class CachingReaderFactoryTest method testLockBusyReader.
@Test
public void testLockBusyReader() throws Exception {
final int readerCount = 5;
int threadCount = 2;
final int iterations = 10000;
// create journals to read
final JournalMetadata<?>[] meta = new JournalMetadata[readerCount];
for (int i = 0; i < readerCount; i++) {
final JournalMetadata<?> m = new JournalStructure("x" + i).$date("ts").$().build();
((WriterFactory) getFactory()).writer(m).close();
meta[i] = m;
}
try {
try (final CachingReaderFactory rf = new CachingReaderFactory(factoryContainer.getConfiguration(), 1000, 2)) {
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 = null;
for (int i = 0; i < iterations; i++) {
if (name == null) {
name = meta[rnd.nextPositiveInt() % readerCount].getName();
}
while (true) {
try {
rf.lock(name);
lockTimes.add(System.currentTimeMillis());
LockSupport.parkNanos(100L);
rf.unlock(name);
name = null;
break;
} catch (JournalException e) {
if (!(e instanceof RetryLockException)) {
e.printStackTrace();
errors.incrementAndGet();
break;
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
errors.incrementAndGet();
}
halt.countDown();
}).start();
new Thread(() -> {
Rnd rnd = new Rnd();
workerTimes.add(System.currentTimeMillis());
for (int i = 0; i < iterations; i++) {
JournalMetadata<?> metadata = meta[rnd.nextPositiveInt() % readerCount];
try (Journal<?> ignored = rf.reader(metadata)) {
if (metadata == meta[readerCount - 1] && barrier.getNumberWaiting() > 0) {
barrier.await();
}
LockSupport.parkNanos(10L);
} catch (JournalLockedException ignored) {
} catch (Exception e) {
e.printStackTrace();
errors.incrementAndGet();
}
}
workerTimes.add(System.currentTimeMillis());
halt.countDown();
}).start();
halt.await();
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);
}
} catch (Throwable e) {
e.printStackTrace();
}
}
use of com.questdb.std.Rnd in project questdb by bluestreak01.
the class AppendObjectPartitioned method main.
/**
* Appends 1 million quotes into journal partitioned by day. Journal can only be partitioned on values of timestamp column.
*
* @param args factory directory
* @throws JournalException in case of any problems with the journal.
*/
public static void main(String[] args) throws JournalException {
if (args.length != 1) {
System.out.println("Usage: " + AppendObjectPartitioned.class.getName() + " <path>");
System.exit(1);
}
String journalLocation = args[0];
try (Factory factory = new Factory(new JournalConfigurationBuilder() {
{
$(Quote.class, "quote-by-day").partitionBy(PartitionBy.DAY).$ts();
}
}.build(journalLocation), 1000, 1, 0)) {
Files.delete(new File(factory.getConfiguration().getJournalBase(), "quote-by-day"));
// you can change it in runtime and also, optionally put journal in alternative location
try (JournalWriter<Quote> writer = factory.writer(Quote.class)) {
final int count = 1000000;
final String[] symbols = { "AGK.L", "BP.L", "TLW.L", "ABF.L", "LLOY.L", "BT-A.L", "WTB.L", "RRS.L", "ADM.L", "GKN.L", "HSBA.L" };
final Rnd r = new Rnd();
// reuse same same instance of Quote class to keep GC under control
final Quote q = new Quote();
long t = System.nanoTime();
for (int i = 0; i < count; i++) {
// prepare object for new set of data
q.clear();
// generate some data
q.setSym(symbols[Math.abs(r.nextInt() % (symbols.length - 1))]);
q.setAsk(Math.abs(r.nextDouble()));
q.setBid(Math.abs(r.nextDouble()));
q.setAskSize(Math.abs(r.nextInt() % 10000));
q.setBidSize(Math.abs(r.nextInt() % 10000));
q.setEx("LXE");
q.setMode("Fast trading");
q.setTimestamp(System.currentTimeMillis() + (i * 100));
writer.append(q);
}
// commit is necessary
writer.commit();
System.out.println("Journal size: " + writer.size());
System.out.println("Generated " + count + " objects in " + TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - t) + "ms.");
}
}
}
use of com.questdb.std.Rnd in project questdb by bluestreak01.
the class AppendRawPartitioned method main.
public static void main(String[] args) throws JournalException, ParserException {
if (args.length < 1) {
System.out.println("Usage: AppendRawPartitioned <path>");
System.exit(1);
}
final String location = args[0];
// factory can be reused in application and must be explicitly closed when no longer needed.
try (Factory factory = new Factory(location, 1000, 1, 0)) {
// to populate it.
try (JournalWriter writer = factory.writer(new JournalStructure("customers").$int("id").$str("name").$ts("updateDate").partitionBy(PartitionBy.DAY).$())) {
Rnd rnd = new Rnd();
long timestamp = System.currentTimeMillis();
for (int i = 0; i < 1000000; i++) {
// enforce timestamp order
JournalEntryWriter ew = writer.entryWriter(timestamp);
// columns accessed by index
ew.putInt(0, rnd.nextPositiveInt());
ew.putStr(1, rnd.nextChars(25));
// increment timestamp by 30 seconds
timestamp += 30000;
// append record to journal
ew.append();
}
// commit all records at once
// there is no limit on how many records can be in the same transaction
writer.commit();
}
}
}
Aggregations