You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@mina.apache.org by GitBox <gi...@apache.org> on 2021/10/27 15:29:23 UTC

[GitHub] [mina-sshd] lgoldstein commented on a change in pull request #206: [SSHD-1217] Performance problem listing directory via SftpFileSystem

lgoldstein commented on a change in pull request #206:
URL: https://github.com/apache/mina-sshd/pull/206#discussion_r737584656



##########
File path: sshd-sftp/src/main/java/org/apache/sshd/sftp/server/RemoteDirectoryHandle.java
##########
@@ -0,0 +1,81 @@
+/*
+ * 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
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.sshd.sftp.server;
+
+import java.io.Closeable;
+import java.io.IOException;
+import java.util.Iterator;
+import java.util.NoSuchElementException;
+
+import org.apache.sshd.sftp.client.SftpClient;
+import org.apache.sshd.sftp.client.SftpClient.DirEntry;
+import org.apache.sshd.sftp.client.fs.SftpPath;
+import org.apache.sshd.sftp.client.impl.SftpIterableDirEntry;
+
+public class RemoteDirectoryHandle extends Handle implements Iterator<SftpClient.DirEntry> {
+
+    private final SftpIterableDirEntry entries;
+
+    private Iterator<SftpClient.DirEntry> iterator;
+
+    private boolean atStart = true;
+
+    private boolean done;
+
+    protected RemoteDirectoryHandle(SftpSubsystem subsystem, SftpPath file, String handle) throws IOException {
+        super(subsystem, file, handle);
+        entries = new SftpIterableDirEntry(file.getFileSystem().getClient(), file.toString());
+    }
+
+    public boolean atStart() {

Review comment:
       I recommend using `isAtStart()` to conform with Java beans conventions (even though this is not a bean...)

##########
File path: sshd-sftp/src/main/java/org/apache/sshd/sftp/server/RemoteDirectoryHandle.java
##########
@@ -0,0 +1,81 @@
+/*
+ * 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
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.sshd.sftp.server;
+
+import java.io.Closeable;
+import java.io.IOException;
+import java.util.Iterator;
+import java.util.NoSuchElementException;
+
+import org.apache.sshd.sftp.client.SftpClient;
+import org.apache.sshd.sftp.client.SftpClient.DirEntry;
+import org.apache.sshd.sftp.client.fs.SftpPath;
+import org.apache.sshd.sftp.client.impl.SftpIterableDirEntry;
+
+public class RemoteDirectoryHandle extends Handle implements Iterator<SftpClient.DirEntry> {
+
+    private final SftpIterableDirEntry entries;
+
+    private Iterator<SftpClient.DirEntry> iterator;
+
+    private boolean atStart = true;
+
+    private boolean done;
+
+    protected RemoteDirectoryHandle(SftpSubsystem subsystem, SftpPath file, String handle) throws IOException {
+        super(subsystem, file, handle);
+        entries = new SftpIterableDirEntry(file.getFileSystem().getClient(), file.toString());
+    }
+
+    public boolean atStart() {
+        return atStart;
+    }
+
+    @Override
+    public boolean hasNext() {
+        if (done) {
+            return false;
+        }
+        if (iterator == null) {
+            iterator = entries.iterator();
+        }
+        done = !iterator.hasNext();
+        return !done;
+    }
+
+    @Override
+    public DirEntry next() {
+        if (!hasNext()) {
+            throw new NoSuchElementException();

Review comment:
       Recommend adding some informative text to the exception - e.g., `"No more entries to iterate for " + getPath()` so that if it occurs the user will know what directory was being iterated

##########
File path: sshd-sftp/src/main/java/org/apache/sshd/sftp/server/RemoteDirectoryHandle.java
##########
@@ -0,0 +1,81 @@
+/*
+ * 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
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.sshd.sftp.server;
+
+import java.io.Closeable;
+import java.io.IOException;
+import java.util.Iterator;
+import java.util.NoSuchElementException;
+
+import org.apache.sshd.sftp.client.SftpClient;
+import org.apache.sshd.sftp.client.SftpClient.DirEntry;
+import org.apache.sshd.sftp.client.fs.SftpPath;
+import org.apache.sshd.sftp.client.impl.SftpIterableDirEntry;
+
+public class RemoteDirectoryHandle extends Handle implements Iterator<SftpClient.DirEntry> {
+
+    private final SftpIterableDirEntry entries;
+
+    private Iterator<SftpClient.DirEntry> iterator;
+
+    private boolean atStart = true;
+
+    private boolean done;
+
+    protected RemoteDirectoryHandle(SftpSubsystem subsystem, SftpPath file, String handle) throws IOException {
+        super(subsystem, file, handle);
+        entries = new SftpIterableDirEntry(file.getFileSystem().getClient(), file.toString());
+    }
+
+    public boolean atStart() {
+        return atStart;
+    }
+
+    @Override
+    public boolean hasNext() {
+        if (done) {
+            return false;
+        }
+        if (iterator == null) {
+            iterator = entries.iterator();
+        }
+        done = !iterator.hasNext();
+        return !done;
+    }
+
+    @Override
+    public DirEntry next() {
+        if (!hasNext()) {
+            throw new NoSuchElementException();
+        }
+        atStart = false;
+        return iterator.next();
+    }
+
+    @Override
+    public void close() throws IOException {
+        super.close();

Review comment:
       What if `super.close()` throws an exception ? the code after it will not be executed. Perhaps better:
   ```java
   try {
       super.close();
   } finally {
       ... the rest of the code ...
   }
   ```

##########
File path: sshd-sftp/src/main/java/org/apache/sshd/sftp/server/SftpEventListener.java
##########
@@ -155,6 +155,21 @@ default void readingEntries(
         // ignored
     }
 
+    /**
+     * About to read entries from a remote directory - <B>Note:</B> might not be the 1st time it is called for the
+     * directory in case several iterations are required in order to go through all the entries in the directory
+     *
+     * @param  session      The {@link ServerSession} through which the request was handled
+     * @param  remoteHandle The (opaque) assigned handle for the directory
+     * @param  localHandle  The associated {@link RemoteDirectoryHandle}
+     * @throws IOException  If failed to handle the call
+     * @see                 #readEntries(ServerSession, String, RemoteDirectoryHandle, Map) readEntries
+     */
+    default void readingEntries(ServerSession session, String remoteHandle, RemoteDirectoryHandle localHandle)

Review comment:
       I think that overloading the method is not a good idea - let's rename this `readingRemoteEntries`

##########
File path: sshd-sftp/src/main/java/org/apache/sshd/sftp/server/SftpEventListener.java
##########
@@ -172,6 +187,24 @@ default void readEntries(
         // ignored
     }
 
+    /**
+     * Result of reading entries from a remote directory - <B>Note:</B> it may be a <U>partial</U> result if the
+     * directory contains more entries than can be accommodated in the response
+     *
+     * @param  session      The {@link ServerSession} through which the request was handled
+     * @param  remoteHandle The (opaque) assigned handle for the directory
+     * @param  localHandle  The associated {@link RemoteDirectoryHandle}
+     * @param  entries      A {@link Map} of the listed entries - key = short name, value = {@link Path} of the
+     *                      sub-entry
+     * @throws IOException  If failed to handle the call
+     */
+    default void readEntries(

Review comment:
       I think that overloading the method is not a good idea - let's rename this `readRemoteEntries`




-- 
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: dev-unsubscribe@mina.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@mina.apache.org
For additional commands, e-mail: dev-help@mina.apache.org