2021-05-11 21:59:05 +02:00
|
|
|
package it.cavallium.dbengine.client;
|
|
|
|
|
2021-08-29 23:18:03 +02:00
|
|
|
import io.netty.buffer.api.Buffer;
|
2021-08-31 09:14:46 +02:00
|
|
|
import io.netty.buffer.api.Send;
|
2021-08-22 21:23:22 +02:00
|
|
|
import it.cavallium.dbengine.database.serialization.SerializationException;
|
2021-05-11 21:59:05 +02:00
|
|
|
import it.cavallium.dbengine.database.serialization.SerializerFixedBinaryLength;
|
|
|
|
import org.jetbrains.annotations.NotNull;
|
|
|
|
|
2021-08-31 09:14:46 +02:00
|
|
|
public class MappedSerializerFixedLength<A, B> implements SerializerFixedBinaryLength<B, Send<Buffer>> {
|
2021-05-11 21:59:05 +02:00
|
|
|
|
2021-08-31 09:14:46 +02:00
|
|
|
private final SerializerFixedBinaryLength<A, Send<Buffer>> fixedLengthSerializer;
|
2021-05-11 21:59:05 +02:00
|
|
|
private final Mapper<A, B> keyMapper;
|
|
|
|
|
2021-08-31 09:14:46 +02:00
|
|
|
public MappedSerializerFixedLength(SerializerFixedBinaryLength<A, Send<Buffer>> fixedLengthSerializer,
|
2021-05-11 21:59:05 +02:00
|
|
|
Mapper<A, B> keyMapper) {
|
|
|
|
this.fixedLengthSerializer = fixedLengthSerializer;
|
|
|
|
this.keyMapper = keyMapper;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-08-31 09:14:46 +02:00
|
|
|
public @NotNull B deserialize(@NotNull Send<Buffer> serialized) throws SerializationException {
|
|
|
|
try (serialized) {
|
|
|
|
return keyMapper.map(fixedLengthSerializer.deserialize(serialized));
|
2021-05-11 21:59:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-08-31 09:14:46 +02:00
|
|
|
public @NotNull Send<Buffer> serialize(@NotNull B deserialized) throws SerializationException {
|
2021-05-11 21:59:05 +02:00
|
|
|
return fixedLengthSerializer.serialize(keyMapper.unmap(deserialized));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getSerializedBinaryLength() {
|
|
|
|
return fixedLengthSerializer.getSerializedBinaryLength();
|
|
|
|
}
|
|
|
|
}
|