private long previousSnapshotTerm = -1;
private int dataSize = 0;
- public AbstractReplicatedLogImpl(long snapshotIndex,
- long snapshotTerm, List<ReplicatedLogEntry> unAppliedEntries, String logContext) {
+ protected AbstractReplicatedLogImpl(long snapshotIndex, long snapshotTerm,
+ List<ReplicatedLogEntry> unAppliedEntries, String logContext) {
this.snapshotIndex = snapshotIndex;
this.snapshotTerm = snapshotTerm;
this.logContext = logContext;
this.journal = new ArrayList<>(unAppliedEntries.size());
- for(ReplicatedLogEntry entry: unAppliedEntries) {
+ for (ReplicatedLogEntry entry: unAppliedEntries) {
append(entry);
}
}
- public AbstractReplicatedLogImpl() {
+ protected AbstractReplicatedLogImpl() {
this(-1L, -1L, Collections.<ReplicatedLogEntry>emptyList(), "");
}
return -1;
}
- for(int i = adjustedIndex; i < journal.size(); i++) {
+ for (int i = adjustedIndex; i < journal.size(); i++) {
dataSize -= journal.get(i).size();
}
@Override
public boolean append(ReplicatedLogEntry replicatedLogEntry) {
- if(replicatedLogEntry.getIndex() > lastIndex()) {
+ if (replicatedLogEntry.getIndex() > lastIndex()) {
journal.add(replicatedLogEntry);
dataSize += replicatedLogEntry.size();
return true;
if (adjustedIndex >= 0 && adjustedIndex < size) {
// physical index should be less than list size and >= 0
int maxIndex = adjustedIndex + maxEntries;
- if(maxIndex > size){
+ if (maxIndex > size) {
maxIndex = size;
}
- if(maxDataSize == NO_MAX_SIZE) {
+ if (maxDataSize == NO_MAX_SIZE) {
return new ArrayList<>(journal.subList(adjustedIndex, maxIndex));
} else {
- List<ReplicatedLogEntry> retList = new ArrayList<>(maxIndex - adjustedIndex);
- long totalSize = 0;
- for(int i = adjustedIndex; i < maxIndex; 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;
+ return copyJournalEntries(adjustedIndex, maxIndex, maxDataSize);
}
} else {
return Collections.emptyList();
}
}
+ 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;
+ }
+
@Override
public long size() {
- return journal.size();
+ return journal.size();
}
@Override
return false;
}
int adjustedIndex = adjustedIndex(logEntryIndex);
- return (adjustedIndex >= 0);
+ return adjustedIndex >= 0;
}
@Override
public boolean isInSnapshot(long logEntryIndex) {
- return logEntryIndex <= snapshotIndex && snapshotIndex != -1;
+ return logEntryIndex >= 0 && logEntryIndex <= snapshotIndex && snapshotIndex != -1;
}
@Override
snapshottedJournal = new ArrayList<>(journal.size());
- List<ReplicatedLogEntry> snapshotJournalEntries = journal.subList(0, (int) (snapshotCapturedIndex - snapshotIndex));
+ List<ReplicatedLogEntry> snapshotJournalEntries =
+ journal.subList(0, (int) (snapshotCapturedIndex - snapshotIndex));
snapshottedJournal.addAll(snapshotJournalEntries);
snapshotJournalEntries.clear();
previousSnapshotTerm = -1;
dataSize = 0;
// need to recalc the datasize based on the entries left after precommit.
- for(ReplicatedLogEntry logEntry : journal) {
+ for (ReplicatedLogEntry logEntry : journal) {
dataSize += logEntry.size();
}