Objects: really lazy deserialization

as it was happening in the cache
This commit is contained in:
2025-03-08 15:27:08 +01:00
parent 174320d389
commit 66bf4b7d18
5 changed files with 23 additions and 2 deletions

View File

@@ -4,4 +4,6 @@ public interface JDataVersionedWrapper {
JData data();
long version();
int estimateSize();
}

View File

@@ -4,5 +4,10 @@ import jakarta.annotation.Nonnull;
import java.io.Serializable;
public record JDataVersionedWrapperImpl(@Nonnull JData data, long version) implements Serializable, JDataVersionedWrapper {
public record JDataVersionedWrapperImpl(@Nonnull JData data,
long version) implements Serializable, JDataVersionedWrapper {
@Override
public int estimateSize() {
return data().estimateSize();
}
}

View File

@@ -34,4 +34,11 @@ public class JDataVersionedWrapperLazy implements JDataVersionedWrapper {
public long version() {
return _version;
}
@Override
public int estimateSize() {
if (_data != null)
return _data.estimateSize();
return _rawData.size();
}
}

View File

@@ -60,7 +60,7 @@ public class CachingObjectPersistentStore {
// Log.tracev("Adding {0} to cache: {1}", key, obj);
_lock.writeLock().lock();
try {
int size = obj.map(o -> o.data().estimateSize()).orElse(16);
int size = obj.map(JDataVersionedWrapper::estimateSize).orElse(16);
_curSize += size;
var entry = new CacheEntry(obj.<MaybeTombstone<JDataVersionedWrapper>>map(Data::new).orElse(new Tombstone<>()), size);

View File

@@ -269,6 +269,13 @@ public class LmdbObjectPersistentStore implements ObjectPersistentStore {
if (!_hasNext) {
throw new NoSuchElementException("No more elements");
}
// TODO: Right now with java serialization it doesn't matter, it's all copied to arrays anyway
// var val = _cursor.val();
// var bbDirect = UninitializedByteBuffer.allocateUninitialized(val.remaining());
// bbDirect.put(val);
// bbDirect.flip();
// var bs = UnsafeByteOperations.unsafeWrap(bbDirect);
// var ret = Pair.of(JObjectKey.fromByteBuffer(_cursor.key()), bs);
var ret = Pair.of(JObjectKey.fromByteBuffer(_cursor.key()), ByteString.copyFrom(_cursor.val()));
if (_goingForward)
_hasNext = _cursor.next();