*/
package io.atomix.storage.journal;
-import java.io.Closeable;
+import io.atomix.storage.journal.JournalReader.Mode;
/**
* Journal.
*
* @author <a href="http://github.com/kuujo">Jordan Halterman</a>
*/
-public interface Journal<E> extends Closeable {
+public interface Journal<E> extends AutoCloseable {
+ /**
+ * Returns the journal writer.
+ *
+ * @return The journal writer.
+ */
+ JournalWriter<E> writer();
- /**
- * Returns the journal writer.
- *
- * @return The journal writer.
- */
- JournalWriter<E> writer();
+ /**
+ * Opens a new journal reader with {@link Mode#ALL}.
+ *
+ * @param index The index at which to start the reader.
+ * @return A new journal reader.
+ */
+ default JournalReader<E> openReader(final long index) {
+ return openReader(index, Mode.ALL);
+ }
- /**
- * Opens a new journal reader.
- *
- * @param index The index at which to start the reader.
- * @return A new journal reader.
- */
- JournalReader<E> openReader(long index);
+ /**
+ * Opens a new journal reader with specified mode.
+ *
+ * @param index The index at which to start the reader.
+ * @param mode the reader mode
+ * @return A new journal reader.
+ */
+ JournalReader<E> openReader(long index, Mode mode);
- /**
- * Opens a new journal reader.
- *
- * @param index The index at which to start the reader.
- * @param mode the reader mode
- * @return A new journal reader.
- */
- JournalReader<E> openReader(long index, JournalReader.Mode mode);
-
- /**
- * Returns a boolean indicating whether the journal is open.
- *
- * @return Indicates whether the journal is open.
- */
- boolean isOpen();
-
- @Override
- void close();
+ @Override
+ void close();
}
import static java.util.Objects.requireNonNull;
import java.io.File;
-import java.util.concurrent.atomic.AtomicBoolean;
/**
* Segmented journal.
*/
public final class SegmentedJournal<E> implements Journal<E> {
- private final AtomicBoolean open = new AtomicBoolean(true);
private final SegmentedByteBufJournal journal;
private final SegmentedJournalWriter<E> writer;
private final ByteBufMapper<E> mapper;
return new SegmentedJournalReader<>(byteReader, mapper);
}
- @Override
- public boolean isOpen() {
- return open.get();
- }
-
@Override
public void close() {
- if (open.compareAndExchange(true, false)) {
- journal.close();
- }
+ journal.close();
}
/**