-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
BaseUnderFileSystem.java
126 lines (109 loc) · 3.9 KB
/
BaseUnderFileSystem.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
* The Alluxio Open Foundation licenses this work under the Apache License, version 2.0
* (the "License"). You may not use this work except in compliance with the License, which is
* available at www.apache.org/licenses/LICENSE-2.0
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied, as more fully set forth in the License.
*
* See the NOTICE file distributed with this work for information regarding copyright ownership.
*/
package alluxio.underfs;
import alluxio.AlluxioURI;
import alluxio.underfs.options.CreateOptions;
import alluxio.underfs.options.DeleteOptions;
import alluxio.underfs.options.MkdirsOptions;
import alluxio.util.io.PathUtils;
import com.google.common.base.Preconditions;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import javax.annotation.concurrent.ThreadSafe;
/**
* A base abstract {@link UnderFileSystem}.
*/
@ThreadSafe
public abstract class BaseUnderFileSystem implements UnderFileSystem {
/** The UFS {@link AlluxioURI} used to create this {@link BaseUnderFileSystem}. */
protected final AlluxioURI mUri;
/** A map of property names to values. */
protected HashMap<String, String> mProperties = new HashMap<>();
/**
* Constructs an {@link BaseUnderFileSystem}.
*
* @param uri the {@link AlluxioURI} used to create this ufs
*/
protected BaseUnderFileSystem(AlluxioURI uri) {
Preconditions.checkNotNull(uri);
mUri = uri;
}
@Override
public void configureProperties() throws IOException {
// Default implementation does not update any properties.
}
@Override
public OutputStream create(String path) throws IOException {
return create(path, CreateOptions.defaults());
}
@Override
public OutputStream create(String path, CreateOptions options) throws IOException {
return new AtomicFileOutputStream(path, options, this);
}
@Override
public boolean deleteDirectory(String path) throws IOException {
return deleteDirectory(path, DeleteOptions.defaults());
}
@Override
public Map<String, String> getProperties() {
return Collections.unmodifiableMap(mProperties);
}
@Override
public String[] listRecursive(String path) throws IOException {
// Clean the path by creating a URI and turning it back to a string
AlluxioURI uri = new AlluxioURI(path);
path = uri.toString();
List<String> returnPaths = new ArrayList<>();
Queue<String> pathsToProcess = new ArrayDeque<>();
// We call list initially, so we can return null if the path doesn't denote a directory
UnderFileStatus[] subpaths = list(path);
if (subpaths == null) {
return null;
} else {
for (UnderFileStatus subp : subpaths) {
pathsToProcess.add(PathUtils.concatPath(path, subp.getName()));
}
}
while (!pathsToProcess.isEmpty()) {
String p = pathsToProcess.remove();
returnPaths.add(p.substring(path.length() + 1));
// Add all of its subpaths
subpaths = list(p);
if (subpaths != null) {
for (UnderFileStatus subp : subpaths) {
pathsToProcess.add(PathUtils.concatPath(p, subp.getName()));
}
}
}
return returnPaths.toArray(new String[returnPaths.size()]);
}
@Override
public boolean mkdirs(String path) throws IOException {
return mkdirs(path, MkdirsOptions.defaults());
}
@Override
public AlluxioURI resolveUri(AlluxioURI ufsBaseUri, String alluxioPath) {
return new AlluxioURI(ufsBaseUri.getScheme(), ufsBaseUri.getAuthority(),
PathUtils.concatPath(ufsBaseUri.getPath(), alluxioPath), ufsBaseUri.getQueryMap());
}
@Override
public void setProperties(Map<String, String> properties) {
mProperties.clear();
mProperties.putAll(properties);
}
}