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

88 lines
2.4 KiB
Java
Raw Normal View History

2021-02-02 19:40:37 +01:00
package it.cavallium.dbengine.database.serialization;
import it.cavallium.dbengine.buffers.BufDataInput;
import it.cavallium.dbengine.buffers.BufDataOutput;
2021-02-11 01:09:15 +01:00
import java.io.IOError;
2021-02-02 19:40:37 +01:00
import java.io.IOException;
import org.jetbrains.annotations.NotNull;
2021-09-02 17:15:40 +02:00
public class CodecSerializer<A> implements Serializer<A> {
2021-02-02 19:40:37 +01:00
private final Codecs<A> deserializationCodecs;
private final Codec<A> serializationCodec;
private final int serializationCodecId;
private final boolean microCodecs;
2021-10-19 00:22:05 +02:00
private final int serializedSizeHint;
2021-02-02 19:40:37 +01:00
/**
*
* @param microCodecs if true, allow only codecs with a value from 0 to 255 to save disk space
2021-10-19 00:22:05 +02:00
* @param serializedSizeHint suggested default buffer size, -1 if unknown
2021-02-02 19:40:37 +01:00
*/
2021-05-03 21:41:51 +02:00
public CodecSerializer(
Codecs<A> deserializationCodecs,
2021-02-02 19:40:37 +01:00
Codec<A> serializationCodec,
int serializationCodecId,
2021-10-19 00:22:05 +02:00
boolean microCodecs,
int serializedSizeHint) {
2021-02-02 19:40:37 +01:00
this.deserializationCodecs = deserializationCodecs;
this.serializationCodec = serializationCodec;
this.serializationCodecId = serializationCodecId;
this.microCodecs = microCodecs;
if (microCodecs && (serializationCodecId > 255 || serializationCodecId < 0)) {
2022-05-10 00:31:16 +02:00
throw new IndexOutOfBoundsException(serializationCodecId);
2021-02-02 19:40:37 +01:00
}
2021-10-19 00:22:05 +02:00
if (serializedSizeHint != -1) {
this.serializedSizeHint = (microCodecs ? Byte.BYTES : Integer.BYTES) + serializedSizeHint;
} else {
this.serializedSizeHint = -1;
}
}
@Override
public int getSerializedSizeHint() {
return serializedSizeHint;
2021-02-02 19:40:37 +01:00
}
@Override
public @NotNull A deserialize(@NotNull BufDataInput is) throws SerializationException {
2021-10-19 00:22:05 +02:00
try {
2021-02-02 19:40:37 +01:00
int codecId;
if (microCodecs) {
codecId = is.readUnsignedByte();
} else {
codecId = is.readInt();
}
var serializer = deserializationCodecs.getCodec(codecId);
2021-10-19 00:22:05 +02:00
return serializer.deserialize(is);
2021-02-02 19:40:37 +01:00
} catch (IOException ex) {
// This shouldn't happen
2021-02-11 01:09:15 +01:00
throw new IOError(ex);
2021-02-02 19:40:37 +01:00
}
}
@Override
public void serialize(@NotNull A deserialized, BufDataOutput os) throws SerializationException {
2021-10-19 00:22:05 +02:00
try {
2021-02-02 19:40:37 +01:00
if (microCodecs) {
os.writeByte(serializationCodecId);
} else {
os.writeInt(serializationCodecId);
}
serializationCodec.serialize(os, deserialized);
} catch (IOException ex) {
// This shouldn't happen
2021-02-11 01:09:15 +01:00
throw new IOError(ex);
}
}
2021-04-03 19:09:06 +02:00
@SuppressWarnings("unused")
2021-02-11 01:09:15 +01:00
public int getCodecHeadersBytes() {
if (microCodecs) {
return Byte.BYTES;
} else {
return Integer.BYTES;
2021-02-02 19:40:37 +01:00
}
}
}