com.scalar.db.storage.dynamo.bytes.TextBytesEncoder 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.TextValue;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import javax.annotation.concurrent.ThreadSafe;
@ThreadSafe
public class TextBytesEncoder implements BytesEncoder {
private static final byte TERM = 0x00;
TextBytesEncoder() {}
@Override
public int encodedLength(TextValue value, Order order) {
assert value.getAsString().isPresent();
return value.getAsString().get().getBytes(StandardCharsets.UTF_8).length + 1;
}
@Override
public void encode(TextValue value, Order order, ByteBuffer dst) {
assert value.getAsString().isPresent();
if (value.getAsString().get().contains("\u0000")) {
throw new IllegalArgumentException("Can't encode a Text value containing '\\u0000'");
}
byte[] bytes = value.getAsString().get().getBytes(StandardCharsets.UTF_8);
for (byte b : bytes) {
dst.put(mask(b, order));
}
dst.put(mask(TERM, order));
}
}