You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@ratis.apache.org by GitBox <gi...@apache.org> on 2020/12/02 06:49:06 UTC

[GitHub] [incubator-ratis] runzhiwang commented on a change in pull request #298: RATIS-1111. Change the FileStore example to use Streaming

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



##########
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) {

Review comment:
       looks duplicated with this two methods.
   ```
     static Path normalize(String path) {
       Objects.requireNonNull(path, "path == null");
       return Paths.get(path).normalize();
     }
   
     Path resolve(Path relative) throws IOException {
       final Path root = getRoot();
       final Path full = root.resolve(relative).normalize().toAbsolutePath();
       if (full.equals(root)) {
         throw new IOException("The file path " + relative + " resolved to " + full
             + " is the root directory " + root);
       } else if (!full.startsWith(root)) {
         throw new IOException("The file path " + relative + " resolved to " + full
             + " is not a sub-path under root directory " + root);
       }
       return full;
     }
   ```

##########
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) {

Review comment:
       seems duplicated with this two methods.
   ```
     static Path normalize(String path) {
       Objects.requireNonNull(path, "path == null");
       return Paths.get(path).normalize();
     }
   
     Path resolve(Path relative) throws IOException {
       final Path root = getRoot();
       final Path full = root.resolve(relative).normalize().toAbsolutePath();
       if (full.equals(root)) {
         throw new IOException("The file path " + relative + " resolved to " + full
             + " is the root directory " + root);
       } else if (!full.startsWith(root)) {
         throw new IOException("The file path " + relative + " resolved to " + full
             + " is not a sub-path under root directory " + root);
       }
       return full;
     }
   ```




----------------------------------------------------------------
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:
users@infra.apache.org