+ public void setSnapshotIndex(final long snapshotIndex) {
+ this.snapshotIndex = snapshotIndex;
+ }
+
+ @Override
+ public void setSnapshotTerm(final long snapshotTerm) {
+ this.snapshotTerm = snapshotTerm;
+ }
+
+ @Override
+ public void clear(final int startIndex, final int endIndex) {
+ journal.subList(startIndex, endIndex).clear();
+ }
+
+ @Override
+ public void snapshotPreCommit(final long snapshotCapturedIndex, final long snapshotCapturedTerm) {
+ Preconditions.checkArgument(snapshotCapturedIndex >= snapshotIndex,
+ "snapshotCapturedIndex must be greater than or equal to snapshotIndex");
+
+ snapshottedJournal = new ArrayList<>(journal.size());
+
+ List<ReplicatedLogEntry> snapshotJournalEntries =
+ journal.subList(0, (int) (snapshotCapturedIndex - snapshotIndex));
+
+ snapshottedJournal.addAll(snapshotJournalEntries);
+ snapshotJournalEntries.clear();
+
+ previousSnapshotIndex = snapshotIndex;
+ setSnapshotIndex(snapshotCapturedIndex);
+
+ previousSnapshotTerm = snapshotTerm;
+ setSnapshotTerm(snapshotCapturedTerm);
+ }
+
+ @Override
+ public void snapshotCommit(final boolean updateDataSize) {
+ snapshottedJournal = null;
+ previousSnapshotIndex = -1;
+ previousSnapshotTerm = -1;
+
+ if (updateDataSize) {
+ // need to recalc the datasize based on the entries left after precommit.
+ int newDataSize = 0;
+ for (ReplicatedLogEntry logEntry : journal) {
+ newDataSize += logEntry.size();
+ }
+ LOG.trace("{}: Updated dataSize from {} to {}", logContext, dataSize, newDataSize);
+ dataSize = newDataSize;
+ }
+ }