Search in sources :

Example 31 with Rnd

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

the class BinaryTest method testBinaryAppend.

@Test
public void testBinaryAppend() throws Exception {
    try (JournalWriter<Band> writer = getFactory().writer(Band.class)) {
        Rnd r = new Rnd(System.currentTimeMillis(), System.currentTimeMillis());
        List<byte[]> bytes = new ArrayList<>();
        for (int i = 0; i < 3; i++) {
            bytes.add(r.nextBytes((3 - i) * 1024));
        }
        writer.append(new Band().setName("Supertramp").setType("jazz").setImage(bytes.get(0)));
        writer.append(new Band().setName("TinieTempah").setType("rap").setImage(bytes.get(1)));
        writer.append(new Band().setName("Rihanna").setType("pop").setImage(bytes.get(2)));
        writer.commit();
        int count = 0;
        for (Band b : writer) {
            Assert.assertArrayEquals(bytes.get(count), b.getImage().array());
            count++;
        }
    }
}
Also used : ArrayList(java.util.ArrayList) Rnd(com.questdb.std.Rnd) Band(com.questdb.model.Band) AbstractTest(com.questdb.test.tools.AbstractTest) Test(org.junit.Test)

Example 32 with Rnd

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

the class ColumnTest method testVarByteBuffer.

@Test
public void testVarByteBuffer() throws Exception {
    // bit hint 12 = 4k buffer, length of stored buffer must be larger than 4k for proper test.
    MemoryFile df1 = new MemoryFile(dataFile, 12, JournalMode.APPEND, false);
    MemoryFile idxFile1 = new MemoryFile(indexFile, 12, JournalMode.APPEND, false);
    final Rnd random = new Rnd(System.currentTimeMillis(), System.currentTimeMillis());
    final int len = 5024;
    try (VariableColumn col = new VariableColumn(df1, idxFile1)) {
        ByteBuffer buf = ByteBuffer.allocate(len);
        String s = random.nextString(buf.remaining() / 2);
        ByteBuffers.putStr(buf, s);
        buf.flip();
        col.putBin(buf);
        col.commit();
        ByteBuffer bb = ByteBuffer.allocate(col.getBinLen(0));
        col.getBin(0, bb);
        bb.flip();
        char[] chars = new char[bb.remaining() / 2];
        for (int i = 0; i < chars.length; i++) {
            chars[i] = bb.getChar();
        }
        String actual = new String(chars);
        Assert.assertEquals(s, actual);
    }
}
Also used : Rnd(com.questdb.std.Rnd) ByteBuffer(java.nio.ByteBuffer)

Example 33 with Rnd

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

the class JournalTest method testTxLagTumbleDrier.

