use of java.nio.file.DirectoryStream in project lucene-solr by apache.
the class ShuffleFS method newDirectoryStream.
@Override
public DirectoryStream<Path> newDirectoryStream(Path dir, Filter<? super Path> filter) throws IOException {
try (DirectoryStream<Path> stream = super.newDirectoryStream(dir, filter)) {
// read complete directory listing
List<Path> contents = new ArrayList<>();
for (Path path : stream) {
contents.add(path);
}
// sort first based only on filename
Collections.sort(contents, (path1, path2) -> path1.getFileName().toString().compareTo(path2.getFileName().toString()));
// sort based on current class seed
Collections.shuffle(contents, new Random(seed));
return new DirectoryStream<Path>() {
@Override
public Iterator<Path> iterator() {
return contents.iterator();
}
@Override
public void close() throws IOException {
}
};
}
}
use of java.nio.file.DirectoryStream in project buck by facebook.
the class DefaultAndroidDirectoryResolver method findNdkFromRepository.
private Optional<Path> findNdkFromRepository(Path repository) {
ImmutableSet<Path> repositoryContents;
try (DirectoryStream<Path> stream = Files.newDirectoryStream(repository)) {
repositoryContents = ImmutableSet.copyOf(stream);
} catch (IOException e) {
ndkErrorMessage = Optional.of("Unable to read contents of Android ndk.repository or " + "ANDROID_NDK_REPOSITORY at " + repository);
return Optional.empty();
}
VersionStringComparator versionComparator = new VersionStringComparator();
List<Pair<Path, Optional<String>>> availableNdks = repositoryContents.stream().filter(Files::isDirectory).map(p -> new Pair<>(p, findNdkVersion(p))).filter(pair -> pair.getSecond().isPresent()).sorted((o1, o2) -> versionComparator.compare(o2.getSecond().get(), o1.getSecond().get())).collect(Collectors.toList());
if (availableNdks.isEmpty()) {
ndkErrorMessage = Optional.of(repository + " does not contain any valid Android NDK. Make" + " sure to specify ANDROID_NDK_REPOSITORY or ndk.repository.");
return Optional.empty();
}
if (targetNdkVersion.isPresent()) {
if (targetNdkVersion.get().isEmpty()) {
ndkErrorMessage = Optional.of(NDK_TARGET_VERSION_IS_EMPTY_MESSAGE);
return Optional.empty();
}
Optional<Path> targetNdkPath = availableNdks.stream().filter(p -> versionsMatch(targetNdkVersion.get(), p.getSecond().get())).map(Pair::getFirst).findFirst();
if (targetNdkPath.isPresent()) {
return targetNdkPath;
}
ndkErrorMessage = Optional.of("Target NDK version " + targetNdkVersion.get() + " is not " + "available. The following versions are available: " + availableNdks.stream().map(Pair::getSecond).map(Optional::get).collect(Collectors.joining(", ")));
return Optional.empty();
}
return Optional.of(availableNdks.get(0).getFirst());
}
use of java.nio.file.DirectoryStream in project buck by facebook.
the class LogFileHandler method newCleaner.
public static DirectoryCleaner newCleaner(long maxLogsSizeBytes, int maxLogsDirectories, int minAmountOfLogsToKeep) {
DirectoryCleanerArgs cleanerArgs = DirectoryCleanerArgs.builder().setPathSelector(new DirectoryCleaner.PathSelector() {
@Override
public Iterable<Path> getCandidatesToDelete(Path rootPath) throws IOException {
List<Path> dirPaths = Lists.newArrayList();
try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(rootPath)) {
for (Path path : directoryStream) {
Matcher matcher = DIR_PATTERN.matcher(path.getFileName().toString());
if (matcher.matches()) {
dirPaths.add(path);
}
}
}
return dirPaths;
}
@Override
public int comparePaths(DirectoryCleaner.PathStats path1, DirectoryCleaner.PathStats path2) {
return Long.compare(path1.getCreationMillis(), path2.getCreationMillis());
}
}).setMaxTotalSizeBytes(maxLogsSizeBytes).setMaxPathCount(maxLogsDirectories).setMinAmountOfEntriesToKeep(minAmountOfLogsToKeep).build();
DirectoryCleaner cleaner = new DirectoryCleaner(cleanerArgs);
return cleaner;
}
use of java.nio.file.DirectoryStream in project presto by prestodb.
the class DataLocation method files.
public List<File> files() {
checkState(location.exists(), "location %s doesn't exist", location);
if (!pattern.isPresent()) {
return ImmutableList.of(location);
}
checkState(location.isDirectory(), "location %s is not a directory", location);
try (DirectoryStream<Path> paths = newDirectoryStream(location.toPath(), pattern.get())) {
ImmutableList.Builder<File> builder = ImmutableList.builder();
for (Path path : paths) {
builder.add(path.toFile());
}
List<File> files = builder.build();
if (files.isEmpty()) {
throw new PrestoException(LOCAL_FILE_NO_FILES, "No matching files found in directory: " + location);
}
return files.stream().sorted((o1, o2) -> Long.compare(o2.lastModified(), o1.lastModified())).collect(Collectors.toList());
} catch (IOException e) {
throw new PrestoException(LOCAL_FILE_FILESYSTEM_ERROR, "Error listing files in directory: " + location, e);
}
}
use of java.nio.file.DirectoryStream in project opennms by OpenNMS.
the class GraphMLEdgeStatusProvider method getStatusForEdges.
@Override
public Map<EdgeRef, Status> getStatusForEdges(EdgeProvider edgeProvider, Collection<EdgeRef> edges, Criteria[] criteria) {
final List<StatusScript> scripts = Lists.newArrayList();
try (final DirectoryStream<Path> stream = Files.newDirectoryStream(getScriptPath())) {
for (final Path path : stream) {
// ignore readme
if (".readme".equals(path.getFileName().toString())) {
LOG.debug("Skipping .readme");
continue;
}
final String extension = FilenameUtils.getExtension(path.toString());
final ScriptEngine scriptEngine = this.scriptEngineManager.getEngineByExtension(extension);
if (scriptEngine == null) {
LOG.warn("No script engine found for extension '{}'", extension);
continue;
}
LOG.debug("Found script: path={}, extension={}, engine={}", path, extension, scriptEngine);
try (final Stream<String> lines = Files.lines(path, Charset.defaultCharset())) {
final String source = lines.collect(Collectors.joining("\n"));
scripts.add(new StatusScript(scriptEngine, source));
}
}
} catch (final IOException e) {
LOG.error("Failed to walk template directory: {}", getScriptPath());
return Collections.emptyMap();
}
return serviceAccessor.getTransactionOperations().execute(transactionStatus -> edges.stream().filter(eachEdge -> eachEdge instanceof GraphMLEdge).map(edge -> (GraphMLEdge) edge).map(edge -> new HashMap.SimpleEntry<>(edge, computeEdgeStatus(scripts, edge))).filter(e -> e.getValue() != null).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)));
}
Aggregations