/**
* Resets the head of the journal to the given index.
*
- * @param index The index to which to reset the head of the journal
+ * @param index the next index to write
+ * @throws IndexOutOfBoundsException if the journal cannot be reset to specified index
*/
- // FIXME: reconcile with reader's reset and truncate()
// FIXME: throws IOException
void reset(long index);
* Truncates the log to the given index.
*
* @param index The index to which to truncate the log.
+ * @throws IndexOutOfBoundsException if the journal cannot be reset to specified index
*/
// FIXME: reconcile with reset()
// FIXME: throws IOException
/**
* Resets the head of the journal to the given index.
*
- * @param index the index to which to reset the head of the journal
+ * @param index the next index to write
+ * @throws IndexOutOfBoundsException if the journal cannot be reset to specified index
*/
// FIXME: reconcile with reader's reset and truncate()
void reset(long index);
* Truncates the log to the given index.
*
* @param index The index to which to truncate the log.
+ * @throws IndexOutOfBoundsException if the journal cannot be reset to specified index
*/
// FIXME: reconcile with reset()
void truncate(long index);
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();
return writer.nextIndex();
}
- @Override
- public void reset(final long index) {
- writer.reset(index);
- }
-
@Override
public void commit(final long index) {
writer.commit(index);
return new Indexed<>(writer.append(buf), entry, buf.readableBytes());
}
+ @Override
+ public void reset(final long index) {
+ writer.reset(index);
+ }
+
@Override
public void truncate(final long index) {
writer.truncate(index);