@Test
public void testTxLagTumbleDrier() throws Exception {
    int SIZE = 1000000;
    try (JournalWriter<Quote> origin = getFactory().writer(Quote.class, "origin", SIZE / 12)) {
        try (JournalWriter<Quote> w = getFactory().writer(Quote.class, "q", SIZE / 12)) {
            TestUtils.generateQuoteData(origin, SIZE, DateFormatUtils.parseDateTime("2014-01-30T00:11:00.000Z"), 100000);
            origin.commit();
            ResultSet<Quote> originRs = origin.query().all().asResultSet();
            int blockSize = 5130;
            Rnd rnd = new Rnd(System.currentTimeMillis(), System.nanoTime());
            try {
                for (int i = 0; i < originRs.size(); ) {
                    int d = Math.min(i + blockSize, originRs.size());
                    w.mergeAppend(originRs.subset(i, d));
                    if (rnd.nextBoolean()) {
                        w.commit();
                        i += blockSize;
                    } else {
                        w.rollback();
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            Assert.assertFalse(w.isTxActive());
            TestUtils.assertDataEquals(origin, w);
        }
    }
}
Also used : Quote(com.questdb.model.Quote) Rnd(com.questdb.std.Rnd) JournalException(com.questdb.std.ex.JournalException) NumericException(com.questdb.common.NumericException) AbstractTest(com.questdb.test.tools.AbstractTest) Test(org.junit.Test)

Example 34 with Rnd

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

the class IntervalFrameCursorTest method testIntervals.

private void testIntervals(int partitionBy, long increment, LongList intervals, int rowCount, CharSequence expected, long expectedCount) throws Exception {
    TestUtils.assertMemoryLeak(() -> {
        try (TableModel model = new TableModel(configuration, "x", partitionBy).col("a", ColumnType.SYMBOL).indexed(true, 4).col("b", ColumnType.SYMBOL).indexed(true, 4).timestamp()) {
            CairoTestUtils.create(model);
        }
        final Rnd rnd = new Rnd();
        long timestamp = DateFormatUtils.parseDateTime("1980-01-01T00:00:00.000Z");
        try (TableWriter writer = new TableWriter(configuration, "x")) {
            for (int i = 0; i < rowCount; i++) {
                TableWriter.Row row = writer.newRow(timestamp);
                row.putSym(0, rnd.nextChars(4));
                row.putSym(1, rnd.nextChars(4));
                row.append();
                timestamp += increment;
            }
            writer.commit();
            timestamp = Dates.addYear(timestamp, 3);
            for (int i = 0; i < rowCount; i++) {
                TableWriter.Row row = writer.newRow(timestamp);
                row.putSym(0, rnd.nextChars(4));
                row.putSym(1, rnd.nextChars(4));
                row.append();
                timestamp += increment;
            }
            writer.commit();
        }
        try (TableReader reader = new TableReader(configuration, "x")) {
            final TableReaderRecord record = new TableReaderRecord();
            IntervalFrameCursor cursor = new IntervalFrameCursor(intervals, reader.getMetadata().getTimestampIndex());
            cursor.of(reader);
            record.of(reader);
            assertEquals(expected, record, cursor);
            if (expected.length() > 0) {
                cursor.toTop();
                assertIndexRowsMatchSymbol(cursor, record, 0, expectedCount);
                cursor.toTop();
                assertIndexRowsMatchSymbol(cursor, record, 1, expectedCount);
            }
            cursor.toTop();
            assertEquals(expected, record, cursor);
        }
    });
}
Also used : Rnd(com.questdb.std.Rnd)

Example 35 with Rnd

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

the class SymbolMapTest method testLookupPerformance.

// @Test
// public void testLookupPerformanceOld() throws JournalException {
// int N = 100000000;
// int symbolCount = 1024;
// ObjList<String> symbols = new ObjList<>();
// MMappedSymbolTable tab = new MMappedSymbolTable(symbolCount, 256, 1, new File(configuration.getRoot().toString()), "x", JournalMode.APPEND, 0, 0, false, true);
// Rnd rnd = new Rnd();
// long prev = -1L;
// for (int i = 0; i < symbolCount; i++) {
// CharSequence cs = rnd.nextChars(10);
// long key = tab.put(cs);
// symbols.add(cs.toString());
// Assert.assertEquals(prev + 1, key);
// prev = key;
// }
// 
// long t = System.nanoTime();
// for (int i = 0; i < N; i++) {
// int key = rnd.nextPositiveInt() % symbolCount;
// Assert.assertEquals(key, tab.put(symbols.getQuick(key)));
// }
// System.out.println(System.nanoTime() - t);
// }
@Test
public void testLookupPerformance() throws Exception {
    TestUtils.assertMemoryLeak(() -> {
        int N = 10000000;
        int symbolCount = 1024;
        ObjList<String> symbols = new ObjList<>();
        try (Path path = new Path().of(configuration.getRoot())) {
            create(path, "x", symbolCount, true);
            try (SymbolMapWriter writer = new SymbolMapWriter(configuration, path, "x", 0)) {
                Rnd rnd = new Rnd();
                long prev = -1L;
                for (int i = 0; i < symbolCount; i++) {
                    CharSequence cs = rnd.nextChars(10);
                    long key = writer.put(cs);
                    symbols.add(cs.toString());
                    Assert.assertEquals(prev + 1, key);
                    prev = key;
                }
                long t = System.nanoTime();
                for (int i = 0; i < N; i++) {
                    int key = rnd.nextPositiveInt() % symbolCount;
                    Assert.assertEquals(key, writer.put(symbols.getQuick(key)));
                }
                System.out.println("SymbolMapWriter lookup performance [10M <500ms]: " + (System.nanoTime() - t) / 1000000);
            }
        }
    });
}
Also used : Path(com.questdb.std.str.Path) ObjList(com.questdb.std.ObjList) Rnd(com.questdb.std.Rnd) Test(org.junit.Test)

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