Search in sources :

Example 6 with DirectoryStream

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 {
            }
        };
    }
}
Also used : Path(java.nio.file.Path) Random(java.util.Random) ArrayList(java.util.ArrayList) DirectoryStream(java.nio.file.DirectoryStream)

Example 7 with DirectoryStream

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());
}
Also used : Path(java.nio.file.Path) Charsets(com.google.common.base.Charsets) ImmutableSet(com.google.common.collect.ImmutableSet) Properties(java.util.Properties) ImmutableMap(com.google.common.collect.ImmutableMap) Files(java.nio.file.Files) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) HumanReadableException(com.facebook.buck.util.HumanReadableException) FileSystem(java.nio.file.FileSystem) Collectors(java.util.stream.Collectors) File(java.io.File) Objects(java.util.Objects) Strings(com.google.common.base.Strings) DirectoryStream(java.nio.file.DirectoryStream) List(java.util.List) Escaper(com.facebook.buck.util.Escaper) Optional(java.util.Optional) Pair(com.facebook.buck.model.Pair) VisibleForTesting(com.google.common.annotations.VisibleForTesting) BufferedReader(java.io.BufferedReader) VersionStringComparator(com.facebook.buck.util.VersionStringComparator) Path(java.nio.file.Path) Optional(java.util.Optional) VersionStringComparator(com.facebook.buck.util.VersionStringComparator) IOException(java.io.IOException) Files(java.nio.file.Files) Pair(com.facebook.buck.model.Pair)

Example 8 with DirectoryStream

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;
}
Also used : Path(java.nio.file.Path) Matcher(java.util.regex.Matcher) DirectoryCleanerArgs(com.facebook.buck.util.DirectoryCleanerArgs) DirectoryStream(java.nio.file.DirectoryStream) DirectoryCleaner(com.facebook.buck.util.DirectoryCleaner) List(java.util.List) IOException(java.io.IOException)

Example 9 with DirectoryStream

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);
    }
}
Also used : Path(java.nio.file.Path) JsonProperty(com.fasterxml.jackson.annotation.JsonProperty) IOException(java.io.IOException) PrestoException(com.facebook.presto.spi.PrestoException) Collectors(java.util.stream.Collectors) File(java.io.File) Preconditions.checkState(com.google.common.base.Preconditions.checkState) DirectoryStream(java.nio.file.DirectoryStream) List(java.util.List) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) ImmutableList(com.google.common.collect.ImmutableList) Objects.requireNonNull(java.util.Objects.requireNonNull) JsonCreator(com.fasterxml.jackson.annotation.JsonCreator) Optional(java.util.Optional) Files.newDirectoryStream(java.nio.file.Files.newDirectoryStream) Path(java.nio.file.Path) LOCAL_FILE_NO_FILES(com.facebook.presto.localfile.LocalFileErrorCode.LOCAL_FILE_NO_FILES) LOCAL_FILE_FILESYSTEM_ERROR(com.facebook.presto.localfile.LocalFileErrorCode.LOCAL_FILE_FILESYSTEM_ERROR) ImmutableList(com.google.common.collect.ImmutableList) PrestoException(com.facebook.presto.spi.PrestoException) IOException(java.io.IOException) File(java.io.File)

Example 10 with DirectoryStream

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)));
}
Also used : Path(java.nio.file.Path) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) EdgeRef(org.opennms.features.topology.api.topo.EdgeRef) EdgeStatusProvider(org.opennms.features.topology.api.topo.EdgeStatusProvider) SimpleScriptContext(javax.script.SimpleScriptContext) AbstractVertex(org.opennms.features.topology.api.topo.AbstractVertex) EdgeProvider(org.opennms.features.topology.api.topo.EdgeProvider) DirectoryStream(java.nio.file.DirectoryStream) Lists(com.google.common.collect.Lists) Charset(java.nio.charset.Charset) Map(java.util.Map) Path(java.nio.file.Path) GraphMLServiceAccessor(org.opennms.features.topology.plugins.topo.graphml.internal.GraphMLServiceAccessor) OnmsNode(org.opennms.netmgt.model.OnmsNode) ScriptException(javax.script.ScriptException) Compilable(javax.script.Compilable) MeasurementsWrapper(org.opennms.features.topology.api.info.MeasurementsWrapper) Logger(org.slf4j.Logger) Files(java.nio.file.Files) StringWriter(java.io.StringWriter) Collection(java.util.Collection) ScriptEngineManager(javax.script.ScriptEngineManager) IOException(java.io.IOException) SimpleConnector(org.opennms.features.topology.api.topo.SimpleConnector) Collectors(java.util.stream.Collectors) ScriptContext(javax.script.ScriptContext) Objects(java.util.Objects) SimpleBindings(javax.script.SimpleBindings) List(java.util.List) Stream(java.util.stream.Stream) Paths(java.nio.file.Paths) Criteria(org.opennms.features.topology.api.topo.Criteria) CompiledScript(javax.script.CompiledScript) ScriptEngine(javax.script.ScriptEngine) Optional(java.util.Optional) Status(org.opennms.features.topology.api.topo.Status) Collections(java.util.Collections) FilenameUtils(org.apache.commons.io.FilenameUtils) IOException(java.io.IOException) ScriptEngine(javax.script.ScriptEngine)

Aggregations

DirectoryStream (java.nio.file.DirectoryStream)15 Path (java.nio.file.Path)15 IOException (java.io.IOException)7 List (java.util.List)5 File (java.io.File)4 ArrayList (java.util.ArrayList)4 Files (java.nio.file.Files)3 Optional (java.util.Optional)3 Collectors (java.util.stream.Collectors)3 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 FileInputStream (java.io.FileInputStream)2 Paths (java.nio.file.Paths)2 Collections (java.util.Collections)2 Objects (java.util.Objects)2 Properties (java.util.Properties)2 Logger (org.slf4j.Logger)2 Pair (com.facebook.buck.model.Pair)1 DirectoryCleaner (com.facebook.buck.util.DirectoryCleaner)1 DirectoryCleanerArgs (com.facebook.buck.util.DirectoryCleanerArgs)1 Escaper (com.facebook.buck.util.Escaper)1