Skip to content
Permalink
Browse files
Merge branch 'gerrit/stabilization-5949a1cb71'
Change-Id: I46c5d89fc13ce9b8f3aec55ece7b5281920c8759
  • Loading branch information
mblow committed Feb 1, 2022
2 parents fa0ddb6 + 594eb45 commit 4ae6e5f3059297847fba8c98dcc47836c763d420
Showing 2 changed files with 45 additions and 39 deletions.
@@ -57,7 +57,6 @@
import org.apache.asterix.common.utils.StorageConstants;
import org.apache.asterix.common.utils.StoragePathUtil;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.filefilter.IOFileFilter;
import org.apache.hyracks.api.exceptions.HyracksDataException;
import org.apache.hyracks.api.io.FileReference;
import org.apache.hyracks.api.io.IIOManager;
@@ -93,41 +92,10 @@ public class PersistentLocalResourceRepository implements ILocalResourceReposito
private static final FilenameFilter MASK_FILES_FILTER =
(dir, name) -> name.startsWith(StorageConstants.MASK_FILE_PREFIX);
private static final int MAX_CACHED_RESOURCES = 1000;
private static final IOFileFilter METADATA_FILES_FILTER = new IOFileFilter() {
@Override
public boolean accept(File file) {
return file.getName().equals(StorageConstants.METADATA_FILE_NAME);
}

@Override
public boolean accept(File dir, String name) {
return false;
}
};

private static final IOFileFilter METADATA_MASK_FILES_FILTER = new IOFileFilter() {
@Override
public boolean accept(File file) {
return file.getName().equals(METADATA_FILE_MASK_NAME);
}

@Override
public boolean accept(File dir, String name) {
return false;
}
};

private static final IOFileFilter ALL_DIR_FILTER = new IOFileFilter() {
@Override
public boolean accept(File file) {
return true;
}

@Override
public boolean accept(File dir, String name) {
return true;
}
};
private static final FilenameFilter METADATA_FILES_FILTER =
(dir, name) -> name.equals(StorageConstants.METADATA_FILE_NAME);
private static final FilenameFilter METADATA_MASK_FILES_FILTER =
(dir, name) -> name.equals(METADATA_FILE_MASK_NAME);

// Finals
private final IIOManager ioManager;
@@ -257,7 +225,10 @@ public synchronized Map<Long, LocalResource> getResources(Predicate<LocalResourc
throws HyracksDataException {
Map<Long, LocalResource> resourcesMap = new HashMap<>();
for (Path root : storageRoots) {
final Collection<File> files = FileUtils.listFiles(root.toFile(), METADATA_FILES_FILTER, ALL_DIR_FILTER);
if (!Files.exists(root) || !Files.isDirectory(root)) {
continue;
}
final Collection<File> files = IoUtil.getMatchingFiles(root, METADATA_FILES_FILTER);
try {
for (File file : files) {
final LocalResource localResource = readLocalResource(file);
@@ -462,8 +433,7 @@ public List<ResourceStorageStats> getStorageStats() throws HyracksDataException

public void deleteCorruptedResources() throws HyracksDataException {
for (Path root : storageRoots) {
final Collection<File> metadataMaskFiles =
FileUtils.listFiles(root.toFile(), METADATA_MASK_FILES_FILTER, ALL_DIR_FILTER);
final Collection<File> metadataMaskFiles = IoUtil.getMatchingFiles(root, METADATA_MASK_FILES_FILTER);
for (File metadataMaskFile : metadataMaskFiles) {
final File resourceFile = new File(metadataMaskFile.getParent(), METADATA_FILE_NAME);
if (resourceFile.exists()) {
@@ -19,11 +19,16 @@
package org.apache.hyracks.api.util;

import java.io.File;
import java.io.FileFilter;
import java.io.FileNotFoundException;
import java.io.FilenameFilter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Objects;

import org.apache.commons.io.FileUtils;
import org.apache.hyracks.api.exceptions.ErrorCode;
@@ -132,4 +137,35 @@ private static File[] verifiedListFiles(File directory) throws IOException {
return files;
}

/**
* Gets a collection of files matching {@code filter} by searching {@code root} directory and
* all of its subdirectories
*
* @param root
* @param filter
* @return a collection of matching files
*/
public static Collection<File> getMatchingFiles(Path root, FilenameFilter filter) {
if (!Files.isDirectory(root)) {
throw new IllegalArgumentException("Parameter 'root' is not a directory: " + root);
}
Objects.requireNonNull(filter);
Collection<File> files = new ArrayList<>();
FileFilter dirOrMatchingFileFilter = file -> file.isDirectory() || filter.accept(file, file.getName());
collectDirFiles(root.toFile(), dirOrMatchingFileFilter, files);
return files;
}

private static void collectDirFiles(File dir, FileFilter filter, Collection<File> files) {
File[] matchingFiles = dir.listFiles(filter);
if (matchingFiles != null) {
for (File file : matchingFiles) {
if (file.isDirectory()) {
collectDirFiles(file, filter, files);
} else {
files.add(file);
}
}
}
}
}

0 comments on commit 4ae6e5f

Please sign in to comment.