Objects: lazy deserialization

This commit is contained in:
2025-03-08 15:05:57 +01:00
parent 46a67b61e5
commit 174320d389
5 changed files with 57 additions and 8 deletions

View File

@@ -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();
}

View File

@@ -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 {
}

View File

@@ -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;
}
}

View File

@@ -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);
}
}

View File

@@ -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());