szetszwo commented on code in PR #998:
URL: https://github.com/apache/ratis/pull/998#discussion_r1445366495


##########
ratis-server/src/main/java/org/apache/ratis/server/raftlog/segmented/LogSegment.java:
##########
@@ -246,13 +247,48 @@ public LogEntryProto load(LogRecord key) throws 
IOException {
     }
   }
 
+  static class EntryCache {
+    private final Map<TermIndex, ReferenceCountedObject<LogEntryProto>> map = 
new ConcurrentHashMap<>();
+    private final AtomicLong size = new AtomicLong();
+
+    long size() {
+      return size.get();
+    }
+
+    LogEntryProto get(TermIndex ti) {
+      return Optional.ofNullable(map.get(ti))
+          .map(ReferenceCountedObject::get)
+          .orElse(null);
+    }
+
+    void clear() {
+      map.values().forEach(ReferenceCountedObject::release);
+      map.clear();
+      size.set(0);
+    }
+
+    void put(TermIndex key, ReferenceCountedObject<LogEntryProto> valueRef, Op 
op) {
+      valueRef.retain();
+      Optional.ofNullable(map.put(key, valueRef)).ifPresent(this::release);
+      size.getAndAdd(getEntrySize(valueRef.get(), op));
+    }
+
+    private void release(ReferenceCountedObject<LogEntryProto> entry) {
+      size.getAndAdd(-getEntrySize(entry.get(), Op.REMOVE_CACHE));
+      entry.release();

Review Comment:
   You are right that `getEntrySize(..)` seems returning different sizes for 
different op, although the size returned are always the same in the current 
usage.  Let me refactor it.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to