amaliujia commented on a change in pull request #298:
URL: https://github.com/apache/incubator-ratis/pull/298#discussion_r534452736



##########
File path: 
ratis-examples/src/main/java/org/apache/ratis/examples/filestore/FileStore.java
##########
@@ -219,4 +225,67 @@ public void close() {
     reader.shutdownNow();
     deleter.shutdownNow();
   }
+
+  CompletableFuture<StreamWriteReplyProto> streamCommit(String p, long 
bytesWritten) {
+    return CompletableFuture.supplyAsync(() -> {
+      final Path full = buildFullPath(p);
+      try {
+        RandomAccessFile file = new RandomAccessFile(full.toFile(), "r");
+        long len = file.length();
+        return StreamWriteReplyProto.newBuilder().setIsSuccess(len == 
bytesWritten).setByteWritten(len).build();
+      } catch (FileNotFoundException e) {
+        throw new CompletionException("Failed to find " + p, e);
+      } catch (IOException e) {
+        throw new CompletionException("Failed to get length of " + p, e);
+      }
+    });
+  }
+
+  public CompletableFuture<FileStoreDataChannel> createDataChannel(String p) {
+    return CompletableFuture.supplyAsync(() -> {
+      final Path full = buildFullPath(p);
+      try {
+        return new FileStoreDataChannel(new RandomAccessFile(full.toFile(), 
"rw"));
+      } catch (IOException e) {
+        throw new CompletionException("Failed to create " + p, e);
+      }
+    }, writer);
+  }
+
+  private Path buildFullPath(String relative) {
+    final Path root = getRoot();
+    final Path relativePath = Paths.get(relative).normalize();
+    return root.resolve(relativePath).normalize().toAbsolutePath();
+  }
+
+  class FileStoreDataChannel implements StateMachine.DataChannel {
+    private final RandomAccessFile randomAccessFile;
+    private final FileChannel fileChannel;

Review comment:
       Good catch. The channel is a unique object of a random access file.

##########
File path: 
ratis-examples/src/main/java/org/apache/ratis/examples/filestore/FileStore.java
##########
@@ -219,4 +225,67 @@ public void close() {
     reader.shutdownNow();
     deleter.shutdownNow();
   }
+
+  CompletableFuture<StreamWriteReplyProto> streamCommit(String p, long 
bytesWritten) {
+    return CompletableFuture.supplyAsync(() -> {
+      final Path full = buildFullPath(p);
+      try {
+        RandomAccessFile file = new RandomAccessFile(full.toFile(), "r");
+        long len = file.length();
+        return StreamWriteReplyProto.newBuilder().setIsSuccess(len == 
bytesWritten).setByteWritten(len).build();
+      } catch (FileNotFoundException e) {
+        throw new CompletionException("Failed to find " + p, e);
+      } catch (IOException e) {
+        throw new CompletionException("Failed to get length of " + p, e);
+      }
+    });
+  }
+
+  public CompletableFuture<FileStoreDataChannel> createDataChannel(String p) {
+    return CompletableFuture.supplyAsync(() -> {
+      final Path full = buildFullPath(p);
+      try {
+        return new FileStoreDataChannel(new RandomAccessFile(full.toFile(), 
"rw"));
+      } catch (IOException e) {
+        throw new CompletionException("Failed to create " + p, e);
+      }
+    }, writer);
+  }
+
+  private Path buildFullPath(String relative) {
+    final Path root = getRoot();
+    final Path relativePath = Paths.get(relative).normalize();
+    return root.resolve(relativePath).normalize().toAbsolutePath();
+  }
+
+  class FileStoreDataChannel implements StateMachine.DataChannel {
+    private final RandomAccessFile randomAccessFile;
+    private final FileChannel fileChannel;
+
+    FileStoreDataChannel(RandomAccessFile file) {
+      randomAccessFile = file;
+      fileChannel = file.getChannel();
+    }
+
+    @Override
+    public void force(boolean metadata) throws IOException {
+      fileChannel.force(metadata);
+    }
+
+    @Override
+    public int write(ByteBuffer src) throws IOException {
+      return fileChannel.write(src);
+    }
+
+    @Override
+    public boolean isOpen() {
+      return fileChannel.isOpen();
+    }
+
+    @Override
+    public void close() throws IOException {

Review comment:
       Yes it is enough: 
https://docs.oracle.com/javase/8/docs/api/java/io/RandomAccessFile.html#close--




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to