+ private List<ReplicatedLogEntry> copyJournalEntries(int fromIndex, int toIndex, long maxDataSize) {
+ List<ReplicatedLogEntry> retList = new ArrayList<>(toIndex - fromIndex);
+ long totalSize = 0;
+ for (int i = fromIndex; i < toIndex; i++) {
+ ReplicatedLogEntry entry = journal.get(i);
+ totalSize += entry.size();
+ if (totalSize <= maxDataSize) {
+ retList.add(entry);
+ } else {
+ if (retList.isEmpty()) {
+ // Edge case - the first entry's size exceeds the threshold. We need to return
+ // at least the first entry so add it here.
+ retList.add(entry);
+ }
+
+ break;
+ }
+ }
+
+ return retList;
+ }