/** * Add the points to the given builder * * @param decompressedBytes the decompressed input stream * @param timeSeriesStart start of the time series * @param timeSeriesEnd end of the time series * @param builder the builder */ public static void from(final InputStream decompressedBytes, long timeSeriesStart, long timeSeriesEnd, MetricTimeSeries.Builder builder) { from(decompressedBytes, timeSeriesStart, timeSeriesEnd, timeSeriesStart, timeSeriesEnd, builder); }
/** * Converts the given iterator of our point class to protocol buffers and compresses (gzip) it. * * @param metricDataPoints - the list with points * @return the serialized points as byte[] */ public static byte[] to(Iterator<Point> metricDataPoints) { return to(metricDataPoints, 0); }
/** * Handles the last point of a time series. We always store the first an the last point as supporting points actualPoints[Last] == serializedPoints[Last] * * @param ddcThreshold the ddc threshold * @param startDate the start date * @param point the current point * @param points the protocol buffer point * @param currentTimestamp the current time stamp */ private static void handleLastPoint(int ddcThreshold, long startDate, MetricProtocolBuffers.Point.Builder point, MetricProtocolBuffers.Points.Builder points, long currentTimestamp) { long calcPoint = calculateTimeStamp(startDate, points.getPList(), ddcThreshold); //Calc offset long deltaToLastTimestamp = currentTimestamp - calcPoint; //everything okay if (deltaToLastTimestamp >= 0) { setTimeStamp(point, deltaToLastTimestamp); points.addP(point); } else { //we have to rearrange the points as we are already behind the actual end timestamp rearrangePoints(startDate, currentTimestamp, deltaToLastTimestamp, ddcThreshold, points, point); } }
setValueOrRefIndexOnPoint(valueIndex, index, p.getValue(), point); handleLastPoint(ddcThreshold, startDate, point, points, currentTimestamp); break; boolean isAlmostEquals = almostEquals(previousDelta, delta, ddcThreshold); long drift = 0; drift = calculateDrift(currentTimestamp, lastStoredDate, timesSinceLastDelta, lastStoredDelta); if (isAlmostEquals && noDrift(drift, ddcThreshold, timesSinceLastDelta) && drift >= 0) { points.addP(point.build()); timesSinceLastDelta += 1; setBPTimeStamp(point, timeStamp); } else { setTimeStamp(point, timeStamp);
long t = getT(mod); newOffset = t - avgPerDelta; MetricProtocolBuffers.Point.Builder modPoint = mod.toBuilder(); setT(modPoint, newOffset); mod = modPoint.build(); long arrangedPoint = calculateTimeStamp(startDate, points.getPList(), ddcThreshold); setBPTimeStamp(point, storedOffsetToEnd);
/** * @param startDate the first time stamp * @param pList the list with serialized points * @param ddcThreshold the threshold of the ddc * @return the calculated timestamp using the ddc threshold */ private static long calculateTimeStamp(long startDate, List<MetricProtocolBuffers.Point> pList, long ddcThreshold) { long lastDelta = ddcThreshold; long calculatedPointDate = startDate; for (int i = 1; i < pList.size(); i++) { MetricProtocolBuffers.Point p = pList.get(i); lastDelta = getTimestamp(p, lastDelta); calculatedPointDate += lastDelta; } return calculatedPointDate; }
lastDelta = getTimestamp(p, lastDelta); calculatedPointDate += lastDelta;
@Override public byte[] dataAsBlob() { byte[] data = ProtoBufMetricTimeSeriesSerializer.to(timeSeries.points().iterator()); //compress data return Compression.compress(data); }
private void fromProtocolBuffers(BinaryTimeSeries binaryTimeSeries, long queryStart, long queryEnd, MetricTimeSeries.Builder builder) { final InputStream decompressed = Compression.decompressToStream(binaryTimeSeries.getPoints()); ProtoBufMetricTimeSeriesSerializer.from(decompressed, binaryTimeSeries.getStart(), binaryTimeSeries.getEnd(), queryStart, queryEnd, builder); IOUtils.closeQuietly(decompressed); }
@Override public BinaryTimeSeries to(MetricTimeSeries timeSeries) { LOGGER.debug("Converting {} to BinaryTimeSeries", timeSeries); BinaryTimeSeries.Builder builder = new BinaryTimeSeries.Builder(); //serialize byte[] serializedPoints = ProtoBufMetricTimeSeriesSerializer.to(timeSeries.points().iterator()); byte[] compressedPoints = Compression.compress(serializedPoints); //Add the minimum required fields builder.start(timeSeries.getStart()) .end(timeSeries.getEnd()) .data(compressedPoints); //Currently we only have a metric builder.field(MetricTSSchema.METRIC, timeSeries.getMetric()); //Add a list of user defined attributes timeSeries.attributes().forEach(builder::field); return builder.build(); } }
ProtoBufMetricTimeSeriesSerializer.from(decompressed, tsStart, tsEnd, queryStart, queryEnd, ts); IOUtils.closeQuietly(decompressed);