Skip to content

Commit

Permalink
Fix issue #11
Browse files Browse the repository at this point in the history
  • Loading branch information
burmanm committed May 22, 2017
1 parent 77e8372 commit 6d353d5
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@ protected int capacityLeft() {
}

protected void flipWord() {
flipWordWithoutExpandCheck();
if(capacityLeft() <= 2) { // We want to have always at least 2 longs available
expandAllocation();
}
flipWordWithoutExpandCheck();
}

protected void flipWordWithoutExpandCheck() {
Expand Down Expand Up @@ -122,7 +122,7 @@ public void writeBits(long value, int bits) {
int firstBitPosition = bits - bitsLeft;
lB |= value >>> firstBitPosition;
bits -= bitsLeft;
flipWordWithoutExpandCheck();
flipWord();
lB |= value << (64 - bits);
bitsLeft -= bits;
}
Expand Down
33 changes: 33 additions & 0 deletions src/test/java/fi/iki/yak/ts/compression/gorilla/EncodeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@
import java.time.ZoneOffset;
import java.time.temporal.ChronoUnit;
import java.util.Arrays;
import java.util.Map;
import java.util.Random;
import java.util.TreeMap;
import java.util.concurrent.ThreadLocalRandom;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
* These are generic tests to test that input matches the output after compression + decompression cycle, using
Expand Down Expand Up @@ -224,4 +228,33 @@ void testLongEncoding() throws Exception {
}
assertNull(d.readPair());
}

@Test
void testIssue11() {
long now = System.currentTimeMillis();
LongArrayOutput output = new LongArrayOutput(500);
GorillaCompressor c = new GorillaCompressor(LocalDateTime.now(ZoneOffset.UTC).truncatedTo(ChronoUnit.HOURS).
toInstant(ZoneOffset.UTC).toEpochMilli(), output);

Map<Long, Double> dps = new TreeMap<>();
int n = 3600;
Random r = new Random();
for(int i=0; i<n; i++) {
dps.put(now + (1000 * i) + r.nextInt(100), 100.0);
}

for(Map.Entry<Long, Double> entry : dps.entrySet()) {
c.addValue(entry.getKey(), entry.getValue());
}
c.close();

LongArrayInput input = new LongArrayInput(output.getLongArray());
GorillaDecompressor dc = new GorillaDecompressor(input);

Pair pair = dc.readPair();
while(pair != null) {
assertTrue(dps.containsKey(pair.getTimestamp()), "Datapoint was missing");
pair = dc.readPair();
}
}
}

0 comments on commit 6d353d5

Please sign in to comment.