partial work for search within

This commit is contained in:
Jonathan Shook 2020-03-27 08:44:14 -05:00
parent 9642a2b545
commit 6264b17844
5 changed files with 170 additions and 1 deletions

View File

@ -0,0 +1,7 @@
package io.nosqlbench.docsys.core;
import static org.junit.Assert.*;
public class PathWalkerTest {
}

View File

@ -73,7 +73,15 @@ public class NBFiles {
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));
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();
}

View File

@ -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);
}
}
}
}

View File

@ -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"));
}
}

View File

@ -0,0 +1,2 @@
bindings:
i: Identity();