jojochuang commented on code in PR #8833:
URL: https://github.com/apache/ozone/pull/8833#discussion_r2250183348


##########
hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/endpoint/BucketEndpoint.java:
##########
@@ -225,61 +225,63 @@ public Response get(
     }
     String lastKey = null;
     int count = 0;
-    while (ozoneKeyIterator != null && ozoneKeyIterator.hasNext()) {
-      OzoneKey next = ozoneKeyIterator.next();
-      if (bucket != null && bucket.getBucketLayout().isFileSystemOptimized() &&
-          StringUtils.isNotEmpty(prefix) &&
-          !next.getName().startsWith(prefix)) {
-        // prefix has delimiter but key don't have
-        // example prefix: dir1/ key: dir123
-        continue;
-      }
-      if (startAfter != null && count == 0 && Objects.equals(startAfter, 
next.getName())) {
-        continue;
-      }
-      String relativeKeyName = next.getName().substring(prefix.length());
-
-      int depth = StringUtils.countMatches(relativeKeyName, delimiter);
-      if (!StringUtils.isEmpty(delimiter)) {
-        if (depth > 0) {
-          // means key has multiple delimiters in its value.
-          // ex: dir/dir1/dir2, where delimiter is "/" and prefix is dir/
-          String dirName = relativeKeyName.substring(0, relativeKeyName
-              .indexOf(delimiter));
-          if (!dirName.equals(prevDir)) {
-            response.addPrefix(EncodingTypeObject.createNullable(
-                prefix + dirName + delimiter, encodingType));
-            prevDir = dirName;
+    if (maxKeys > 0) {
+      while (ozoneKeyIterator != null && ozoneKeyIterator.hasNext()) {
+        OzoneKey next = ozoneKeyIterator.next();
+        if (bucket != null && bucket.getBucketLayout().isFileSystemOptimized() 
&&
+            StringUtils.isNotEmpty(prefix) &&
+            !next.getName().startsWith(prefix)) {
+          // prefix has delimiter but key don't have
+          // example prefix: dir1/ key: dir123
+          continue;
+        }
+        if (startAfter != null && count == 0 && Objects.equals(startAfter, 
next.getName())) {
+          continue;
+        }
+        String relativeKeyName = next.getName().substring(prefix.length());
+
+        int depth = StringUtils.countMatches(relativeKeyName, delimiter);
+        if (!StringUtils.isEmpty(delimiter)) {
+          if (depth > 0) {
+            // means key has multiple delimiters in its value.
+            // ex: dir/dir1/dir2, where delimiter is "/" and prefix is dir/
+            String dirName = relativeKeyName.substring(0, relativeKeyName
+                .indexOf(delimiter));
+            if (!dirName.equals(prevDir)) {
+              response.addPrefix(EncodingTypeObject.createNullable(
+                  prefix + dirName + delimiter, encodingType));
+              prevDir = dirName;
+              count++;
+            }
+          } else if (relativeKeyName.endsWith(delimiter)) {
+            // means or key is same as prefix with delimiter at end and ends 
with
+            // delimiter. ex: dir/, where prefix is dir and delimiter is /
+            response.addPrefix(
+                EncodingTypeObject.createNullable(relativeKeyName, 
encodingType));
+            count++;
+          } else {
+            // means our key is matched with prefix if prefix is given and it
+            // does not have any common prefix.
+            addKey(response, next);
             count++;
           }
-        } else if (relativeKeyName.endsWith(delimiter)) {
-          // means or key is same as prefix with delimiter at end and ends with
-          // delimiter. ex: dir/, where prefix is dir and delimiter is /
-          response.addPrefix(
-              EncodingTypeObject.createNullable(relativeKeyName, 
encodingType));
-          count++;
         } else {
-          // means our key is matched with prefix if prefix is given and it
-          // does not have any common prefix.
           addKey(response, next);
           count++;
         }
-      } else {
-        addKey(response, next);
-        count++;
-      }
 
-      if (count == maxKeys) {
-        lastKey = next.getName();
-        break;
+        if (count == maxKeys) {
+          lastKey = next.getName();
+          break;
+        }
       }
     }
 
     response.setKeyCount(count);
 
     if (count < maxKeys) {
       response.setTruncated(false);
-    } else if (ozoneKeyIterator.hasNext()) {
+    } else if (ozoneKeyIterator.hasNext() && lastKey != null) {

Review Comment:
   ozoneKeyIterator could be null and throw NPE here.
   But that's an existing problem. Suggest to move to a new issue.



##########
hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/endpoint/BucketEndpoint.java:
##########
@@ -225,61 +225,63 @@ public Response get(
     }
     String lastKey = null;
     int count = 0;
-    while (ozoneKeyIterator != null && ozoneKeyIterator.hasNext()) {
-      OzoneKey next = ozoneKeyIterator.next();
-      if (bucket != null && bucket.getBucketLayout().isFileSystemOptimized() &&
-          StringUtils.isNotEmpty(prefix) &&
-          !next.getName().startsWith(prefix)) {
-        // prefix has delimiter but key don't have
-        // example prefix: dir1/ key: dir123
-        continue;
-      }
-      if (startAfter != null && count == 0 && Objects.equals(startAfter, 
next.getName())) {
-        continue;
-      }
-      String relativeKeyName = next.getName().substring(prefix.length());
-
-      int depth = StringUtils.countMatches(relativeKeyName, delimiter);
-      if (!StringUtils.isEmpty(delimiter)) {
-        if (depth > 0) {
-          // means key has multiple delimiters in its value.
-          // ex: dir/dir1/dir2, where delimiter is "/" and prefix is dir/
-          String dirName = relativeKeyName.substring(0, relativeKeyName
-              .indexOf(delimiter));
-          if (!dirName.equals(prevDir)) {
-            response.addPrefix(EncodingTypeObject.createNullable(
-                prefix + dirName + delimiter, encodingType));
-            prevDir = dirName;
+    if (maxKeys > 0) {
+      while (ozoneKeyIterator != null && ozoneKeyIterator.hasNext()) {
+        OzoneKey next = ozoneKeyIterator.next();
+        if (bucket != null && bucket.getBucketLayout().isFileSystemOptimized() 
&&

Review Comment:
   bucket is always non-null here.



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

To unsubscribe, e-mail: [email protected]

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


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

Reply via email to