<commit_before>/**
 * Copyright (c) 2017, RTE (http://www.rte-france.com)
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */
package com.powsybl.afs.local.storage;

import com.google.common.collect.ImmutableList;
import com.powsybl.afs.Folder;
import com.powsybl.afs.storage.AppStorage;
import com.powsybl.afs.storage.NodeDependency;
import com.powsybl.afs.storage.NodeGenericMetadata;
import com.powsybl.afs.storage.NodeInfo;
import com.powsybl.commons.exceptions.UncheckedUnsupportedEncodingException;
import com.powsybl.computation.ComputationManager;
import com.powsybl.math.timeseries.DoubleArrayChunk;
import com.powsybl.math.timeseries.StringArrayChunk;
import com.powsybl.math.timeseries.TimeSeriesIndex;
import com.powsybl.math.timeseries.TimeSeriesMetadata;

import java.io.*;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author <NAME> <geoffroy.jamgotchian at rte-france.com>
 */
public class LocalAppStorage implements AppStorage {

    private static final int DEFAULT_VERSION = 0;

    private final Path rootDir;

    private final String fileSystemName;

    private final List<LocalFileScanner> fileScanners;

    private final List<LocalFolderScanner> folderScanners;

    private final ComputationManager computationManager;

    private final Map<Path, LocalFile> fileCache = new HashMap<>();

    private final Map<Path, LocalFolder> folderCache = new HashMap<>();

    public LocalAppStorage(Path rootDir, String fileSystemName, List<LocalFileScanner> fileScanners,
                           List<LocalFolderScanner> folderScanners, ComputationManager computationManager) {
        this.rootDir = Objects.requireNonNull(rootDir);
        this.fileSystemName = Objects.requireNonNull(fileSystemName);
        this.fileScanners = Objects.requireNonNull(fileScanners);
        this.folderScanners = ImmutableList.<LocalFolderScanner>builder()
                .addAll(Objects.requireNonNull(folderScanners))
                .add(new DefaultLocalFolderScanner())
                .build();
        this.computationManager = Objects.requireNonNull(computationManager);
    }

    private LocalFile scanFile(Path path, boolean useCache) {
        LocalFile file = null;
        if (Files.isReadable(path)) {
            if (useCache && fileCache.containsKey(path)) {
                file = fileCache.get(path);
            } else {
                LocalFileScannerContext context = new LocalFileScannerContext(computationManager);
                for (LocalFileScanner fileScanner : fileScanners) {
                    file = fileScanner.scanFile(path, context);
                    if (file != null) {
                        break;
                    }
                }
                fileCache.put(path, file);
            }
        }
        return file;
    }

    private LocalFolder scanFolder(Path path, boolean useCache) {
        LocalFolder folder = null;
        if (Files.isReadable(path) && Files.isExecutable(path)) {
            if (useCache && folderCache.containsKey(path)) {
                folder = folderCache.get(path);
            } else {
                LocalFolderScannerContext context = new LocalFolderScannerContext(rootDir, fileSystemName, computationManager);
                for (LocalFolderScanner folderScanner : folderScanners) {
                    folder = folderScanner.scanFolder(path, context);
                    if (folder != null) {
                        break;
                    }
                }
                folderCache.put(path, folder);
            }
        }
        return folder;
    }<commit_msg>Fix local file system regression<commit_after>/**
 * Copyright (c) 2017, RTE (http://www.rte-france.com)
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */
package com.powsybl.afs.local.storage;

import com.google.common.collect.ImmutableList;
import com.powsybl.afs.Folder;
import com.powsybl.afs.storage.AppStorage;
import com.powsybl.afs.storage.NodeDependency;
import com.powsybl.afs.storage.NodeGenericMetadata;
import com.powsybl.afs.storage.NodeInfo;
import com.powsybl.commons.exceptions.UncheckedUnsupportedEncodingException;
import com.powsybl.computation.ComputationManager;
import com.powsybl.math.timeseries.DoubleArrayChunk;
import com.powsybl.math.timeseries.StringArrayChunk;
import com.powsybl.math.timeseries.TimeSeriesIndex;
import com.powsybl.math.timeseries.TimeSeriesMetadata;

import java.io.*;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author <NAME> <geoffroy.jamgotchian at rte-france.com>
 */
public class LocalAppStorage implements AppStorage {

    private static final int DEFAULT_VERSION = 0;

    private final Path rootDir;

    private final String fileSystemName;

    private final List<LocalFileScanner> fileScanners;

    private final List<LocalFolderScanner> folderScanners;

    private final ComputationManager computationManager;

    private final Map<Path, LocalFile> fileCache = new HashMap<>();

    private final Map<Path, LocalFolder> folderCache = new HashMap<>();

    public LocalAppStorage(Path rootDir, String fileSystemName, List<LocalFileScanner> fileScanners,
                           List<LocalFolderScanner> folderScanners, ComputationManager computationManager) {
        this.rootDir = Objects.requireNonNull(rootDir);
        this.fileSystemName = Objects.requireNonNull(fileSystemName);
        this.fileScanners = Objects.requireNonNull(fileScanners);
        this.folderScanners = ImmutableList.<LocalFolderScanner>builder()
                .addAll(Objects.requireNonNull(folderScanners))
                .add(new DefaultLocalFolderScanner())
                .build();
        this.computationManager = Objects.requireNonNull(computationManager);
    }

    private LocalFile scanFile(Path path, boolean useCache) {
        LocalFile file = null;
        if (Files.isReadable(path)) {
            if (useCache && fileCache.containsKey(path)) {
                file = fileCache.get(path);
            } else {
                LocalFileScannerContext context = new LocalFileScannerContext(computationManager);
                for (LocalFileScanner fileScanner : fileScanners) {
                    file = fileScanner.scanFile(path, context);
                    if (file != null) {
                        break;
                    }
                }
                fileCache.put(path, file);
            }
        }
        return file;
    }

    private LocalFolder scanFolder(Path path, boolean useCache) {
        LocalFolder folder = null;
        if (Files.isReadable(path)) {
            if (useCache && folderCache.containsKey(path)) {
                folder = folderCache.get(path);
            } else {
                LocalFolderScannerContext context = new LocalFolderScannerContext(rootDir, fileSystemName, computationManager);
                for (LocalFolderScanner folderScanner : folderScanners) {
                    folder = folderScanner.scanFolder(path, context);
                    if (folder != null) {
                        break;
                    }
                }
                folderCache.put(path, folder);
            }
        }
        return folder;
    }
{'fraction_non_alphanumeric': 0.09044908285895004, 'fraction_numerical': 0.0022770398481973433, 'mean_word_length': 2.694392523364486, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3959460', 'n_tokens_mistral': 2189, 'n_tokens_neox': 2128, 'n_words': 471}