mirror of
https://github.com/usatiuk/dhfs.git
synced 2025-10-28 20:47:49 +01:00
Objects: lazy deserialization
This commit is contained in:
@@ -1,8 +1,7 @@
|
||||
package com.usatiuk.dhfs.objects;
|
||||
|
||||
import jakarta.annotation.Nonnull;
|
||||
public interface JDataVersionedWrapper {
|
||||
JData data();
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
public record JDataVersionedWrapper(@Nonnull JData data, long version) implements Serializable {
|
||||
long version();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
package com.usatiuk.dhfs.objects;
|
||||
|
||||
import jakarta.annotation.Nonnull;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
public record JDataVersionedWrapperImpl(@Nonnull JData data, long version) implements Serializable, JDataVersionedWrapper {
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package com.usatiuk.dhfs.objects;
|
||||
|
||||
import com.google.protobuf.ByteString;
|
||||
import com.usatiuk.dhfs.utils.SerializationHelper;
|
||||
|
||||
public class JDataVersionedWrapperLazy implements JDataVersionedWrapper {
|
||||
private final long _version;
|
||||
private ByteString _rawData;
|
||||
private JData _data;
|
||||
|
||||
public JDataVersionedWrapperLazy(long version, ByteString rawData) {
|
||||
_version = version;
|
||||
_rawData = rawData;
|
||||
}
|
||||
|
||||
public JData data() {
|
||||
if (_data != null)
|
||||
return _data;
|
||||
|
||||
synchronized (this) {
|
||||
if (_data != null)
|
||||
return _data;
|
||||
|
||||
try (var is = _rawData.newInput()) {
|
||||
_data = SerializationHelper.deserialize(is);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
_rawData = null;
|
||||
return _data;
|
||||
}
|
||||
}
|
||||
|
||||
public long version() {
|
||||
return _version;
|
||||
}
|
||||
}
|
||||
@@ -5,17 +5,22 @@ import com.google.protobuf.ByteString;
|
||||
import com.usatiuk.dhfs.utils.SerializationHelper;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
@ApplicationScoped
|
||||
public class JavaDataSerializer implements ObjectSerializer<JDataVersionedWrapper> {
|
||||
@Override
|
||||
public ByteString serialize(JDataVersionedWrapper obj) {
|
||||
return SerializationHelper.serialize((Serializable) obj);
|
||||
ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES);
|
||||
buffer.putLong(obj.version());
|
||||
buffer.flip();
|
||||
return ByteString.copyFrom(buffer).concat(SerializationHelper.serialize(obj.data()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public JDataVersionedWrapper deserialize(ByteString data) {
|
||||
return SerializationHelper.deserialize(data.toByteArray());
|
||||
var version = data.substring(0, Long.BYTES).asReadOnlyByteBuffer().getLong();
|
||||
var rawData = data.substring(Long.BYTES);
|
||||
return new JDataVersionedWrapperLazy(version, rawData);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -432,7 +432,7 @@ public class WritebackObjectPersistentStore {
|
||||
switch (action) {
|
||||
case TxRecord.TxObjectRecordWrite<?> write -> {
|
||||
Log.trace("Flushing object " + write.key());
|
||||
bundle.commit(new JDataVersionedWrapper(write.data(), bundleId));
|
||||
bundle.commit(new JDataVersionedWrapperImpl(write.data(), bundleId));
|
||||
}
|
||||
case TxRecord.TxObjectRecordDeleted deleted -> {
|
||||
Log.trace("Deleting object " + deleted.key());
|
||||
|
||||
Reference in New Issue
Block a user