arp7 commented on a change in pull request #1044: HDDS-1731. Implement File 
CreateFile Request to use Cache and DoubleBuffer.
URL: https://github.com/apache/hadoop/pull/1044#discussion_r299616466
 
 

 ##########
 File path: 
hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/request/file/OMFileRequest.java
 ##########
 @@ -0,0 +1,93 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.hadoop.ozone.om.request.file;
+
+import java.io.IOException;
+import java.nio.file.Path;
+
+import org.apache.hadoop.ozone.om.OMMetadataManager;
+
+import javax.annotation.Nonnull;
+
+/**
+ * Base class for file requests.
+ */
+public interface OMFileRequest {
+  /**
+   * Verify any files exist in the given path in the specified volume/bucket.
+   * @param omMetadataManager
+   * @param volumeName
+   * @param bucketName
+   * @param keyPath
+   * @return true - if file exist in the given path, else false.
+   * @throws IOException
+   */
+  default OMDirectoryResult verifyFilesInPath(
+      @Nonnull OMMetadataManager omMetadataManager, @Nonnull String volumeName,
+      @Nonnull String bucketName, @Nonnull String keyName,
+      @Nonnull Path keyPath) throws IOException {
+
+    String fileNameFromDetails = omMetadataManager.getOzoneKey(volumeName,
+        bucketName, keyName);
+    String dirNameFromDetails = omMetadataManager.getOzoneDirKey(volumeName,
+        bucketName, keyName);
+
+    while (keyPath != null) {
+      String pathName = keyPath.toString();
+
+      String dbKeyName = omMetadataManager.getOzoneKey(volumeName,
+          bucketName, pathName);
+      String dbDirKeyName = omMetadataManager.getOzoneDirKey(volumeName,
+          bucketName, pathName);
+
+      if (omMetadataManager.getKeyTable().get(dbKeyName) != null) {
+        // Found a file in the given path.
+        // Check if this is actual file or a file in the given path
+        if (dbKeyName.equals(fileNameFromDetails)) {
+          return OMDirectoryResult.FILE_EXISTS;
+        } else {
+          return OMDirectoryResult.FILE_EXISTS_IN_GIVENPATH;
+        }
+      } else if (omMetadataManager.getKeyTable().get(dbDirKeyName) != null) {
+        // Found a directory in the given path.
+        // Check if this is actual directory or a directory in the given path
+        if (dbDirKeyName.equals(dirNameFromDetails)) {
+          return OMDirectoryResult.DIRECTORY_EXISTS;
+        } else {
+          return OMDirectoryResult.DIRECTORY_EXISTS_IN_GIVENPATH;
+        }
+      }
+      keyPath = keyPath.getParent();
+    }
+
+    // Found no files/ directories in the given path.
+    return OMDirectoryResult.NONE;
+  }
+
+  /**
+   * Return codes used by verifyFilesInPath method.
+   */
+  enum OMDirectoryResult {
+    DIRECTORY_EXISTS_IN_GIVENPATH,
 
 Review comment:
   Can you add a one-line comment to explain what each of these means. I got 
confused about the meaning of `FILE_EXISTS_IN_GIVENPATH`. I think that is new.

----------------------------------------------------------------
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]


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to