Merge pull request #960 from andrewgaul/filesystem-blobkeys-container

Ensure that callers get a well-behaving Set
This commit is contained in:
Adrian Cole 2012-11-05 11:26:59 -08:00
commit 1ff91e508d
1 changed files with 12 additions and 13 deletions

View File

@ -22,7 +22,6 @@ import static com.google.common.base.Preconditions.checkNotNull;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.HashSet;
import java.util.Set; import java.util.Set;
import javax.annotation.Resource; import javax.annotation.Resource;
@ -30,8 +29,10 @@ import javax.inject.Inject;
import javax.inject.Named; import javax.inject.Named;
import javax.inject.Provider; import javax.inject.Provider;
import com.google.common.base.Function;
import com.google.common.base.Throwables; import com.google.common.base.Throwables;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import com.google.common.collect.Sets;
import com.google.common.io.ByteStreams; import com.google.common.io.ByteStreams;
import com.google.common.io.Files; import com.google.common.io.Files;
@ -158,22 +159,19 @@ public class FilesystemStorageStrategyImpl implements LocalStorageStrategy {
filesystemContainerNameValidator.validate(container); filesystemContainerNameValidator.validate(container);
// check if container exists // check if container exists
// TODO maybe an error is more appropriate // TODO maybe an error is more appropriate
Set<String> blobNames = Sets.newHashSet();
if (!containerExists(container)) { if (!containerExists(container)) {
return new HashSet<String>(); return blobNames;
} }
File containerFile = openFolder(container); File containerFile = openFolder(container);
final int containerPathLength = containerFile.getAbsolutePath().length() + 1; final int containerPathLength = containerFile.getAbsolutePath().length() + 1;
Set<String> blobNames = new HashSet<String>() { populateBlobKeysInContainer(containerFile, blobNames, new Function<String, String>() {
private static final long serialVersionUID = 3152191346558570795L;
@Override @Override
public boolean add(String e) { public String apply(String string) {
return super.add(e.substring(containerPathLength)); return string.substring(containerPathLength);
} }
}; });
populateBlobKeysInContainer(containerFile, blobNames);
return blobNames; return blobNames;
} }
@ -433,13 +431,14 @@ public class FilesystemStorageStrategyImpl implements LocalStorageStrategy {
return folder; return folder;
} }
private void populateBlobKeysInContainer(File directory, Set<String> blobNames) { private static void populateBlobKeysInContainer(File directory, Set<String> blobNames,
Function<String, String> function) {
File[] children = directory.listFiles(); File[] children = directory.listFiles();
for (File child : children) { for (File child : children) {
if (child.isFile()) { if (child.isFile()) {
blobNames.add(child.getAbsolutePath()); blobNames.add(function.apply(child.getAbsolutePath()));
} else if (child.isDirectory()) { } else if (child.isDirectory()) {
populateBlobKeysInContainer(child, blobNames); populateBlobKeysInContainer(child, blobNames, function);
} }
} }
} }