Skip to content

Commit

Permalink
compress values only
Browse files Browse the repository at this point in the history
  • Loading branch information
panagiotisl committed Mar 12, 2022
1 parent f1a28ee commit 405543d
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 20 deletions.
13 changes: 7 additions & 6 deletions src/main/java/fi/iki/yak/ts/compression/gorilla/Compressor.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public Compressor(long timestamp, BitOutput output) {
private void addHeader(long timestamp) {
// One byte: length of the first delta
// One byte: precision of timestamps
out.writeBits(timestamp, 64);
// out.writeBits(timestamp, 64);
}

/**
Expand All @@ -43,7 +43,7 @@ public void addValue(long timestamp, long value) {
if(storedTimestamp == 0) {
writeFirst(timestamp, value);
} else {
compressTimestamp(timestamp);
// compressTimestamp(timestamp);
compressValue(value);
}
}
Expand All @@ -58,7 +58,7 @@ public void addValue(long timestamp, double value) {
if(storedTimestamp == 0) {
writeFirst(timestamp, Double.doubleToRawLongBits(value));
} else {
compressTimestamp(timestamp);
// compressTimestamp(timestamp);
compressValue(Double.doubleToRawLongBits(value));
}
}
Expand All @@ -68,7 +68,7 @@ private void writeFirst(long timestamp, long value) {
storedTimestamp = timestamp;
storedVal = value;

out.writeBits(storedDelta, FIRST_DELTA_BITS);
// out.writeBits(storedDelta, FIRST_DELTA_BITS);
out.writeBits(storedVal, 64);
}

Expand All @@ -77,8 +77,9 @@ private void writeFirst(long timestamp, long value) {
*/
public void close() {
// These are selected to test interoperability and correctness of the solution, this can be read with go-tsz
out.writeBits(0x0F, 4);
out.writeBits(0xFFFFFFFF, 32);
addValue(0, Double.NaN);
// out.writeBits(0x0F, 4);
// out.writeBits(0xFFFFFFFF, 32);
out.skipBit();
out.flush();
}
Expand Down
38 changes: 29 additions & 9 deletions src/main/java/fi/iki/yak/ts/compression/gorilla/Decompressor.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,18 @@ public class Decompressor {

private BitInput in;

private final static long NAN_LONG = 0x7ff8000000000000L;

private final static int NAN_INT = 0x7fc00000;

private final static double NAN_DOUBLE = Double.longBitsToDouble(0x7ff8000000000000L);

private final static float NAN_FLOAT = Float.intBitsToFloat(0x7fc00000);


public Decompressor(BitInput input) {
in = input;
readHeader();
// readHeader();
}

private void readHeader() {
Expand All @@ -44,15 +53,20 @@ public Pair readPair() {
private void next() {
if (storedTimestamp == 0) {
// First item to read
storedDelta = in.getLong(Compressor.FIRST_DELTA_BITS);
if(storedDelta == (1<<27) - 1) {
endOfStream = true;
return;
}
// storedDelta = in.getLong(Compressor.FIRST_DELTA_BITS);
// if(storedDelta == (1<<27) - 1) {
// endOfStream = true;
// return;
// }
storedVal = in.getLong(64);
storedTimestamp = blockTimestamp + storedDelta;
if (storedVal == NAN_LONG) {
endOfStream =true;
}
// storedTimestamp = blockTimestamp + storedDelta;
storedTimestamp = 1;
} else {
nextTimestamp();
// nextTimestamp();
nextValue();
}
}

Expand Down Expand Up @@ -125,7 +139,13 @@ private void nextValue() {
long value = in.getLong(64 - storedLeadingZeros - storedTrailingZeros);
value <<= storedTrailingZeros;
value = storedVal ^ value;
storedVal = value;
if (value == NAN_LONG) {
endOfStream = true;
return;
} else {
storedVal = value;
}

}
}

Expand Down
11 changes: 6 additions & 5 deletions src/test/java/fi/iki/yak/ts/compression/gorilla/EncodeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ private void comparePairsToCompression(long blockTimestamp, Pair[] pairs) {
// Replace with stream once decompressor supports it
for(int i = 0; i < pairs.length; i++) {
Pair pair = d.readPair();
assertEquals(pairs[i].getTimestamp(), pair.getTimestamp(), "Timestamp did not match");
// assertEquals(pairs[i].getTimestamp(), pair.getTimestamp(), "Timestamp did not match");
assertEquals(pairs[i].getDoubleValue(), pair.getDoubleValue(), "Value did not match");
}

Expand All @@ -56,7 +56,7 @@ void simpleEncodeAndDecodeTest() throws Exception {
new Pair(now + 400, Double.doubleToRawLongBits(2147483650.0)),
new Pair(now + 2300, Double.doubleToRawLongBits(-16384)),
new Pair(now + 16384, Double.doubleToRawLongBits(2.8)),
new Pair(now + 16500, Double.doubleToRawLongBits(-38.0))
new Pair(now + 16500, Double.doubleToRawLongBits(-38.0)),
};

comparePairsToCompression(now, pairs);
Expand Down Expand Up @@ -368,7 +368,8 @@ void testEncodeSimilarFloats() throws Exception {
// Replace with stream once decompressor supports it
for(int i = 0; i < 5; i++) {
Pair pair = d.readPair();
assertEquals(bb.getLong(), pair.getTimestamp(), "Timestamp did not match");
// assertEquals(bb.getLong(), pair.getTimestamp(), "Timestamp did not match");
bb.getLong(); // read timestamp
assertEquals(bb.getDouble(), pair.getDoubleValue(), "Value did not match");
}
assertNull(d.readPair());
Expand Down Expand Up @@ -416,7 +417,7 @@ void testEncodeLargeAmountOfData() throws Exception {
long tStamp = bb.getLong();
double val = bb.getDouble();
Pair pair = d.readPair();
assertEquals(tStamp, pair.getTimestamp(), "Expected timestamp did not match at point " + i);
// assertEquals(tStamp, pair.getTimestamp(), "Expected timestamp did not match at point " + i);
assertEquals(val, pair.getDoubleValue());
}
assertNull(d.readPair());
Expand Down Expand Up @@ -482,7 +483,7 @@ void testLongEncoding() throws Exception {
long tStamp = bb.getLong();
long val = bb.getLong();
Pair pair = d.readPair();
assertEquals(tStamp, pair.getTimestamp(), "Expected timestamp did not match at point " + i);
// assertEquals(tStamp, pair.getTimestamp(), "Expected timestamp did not match at point " + i);
assertEquals(val, pair.getLongValue());
}
assertNull(d.readPair());
Expand Down

0 comments on commit 405543d

Please sign in to comment.