Add container creation date to local blobstores

This commit is contained in:
Andrew Gaul 2015-01-05 11:26:20 -08:00
parent 35d6313e63
commit 77eaa2c1f9
4 changed files with 46 additions and 12 deletions

View File

@ -18,6 +18,7 @@ package org.jclouds.filesystem.strategy.internal;
import static java.nio.file.Files.getFileAttributeView;
import static java.nio.file.Files.getFileStore;
import static java.nio.file.Files.readAttributes;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Strings.isNullOrEmpty;
@ -28,6 +29,7 @@ import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.attribute.UserDefinedFileAttributeView;
import java.nio.charset.StandardCharsets;
import java.util.Date;
@ -42,6 +44,10 @@ import javax.inject.Provider;
import org.jclouds.blobstore.LocalStorageStrategy;
import org.jclouds.blobstore.domain.Blob;
import org.jclouds.blobstore.domain.BlobBuilder;
import org.jclouds.blobstore.domain.MutableStorageMetadata;
import org.jclouds.blobstore.domain.StorageMetadata;
import org.jclouds.blobstore.domain.StorageType;
import org.jclouds.blobstore.domain.internal.MutableStorageMetadataImpl;
import org.jclouds.blobstore.options.ListContainerOptions;
import org.jclouds.domain.Location;
import org.jclouds.filesystem.predicates.validators.FilesystemBlobKeyValidator;
@ -163,6 +169,23 @@ public class FilesystemStorageStrategyImpl implements LocalStorageStrategy {
}
}
@Override
public StorageMetadata getContainerMetadata(String container) {
MutableStorageMetadata metadata = new MutableStorageMetadataImpl();
metadata.setName(container);
metadata.setType(StorageType.CONTAINER);
metadata.setLocation(getLocation(container));
Path path = new File(buildPathStartingFromBaseDir(container)).toPath();
BasicFileAttributes attr;
try {
attr = readAttributes(path, BasicFileAttributes.class);
} catch (IOException e) {
throw Throwables.propagate(e);
}
metadata.setCreationDate(new Date(attr.creationTime().toMillis()));
return metadata;
}
@Override
public boolean blobExists(String container, String key) {
filesystemContainerNameValidator.validate(container);

View File

@ -19,6 +19,7 @@ package org.jclouds.blobstore;
import java.io.IOException;
import org.jclouds.blobstore.domain.Blob;
import org.jclouds.blobstore.domain.StorageMetadata;
import org.jclouds.blobstore.options.ListContainerOptions;
import org.jclouds.domain.Location;
@ -72,6 +73,9 @@ public interface LocalStorageStrategy {
*/
void clearContainer(String container, ListContainerOptions options);
/** @return StorageMetadata associated with a container name, e.g., creation date and location */
StorageMetadata getContainerMetadata(String container);
/**
* Return true if a blob named by key exists
* @param container

View File

@ -30,6 +30,10 @@ import javax.inject.Inject;
import org.jclouds.blobstore.domain.Blob;
import org.jclouds.blobstore.domain.Blob.Factory;
import org.jclouds.blobstore.domain.MutableStorageMetadata;
import org.jclouds.blobstore.domain.StorageMetadata;
import org.jclouds.blobstore.domain.StorageType;
import org.jclouds.blobstore.domain.internal.MutableStorageMetadataImpl;
import org.jclouds.blobstore.options.ListContainerOptions;
import org.jclouds.blobstore.util.BlobStoreUtils;
import org.jclouds.date.DateService;
@ -53,7 +57,7 @@ import com.google.common.net.HttpHeaders;
public class TransientStorageStrategy implements LocalStorageStrategy {
private final ConcurrentMap<String, ConcurrentMap<String, Blob>> containerToBlobs = new ConcurrentHashMap<String, ConcurrentMap<String, Blob>>();
private final ConcurrentMap<String, Location> containerToLocation = new ConcurrentHashMap<String, Location>();
private final ConcurrentMap<String, StorageMetadata> containerMetadata = new ConcurrentHashMap<String, StorageMetadata>();
private final Supplier<Location> defaultLocation;
private final DateService dateService;
private final Factory blobFactory;
@ -85,7 +89,13 @@ public class TransientStorageStrategy implements LocalStorageStrategy {
if (origValue != null) {
return false;
}
containerToLocation.put(containerName, location != null ? location : defaultLocation.get());
MutableStorageMetadata metadata = new MutableStorageMetadataImpl();
metadata.setName(containerName);
metadata.setType(StorageType.CONTAINER);
metadata.setLocation(location);
metadata.setCreationDate(new Date());
containerMetadata.put(containerName, metadata);
return true;
}
@ -105,6 +115,11 @@ public class TransientStorageStrategy implements LocalStorageStrategy {
containerToBlobs.get(containerName).clear();
}
@Override
public StorageMetadata getContainerMetadata(String container) {
return containerMetadata.get(container);
}
@Override
public boolean blobExists(final String containerName, final String blobName) {
Map<String, Blob> map = containerToBlobs.get(containerName);
@ -154,7 +169,7 @@ public class TransientStorageStrategy implements LocalStorageStrategy {
@Override
public Location getLocation(final String containerName) {
return containerToLocation.get(containerName);
return containerMetadata.get(containerName).getLocation();
}
@Override

View File

@ -345,19 +345,11 @@ public final class LocalBlobStore implements BlobStore {
return new PageSetImpl<StorageMetadata>(transform(
containers, new Function<String, StorageMetadata>() {
public StorageMetadata apply(String name) {
MutableStorageMetadata cmd = create();
cmd.setName(name);
cmd.setType(StorageType.CONTAINER);
cmd.setLocation(storageStrategy.getLocation(name));
return cmd;
return storageStrategy.getContainerMetadata(name);
}
}), null);
}
private MutableStorageMetadata create() {
return new MutableStorageMetadataImpl();
}
@Override
public boolean createContainerInLocation(Location location, String name) {
return storageStrategy.createContainerInLocation(name, location);