mirror of
https://github.com/nosqlbench/nosqlbench.git
synced 2025-02-25 18:55:28 -06:00
partial work for search within
This commit is contained in:
parent
9642a2b545
commit
6264b17844
@ -0,0 +1,7 @@
|
|||||||
|
package io.nosqlbench.docsys.core;
|
||||||
|
|
||||||
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
|
public class PathWalkerTest {
|
||||||
|
|
||||||
|
}
|
@ -73,7 +73,15 @@ public class NBFiles {
|
|||||||
return Optional.empty();
|
return Optional.empty();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Optional<Path> findOptionalPath(String basename, String extension, String... searchPaths) {
|
/**
|
||||||
|
* Search for the path
|
||||||
|
* @param basename Basename of path, with or without extension
|
||||||
|
* @param extension The extension of the filename
|
||||||
|
* @param searchWithin If enabled, all searchPaths are traversed, looking for a matching suffix pattern.
|
||||||
|
* @param searchPaths Additional places to look for the path suffix
|
||||||
|
* @return An optional path
|
||||||
|
*/
|
||||||
|
public static Optional<Path> findOptionalPath(String basename, String extension, boolean searchWithin, String... searchPaths) {
|
||||||
|
|
||||||
boolean needsExtension = (extension != null && !extension.isEmpty() && !basename.endsWith("." + extension));
|
boolean needsExtension = (extension != null && !extension.isEmpty() && !basename.endsWith("." + extension));
|
||||||
String filename = basename + (needsExtension ? "." + extension : "");
|
String filename = basename + (needsExtension ? "." + extension : "");
|
||||||
@ -95,6 +103,12 @@ public class NBFiles {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (searchWithin) {
|
||||||
|
throw new RuntimeException("not implemented");
|
||||||
|
// for (String searchPath : searchPaths) {
|
||||||
|
// NBPathWalker.findEndMatching(Path.of(searchPath), Path.of(filename));
|
||||||
|
// }
|
||||||
|
}
|
||||||
return Optional.empty();
|
return Optional.empty();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -0,0 +1,125 @@
|
|||||||
|
package io.nosqlbench.engine.api.util;
|
||||||
|
|
||||||
|
import org.apache.logging.log4j.LogManager;
|
||||||
|
import org.apache.logging.log4j.Logger;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.file.DirectoryStream;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.nio.file.attribute.BasicFileAttributes;
|
||||||
|
import java.nio.file.spi.FileSystemProvider;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class NBPathWalker {
|
||||||
|
private final static Logger logger = LogManager.getLogger(NBPathWalker.class);
|
||||||
|
|
||||||
|
public static void walk(Path p, PathVisitor v) {
|
||||||
|
walk(p, v, NBPathWalker.WALK_ALL);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static List<Path> findAll(Path p) {
|
||||||
|
Collect fileCollector = new Collect(true, false);
|
||||||
|
walk(p, fileCollector);
|
||||||
|
return fileCollector.get();
|
||||||
|
}
|
||||||
|
public static List<Path> findEndMatching(Path p, Path endingName) {
|
||||||
|
Collect fileCollector = new Collect(true, false);
|
||||||
|
MatchEnding ending = new MatchEnding(endingName.toString());
|
||||||
|
walk(p, fileCollector, ending);
|
||||||
|
return fileCollector.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void walk(Path p, PathVisitor v, DirectoryStream.Filter<Path> filter) {
|
||||||
|
try {
|
||||||
|
FileSystemProvider provider = p.getFileSystem().provider();
|
||||||
|
DirectoryStream<Path> paths = provider.newDirectoryStream(p, (Path r) -> true);
|
||||||
|
List<Path> pathlist = new ArrayList<>();
|
||||||
|
for (Path path : paths) {
|
||||||
|
pathlist.add(path);
|
||||||
|
}
|
||||||
|
for (Path path : pathlist) {
|
||||||
|
if (path.getFileSystem().provider().readAttributes(path, BasicFileAttributes.class).isDirectory()) {
|
||||||
|
v.preVisitDir(path);
|
||||||
|
walk(path, v, filter);
|
||||||
|
v.postVisitDir(path);
|
||||||
|
} else if (filter.accept(path)) {
|
||||||
|
v.preVisitFile(path);
|
||||||
|
v.visit(path);
|
||||||
|
v.postVisitFile(path);
|
||||||
|
|
||||||
|
} else {
|
||||||
|
logger.error("error");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public interface PathVisitor {
|
||||||
|
void visit(Path p);
|
||||||
|
|
||||||
|
default void preVisitFile(Path path) {
|
||||||
|
}
|
||||||
|
|
||||||
|
default void postVisitFile(Path path) {
|
||||||
|
}
|
||||||
|
|
||||||
|
default void preVisitDir(Path path) {
|
||||||
|
}
|
||||||
|
|
||||||
|
default void postVisitDir(Path path) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static DirectoryStream.Filter<Path> WALK_ALL = entry -> true;
|
||||||
|
|
||||||
|
public static class MatchEnding implements DirectoryStream.Filter<Path> {
|
||||||
|
private final String regex;
|
||||||
|
|
||||||
|
public MatchEnding(String pathEnd) {
|
||||||
|
this.regex = pathEnd;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean accept(Path entry) throws IOException {
|
||||||
|
return entry.toString().endsWith(regex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class Collect implements PathVisitor {
|
||||||
|
private final List<Path> listing = new ArrayList<>();
|
||||||
|
private final boolean collectFiles;
|
||||||
|
private final boolean collectDirectories;
|
||||||
|
|
||||||
|
public Collect(boolean collectFiles, boolean collectDirectories) {
|
||||||
|
|
||||||
|
this.collectFiles = collectFiles;
|
||||||
|
this.collectDirectories = collectDirectories;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Path> get() {
|
||||||
|
return listing;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void visit(Path p) {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void preVisitFile(Path path) {
|
||||||
|
if (this.collectFiles) {
|
||||||
|
listing.add(path);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void preVisitDir(Path path) {
|
||||||
|
if (this.collectDirectories) {
|
||||||
|
listing.add(path);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,21 @@
|
|||||||
|
package io.nosqlbench.engine.api.util;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
public class NBPathWalkerTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testBasicPathmatching() {
|
||||||
|
List<Path> found = NBPathWalker.findEndMatching(
|
||||||
|
Path.of("testdocs"),
|
||||||
|
Path.of("identity.yaml")
|
||||||
|
);
|
||||||
|
|
||||||
|
assertThat(found).containsExactly(Path.of("flsd"));
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,2 @@
|
|||||||
|
bindings:
|
||||||
|
i: Identity();
|
Loading…
Reference in New Issue
Block a user