CavalliumDBEngine/src/main/java/it/cavallium/dbengine/database/collections/ValuesSetSerializer.java

59 lines
2.3 KiB
Java
Raw Normal View History

2021-05-08 03:09:00 +02:00
package it.cavallium.dbengine.database.collections;
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-22 21:23:22 +02:00
import it.cavallium.dbengine.database.serialization.SerializationException;
2021-05-08 03:09:00 +02:00
import it.cavallium.dbengine.database.serialization.Serializer;
import it.unimi.dsi.fastutil.objects.ObjectArraySet;
2021-07-13 22:58:08 +02:00
import java.util.ArrayList;
2021-09-22 18:33:28 +02:00
import java.util.Objects;
2021-05-08 03:09:00 +02:00
import org.jetbrains.annotations.NotNull;
2021-09-22 18:33:28 +02:00
import org.jetbrains.annotations.Nullable;
2021-05-08 03:09:00 +02:00
2021-09-01 00:01:56 +02:00
class ValuesSetSerializer<X> implements Serializer<ObjectArraySet<X>> {
2021-05-08 03:09:00 +02:00
2021-08-29 23:18:03 +02:00
private final BufferAllocator allocator;
2021-09-01 00:01:56 +02:00
private final Serializer<X> entrySerializer;
2021-05-08 03:09:00 +02:00
2021-09-01 00:01:56 +02:00
ValuesSetSerializer(BufferAllocator allocator, Serializer<X> entrySerializer) {
2021-05-08 03:09:00 +02:00
this.allocator = allocator;
this.entrySerializer = entrySerializer;
}
@Override
2021-09-22 18:33:28 +02:00
public @NotNull DeserializationResult<ObjectArraySet<X>> deserialize(@Nullable Send<Buffer> serializedToReceive) throws SerializationException {
Objects.requireNonNull(serializedToReceive);
2021-08-29 23:18:03 +02:00
try (var serialized = serializedToReceive.receive()) {
2021-09-02 17:15:40 +02:00
int initialReaderOffset = serialized.readerOffset();
2021-05-08 03:09:00 +02:00
int entriesLength = serialized.readInt();
2021-07-13 22:58:08 +02:00
ArrayList<X> deserializedElements = new ArrayList<>(entriesLength);
2021-05-08 03:09:00 +02:00
for (int i = 0; i < entriesLength; i++) {
2021-09-02 17:15:40 +02:00
var deserializationResult = entrySerializer.deserialize(serialized
.copy(serialized.readerOffset(), serialized.readableBytes())
.send());
deserializedElements.add(deserializationResult.deserializedData());
serialized.readerOffset(serialized.readerOffset() + deserializationResult.bytesRead());
2021-05-08 03:09:00 +02:00
}
2021-09-02 17:15:40 +02:00
return new DeserializationResult<>(new ObjectArraySet<>(deserializedElements), serialized.readerOffset() - initialReaderOffset);
2021-05-08 03:09:00 +02:00
}
}
@Override
2021-08-29 23:18:03 +02:00
public @NotNull Send<Buffer> serialize(@NotNull ObjectArraySet<X> deserialized) throws SerializationException {
try (Buffer output = allocator.allocate(64)) {
2021-05-08 03:09:00 +02:00
output.writeInt(deserialized.size());
2021-08-22 21:23:22 +02:00
for (X entry : deserialized) {
2021-09-22 18:33:28 +02:00
var serializedToReceive = entrySerializer.serialize(entry);
try (Buffer serialized = serializedToReceive.receive()) {
if (serialized.readableBytes() > 0) {
2021-09-22 18:33:28 +02:00
output.ensureWritable(serialized.readableBytes());
output.writeBytes(serialized);
}
2021-05-08 03:09:00 +02:00
}
2021-08-22 21:23:22 +02:00
}
2021-08-29 23:18:03 +02:00
return output.send();
2021-05-08 03:09:00 +02:00
}
}
}