Separate out FileAccess
[controller.git] / atomix-storage / src / main / java / io / atomix / storage / journal / MappedFileWriter.java
index a8877fabf7dfe30abf530da71bc9003f0146aa24..589f4d776c246f1bb2e5a9ed316d8e5572e4386d 100644 (file)
  */
 package io.atomix.storage.journal;
 
-import io.netty.util.internal.PlatformDependent;
+import static java.util.Objects.requireNonNull;
+
+import java.io.Flushable;
 import java.io.IOException;
 import java.nio.ByteBuffer;
-import java.nio.MappedByteBuffer;
-import org.eclipse.jdt.annotation.NonNull;
 
 /**
  * A {@link StorageLevel#MAPPED} {@link FileWriter}.
  */
 final class MappedFileWriter extends FileWriter {
-    private final @NonNull MappedByteBuffer mappedBuffer;
     private final MappedFileReader reader;
     private final ByteBuffer buffer;
+    private final Flushable flush;
 
-    MappedFileWriter(final JournalSegmentFile file, final int maxEntrySize) {
+    MappedFileWriter(final JournalSegmentFile file, final int maxEntrySize, final ByteBuffer buffer,
+            final Flushable flush) {
         super(file, maxEntrySize);
-
-        try {
-            mappedBuffer = file.map();
-        } catch (IOException e) {
-            throw new StorageException(e);
-        }
-        buffer = mappedBuffer.slice();
-        reader = new MappedFileReader(file, mappedBuffer);
+        this.buffer = requireNonNull(buffer);
+        this.flush = requireNonNull(flush);
+        reader = new MappedFileReader(file, buffer);
     }
 
     @Override
@@ -46,22 +42,6 @@ final class MappedFileWriter extends FileWriter {
         return reader;
     }
 
-    @Override
-    MappedByteBuffer buffer() {
-        return mappedBuffer;
-    }
-
-    @Override
-    MappedFileWriter toMapped() {
-        return null;
-    }
-
-    @Override
-    DiskFileWriter toDisk() {
-        close();
-        return new DiskFileWriter(file, maxEntrySize);
-    }
-
     @Override
     void writeEmptyHeader(final int position) {
         // Note: we issue a single putLong() instead of two putInt()s.
@@ -79,13 +59,7 @@ final class MappedFileWriter extends FileWriter {
     }
 
     @Override
-    void flush() {
-        mappedBuffer.force();
-    }
-
-    @Override
-    void close() {
-        flush();
-        PlatformDependent.freeDirectBuffer(mappedBuffer);
+    void flush() throws IOException {
+        flush.flush();
     }
 }