2021-05-08 03:09:00 +02:00
|
|
|
package it.cavallium.dbengine.database.collections;
|
|
|
|
|
2023-02-09 23:34:25 +01:00
|
|
|
import it.cavallium.dbengine.buffers.BufDataInput;
|
|
|
|
import it.cavallium.dbengine.buffers.BufDataOutput;
|
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-12-27 18:44:54 +01:00
|
|
|
import org.apache.logging.log4j.LogManager;
|
|
|
|
import org.apache.logging.log4j.Logger;
|
2021-05-08 03:09:00 +02:00
|
|
|
import org.jetbrains.annotations.NotNull;
|
|
|
|
|
2021-09-01 00:01:56 +02:00
|
|
|
class ValuesSetSerializer<X> implements Serializer<ObjectArraySet<X>> {
|
2021-05-08 03:09:00 +02:00
|
|
|
|
2021-12-27 18:44:54 +01:00
|
|
|
private static final Logger logger = LogManager.getLogger(ValuesSetSerializer.class);
|
|
|
|
|
2021-09-01 00:01:56 +02:00
|
|
|
private final Serializer<X> entrySerializer;
|
2021-05-08 03:09:00 +02:00
|
|
|
|
2021-10-19 00:22:05 +02:00
|
|
|
ValuesSetSerializer(Serializer<X> entrySerializer) {
|
2021-05-08 03:09:00 +02:00
|
|
|
this.entrySerializer = entrySerializer;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2023-02-09 23:34:25 +01:00
|
|
|
public @NotNull ObjectArraySet<X> deserialize(@NotNull BufDataInput in) throws SerializationException {
|
2021-12-27 18:44:54 +01:00
|
|
|
try {
|
2023-02-09 23:34:25 +01:00
|
|
|
Objects.requireNonNull(in);
|
|
|
|
if (in.available() <= 0) {
|
2021-12-27 18:44:54 +01:00
|
|
|
logger.error("Can't deserialize, 0 bytes are readable");
|
|
|
|
return new ObjectArraySet<>();
|
|
|
|
}
|
2023-02-09 23:34:25 +01:00
|
|
|
int entriesLength = in.readInt();
|
2021-12-27 18:44:54 +01:00
|
|
|
ArrayList<X> deserializedElements = new ArrayList<>(entriesLength);
|
|
|
|
for (int i = 0; i < entriesLength; i++) {
|
2023-02-09 23:34:25 +01:00
|
|
|
var deserializationResult = entrySerializer.deserialize(in);
|
2021-12-27 18:44:54 +01:00
|
|
|
deserializedElements.add(deserializationResult);
|
|
|
|
}
|
|
|
|
return new ObjectArraySet<>(deserializedElements);
|
|
|
|
} catch (IndexOutOfBoundsException ex) {
|
|
|
|
logger.error("Error during deserialization of value set, returning an empty set", ex);
|
|
|
|
return new ObjectArraySet<>();
|
2021-05-08 03:09:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2023-02-09 23:34:25 +01:00
|
|
|
public void serialize(@NotNull ObjectArraySet<X> deserialized, BufDataOutput out) throws SerializationException {
|
|
|
|
out.writeInt(deserialized.size());
|
2021-10-19 00:22:05 +02:00
|
|
|
for (X entry : deserialized) {
|
2023-02-09 23:34:25 +01:00
|
|
|
entrySerializer.serialize(entry, out);
|
2021-05-08 03:09:00 +02:00
|
|
|
}
|
|
|
|
}
|
2021-10-19 00:22:05 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getSerializedSizeHint() {
|
|
|
|
return -1;
|
|
|
|
}
|
2021-05-08 03:09:00 +02:00
|
|
|
}
|