Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove all remaining references of Guava's Files class. #7278

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING

import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableSet;
import com.google.common.io.Files;
import diskCacheV111.poolManager.PoolSelectionUnit;
import diskCacheV111.poolManager.PoolSelectionUnit.SelectionPool;
import diskCacheV111.pools.PoolV2Mode;
Expand Down Expand Up @@ -171,8 +170,8 @@ private static Collection<String> load(String excludedPoolsFile) {

Collection<String> excluded = new ArrayList<>();

try (BufferedReader fr = new BufferedReader(new FileReader(current))) {
excluded = Files.readLines(current, StandardCharsets.US_ASCII);
try (BufferedReader fr = new BufferedReader(new FileReader(current, StandardCharsets.US_ASCII))) {
excluded = fr.lines().collect(Collectors.toList());
current.delete();
} catch (FileNotFoundException e) {
LOGGER.error("Unable to reload excluded pools file: {}", e.getMessage());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
package org.dcache.services.billing.text;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.io.Files.isFile;
import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.Arrays.asList;
import static java.util.stream.Collectors.toList;
import static org.dcache.util.ByteUnit.KiB;

import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.base.Strings;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableMap.Builder;
import com.google.common.collect.Maps;
import com.google.common.collect.Ordering;
import com.google.common.collect.Sets;
import com.google.common.collect.TreeTraverser;
import com.google.common.graph.Traverser;
import com.google.common.hash.BloomFilter;
import com.google.common.hash.Funnels;
import com.google.common.io.ByteSource;
Expand Down Expand Up @@ -61,9 +59,12 @@
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.function.Predicate;
import java.util.logging.LogManager;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
import org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream;
import org.apache.commons.compress.compressors.bzip2.BZip2CompressorOutputStream;
import org.dcache.boot.LayoutBuilder;
Expand Down Expand Up @@ -102,20 +103,18 @@ public class Indexer {
* Almost identical to the file tree traverser from Guava, sorts directory entries
* lexicographically.
*/
private static final TreeTraverser<File> SORTED_FILE_TREE_TRAVERSER = new TreeTraverser<File>() {
@Override
public Iterable<File> children(File file) {
// check isDirectory() just because it may be faster than listFiles() on a non-directory
if (file.isDirectory()) {
File[] files = file.listFiles();
if (files != null) {
return Ordering.natural().sortedCopy(asList(files));
}
}

return Collections.emptyList();
}
};
private static final Traverser<File> SORTED_FILE_TREE_TRAVERSER = Traverser.forTree(
file -> {
// check isDirectory() just because it may be faster than listFiles() on a non-directory
if (file.isDirectory()) {
File[] files = file.listFiles();
if (files != null) {
return Ordering.natural().sortedCopy(asList(files));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this can be de-guavaed as well

}
}

return Collections.emptyList();
});

private final boolean isFlat;
private final File dir;
Expand All @@ -140,46 +139,48 @@ private Indexer(Args args) throws IOException, URISyntaxException, ClassNotFound
searchTerms = ImmutableList.of("");
}

FluentIterable<File> filesWithPossibleMatch =
SORTED_FILE_TREE_TRAVERSER
.preOrderTraversal(dir);
Stream<File> fileStream = StreamSupport.stream(
SORTED_FILE_TREE_TRAVERSER.depthFirstPreOrder(dir).spliterator(), false
);
if (args.hasOption("since") || args.hasOption("until")) {
LocalDate since = args.hasOption("since")
? LocalDate.parse(args.getOption("since"), CLI_DATE_FORMAT)
: LocalDate.ofEpochDay(0);
LocalDate until = args.hasOption("until")
? LocalDate.parse(args.getOption("until"), CLI_DATE_FORMAT)
: LocalDate.now().plusDays(1);
filesWithPossibleMatch =
filesWithPossibleMatch.filter(file -> isInRange(file, since, until));
fileStream = fileStream.filter(file -> isInRange(file, since, until));
}
if (searchTerms.contains("")) {
filesWithPossibleMatch =
filesWithPossibleMatch.filter(file -> isBillingFile(file));
fileStream = fileStream.filter(Indexer::isBillingFile);
} else {
filesWithPossibleMatch =
filesWithPossibleMatch.filter(isBillingFileAndMightContain(searchTerms));
fileStream = fileStream.filter(isBillingFileAndMightContain(searchTerms));
}

Iterable<File> fileIterable = fileStream.collect(toList());

if (args.hasOption("files")) {
for (File file : filesWithPossibleMatch) {
for (File file : fileIterable) {
System.out.println(file);
}
} else if (args.hasOption("yaml")) {
try (OutputWriter out = toYaml(System.out)) {
find(searchTerms, filesWithPossibleMatch, out);
find(searchTerms, fileIterable, out);
}
} else if (args.hasOption("json")) {
try (OutputWriter out = toJson(System.out)) {
find(searchTerms, filesWithPossibleMatch, out);
find(searchTerms, fileIterable, out);
}
} else {
try (OutputWriter out = toText(System.out)) {
find(searchTerms, filesWithPossibleMatch, out);
find(searchTerms, fileIterable, out);
}
}
} else if (args.hasOption("all")) {
for (File file : SORTED_FILE_TREE_TRAVERSER.preOrderTraversal(dir).filter(isFile())) {
for (File file : StreamSupport.stream(
SORTED_FILE_TREE_TRAVERSER.depthFirstPreOrder(dir).spliterator(), false
).filter(File::isFile).collect(toList())
) {
Matcher matcher = BILLING_NAME_PATTERN.matcher(file.getName());
if (matcher.matches()) {
System.out.println("Indexing " + file);
Expand Down Expand Up @@ -323,7 +324,7 @@ public void close() {
/**
* Searches for searchTerm in files and writes any matching lines to out.
*/
private static void find(final Collection<String> searchTerms, FluentIterable<File> files,
private static void find(final Collection<String> searchTerms, Iterable<File> files,
final OutputWriter out)
throws IOException {
int threads = Runtime.getRuntime().availableProcessors();
Expand Down Expand Up @@ -408,7 +409,7 @@ private static void decompress(File compressedFile) throws IOException {
new BufferedInputStream(new FileInputStream(file)))) {
Files.copy(in, file.toPath());
}
java.nio.file.Files.delete(compressedFile.toPath());
Files.delete(compressedFile.toPath());
}

private static void compress(File file) throws IOException {
Expand All @@ -419,7 +420,7 @@ private static void compress(File file) throws IOException {
try (OutputStream out = new BZip2CompressorOutputStream(bufOut)) {
Files.copy(file.toPath(), out);
}
java.nio.file.Files.delete(file.toPath());
Files.delete(file.toPath());
}

private static void help(PrintStream out) {
Expand Down Expand Up @@ -528,7 +529,12 @@ public InputStream openStream() throws IOException {
}
};
} else {
source = com.google.common.io.Files.asByteSource(file);
source = new ByteSource() {
@Override
public InputStream openStream() throws IOException {
return new FileInputStream(file);
}
};
}
return source.asCharSource(charset);
}
Expand Down Expand Up @@ -567,7 +573,7 @@ private static boolean isBillingFile(File file) {
*/
private static ImmutableMap<String, String> getBillingFormats(
ConfigurationProperties configuration) {
ImmutableMap.Builder<String, String> formats = ImmutableMap.builder();
Builder<String, String> formats = ImmutableMap.builder();
for (String name : configuration.stringPropertyNames()) {
if (name.startsWith(BILLING_TEXT_FORMAT_PREFIX)) {
formats.put(name.substring(BILLING_TEXT_FORMAT_PREFIX.length()),
Expand All @@ -581,9 +587,9 @@ private static Predicate<File> isBillingFileAndMightContain(Collection<String> t
final List<String> searchTerms = terms.stream()
.map(str -> str.endsWith("/") ? str.substring(0, str.length() - 1) : str)
.collect(toList());
return new Predicate<File>() {
return new Predicate<>() {
@Override
public boolean apply(File file) {
public boolean test(File file) {
if (!file.isFile()) {
return false;
}
Expand Down
23 changes: 18 additions & 5 deletions modules/srm-server/src/main/java/org/dcache/srm/SRM.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,11 @@ LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.common.io.Files;
import dmg.cells.nucleus.CellLifeCycleAware;
import java.io.IOException;
import java.lang.reflect.Method;
import java.net.URI;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
Expand All @@ -93,7 +93,6 @@ LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
import javax.annotation.Nonnull;
import org.dcache.commons.stats.MonitoringProxy;
import org.dcache.commons.stats.RequestCounters;
Expand Down Expand Up @@ -401,7 +400,7 @@ public void listRequest(StringBuilder sb, long requestId, boolean longformat)
public void cancelRequest(StringBuilder sb, long requestId)
throws SRMInvalidRequestException {
Job job = Job.getJob(requestId, Job.class);
if (job == null || !(job instanceof ContainerRequest)) {
if (!(job instanceof ContainerRequest)) {
sb.append("request with id ").append(requestId)
.append(" is not found\n");
return;
Expand Down Expand Up @@ -609,7 +608,7 @@ public <T extends FileRequest<?>> Iterable<T> getActiveFileRequests(Class<T> typ
public Stream<PutFileRequest> getActivePutFileRequests(URI surl)
throws DataAccessException {
String path = getPath(surl);
return StreamSupport.stream(getActiveJobs(PutFileRequest.class).spliterator(), false)
return getActiveJobs(PutFileRequest.class).stream()
.filter(r -> getPath(r.getSurl()).startsWith(path));
}

Expand Down Expand Up @@ -688,6 +687,10 @@ public void checkRemoveDirectory(URI surl) throws SRMException {

private static String getPath(URI surl) {
String path = surl.getPath();
if (path == null) {
throw new NullPointerException("path cannot be null.");
}
// Handle the query.
String query = surl.getQuery();
if (query != null) {
int i = query.indexOf(SFN_STRING);
Expand All @@ -705,7 +708,17 @@ private static String getPath(URI surl) {
* paths to an absolute path, which requires additional name space
* lookups.
*/
path = Files.simplifyPath(path);

// Trim away any whitespace. Shouldn't be the case though.
path = path.trim();
// Now we need to simplify the path
// Just return the base directory.
if (path.isEmpty() || path.isBlank() || path.equals(".")) {
return "/";
}
// Use Path.normalize() to remove trailin slashes and simplify "./" and "/../"
path = Path.of(path).normalize().toString();

if (!path.endsWith("/")) {
path = path + "/";
}
Expand Down