2021-02-02 19:40:37 +01:00
|
|
|
package it.cavallium.dbengine.database.serialization;
|
|
|
|
|
2021-08-29 23:18:03 +02:00
|
|
|
import io.netty.buffer.api.Buffer;
|
|
|
|
import io.netty.buffer.api.BufferAllocator;
|
|
|
|
import io.netty.buffer.api.Send;
|
|
|
|
import it.cavallium.dbengine.database.LLUtils;
|
2021-04-30 19:15:04 +02:00
|
|
|
import java.nio.charset.StandardCharsets;
|
2021-02-02 19:40:37 +01:00
|
|
|
import org.jetbrains.annotations.NotNull;
|
|
|
|
|
2021-02-03 14:37:02 +01:00
|
|
|
@SuppressWarnings("unused")
|
2021-02-02 19:40:37 +01:00
|
|
|
public interface SerializerFixedBinaryLength<A, B> extends Serializer<A, B> {
|
|
|
|
|
|
|
|
int getSerializedBinaryLength();
|
|
|
|
|
2021-08-29 23:18:03 +02:00
|
|
|
static SerializerFixedBinaryLength<Send<Buffer>, Send<Buffer>> noop(int length) {
|
2021-02-02 19:40:37 +01:00
|
|
|
return new SerializerFixedBinaryLength<>() {
|
|
|
|
@Override
|
2021-08-29 23:18:03 +02:00
|
|
|
public @NotNull Send<Buffer> deserialize(@NotNull Send<Buffer> serialized) {
|
|
|
|
try (var buf = serialized.receive()) {
|
|
|
|
if (buf.readableBytes() != getSerializedBinaryLength()) {
|
2021-04-30 19:15:04 +02:00
|
|
|
throw new IllegalArgumentException(
|
|
|
|
"Fixed serializer with " + getSerializedBinaryLength() + " bytes has tried to deserialize an element with "
|
2021-08-29 23:18:03 +02:00
|
|
|
+ buf.readableBytes() + " bytes instead");
|
2021-04-30 19:15:04 +02:00
|
|
|
}
|
2021-08-29 23:18:03 +02:00
|
|
|
return buf.send();
|
2021-04-30 19:15:04 +02:00
|
|
|
}
|
2021-02-02 19:40:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-08-29 23:18:03 +02:00
|
|
|
public @NotNull Send<Buffer> serialize(@NotNull Send<Buffer> deserialized) {
|
|
|
|
try (Buffer buf = deserialized.receive()) {
|
|
|
|
if (buf.readableBytes() != getSerializedBinaryLength()) {
|
|
|
|
throw new IllegalArgumentException(
|
|
|
|
"Fixed serializer with " + getSerializedBinaryLength() + " bytes has tried to serialize an element with "
|
|
|
|
+ buf.readableBytes() + " bytes instead");
|
|
|
|
}
|
|
|
|
return buf.send();
|
2021-04-30 19:15:04 +02:00
|
|
|
}
|
2021-02-02 19:40:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getSerializedBinaryLength() {
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-08-29 23:18:03 +02:00
|
|
|
static SerializerFixedBinaryLength<String, Send<Buffer>> utf8(BufferAllocator allocator, int length) {
|
2021-02-02 19:40:37 +01:00
|
|
|
return new SerializerFixedBinaryLength<>() {
|
|
|
|
@Override
|
2021-08-29 23:18:03 +02:00
|
|
|
public @NotNull String deserialize(@NotNull Send<Buffer> serializedToReceive) throws SerializationException {
|
|
|
|
try (var serialized = serializedToReceive.receive()) {
|
2021-04-30 19:15:04 +02:00
|
|
|
if (serialized.readableBytes() != getSerializedBinaryLength()) {
|
|
|
|
throw new SerializationException(
|
|
|
|
"Fixed serializer with " + getSerializedBinaryLength() + " bytes has tried to deserialize an element with "
|
|
|
|
+ serialized.readableBytes() + " bytes instead");
|
|
|
|
}
|
2021-08-29 23:18:03 +02:00
|
|
|
var readerOffset = serialized.readerOffset();
|
|
|
|
return LLUtils.deserializeString(serialized.send(), readerOffset, length, StandardCharsets.UTF_8);
|
2021-04-30 19:15:04 +02:00
|
|
|
}
|
2021-02-02 19:40:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-08-29 23:18:03 +02:00
|
|
|
public @NotNull Send<Buffer> serialize(@NotNull String deserialized) throws SerializationException {
|
2021-04-30 19:15:04 +02:00
|
|
|
// UTF-8 uses max. 3 bytes per char, so calculate the worst case.
|
2021-08-29 23:18:03 +02:00
|
|
|
try (Buffer buf = allocator.allocate(LLUtils.utf8MaxBytes(deserialized))) {
|
2021-08-31 09:14:46 +02:00
|
|
|
buf.writeBytes(deserialized.getBytes(StandardCharsets.UTF_8));
|
2021-04-30 19:15:04 +02:00
|
|
|
if (buf.readableBytes() != getSerializedBinaryLength()) {
|
2021-05-02 19:18:15 +02:00
|
|
|
throw new SerializationException("Fixed serializer with " + getSerializedBinaryLength()
|
|
|
|
+ " bytes has tried to serialize an element with "
|
2021-04-30 19:15:04 +02:00
|
|
|
+ buf.readableBytes() + " bytes instead");
|
|
|
|
}
|
2021-08-29 23:18:03 +02:00
|
|
|
return buf.send();
|
2021-04-30 19:15:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getSerializedBinaryLength() {
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-08-29 23:18:03 +02:00
|
|
|
static SerializerFixedBinaryLength<Integer, Send<Buffer>> intSerializer(BufferAllocator allocator) {
|
2021-04-30 19:15:04 +02:00
|
|
|
return new SerializerFixedBinaryLength<>() {
|
|
|
|
@Override
|
2021-08-29 23:18:03 +02:00
|
|
|
public @NotNull Integer deserialize(@NotNull Send<Buffer> serializedToReceive) {
|
|
|
|
try (var serialized = serializedToReceive.receive()) {
|
2021-04-30 19:15:04 +02:00
|
|
|
if (serialized.readableBytes() != getSerializedBinaryLength()) {
|
|
|
|
throw new IllegalArgumentException(
|
|
|
|
"Fixed serializer with " + getSerializedBinaryLength() + " bytes has tried to deserialize an element with "
|
|
|
|
+ serialized.readableBytes() + " bytes instead");
|
|
|
|
}
|
|
|
|
return serialized.readInt();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-08-29 23:18:03 +02:00
|
|
|
public @NotNull Send<Buffer> serialize(@NotNull Integer deserialized) {
|
|
|
|
try (Buffer buf = allocator.allocate(Integer.BYTES)) {
|
|
|
|
return buf.writeInt(deserialized).send();
|
|
|
|
}
|
2021-02-02 19:40:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getSerializedBinaryLength() {
|
|
|
|
return Integer.BYTES;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-08-29 23:18:03 +02:00
|
|
|
static SerializerFixedBinaryLength<Long, Send<Buffer>> longSerializer(BufferAllocator allocator) {
|
2021-02-02 19:40:37 +01:00
|
|
|
return new SerializerFixedBinaryLength<>() {
|
|
|
|
@Override
|
2021-08-29 23:18:03 +02:00
|
|
|
public @NotNull Long deserialize(@NotNull Send<Buffer> serializedToReceive) {
|
|
|
|
try (var serialized = serializedToReceive.receive()) {
|
2021-04-30 19:15:04 +02:00
|
|
|
if (serialized.readableBytes() != getSerializedBinaryLength()) {
|
|
|
|
throw new IllegalArgumentException(
|
|
|
|
"Fixed serializer with " + getSerializedBinaryLength() + " bytes has tried to deserialize an element with "
|
|
|
|
+ serialized.readableBytes() + " bytes instead");
|
|
|
|
}
|
|
|
|
return serialized.readLong();
|
|
|
|
}
|
2021-02-02 19:40:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-08-29 23:18:03 +02:00
|
|
|
public @NotNull Send<Buffer> serialize(@NotNull Long deserialized) {
|
|
|
|
try (Buffer buf = allocator.allocate(Long.BYTES)) {
|
|
|
|
return buf.writeLong(deserialized).send();
|
|
|
|
}
|
2021-02-02 19:40:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getSerializedBinaryLength() {
|
|
|
|
return Long.BYTES;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|