summaryrefslogtreecommitdiffstats
path: root/vespajlib
diff options
context:
space:
mode:
authorHenning Baldersheim <balder@yahoo-inc.com>2023-01-08 11:33:44 +0100
committerGitHub <noreply@github.com>2023-01-08 11:33:44 +0100
commit6cedb4636301e1348d64e58886fe135be8b2b527 (patch)
tree62b7e1e947697ddc92e410a0ed1b951e67302f3d /vespajlib
parent9fd55bd72ee01b27efa2a27b32bc416483d2fda0 (diff)
parentf1f3dba5e56f7b0d074a5557fc8967e22a715b6e (diff)
Merge pull request #21773 from vespa-engine/jonmv/multi-range-item-2
Jonmv/multi range item 2
Diffstat (limited to 'vespajlib')
-rw-r--r--vespajlib/src/main/java/com/yahoo/compress/IntegerCompressor.java9
-rw-r--r--vespajlib/src/test/java/com/yahoo/compress/IntegerCompressorTest.java52
2 files changed, 36 insertions, 25 deletions
diff --git a/vespajlib/src/main/java/com/yahoo/compress/IntegerCompressor.java b/vespajlib/src/main/java/com/yahoo/compress/IntegerCompressor.java
index 982ca309015..42178fa3ffd 100644
--- a/vespajlib/src/main/java/com/yahoo/compress/IntegerCompressor.java
+++ b/vespajlib/src/main/java/com/yahoo/compress/IntegerCompressor.java
@@ -10,10 +10,19 @@ import java.nio.ByteBuffer;
*/
public class IntegerCompressor {
+ public enum Mode { NONE, COMPRESSED, COMPRESSED_POSITIVE; }
+
+ public static Mode compressionMode(int min, int max) {
+ if (min >= 0 && max < 1 << 30) return Mode.COMPRESSED_POSITIVE;
+ if (min > -1 << 29 && max < 1 << 29) return Mode.COMPRESSED;
+ return Mode.NONE;
+ }
+
public static void putCompressedNumber(int n, ByteBuffer buf) {
int negative = n < 0 ? 0x80 : 0x0;
if (negative != 0) {
n = -n;
+ if (n == -n) --n; // underflow, caught as "too big" later.
}
if (n < (0x1 << 5)) {
byte b = (byte)(n | negative);
diff --git a/vespajlib/src/test/java/com/yahoo/compress/IntegerCompressorTest.java b/vespajlib/src/test/java/com/yahoo/compress/IntegerCompressorTest.java
index 5a5cb2745d2..c53a834659c 100644
--- a/vespajlib/src/test/java/com/yahoo/compress/IntegerCompressorTest.java
+++ b/vespajlib/src/test/java/com/yahoo/compress/IntegerCompressorTest.java
@@ -1,13 +1,14 @@
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.compress;
+import com.yahoo.compress.IntegerCompressor.Mode;
import org.junit.Test;
import java.nio.ByteBuffer;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.assertThrows;
/**
* Test that integers compresses correctly.
@@ -15,6 +16,7 @@ import static org.junit.Assert.assertTrue;
* @author baldersheim
*/
public class IntegerCompressorTest {
+
private void verifyPositiveNumber(int n, byte [] expected) {
ByteBuffer buf = ByteBuffer.allocate(expected.length);
IntegerCompressor.putCompressedPositiveNumber(n, buf);
@@ -43,18 +45,10 @@ public class IntegerCompressorTest {
byte [] x3fffffff = {(byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff};
verifyPositiveNumber(0x3fffffff, x3fffffff);
byte [] x40000000 = {0,0,0,0};
- try {
- verifyPositiveNumber(0x40000000, x40000000);
- assertTrue(false);
- } catch (IllegalArgumentException e) {
- assertEquals("Number '1073741824' too big, must extend encoding", e.getMessage());
- }
- try {
- verifyPositiveNumber(-1, x40000000);
- assertTrue(false);
- } catch (IllegalArgumentException e) {
- assertEquals("Number '-1' must be positive", e.getMessage());
- }
+ assertEquals("Number '1073741824' too big, must extend encoding",
+ assertThrows(IllegalArgumentException.class, () -> verifyPositiveNumber(0x40000000, x40000000)).getMessage());
+ assertEquals("Number '-1' must be positive",
+ assertThrows(IllegalArgumentException.class, () -> verifyPositiveNumber(-1, x40000000)).getMessage());
}
@Test
@@ -74,12 +68,8 @@ public class IntegerCompressorTest {
byte [] x1fffffff = {0x7f, (byte)0xff, (byte)0xff, (byte)0xff};
verifyNumber(0x1fffffff, x1fffffff);
byte [] x20000000 = {0,0,0,0};
- try {
- verifyNumber(0x20000000, x20000000);
- assertTrue(false);
- } catch (IllegalArgumentException e) {
- assertEquals("Number '536870912' too big, must extend encoding", e.getMessage());
- }
+ assertEquals("Number '536870912' too big, must extend encoding",
+ assertThrows(IllegalArgumentException.class, () -> verifyNumber(0x20000000, x20000000)).getMessage());
byte [] mzero = {(byte)0x81};
verifyNumber(-1, mzero);
byte [] mone = {(byte)0x82};
@@ -95,13 +85,25 @@ public class IntegerCompressorTest {
byte [] mx1fffffff = {(byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff};
verifyNumber(-0x1fffffff, mx1fffffff);
byte [] mx20000000 = {0,0,0,0};
- try {
- verifyNumber(-0x20000000, mx20000000);
- assertTrue(false);
- } catch (IllegalArgumentException e) {
- assertEquals("Number '-536870912' too big, must extend encoding", e.getMessage());
- }
+ assertEquals("Number '-536870912' too big, must extend encoding",
+ assertThrows(IllegalArgumentException.class, () -> verifyNumber(-0x20000000, mx20000000)).getMessage());
+ byte [] mx80000000 = {0,0,0,0};
+ assertEquals("Number '-2147483647' too big, must extend encoding",
+ assertThrows(IllegalArgumentException.class, () -> verifyNumber(-0x80000000, mx80000000)).getMessage());
+ }
+ @Test
+ public void testMode() {
+ assertEquals(Mode.NONE, IntegerCompressor.compressionMode(-0x80000000, 0x00000000));
+ assertEquals(Mode.NONE, IntegerCompressor.compressionMode(-0x20000000, 0x00000000));
+ assertEquals(Mode.NONE, IntegerCompressor.compressionMode(-0x00000001, 0x3fffffff));
+ assertEquals(Mode.COMPRESSED, IntegerCompressor.compressionMode(-0x1fffffff, 0x1fffffff));
+ assertEquals(Mode.COMPRESSED, IntegerCompressor.compressionMode(-0x1fffffff, -0x1fffffff));
+ assertEquals(Mode.COMPRESSED, IntegerCompressor.compressionMode(-0x1fffffff, 0x00000000));
+ assertEquals(Mode.COMPRESSED, IntegerCompressor.compressionMode(-0x00000001, 0x1fffffff));
+ assertEquals(Mode.COMPRESSED_POSITIVE, IntegerCompressor.compressionMode( 0x00000000, 0x00000000));
+ assertEquals(Mode.COMPRESSED_POSITIVE, IntegerCompressor.compressionMode( 0x00000000, 0x3fffffff));
+ assertEquals(Mode.COMPRESSED_POSITIVE, IntegerCompressor.compressionMode( 0x3fffffff, 0x3fffffff));
}
}