Refactor Journal interface 43/111643/2
authorRobert Varga <robert.varga@pantheon.tech>
Mon, 6 May 2024 18:59:16 +0000 (20:59 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Tue, 7 May 2024 20:58:00 +0000 (22:58 +0200)
There are two basic issues:
- isOpen() is completely unused
- close() is inherited from Closeable, not AutoCloseable

Address these and move implementation of openReader(long), so it
is canonical.

JIRA: CONTROLLER-2100
Change-Id: I1468ed5a3e9ee1abefe35a4bfaf653696763907f
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
atomix-storage/src/main/java/io/atomix/storage/journal/Journal.java
atomix-storage/src/main/java/io/atomix/storage/journal/SegmentedJournal.java

index 5e37c12222ece712b0b678f655dabaec5ccbfb41..93ae0a565bcaa7e1d941c703490f069273329daa 100644 (file)
  */
 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();
 }
index cd074926925eae80cea3c9ad2e39e85e656169fa..8f0464ebe3fabc0fd8ef69cca57c1fd346bd135a 100644 (file)
@@ -19,13 +19,11 @@ package io.atomix.storage.journal;
 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;
@@ -62,16 +60,9 @@ public final class SegmentedJournal<E> implements Journal<E> {
         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();
     }
 
     /**