Search in sources :

Example 6 with DataBlock

use of org.spf4j.tsdb2.avro.DataBlock in project spf4j by zolyfarkas.

the class AvroTest method testRw.

@Test
public void testRw() throws IOException {
    DataBlock data = DataBlock.newBuilder().setBaseTimestamp(0).setValues(Collections.EMPTY_LIST).build();
    try (ByteArrayBuilder bab = new ByteArrayBuilder()) {
        Schema schema = data.getSchema();
        SpecificDatumWriter<DataBlock> writer = new SpecificDatumWriter<>(schema);
        final BinaryEncoder directBinaryEncoder = EncoderFactory.get().directBinaryEncoder(bab, null);
        writer.write(data, directBinaryEncoder);
        directBinaryEncoder.flush();
        ByteArrayInputStream bis = new ByteArrayInputStream(bab.getBuffer(), 0, bab.size());
        SpecificDatumReader<DataBlock> reader = new SpecificDatumReader<>(schema);
        BinaryDecoder directBinaryDecoder = DecoderFactory.get().directBinaryDecoder(bis, null);
        DataBlock read = reader.read(null, directBinaryDecoder);
        Assert.assertEquals(read, data);
    }
}
Also used : BinaryEncoder(org.apache.avro.io.BinaryEncoder) ByteArrayInputStream(java.io.ByteArrayInputStream) Schema(org.apache.avro.Schema) ByteArrayBuilder(org.spf4j.io.ByteArrayBuilder) SpecificDatumReader(org.apache.avro.specific.SpecificDatumReader) DataBlock(org.spf4j.tsdb2.avro.DataBlock) BinaryDecoder(org.apache.avro.io.BinaryDecoder) SpecificDatumWriter(org.apache.avro.specific.SpecificDatumWriter) Test(org.junit.Test)

Example 7 with DataBlock

use of org.spf4j.tsdb2.avro.DataBlock in project spf4j by zolyfarkas.

the class TSDBReaderTest method testTsdb.

@Test
@SuppressFBWarnings({ "RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE", "RCN_REDUNDANT_NULLCHECK_OF_NULL_VALUE", "NP_LOAD_OF_KNOWN_NULL_VALUE", "CLI_CONSTANT_LIST_INDEX" })
// try with resources trips up findbugs sometimes.
@SuppressWarnings("checkstyle:EmptyBlock")
public void testTsdb() throws IOException {
    File testFile = File.createTempFile("test", ".tsdb2");
    long tableId;
    try (TSDBWriter writer = new TSDBWriter(testFile, 4, "test", false)) {
    }
    try (TSDBWriter writer = new TSDBWriter(testFile, 4, "test", false)) {
        tableId = writer.writeTableDef(tableDef);
        final long time = System.currentTimeMillis();
        writer.writeDataRow(tableId, time, 0, 1, 2);
        writer.writeDataRow(tableId, time + 10, 1, 1, 2);
        writer.writeDataRow(tableId, time + 20, 2, 1, 2);
        writer.writeDataRow(tableId, time + 30, 3, 1, 2);
        writer.writeDataRow(tableId, time + 40, 4, 1, 2);
    }
    try (TSDBReader reader = new TSDBReader(testFile, 1024)) {
        Either<TableDef, DataBlock> read;
        while ((read = reader.read()) != null) {
            LOG.debug("TSDB block: {}", read);
        }
    }
    ListMultimap<String, TableDef> allTables = TSDBQuery.getAllTables(testFile);
    Assert.assertEquals(1, allTables.size());
    Assert.assertTrue(allTables.containsKey(tableDef.name));
    TimeSeries timeSeries = TSDBQuery.getTimeSeries(testFile, new long[] { tableId }, 0, Long.MAX_VALUE);
    Assert.assertEquals(2L, timeSeries.getValues()[2][0]);
}
Also used : File(java.io.File) TableDef(org.spf4j.tsdb2.avro.TableDef) DataBlock(org.spf4j.tsdb2.avro.DataBlock) Test(org.junit.Test) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 8 with DataBlock

use of org.spf4j.tsdb2.avro.DataBlock in project spf4j by zolyfarkas.

the class TSDBReaderTest method testTailing.

@Test
public void testTailing() throws IOException, InterruptedException, ExecutionException, TimeoutException {
    File testFile = File.createTempFile("test", ".tsdb2");
    try (TSDBWriter writer = new TSDBWriter(testFile, 4, "test", true);
        TSDBReader reader = new TSDBReader(testFile, 1024)) {
        writer.flush();
        final BlockingQueue<Either<TableDef, DataBlock>> queue = new ArrayBlockingQueue<>(100);
        Future<Void> bgWatch = reader.bgWatch((Either<TableDef, DataBlock> object, long deadline) -> {
            queue.put(object);
        }, TSDBReader.EventSensitivity.HIGH);
        long tableId = writer.writeTableDef(tableDef);
        writer.flush();
        final long time = System.currentTimeMillis();
        writer.writeDataRow(tableId, time, 0, 1, 2);
        writer.writeDataRow(tableId, time + 10, 1, 1, 2);
        writer.flush();
        Either<TableDef, DataBlock> td = queue.take();
        Assert.assertEquals(tableDef, td.getLeft());
        Either<TableDef, DataBlock> take = queue.take();
        Assert.assertEquals(2, take.getRight().getValues().size());
        writer.writeDataRow(tableId, time + 20, 2, 1, 2);
        writer.writeDataRow(tableId, time + 30, 3, 1, 2);
        writer.writeDataRow(tableId, time + 40, 4, 1, 2);
        writer.flush();
        Assert.assertEquals(3, queue.take().getRight().getValues().size());
        reader.stopWatching();
        bgWatch.get(10000, TimeUnit.MILLISECONDS);
    }
}
Also used : ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) Either(org.spf4j.base.Either) File(java.io.File) TableDef(org.spf4j.tsdb2.avro.TableDef) DataBlock(org.spf4j.tsdb2.avro.DataBlock) Test(org.junit.Test)

Aggregations

DataBlock (org.spf4j.tsdb2.avro.DataBlock)7 TableDef (org.spf4j.tsdb2.avro.TableDef)7 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)4 Test (org.junit.Test)3 File (java.io.File)2 IOException (java.io.IOException)2 DataRow (org.spf4j.tsdb2.avro.DataRow)2 TLongList (gnu.trove.list.TLongList)1 TLongArrayList (gnu.trove.list.array.TLongArrayList)1 TLongObjectHashMap (gnu.trove.map.hash.TLongObjectHashMap)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ArrayList (java.util.ArrayList)1 ArrayBlockingQueue (java.util.concurrent.ArrayBlockingQueue)1 Nullable (javax.annotation.Nullable)1 Schema (org.apache.avro.Schema)1 BinaryDecoder (org.apache.avro.io.BinaryDecoder)1 BinaryEncoder (org.apache.avro.io.BinaryEncoder)1 SpecificDatumReader (org.apache.avro.specific.SpecificDatumReader)1 SpecificDatumWriter (org.apache.avro.specific.SpecificDatumWriter)1 Either (org.spf4j.base.Either)1