return currentWriter.getNextIndex();
}
- @Override
- public void reset(final long index) {
- if (index > currentSegment.firstIndex()) {
- currentSegment.releaseWriter();
- currentSegment = journal.resetSegments(index);
- currentWriter = currentSegment.acquireWriter();
- } else {
- truncate(index - 1);
- }
- journal.resetHead(index);
- }
-
@Override
public void commit(final long index) {
if (index > journal.getCommitIndex()) {
return verifyNotNull(currentWriter.append(buf));
}
+ @Override
+ public void reset(final long index) {
+ final long commitIndex = journal.getCommitIndex();
+ if (index <= commitIndex) {
+ // also catches index == 0, which is not a valid next index
+ throw new IndexOutOfBoundsException("Cannot reset to: " + index + ", committed index: " + commitIndex);
+ }
+
+ if (index > currentSegment.firstIndex()) {
+ currentSegment.releaseWriter();
+ currentSegment = journal.resetSegments(index);
+ currentWriter = currentSegment.acquireWriter();
+ } else {
+ checkedTruncate(index - 1);
+ }
+ journal.resetHead(index);
+ }
+
@Override
public void truncate(final long index) {
if (index < journal.getCommitIndex()) {
throw new IndexOutOfBoundsException("Cannot truncate committed index: " + index);
}
+ checkedTruncate(index);
+ }
+ private void checkedTruncate(final long index) {
// Delete all segments with first indexes greater than the given index.
while (index < currentSegment.firstIndex() && currentSegment != journal.firstSegment()) {
currentSegment.releaseWriter();