summaryrefslogtreecommitdiffstats
path: root/vespajlib/src/main/java/com/yahoo/slime
diff options
context:
space:
mode:
authorHenning Baldersheim <balder@yahoo-inc.com>2021-08-30 16:51:11 +0200
committerHenning Baldersheim <balder@yahoo-inc.com>2021-08-30 16:51:11 +0200
commit99531f22f32354317cc3dff9e5d48730270aafa7 (patch)
treebb33ab9c9f39eb4fd764a51f3a9b143bc5928a87 /vespajlib/src/main/java/com/yahoo/slime
parentbeea81233a2b9334aac10ed32d14ad9ad8f73d9a (diff)
Avoid copying data just to compress them when it is not necessary.
Diffstat (limited to 'vespajlib/src/main/java/com/yahoo/slime')
-rw-r--r--vespajlib/src/main/java/com/yahoo/slime/BinaryDecoder.java10
-rw-r--r--vespajlib/src/main/java/com/yahoo/slime/BinaryEncoder.java20
-rw-r--r--vespajlib/src/main/java/com/yahoo/slime/BinaryFormat.java15
-rw-r--r--vespajlib/src/main/java/com/yahoo/slime/BufferedInput.java24
-rw-r--r--vespajlib/src/main/java/com/yahoo/slime/BufferedOutput.java17
5 files changed, 53 insertions, 33 deletions
diff --git a/vespajlib/src/main/java/com/yahoo/slime/BinaryDecoder.java b/vespajlib/src/main/java/com/yahoo/slime/BinaryDecoder.java
index ecee5e9504e..13e08484d7c 100644
--- a/vespajlib/src/main/java/com/yahoo/slime/BinaryDecoder.java
+++ b/vespajlib/src/main/java/com/yahoo/slime/BinaryDecoder.java
@@ -1,7 +1,11 @@
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.slime;
-import static com.yahoo.slime.BinaryFormat.*;
+
+import static com.yahoo.slime.BinaryFormat.decode_double;
+import static com.yahoo.slime.BinaryFormat.decode_meta;
+import static com.yahoo.slime.BinaryFormat.decode_type;
+import static com.yahoo.slime.BinaryFormat.decode_zigzag;
final class BinaryDecoder {
BufferedInput in;
@@ -135,9 +139,7 @@ final class BinaryDecoder {
void decodeValue(Inserter inserter) {
byte b = in.getByte();
- Cursor cursor = decodeValue(inserter,
- decode_type(b),
- decode_meta(b));
+ Cursor cursor = decodeValue(inserter, decode_type(b), decode_meta(b));
if (!cursor.valid()) {
in.fail("failed to decode value");
}
diff --git a/vespajlib/src/main/java/com/yahoo/slime/BinaryEncoder.java b/vespajlib/src/main/java/com/yahoo/slime/BinaryEncoder.java
index 4d9fdbba6d4..8f654b46032 100644
--- a/vespajlib/src/main/java/com/yahoo/slime/BinaryEncoder.java
+++ b/vespajlib/src/main/java/com/yahoo/slime/BinaryEncoder.java
@@ -1,28 +1,30 @@
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.slime;
-import static com.yahoo.slime.BinaryFormat.*;
+import static com.yahoo.slime.BinaryFormat.encode_double;
+import static com.yahoo.slime.BinaryFormat.encode_type_and_meta;
+import static com.yahoo.slime.BinaryFormat.encode_zigzag;
final class BinaryEncoder implements
ArrayTraverser, ObjectSymbolTraverser
{
- BufferedOutput out;
+ private final BufferedOutput out;
- public BinaryEncoder(int capacity) {
- out = new BufferedOutput(capacity);
+ BinaryEncoder() {
+ this(new BufferedOutput());
}
-
- public BinaryEncoder() {
- out = new BufferedOutput();
+ BinaryEncoder(BufferedOutput output) {
+ out = output;
}
- public byte[] encode(Slime slime) {
+ BufferedOutput encode(Slime slime) {
out.reset();
encodeSymbolTable(slime);
encodeValue(slime.get());
- return out.toArray();
+ return out;
}
+
void encode_cmpr_long(long value) {
byte next = (byte)(value & 0x7f);
value >>>= 7; // unsigned shift
diff --git a/vespajlib/src/main/java/com/yahoo/slime/BinaryFormat.java b/vespajlib/src/main/java/com/yahoo/slime/BinaryFormat.java
index c212500eca7..b6cde18f824 100644
--- a/vespajlib/src/main/java/com/yahoo/slime/BinaryFormat.java
+++ b/vespajlib/src/main/java/com/yahoo/slime/BinaryFormat.java
@@ -1,6 +1,8 @@
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.slime;
+import com.yahoo.compress.Compressor;
+
/**
* Class for serializing Slime data into binary format, or deserializing
* the binary format into a Slime object.
@@ -40,8 +42,17 @@ public class BinaryFormat {
* @return a new byte array with just the encoded slime.
**/
public static byte[] encode(Slime slime) {
- BinaryEncoder encoder = new BinaryEncoder();
- return encoder.encode(slime);
+ return new BinaryEncoder().encode(slime).toArray();
+ }
+
+ /**
+ * Take a Slime object and serialize it into binary format, and compresses it.
+ * @param slime the object which is to be serialized.
+ * @param compressor the compressor to use.
+ * @return a new byte array with just the encoded and compressed slime.
+ **/
+ public static Compressor.Compression encode_and_compress(Slime slime, Compressor compressor) {
+ return new BinaryEncoder().encode(slime).compress(compressor);
}
/**
diff --git a/vespajlib/src/main/java/com/yahoo/slime/BufferedInput.java b/vespajlib/src/main/java/com/yahoo/slime/BufferedInput.java
index 8a647269697..1e67aac3cb9 100644
--- a/vespajlib/src/main/java/com/yahoo/slime/BufferedInput.java
+++ b/vespajlib/src/main/java/com/yahoo/slime/BufferedInput.java
@@ -19,17 +19,17 @@ final class BufferedInput {
position = end;
}
- public BufferedInput(byte[] bytes) {
+ BufferedInput(byte[] bytes) {
this(bytes, 0, bytes.length);
}
- public BufferedInput(byte[] bytes, int offset, int length) {
+ BufferedInput(byte[] bytes, int offset, int length) {
this.source = bytes;
this.start = offset;
position = offset;
this.end = offset + length;
}
- public final byte getByte() {
+ byte getByte() {
if (position == end) {
fail("underflow");
return 0;
@@ -37,31 +37,31 @@ final class BufferedInput {
return source[position++];
}
- public boolean failed() {
+ boolean failed() {
return failReason != null;
}
- public boolean eof() {
+ boolean eof() {
return this.position == this.end;
}
- public String getErrorMessage() {
+ String getErrorMessage() {
return failReason;
}
- public int getConsumedSize() {
+ int getConsumedSize() {
return failed() ? 0 : position - start;
}
- public byte[] getOffending() {
+ byte[] getOffending() {
byte[] ret = new byte[failPos-start];
System.arraycopy(source, start, ret, 0, failPos-start);
return ret;
}
- public final byte [] getBacking() { return source; }
- public final int getPosition() { return position; }
- public final void skip(int size) {
+ byte [] getBacking() { return source; }
+ int getPosition() { return position; }
+ void skip(int size) {
if (position + size > end) {
fail("underflow");
} else {
@@ -69,7 +69,7 @@ final class BufferedInput {
}
}
- public final byte[] getBytes(int size) {
+ byte[] getBytes(int size) {
if (position + size > end) {
fail("underflow");
return new byte[0];
diff --git a/vespajlib/src/main/java/com/yahoo/slime/BufferedOutput.java b/vespajlib/src/main/java/com/yahoo/slime/BufferedOutput.java
index 20ed0fc8018..61a00185e76 100644
--- a/vespajlib/src/main/java/com/yahoo/slime/BufferedOutput.java
+++ b/vespajlib/src/main/java/com/yahoo/slime/BufferedOutput.java
@@ -1,22 +1,24 @@
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.slime;
-final class BufferedOutput {
+import com.yahoo.compress.Compressor;
+
+class BufferedOutput {
private byte[] buf;
private int capacity;
private int pos;
- public BufferedOutput(int cap) {
+ BufferedOutput(int cap) {
capacity = (cap < 64) ? 64 : cap;
buf = new byte[capacity];
}
- public BufferedOutput() {
+ BufferedOutput() {
this(4096);
}
- public void reset() {
+ void reset() {
pos = 0;
}
@@ -31,7 +33,7 @@ final class BufferedOutput {
}
}
- public int position() { return pos; }
+ int position() { return pos; }
void put(byte b) {
reserve(1);
@@ -49,9 +51,12 @@ final class BufferedOutput {
}
}
- public byte[] toArray() {
+ byte[] toArray() {
byte[] ret = new byte[pos];
System.arraycopy(buf, 0, ret, 0, pos);
return ret;
}
+ Compressor.Compression compress(Compressor compressor) {
+ return compressor.compress(buf, pos);
+ }
}