Search in sources :

Example 1 with DataBlock

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

the class TSDBQuery method getAllTables.

@SuppressFBWarnings("RV_RETURN_VALUE_IGNORED")
public static ListMultimap<String, TableDef> getAllTables(final File tsdbFile) throws IOException {
    ListMultimap<String, TableDef> result = ArrayListMultimap.create();
    try (TSDBReader reader = new TSDBReader(tsdbFile, 8192)) {
        Either<TableDef, DataBlock> read;
        while ((read = reader.read()) != null) {
            if (read.isLeft()) {
                final TableDef tdef = read.getLeft();
                result.put(tdef.getName(), tdef);
            }
        }
    }
    return result;
}
Also used : TableDef(org.spf4j.tsdb2.avro.TableDef) DataBlock(org.spf4j.tsdb2.avro.DataBlock) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 2 with DataBlock

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

the class TSDBQuery method getAllTablesWithDataRanges.

@SuppressFBWarnings("RV_RETURN_VALUE_IGNORED")
public static ListMultimap<String, TableDefEx> getAllTablesWithDataRanges(final File tsdbFile) throws IOException {
    ListMultimap<String, TableDefEx> result = ArrayListMultimap.create();
    TLongObjectMap<TableDefEx> id2Def = new TLongObjectHashMap<>();
    try (TSDBReader reader = new TSDBReader(tsdbFile, 8192)) {
        Either<TableDef, DataBlock> read;
        while ((read = reader.read()) != null) {
            if (read.isLeft()) {
                final TableDef left = read.getLeft();
                final TableDefEx tableDefEx = new TableDefEx(left, Long.MAX_VALUE, 0L);
                id2Def.put(left.id, tableDefEx);
                result.put(tableDefEx.getTableDef().getName(), tableDefEx);
            } else {
                DataBlock right = read.getRight();
                long baseTs = right.baseTimestamp;
                for (DataRow row : right.getValues()) {
                    TableDefEx tdex = id2Def.get(row.tableDefId);
                    if (tdex == null) {
                        throw new IOException("Potentially corupted file data row with no tableDef " + row);
                    }
                    long ts = baseTs + row.relTimeStamp;
                    if (ts < tdex.getStartTime()) {
                        tdex.setStartTime(ts);
                    }
                    if (ts > tdex.getEndTime()) {
                        tdex.setEndTime(ts);
                    }
                }
            }
        }
    }
    return result;
}
Also used : TLongObjectHashMap(gnu.trove.map.hash.TLongObjectHashMap) IOException(java.io.IOException) TableDef(org.spf4j.tsdb2.avro.TableDef) DataRow(org.spf4j.tsdb2.avro.DataRow) DataBlock(org.spf4j.tsdb2.avro.DataBlock) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 3 with DataBlock

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

the class TSDBQuery method getTimeSeries.

public static TimeSeries getTimeSeries(final File tsdbFile, final long[] tableIds, final long startTimeMillis, final long endTimeMillis) throws IOException {
    TLongList timestamps = new TLongArrayList();
    List<long[]> metrics = new ArrayList<>();
    try (TSDBReader reader = new TSDBReader(tsdbFile, 8192)) {
        Either<TableDef, DataBlock> read;
        while ((read = reader.read()) != null) {
            if (read.isRight()) {
                DataBlock data = read.getRight();
                long baseTs = data.baseTimestamp;
                for (DataRow row : data.getValues()) {
                    for (long tableId : tableIds) {
                        if (tableId == row.tableDefId) {
                            final long ts = baseTs + row.relTimeStamp;
                            if (ts >= startTimeMillis && ts <= endTimeMillis) {
                                timestamps.add(ts);
                                metrics.add(Longs.toArray(row.data));
                            }
                        }
                    }
                }
            }
        }
    }
    return new TimeSeries(timestamps.toArray(), metrics.toArray(new long[metrics.size()][]));
}
Also used : TLongArrayList(gnu.trove.list.array.TLongArrayList) TLongList(gnu.trove.list.TLongList) ArrayList(java.util.ArrayList) TLongArrayList(gnu.trove.list.array.TLongArrayList) TableDef(org.spf4j.tsdb2.avro.TableDef) DataRow(org.spf4j.tsdb2.avro.DataRow) DataBlock(org.spf4j.tsdb2.avro.DataBlock)

Example 4 with DataBlock

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

the class TSDBQuery method getTables.

@SuppressFBWarnings("RV_RETURN_VALUE_IGNORED")
public static ListMultimap<String, TableDef> getTables(final File tsdbFile, final Set<String> tables) throws IOException {
    ListMultimap<String, TableDef> result = ArrayListMultimap.create();
    try (TSDBReader reader = new TSDBReader(tsdbFile, 8192)) {
        Either<TableDef, DataBlock> read;
        while ((read = reader.read()) != null) {
            if (read.isLeft()) {
                final TableDef tdef = read.getLeft();
                final String name = tdef.getName();
                if (tables.contains(name)) {
                    result.put(name, tdef);
                }
            }
        }
    }
    return result;
}
Also used : TableDef(org.spf4j.tsdb2.avro.TableDef) DataBlock(org.spf4j.tsdb2.avro.DataBlock) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 5 with DataBlock

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

the class TSDBReader method read.

@Nullable
public synchronized Either<TableDef, DataBlock> read() throws IOException {
    final long position = bis.getCount();
    if (position >= size) {
        return null;
    }
    Object result;
    try {
        result = recordReader.read(null, decoder);
    } catch (IOException | RuntimeException ex) {
        if (CORUPTION_LENIENT) {
            return null;
        } else {
            throw new IOException("Error reading tsdb file at " + position + ", this= " + this, ex);
        }
    }
    if (result instanceof TableDef) {
        final TableDef td = (TableDef) result;
        if (position != td.id) {
            throw new IOException("Table Id should be equal with file position " + position + ", " + td.id);
        }
        return Either.left(td);
    } else {
        return Either.right((DataBlock) result);
    }
}
Also used : IOException(java.io.IOException) TableDef(org.spf4j.tsdb2.avro.TableDef) Nullable(javax.annotation.Nullable)

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