forked from burmanm/gorilla-tsc
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Separate ValueCompression from GorillaCompressor and allow the usage of
different value predictors. Include DFCM as another predictor possibility. (cherry picked from commit 6d0d61a)
- Loading branch information
Showing
8 changed files
with
326 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
src/main/java/fi/iki/yak/ts/compression/gorilla/Predictor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
* Copyright 2017 Red Hat, Inc. and/or its affiliates | ||
* and other contributors as indicated by the @author tags. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package fi.iki.yak.ts.compression.gorilla; | ||
|
||
/** | ||
* @author miburman | ||
*/ | ||
public interface Predictor { | ||
|
||
/** | ||
* Give the real value | ||
* | ||
* @param value Long / bits of Double | ||
*/ | ||
void update(long value); | ||
|
||
/** | ||
* Predicts the next value | ||
* | ||
* @return Predicted value | ||
*/ | ||
long predict(); | ||
} |
92 changes: 92 additions & 0 deletions
92
src/main/java/fi/iki/yak/ts/compression/gorilla/ValueCompressor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package fi.iki.yak.ts.compression.gorilla; | ||
|
||
import fi.iki.yak.ts.compression.gorilla.predictors.LastValuePredictor; | ||
|
||
/** | ||
* ValueCompressor for the Gorilla encoding format. Supply with long presentation of the value, | ||
* in case of doubles use Double.doubleToRawLongBits(value) | ||
* | ||
* @author Michael Burman | ||
*/ | ||
public class ValueCompressor { | ||
private int storedLeadingZeros = Integer.MAX_VALUE; | ||
private int storedTrailingZeros = 0; | ||
|
||
private Predictor predictor; | ||
private BitOutput out; | ||
|
||
public ValueCompressor(BitOutput out) { | ||
this(out, new LastValuePredictor()); | ||
} | ||
|
||
public ValueCompressor(BitOutput out, Predictor predictor) { | ||
this.out = out; | ||
this.predictor = predictor; | ||
} | ||
|
||
void writeFirst(long value) { | ||
predictor.update(value); | ||
out.writeBits(value, 64); | ||
} | ||
|
||
protected void compressValue(long value) { | ||
// In original Gorilla, Last-Value predictor is used | ||
long diff = predictor.predict() ^ value; | ||
predictor.update(value); | ||
|
||
if(diff == 0) { | ||
// Write 0 | ||
out.skipBit(); | ||
} else { | ||
int leadingZeros = Long.numberOfLeadingZeros(diff); | ||
int trailingZeros = Long.numberOfTrailingZeros(diff); | ||
|
||
out.writeBit(); // Optimize to writeNewLeading / writeExistingLeading? | ||
|
||
if(leadingZeros >= storedLeadingZeros && trailingZeros >= storedTrailingZeros) { | ||
writeExistingLeading(diff); | ||
} else { | ||
writeNewLeading(diff, leadingZeros, trailingZeros); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* If there at least as many leading zeros and as many trailing zeros as previous value, control bit = 0 (type a) | ||
* store the meaningful XORed value | ||
* | ||
* @param xor XOR between previous value and current | ||
*/ | ||
private void writeExistingLeading(long xor) { | ||
out.skipBit(); | ||
|
||
int significantBits = 64 - storedLeadingZeros - storedTrailingZeros; | ||
xor >>>= storedTrailingZeros; | ||
out.writeBits(xor, significantBits); | ||
} | ||
|
||
/** | ||
* store the length of the number of leading zeros in the next 5 bits | ||
* store length of the meaningful XORed value in the next 6 bits, | ||
* store the meaningful bits of the XORed value | ||
* (type b) | ||
* | ||
* @param xor XOR between previous value and current | ||
* @param leadingZeros New leading zeros | ||
* @param trailingZeros New trailing zeros | ||
*/ | ||
private void writeNewLeading(long xor, int leadingZeros, int trailingZeros) { | ||
out.writeBit(); | ||
|
||
// Different from version 1.x, use (significantBits - 1) in storage - avoids a branch | ||
int significantBits = 64 - leadingZeros - trailingZeros; | ||
|
||
// Different from original, bits 5 -> 6, avoids a branch, allows storing small longs | ||
out.writeBits(leadingZeros, 6); // Number of leading zeros in the next 6 bits | ||
out.writeBits(significantBits - 1, 6); // Length of meaningful bits in the next 6 bits | ||
out.writeBits(xor >>> trailingZeros, significantBits); // Store the meaningful bits of XOR | ||
|
||
storedLeadingZeros = leadingZeros; | ||
storedTrailingZeros = trailingZeros; | ||
} | ||
} |
Oops, something went wrong.