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

96 lines
2.7 KiB
Java
Raw Normal View History

2021-02-02 19:40:37 +01:00
package it.cavallium.dbengine.database.serialization;
2021-10-19 00:22:05 +02:00
import io.net5.buffer.ByteBufUtil;
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-10-19 00:22:05 +02:00
import io.net5.util.internal.StringUtil;
2021-08-29 23:18:03 +02:00
import it.cavallium.dbengine.database.LLUtils;
import it.cavallium.dbengine.netty.NullableBuffer;
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-09-01 00:01:56 +02:00
public interface Serializer<A> {
2021-02-02 19:40:37 +01:00
2021-10-19 00:22:05 +02:00
/**
*
* @param serialized the serialized data should be split!
*/
@NotNull A deserialize(@NotNull Buffer serialized) throws SerializationException;
2021-02-02 19:40:37 +01:00
2021-10-19 00:22:05 +02:00
/**
* @param output its writable size will be at least equal to the size hint
*/
void serialize(@NotNull A deserialized, Buffer output) throws SerializationException;
2021-02-02 19:40:37 +01:00
2021-10-19 00:22:05 +02:00
/**
* @return suggested default buffer size, -1 if unknown
*/
int getSerializedSizeHint();
2021-09-01 00:01:56 +02:00
2021-10-19 00:22:05 +02:00
Serializer<Buffer> NOOP_SERIALIZER = new Serializer<>() {
2021-03-14 03:13:19 +01:00
@Override
2021-10-19 00:22:05 +02:00
public @NotNull Buffer deserialize(@NotNull Buffer serialized) {
return serialized.split();
}
@Override
public void serialize(@NotNull Buffer deserialized, @NotNull Buffer deserializedToReceive) {
deserializedToReceive.ensureWritable(deserialized.readableBytes());
deserializedToReceive.writeBytes(deserialized);
2021-03-14 03:13:19 +01:00
}
@Override
2021-10-19 00:22:05 +02:00
public int getSerializedSizeHint() {
return -1;
2021-03-14 03:13:19 +01:00
}
};
2021-02-02 19:40:37 +01:00
2021-10-19 00:22:05 +02:00
Serializer<Send<Buffer>> NOOP_SEND_SERIALIZER = new Serializer<>() {
@Override
public @NotNull Send<Buffer> deserialize(@NotNull Buffer serialized) {
return serialized.split().send();
}
2021-10-19 00:22:05 +02:00
@Override
public void serialize(@NotNull Send<Buffer> deserialized, @NotNull Buffer deserializedToReceive) {
try (var received = deserialized.receive()) {
deserializedToReceive.ensureWritable(received.readableBytes());
deserializedToReceive.writeBytes(received);
2021-05-03 21:41:51 +02:00
}
2021-10-19 00:22:05 +02:00
}
2021-05-03 21:41:51 +02:00
2021-10-19 00:22:05 +02:00
@Override
public int getSerializedSizeHint() {
return -1;
}
};
Serializer<String> UTF8_SERIALIZER = new Serializer<>() {
@Override
public @NotNull String deserialize(@NotNull Buffer serialized) {
assert serialized.isAccessible();
int length = serialized.readInt();
try (var strBuf = serialized.readSplit(length)) {
return LLUtils.deserializeString(strBuf, strBuf.readerOffset(), length, StandardCharsets.UTF_8);
2021-05-03 21:41:51 +02:00
}
2021-10-19 00:22:05 +02:00
}
@Override
public void serialize(@NotNull String deserialized, Buffer output) {
var bytes = deserialized.getBytes(StandardCharsets.UTF_8);
output.ensureWritable(Integer.BYTES + bytes.length);
output.writeInt(bytes.length);
output.writeBytes(bytes);
}
@Override
public int getSerializedSizeHint() {
return -1;
}
};
2021-02-02 19:40:37 +01:00
}