diff options
| author | Chris Lu <chris.lu@gmail.com> | 2018-12-14 09:16:21 -0800 |
|---|---|---|
| committer | Chris Lu <chris.lu@gmail.com> | 2018-12-14 09:16:21 -0800 |
| commit | 21315f709de9520ae8dc8cd7a8998802ee89af72 (patch) | |
| tree | fedfc4c3339f0414d6e8500132926f6ed21158a3 /other/java/client | |
| parent | 42b8f1145a9bca6dcd461a09fb1dd65da67f6ada (diff) | |
| download | seaweedfs-21315f709de9520ae8dc8cd7a8998802ee89af72.tar.xz seaweedfs-21315f709de9520ae8dc8cd7a8998802ee89af72.zip | |
HCFS 1.0.2
Diffstat (limited to 'other/java/client')
| -rw-r--r-- | other/java/client/pom.xml | 7 | ||||
| -rw-r--r-- | other/java/client/src/main/java/seaweedfs/client/FilerClient.java | 207 | ||||
| -rw-r--r-- | other/java/client/src/main/proto/filer.proto | 2 |
3 files changed, 214 insertions, 2 deletions
diff --git a/other/java/client/pom.xml b/other/java/client/pom.xml index 74404823b..a3c56856e 100644 --- a/other/java/client/pom.xml +++ b/other/java/client/pom.xml @@ -4,7 +4,7 @@ <groupId>com.github.chrislusf</groupId> <artifactId>seaweedfs-client</artifactId> - <version>1.0</version> + <version>1.0.2</version> <parent> <groupId>org.sonatype.oss</groupId> @@ -45,6 +45,11 @@ <artifactId>grpc-stub</artifactId> <version>${grpc.version}</version> </dependency> + <dependency> + <groupId>org.slf4j</groupId> + <artifactId>slf4j-api</artifactId> + <version>1.7.25</version> + </dependency> </dependencies> <distributionManagement> diff --git a/other/java/client/src/main/java/seaweedfs/client/FilerClient.java b/other/java/client/src/main/java/seaweedfs/client/FilerClient.java new file mode 100644 index 000000000..8414ee303 --- /dev/null +++ b/other/java/client/src/main/java/seaweedfs/client/FilerClient.java @@ -0,0 +1,207 @@ +package seaweedfs.client; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Arrays; +import java.util.List; + +public class FilerClient { + + private static final Logger LOG = LoggerFactory.getLogger(FilerClient.class); + + private FilerGrpcClient filerGrpcClient; + + public FilerClient(String host, int grpcPort) { + filerGrpcClient = new FilerGrpcClient(host, grpcPort); + } + + public FilerClient(FilerGrpcClient filerGrpcClient) { + this.filerGrpcClient = filerGrpcClient; + } + + public boolean mkdirs(String path, int mode) { + String currentUser = System.getProperty("user.name"); + return mkdirs(path, mode, 0, 0, currentUser, new String[]{}); + } + + public boolean mkdirs(String path, int mode, String userName, String[] groupNames) { + return mkdirs(path, mode, 0, 0, userName, groupNames); + } + + public boolean mkdirs(String path, int mode, int uid, int gid, String userName, String[] groupNames) { + + Path pathObject = Paths.get(path); + String parent = pathObject.getParent().toString(); + String name = pathObject.getFileName().toString(); + + if ("/".equals(parent)) { + return true; + } + + mkdirs(parent, mode, uid, gid, userName, groupNames); + + FilerProto.Entry existingEntry = lookupEntry(parent, name); + + if (existingEntry != null) { + return true; + } + + return createEntry( + parent, + newDirectoryEntry(name, mode, uid, gid, userName, groupNames).build() + ); + + } + + public boolean rm(String path, boolean isRecursive) { + + Path pathObject = Paths.get(path); + String parent = pathObject.getParent().toString(); + String name = pathObject.getFileName().toString(); + + return deleteEntry( + parent, + name, + true, + isRecursive); + } + + public boolean touch(String path, int mode) { + String currentUser = System.getProperty("user.name"); + return touch(path, mode, 0, 0, currentUser, new String[]{}); + } + + public boolean touch(String path, int mode, int uid, int gid, String userName, String[] groupNames) { + + Path pathObject = Paths.get(path); + String parent = pathObject.getParent().toString(); + String name = pathObject.getFileName().toString(); + + FilerProto.Entry entry = lookupEntry(parent, name); + if (entry == null) { + return createEntry( + parent, + newFileEntry(name, mode, uid, gid, userName, groupNames).build() + ); + } + long now = System.currentTimeMillis() / 1000L; + FilerProto.FuseAttributes.Builder attr = entry.getAttributes().toBuilder() + .setMtime(now) + .setUid(uid) + .setGid(gid) + .setUserName(userName) + .clearGroupName() + .addAllGroupName(Arrays.asList(groupNames)); + return updateEntry(parent, entry.toBuilder().setAttributes(attr).build()); + } + + public FilerProto.Entry.Builder newDirectoryEntry(String name, int mode, + int uid, int gid, String userName, String[] groupNames) { + + long now = System.currentTimeMillis() / 1000L; + + return FilerProto.Entry.newBuilder() + .setName(name) + .setIsDirectory(true) + .setAttributes(FilerProto.FuseAttributes.newBuilder() + .setMtime(now) + .setCrtime(now) + .setUid(uid) + .setGid(gid) + .setFileMode(mode | 1 << 31) + .setUserName(userName) + .clearGroupName() + .addAllGroupName(Arrays.asList(groupNames))); + } + + public FilerProto.Entry.Builder newFileEntry(String name, int mode, + int uid, int gid, String userName, String[] groupNames) { + + long now = System.currentTimeMillis() / 1000L; + + return FilerProto.Entry.newBuilder() + .setName(name) + .setIsDirectory(false) + .setAttributes(FilerProto.FuseAttributes.newBuilder() + .setMtime(now) + .setCrtime(now) + .setUid(uid) + .setGid(gid) + .setFileMode(mode) + .setUserName(userName) + .clearGroupName() + .addAllGroupName(Arrays.asList(groupNames))); + } + + public List<FilerProto.Entry> listEntries(String path) { + return listEntries(path, "", "", 100000); + } + + public List<FilerProto.Entry> listEntries(String path, String entryPrefix, String lastEntryName, int limit) { + return filerGrpcClient.getBlockingStub().listEntries(FilerProto.ListEntriesRequest.newBuilder() + .setDirectory(path) + .setPrefix(entryPrefix) + .setStartFromFileName(lastEntryName) + .setLimit(limit) + .build()).getEntriesList(); + } + + public FilerProto.Entry lookupEntry(String directory, String entryName) { + try { + return filerGrpcClient.getBlockingStub().lookupDirectoryEntry( + FilerProto.LookupDirectoryEntryRequest.newBuilder() + .setDirectory(directory) + .setName(entryName) + .build()).getEntry(); + } catch (Exception e) { + LOG.warn("lookupEntry {}/{}: {}", directory, entryName, e); + return null; + } + } + + + public boolean createEntry(String parent, FilerProto.Entry entry) { + try { + filerGrpcClient.getBlockingStub().createEntry(FilerProto.CreateEntryRequest.newBuilder() + .setDirectory(parent) + .setEntry(entry) + .build()); + } catch (Exception e) { + LOG.warn("createEntry {}/{}: {}", parent, entry.getName(), e); + return false; + } + return true; + } + + public boolean updateEntry(String parent, FilerProto.Entry entry) { + try { + filerGrpcClient.getBlockingStub().updateEntry(FilerProto.UpdateEntryRequest.newBuilder() + .setDirectory(parent) + .setEntry(entry) + .build()); + } catch (Exception e) { + LOG.warn("createEntry {}/{}: {}", parent, entry.getName(), e); + return false; + } + return true; + } + + public boolean deleteEntry(String parent, String entryName, boolean isDeleteFileChunk, boolean isRecursive) { + try { + filerGrpcClient.getBlockingStub().deleteEntry(FilerProto.DeleteEntryRequest.newBuilder() + .setDirectory(parent) + .setName(entryName) + .setIsDeleteData(isDeleteFileChunk) + .setIsRecursive(isRecursive) + .build()); + } catch (Exception e) { + LOG.warn("deleteEntry {}/{}: {}", parent, entryName, e); + return false; + } + return true; + } + +} diff --git a/other/java/client/src/main/proto/filer.proto b/other/java/client/src/main/proto/filer.proto index 124eabcd2..bb33eb48e 100644 --- a/other/java/client/src/main/proto/filer.proto +++ b/other/java/client/src/main/proto/filer.proto @@ -117,7 +117,7 @@ message UpdateEntryResponse { message DeleteEntryRequest { string directory = 1; string name = 2; - bool is_directory = 3; + // bool is_directory = 3; bool is_delete_data = 4; bool is_recursive = 5; } |
