CavalliumDBEngine/src/main/java/it/cavallium/dbengine/database/serialization/SerializerFixedBinaryLength.java

141 lines
4.6 KiB
Java
Raw Normal View History

2021-02-02 19:40:37 +01:00
package it.cavallium.dbengine.database.serialization;
2021-09-17 16:56:28 +02:00
import io.net5.buffer.api.Buffer;
import io.net5.buffer.api.BufferAllocator;
import io.net5.buffer.api.Send;
2021-08-29 23:18:03 +02:00
import it.cavallium.dbengine.database.LLUtils;
import java.nio.charset.StandardCharsets;
2021-09-22 18:33:28 +02:00
import java.util.Objects;
2021-02-02 19:40:37 +01:00
import org.jetbrains.annotations.NotNull;
2021-09-22 18:33:28 +02:00
import org.jetbrains.annotations.Nullable;
2021-02-02 19:40:37 +01:00
2021-02-03 14:37:02 +01:00
@SuppressWarnings("unused")
2021-09-01 00:01:56 +02:00
public interface SerializerFixedBinaryLength<A> extends Serializer<A> {
2021-02-02 19:40:37 +01:00
int getSerializedBinaryLength();
2021-10-19 00:22:05 +02:00
@Override
default int getSerializedSizeHint() {
return getSerializedBinaryLength();
}
static SerializerFixedBinaryLength<Buffer> noop(int length) {
2021-02-02 19:40:37 +01:00
return new SerializerFixedBinaryLength<>() {
@Override
2021-10-19 00:22:05 +02:00
public @NotNull Buffer deserialize(@NotNull Buffer serialized) {
2021-09-22 18:33:28 +02:00
Objects.requireNonNull(serialized);
2021-10-19 00:22:05 +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");
}
2021-10-19 00:22:05 +02:00
return serialized.readSplit(getSerializedBinaryLength());
2021-02-02 19:40:37 +01:00
}
@Override
2021-10-19 00:22:05 +02:00
public void serialize(@NotNull Buffer deserialized, Buffer output) {
try (deserialized) {
if (deserialized.readableBytes() != getSerializedBinaryLength()) {
2021-08-29 23:18:03 +02:00
throw new IllegalArgumentException(
"Fixed serializer with " + getSerializedBinaryLength() + " bytes has tried to serialize an element with "
2021-10-19 00:22:05 +02:00
+ deserialized.readableBytes() + " bytes instead");
2021-08-29 23:18:03 +02:00
}
2021-10-19 00:22:05 +02:00
output.writeBytes(deserialized);
}
2021-02-02 19:40:37 +01:00
}
@Override
public int getSerializedBinaryLength() {
return length;
}
};
}
2021-10-19 00:22:05 +02:00
static SerializerFixedBinaryLength<String> utf8(int length) {
2021-02-02 19:40:37 +01:00
return new SerializerFixedBinaryLength<>() {
2021-10-19 00:22:05 +02:00
2021-02-02 19:40:37 +01:00
@Override
2021-10-19 00:22:05 +02:00
public @NotNull String deserialize(@NotNull Buffer serialized) throws SerializationException {
if (serialized.readableBytes() < getSerializedBinaryLength()) {
throw new SerializationException(
"Fixed serializer with " + getSerializedBinaryLength() + " bytes has tried to deserialize an element with "
+ serialized.readableBytes() + " bytes instead");
}
2021-10-19 00:22:05 +02:00
var readerOffset = serialized.readerOffset();
return LLUtils.deserializeString(serialized.send(), readerOffset, length, StandardCharsets.UTF_8);
2021-02-02 19:40:37 +01:00
}
@Override
2021-10-19 00:22:05 +02:00
public void serialize(@NotNull String deserialized, Buffer output) throws SerializationException {
assert output.isAccessible();
var bytes = deserialized.getBytes(StandardCharsets.UTF_8);
output.ensureWritable(bytes.length);
output.writeBytes(bytes);
if (output.readableBytes() < getSerializedBinaryLength()) {
throw new SerializationException("Fixed serializer with " + getSerializedBinaryLength()
+ " bytes has tried to serialize an element with "
+ output.readableBytes() + " bytes instead");
}
2021-10-19 00:22:05 +02:00
assert output.isAccessible();
}
@Override
public int getSerializedBinaryLength() {
return length;
}
};
}
2021-09-01 00:01:56 +02:00
static SerializerFixedBinaryLength<Integer> intSerializer(BufferAllocator allocator) {
return new SerializerFixedBinaryLength<>() {
2021-10-19 00:22:05 +02:00
@Override
2021-10-19 00:22:05 +02:00
public @NotNull Integer deserialize(@NotNull Buffer serialized) {
Objects.requireNonNull(serialized);
if (serialized.readableBytes() < getSerializedBinaryLength()) {
throw new IllegalArgumentException(
"Fixed serializer with " + getSerializedBinaryLength() + " bytes has tried to deserialize an element with "
+ serialized.readableBytes() + " bytes instead");
}
2021-10-19 00:22:05 +02:00
return serialized.readInt();
}
@Override
2021-10-19 00:22:05 +02:00
public void serialize(@NotNull Integer deserialized, Buffer output) {
output.writeInt(deserialized);
2021-02-02 19:40:37 +01:00
}
@Override
public int getSerializedBinaryLength() {
return Integer.BYTES;
}
};
}
2021-09-01 00:01:56 +02:00
static SerializerFixedBinaryLength<Long> longSerializer(BufferAllocator allocator) {
2021-02-02 19:40:37 +01:00
return new SerializerFixedBinaryLength<>() {
2021-10-19 00:22:05 +02:00
2021-02-02 19:40:37 +01:00
@Override
2021-10-19 00:22:05 +02:00
public @NotNull Long deserialize(@NotNull Buffer serialized) {
Objects.requireNonNull(serialized);
if (serialized.readableBytes() < getSerializedBinaryLength()) {
throw new IllegalArgumentException(
"Fixed serializer with " + getSerializedBinaryLength() + " bytes has tried to deserialize an element with "
+ serialized.readableBytes() + " bytes instead");
}
2021-10-19 00:22:05 +02:00
return serialized.readLong();
2021-02-02 19:40:37 +01:00
}
@Override
2021-10-19 00:22:05 +02:00
public void serialize(@NotNull Long deserialized, Buffer output) {
output.writeLong(deserialized);
2021-02-02 19:40:37 +01:00
}
@Override
public int getSerializedBinaryLength() {
return Long.BYTES;
}
};
}
}