Skip to content

Commit cfaaf18

Browse files
committed
fix: in memory kva path normalization
* avoid using system specific Path implementation
1 parent cb071fe commit cfaaf18

1 file changed

Lines changed: 31 additions & 53 deletions

File tree

src/main/java/org/janelia/saalfeldlab/n5/InMemoryKeyValueAccess.java

Lines changed: 31 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,18 @@ public InMemoryKeyValueAccess() {
6868
directories.add(ROOT);
6969
}
7070

71+
/**
72+
* Returns a normalized path.
73+
*
74+
* @param path
75+
* the path
76+
* @return the normalized path
77+
*/
78+
@Override
79+
public String normalize(final String path) {
80+
return N5URI.normalizeGroupPath(path);
81+
}
82+
7183
@Override
7284
public VolatileReadData createReadData(final String normalPath) {
7385

@@ -124,14 +136,12 @@ public String[] listDirectories(final String normalPath) throws N5IOException {
124136

125137
private ArrayList<String> listDirectoriesHelper(final String normalPath) {
126138

127-
Path dir = Paths.get(normalPath);
128139
final ArrayList<String> children = new ArrayList<>();
129-
for (String p : directories) {
130-
131-
Path path = Paths.get(p);
132-
Path parent = path.getParent();
133-
if (dir.equals(parent))
134-
children.add(parent.relativize(path).toString());
140+
for (String d : directories) {
141+
// the parent method removes leading slash, so add it back
142+
String parent = "/" + parent(d);
143+
if (normalPath.equals(parent))
144+
children.add(relativize(d, parent));
135145
}
136146
return children;
137147
}
@@ -142,69 +152,37 @@ public String[] list(final String normalPath) throws N5IOException {
142152
if (!isDirectory(normalPath))
143153
throw new N5IOException("Attempted to list directory that does not exist: " + normalPath);
144154

145-
final Path dir = Paths.get(normalPath);
146155
final ArrayList<String> children = new ArrayList<>();
156+
// all keys are normalized
147157
for (String p : files.keySet()) {
148-
149-
Path path = Paths.get(p);
150-
Path parent = path.getParent();
151-
if (dir.equals(parent))
152-
children.add(parent.relativize(path).toString());
158+
final String parent = parent(p);
159+
if (normalPath.equals(parent))
160+
children.add(relativize(p, parent));
153161
}
154162

155163
children.addAll(listDirectoriesHelper(normalPath));
156164
return children.toArray(new String[0]);
157165
}
158166

159-
/**
160-
* Returns a normalized path. It ensures correctness on both Unix and
161-
* Windows, otherwise {@code pathName} is treated as UNC path on Windows,
162-
* and {@code Paths.get(pathName, ...)} fails with
163-
* {@code InvalidPathException}.
164-
*
165-
* @param path
166-
* the path
167-
* @return the normalized path, without leading slash
168-
*/
169167
@Override
170-
public String normalize(final String path) {
171-
return N5URI.normalizeGroupPath(path);
172-
}
168+
public void createDirectories(final String normalPath) throws N5IOException {
173169

174-
/**
175-
* Get the parent of a path string.
176-
*
177-
* @param path
178-
* the path
179-
* @return the parent path or null if the path has no parent
180-
*/
181-
@Override
182-
public String parent(final String path) {
183-
final String removeTrailingSlash = path.replaceAll("/+$", "");
184-
return N5URI.getAsUri(removeTrailingSlash).resolve("").toString();
170+
String p = normalPath;
171+
while( !isRoot(p)) {
172+
directories.add(p);
173+
p = "/" + parent(p);
174+
}
185175
}
186176

187-
@Override
188-
public synchronized void createDirectories(final String normalPath) throws N5IOException {
189-
190-
try {
191-
String p = normalPath;
192-
p = p.replaceAll("/+$", "");
193-
while( !p.equals(ROOT)) {
194-
p = p.replaceAll("/+$", "");
195-
directories.add(p);
196-
p = parent(p);
197-
}
198-
} catch (UncheckedIOException e) {
199-
throw new N5IOException("Failed to create directories", e);
200-
}
177+
private boolean isRoot(String path) {
178+
return path.isEmpty() || path.equals(ROOT);
201179
}
202180

203181
@Override
204-
public synchronized void delete(final String normalPath) throws N5IOException {
182+
public void delete(final String normalPath) throws N5IOException {
205183

206184
// disallow deletion of root
207-
if (normalPath.equals("/") || normalPath.equals(""))
185+
if (isRoot(normalPath))
208186
return;
209187

210188
deleteHelper(normalPath, directories);

0 commit comments

Comments
 (0)