com.scalar.db.storage.dynamo.bytes.BlobBytesEncoder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of scalardb Show documentation
Show all versions of scalardb Show documentation
A universal transaction manager that achieves database-agnostic transactions and distributed transactions that span multiple databases
package com.scalar.db.storage.dynamo.bytes;
import static com.scalar.db.storage.dynamo.bytes.BytesUtils.mask;
import com.scalar.db.api.Scan.Ordering.Order;
import com.scalar.db.io.BlobValue;
import java.nio.ByteBuffer;
import javax.annotation.concurrent.ThreadSafe;
@ThreadSafe
public class BlobBytesEncoder implements BytesEncoder {
private static final byte TERM = (byte) 0x00;
private static final byte MASKED_TERM = (byte) 0xff;
BlobBytesEncoder() {}
@Override
public int encodedLength(BlobValue value, Order order) {
assert value.getAsBytes().isPresent();
return value.getAsBytes().get().length + (order == Order.ASC ? 0 : 1);
}
@Override
public void encode(BlobValue value, Order order, ByteBuffer dst) {
assert value.getAsBytes().isPresent();
if (order == Order.DESC) {
for (byte b : value.getAsBytes().get()) {
if (b == TERM) {
throw new IllegalArgumentException(
"0x00 bytes not accepted in blob values in DESC order");
}
}
}
for (byte b : value.getAsBytes().get()) {
dst.put(mask(b, order));
}
if (order == Order.DESC) {
// DESC ordered BlobValue requires a termination bit to preserve the sort order
dst.put(MASKED_TERM);
}
}
}