org.jclouds.blobstore.internal.BaseBlobMap Maven / Gradle / Ivy
/**
*
* Copyright (C) 2010 Cloud Conscious, LLC.
*
* ====================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ====================================================================
*/
package org.jclouds.blobstore.internal;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import java.util.Map;
import java.util.Set;
import org.jclouds.blobstore.BlobStore;
import org.jclouds.blobstore.ListableMap;
import org.jclouds.blobstore.domain.Blob;
import org.jclouds.blobstore.domain.BlobMetadata;
import org.jclouds.blobstore.domain.MutableBlobMetadata;
import org.jclouds.blobstore.domain.internal.MutableBlobMetadataImpl;
import org.jclouds.blobstore.options.ListContainerOptions;
import org.jclouds.blobstore.options.ListContainerOptions.ImmutableListContainerOptions;
import org.jclouds.blobstore.strategy.ContainsValueInListStrategy;
import org.jclouds.blobstore.strategy.GetBlobsInListStrategy;
import org.jclouds.blobstore.strategy.PutBlobsStrategy;
import org.jclouds.blobstore.strategy.internal.ListContainerAndRecurseThroughFolders;
import com.google.common.base.Function;
import com.google.common.collect.Iterables;
import com.google.common.collect.Sets;
import com.google.inject.Inject;
/**
* Implements core Map functionality with a {@link BlobStore}
*
*
* @author Adrian Cole
*/
public abstract class BaseBlobMap implements ListableMap {
protected final BlobStore blobstore;
protected final String containerName;
protected final Function prefixer;
protected final Function pathStripper;
protected final ListContainerOptions options;
protected final GetBlobsInListStrategy getAllBlobs;
protected final ContainsValueInListStrategy containsValueStrategy;
protected final ListContainerAndRecurseThroughFolders listStrategy;
protected final PutBlobsStrategy putBlobsStrategy;
static class StripPath implements Function {
private final String prefix;
private final String delimiter;
StripPath(String prefix, String delimiter) {
this.prefix = checkNotNull(prefix, "prefix");
this.delimiter = checkNotNull(delimiter, "delimiter");
}
public String apply(String from) {
return from.replaceFirst(prefix + delimiter, "");
}
}
static class PrefixKey implements Function {
private final String prefix;
private final String delimiter;
PrefixKey(String prefix, String delimiter) {
this.prefix = checkNotNull(prefix, "prefix");
this.delimiter = checkNotNull(delimiter, "delimiter");
}
public String apply(String from) {
return prefix + delimiter + from;
}
}
static class PassThrough implements Function {
public T apply(T from) {
return from;
}
}
@Inject
public BaseBlobMap(BlobStore blobstore, GetBlobsInListStrategy getAllBlobs,
ContainsValueInListStrategy containsValueStrategy, PutBlobsStrategy putBlobsStrategy,
ListContainerAndRecurseThroughFolders listStrategy, String containerName,
ListContainerOptions options) {
this.blobstore = checkNotNull(blobstore, "blobstore");
this.containerName = checkNotNull(containerName, "container");
checkArgument(containerName.indexOf('/') == -1,
"please specify directory path using the option: inDirectory, not encoded in the container name");
this.options = checkNotNull(options, "options") instanceof ImmutableListContainerOptions ? options
: new ImmutableListContainerOptions(options);
String dir = options.getDir();
if (dir == null) {
prefixer = new PassThrough();
pathStripper = prefixer;
} else {
prefixer = new PrefixKey(dir, "/");
pathStripper = new StripPath(dir, "/");
}
this.getAllBlobs = checkNotNull(getAllBlobs, "getAllBlobs");
this.listStrategy = checkNotNull(listStrategy, "listStrategy");
this.containsValueStrategy = checkNotNull(containsValueStrategy, "containsValueStrategy");
this.putBlobsStrategy = checkNotNull(putBlobsStrategy, "putBlobsStrategy");
checkArgument(!containerName.equals(""), "container name must not be a blank string!");
}
@Override
public Set> entrySet() {
return Sets.newHashSet(Iterables.transform(list(),
new Function>() {
@Override
public java.util.Map.Entry apply(BlobMetadata from) {
return new Entry(pathStripper.apply(from.getName()));
}
}));
}
public class Entry implements java.util.Map.Entry {
private final String key;
Entry(String key) {
this.key = key;
}
@Override
public String getKey() {
return key;
}
@Override
public V getValue() {
return get(prefixer.apply(key));
}
@Override
public V setValue(V value) {
return put(prefixer.apply(key), value);
}
}
@Override
public int size() {
return (int) blobstore.countBlobs(containerName, options);
}
protected Iterable getAllBlobs() {
Iterable returnVal = getAllBlobs.execute(containerName, options);
if (options != null) {
for (Blob from : returnVal)
stripPrefix(from);
}
return returnVal;
}
protected Blob stripPrefix(Blob from) {
from.getMetadata().setName(pathStripper.apply(from.getMetadata().getName()));
return from;
}
@Override
public boolean containsValue(Object value) {
return containsValueStrategy.execute(containerName, value, options);
}
@Override
public void clear() {
blobstore.clearContainer(containerName, options);
}
@Override
public Set keySet() {
return Sets.newHashSet(Iterables.transform(list(), new Function() {
@Override
public String apply(BlobMetadata from) {
return from.getName();
}
}));
}
@Override
public boolean containsKey(Object key) {
String realKey = prefixer.apply(key.toString());
return blobstore.blobExists(containerName, realKey);
}
@Override
public boolean isEmpty() {
return size() == 0;
}
public Iterable extends BlobMetadata> list() {
return Iterables.transform(listStrategy.execute(containerName, options),
new Function() {
public BlobMetadata apply(BlobMetadata from) {
MutableBlobMetadata md = new MutableBlobMetadataImpl(from);
if (options.getDir() != null)
md.setName(pathStripper.apply(from.getName()));
return md;
}
});
}
@Override
public String toString() {
return "BaseBlobMap [containerName=" + containerName + ", options=" + options + "]";
}
} © 2015 - 2025 Weber Informatics LLC | Privacy Policy