forked from apache/mina-sshd
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
apacheGH-643: provide interfaces for caching file attributes on paths
Remote file systems may have a need to cache file attributes on Path instances. A typical use case is iterating over all files in a directory: the directory listing returns paths, but the underlying remote listing operation may also return file attributes for each entry. This is the case in Sftp, but may also occur for other file systems, for instance a file system wrapping Amazon S3. It would be unfortunate and inefficient if iterating through the paths returned and doing something with the attributes would have to re-fetch the attributes again if they were already available. By implementing WithFileAttributes of its Paths, a file system can associate file attributes with a path instance, and client code can access them. If a file system also makes its paths implement the second interface WithFileAttributeCache, then the SftpSubsystem uses it internally to avoid making repeated remote calls to get file attributes.
- Loading branch information
Showing
8 changed files
with
128 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
sshd-sftp/src/main/java/org/apache/sshd/sftp/client/fs/WithFileAttributeCache.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* 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.client.fs; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Path; | ||
|
||
import org.apache.sshd.common.util.io.functors.IOFunction; | ||
import org.apache.sshd.sftp.client.SftpClient; | ||
|
||
/** | ||
* A mix-in interface for paths that can carry and cache file attributes of the referenced file. | ||
*/ | ||
public interface WithFileAttributeCache extends WithFileAttributes { | ||
|
||
/** | ||
* Sets the attributes. | ||
* | ||
* @param attributes {@link SftpClient.Attributes} to set | ||
*/ | ||
void setAttributes(SftpClient.Attributes attributes); | ||
|
||
/** | ||
* Performs the given operation with attribute caching. If {@code SftpClient.Attributes} are fetched by the | ||
* operation, they will be cached and subsequently these cached attributes will be re-used for this {@link SftpPath} | ||
* instance throughout the operation. Calls to {@link #withAttributeCache(IOFunction)} may be nested. The cache is | ||
* cleared at the start and at the end of the outermost invocation. | ||
* | ||
* @param <T> result type of the {@code operation} | ||
* @param operation to perform; may return {@code null} if it has no result | ||
* @return the result of the {@code operation} | ||
* @throws IOException if thrown by the {@code operation} | ||
*/ | ||
<T> T withAttributeCache(IOFunction<Path, T> operation) throws IOException; | ||
|
||
/** | ||
* Performs the given operation with attribute caching, if the given {@link Path} implements the | ||
* {@link WithFileAttributeCache} interface, otherwise simply executes the operation. | ||
* | ||
* @param <T> result type of the {@code operation} | ||
* @param path {@link Path} to operate on | ||
* @param operation to perform; may return {@code null} if it has no result | ||
* @return the result of the {@code operation} | ||
* @throws IOException if thrown by the {@code operation} | ||
* | ||
* @see #withAttributeCache(IOFunction) | ||
*/ | ||
static <T> T withAttributeCache(Path path, IOFunction<Path, T> operation) throws IOException { | ||
if (path instanceof WithFileAttributeCache) { | ||
return ((WithFileAttributeCache) path).withAttributeCache(operation); | ||
} | ||
return operation.apply(path); | ||
} | ||
|
||
} |
35 changes: 35 additions & 0 deletions
35
sshd-sftp/src/main/java/org/apache/sshd/sftp/client/fs/WithFileAttributes.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* 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.client.fs; | ||
|
||
import org.apache.sshd.sftp.client.SftpClient.Attributes; | ||
|
||
/** | ||
* A mix-in interface for paths that may have file attributes of the file referenced by the path. | ||
*/ | ||
public interface WithFileAttributes { | ||
|
||
/** | ||
* Retrieves the {@link Attributes} of this object, if it has any. | ||
* | ||
* @return the {@link Attributes} or {@code null} if there are none | ||
*/ | ||
Attributes getAttributes(); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters