Search in sources :

Example 1 with Rnd

use of com.questdb.std.Rnd in project questdb by bluestreak01.

the class ScenarioTest method testLagTrickle.

@Test
public void testLagTrickle() throws Exception {
    // prepare test data
    try (JournalWriter<Quote> origin = getFactory().writer(Quote.class, "origin")) {
        TestData.appendQuoteData2(origin);
        try (final JournalWriter<Quote> randomOrigin = getFactory().writer(new JournalKey<>(Quote.class, "origin-rnd", PartitionBy.NONE, Constants.NULL_RECORD_HINT, false))) {
            randomOrigin.append(origin.query().all().asResultSet().shuffle(new Rnd()));
            try (final JournalWriter<Quote> remote = getFactory().writer(Quote.class, "remote")) {
                try (final Journal<Quote> remoteReader = getFactory().reader(Quote.class, "remote")) {
                    // create empty journal
                    getFactory().writer(Quote.class, "local").close();
                    // setup local where data should be trickling from client
                    try (final Journal<Quote> local = getFactory().reader(Quote.class, "local")) {
                        Assert.assertEquals(0, local.size());
                        JournalServer server = new JournalServer(serverConfig, getFactory());
                        JournalClient client = new JournalClient(clientConfig, getFactory());
                        server.publish(remote);
                        server.start();
                        final AtomicInteger errors = new AtomicInteger();
                        final CountDownLatch ready = new CountDownLatch(1);
                        client.subscribe(Quote.class, "remote", "local", new JournalListener() {

                            @Override
                            public void onCommit() {
                                try {
                                    if (local.refresh() && local.size() == 33) {
                                        ready.countDown();
                                    }
                                } catch (JournalException e) {
                                    errors.incrementAndGet();
                                    e.printStackTrace();
                                }
                            }

                            @Override
                            public void onEvent(int event) {
                                if (event != JournalEvents.EVT_JNL_SUBSCRIBED) {
                                    errors.incrementAndGet();
                                }
                            }
                        });
                        client.start();
                        int n = 0;
                        while (n < 400) {
                            lagIteration(randomOrigin, remote, n, n + 10);
                            n += 10;
                        }
                        Assert.assertTrue(ready.await(10, TimeUnit.SECONDS));
                        server.halt();
                        client.halt();
                        local.refresh();
                        remoteReader.refresh();
                        TestUtils.assertEquals(remoteReader, local);
                        Assert.assertEquals(0, errors.get());
                    }
                }
            }
        }
    }
}
Also used : JournalException(com.questdb.std.ex.JournalException) Rnd(com.questdb.std.Rnd) CountDownLatch(java.util.concurrent.CountDownLatch) Quote(com.questdb.model.Quote) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AbstractTest(com.questdb.test.tools.AbstractTest) Test(org.junit.Test)

Example 2 with Rnd

use of com.questdb.std.Rnd in project questdb by bluestreak01.

the class AppendRawTimeSeries method main.

public static void main(String[] args) throws JournalException, ParserException {
    if (args.length < 1) {
        System.out.println("Usage: AppendRawTimeSeries <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").$())) {
            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();
        }
    }
}
Also used : JournalWriter(com.questdb.store.JournalWriter) JournalStructure(com.questdb.store.factory.configuration.JournalStructure) Factory(com.questdb.store.factory.Factory) Rnd(com.questdb.std.Rnd) JournalEntryWriter(com.questdb.store.JournalEntryWriter)

Example 3 with Rnd

use of com.questdb.std.Rnd in project questdb by bluestreak01.

the class NumbersTest method setUp.

@Before
public void setUp() {
    rnd = new Rnd();
    sink.clear();
}
Also used : Rnd(com.questdb.std.Rnd) Before(org.junit.Before)

Example 4 with Rnd

use of com.questdb.std.Rnd in project questdb by bluestreak01.

the class NumbersTest method testRoundUp.

@Test
public void testRoundUp() throws Exception {
    Assert.assertEquals(-0.2345678098023, Numbers.roundUp(-0.234567809802242442424242423122388, 13), 1E-14);
    Assert.assertEquals(0.2345678098023, Numbers.roundUp(0.234567809802242442424242423122388, 13), 1E-14);
    Rnd rnd = new Rnd();
    for (int i = 0; i < 1000; i++) {
        double d = rnd.nextDouble();
        double n = Numbers.roundUp(d, 8);
        Assert.assertTrue(d + " " + n + " " + (n - d - 1E-8), n - d - 1E-8 < Numbers.TOLERANCE);
    }
}
Also used : Rnd(com.questdb.std.Rnd) Test(org.junit.Test)

Example 5 with Rnd

use of com.questdb.std.Rnd in project questdb by bluestreak01.

the class GenericBinaryTest method getBytes.

private List<byte[]> getBytes() {
    Rnd r = new Rnd();
    List<byte[]> bytes = new ArrayList<>();
    for (int i = 0; i < 3; i++) {
        bytes.add(r.nextBytes((3 - i) * 1024));
    }
    return bytes;
}
Also used : ArrayList(java.util.ArrayList) Rnd(com.questdb.std.Rnd)

Aggregations

Rnd (com.questdb.std.Rnd)82 Test (org.junit.Test)50 JournalEntryWriter (com.questdb.store.JournalEntryWriter)43 JournalStructure (com.questdb.store.factory.configuration.JournalStructure)43 JournalWriter (com.questdb.store.JournalWriter)42 AbstractTest (com.questdb.test.tools.AbstractTest)30 BeforeClass (org.junit.BeforeClass)8 Path (com.questdb.std.str.Path)7 JournalException (com.questdb.std.ex.JournalException)6 StringSink (com.questdb.std.str.StringSink)5 ObjList (com.questdb.std.ObjList)4 Factory (com.questdb.store.factory.Factory)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 NumericException (com.questdb.common.NumericException)3 Record (com.questdb.common.Record)3 RecordCursor (com.questdb.common.RecordCursor)3 Quote (com.questdb.model.Quote)3 ObjHashSet (com.questdb.std.ObjHashSet)3 File (java.io.File)3 BootstrapEnv (com.questdb.BootstrapEnv)2