Skip to content
This repository has been archived by the owner on Mar 25, 2018. It is now read-only.

Use ConcurrentSkipListMap for transient blobstore. #1565

Closed
wants to merge 1 commit into from
Closed
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 @@ -24,7 +24,8 @@

import java.io.File;
import java.io.IOException;
import java.util.Set;
import java.util.Collection;
import java.util.List;

import javax.annotation.Resource;
import javax.inject.Inject;
Expand All @@ -42,13 +43,14 @@
import org.jclouds.filesystem.util.Utils;
import org.jclouds.io.Payload;
import org.jclouds.io.Payloads;
import org.jclouds.javax.annotation.Nullable;
import org.jclouds.logging.Logger;
import org.jclouds.rest.annotations.ParamValidators;

import com.google.common.base.Function;
import com.google.common.base.Throwables;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Sets;
import com.google.common.collect.Lists;
import com.google.common.io.ByteStreams;
import com.google.common.io.Files;

Expand Down Expand Up @@ -143,7 +145,14 @@ public boolean blobExists(String container, String key) {
filesystemBlobKeyValidator.validate(key);
return buildPathAndChecksIfFileExists(container, key);
}


@Override
public Iterable<String> getBlobKeysInsideContainer(String container, String fromElement)
throws IOException {
ImmutableList<String> blobNames = ImmutableList.copyOf(getBlobKeysInsideContainer(container));
return blobNames.subList(blobNames.indexOf(fromElement), blobNames.size());
}

/**
* Returns all the blobs key inside a container
*
Expand All @@ -156,11 +165,11 @@ public Iterable<String> getBlobKeysInsideContainer(String container) throws IOEx
filesystemContainerNameValidator.validate(container);
// check if container exists
// TODO maybe an error is more appropriate
Set<String> blobNames = Sets.newHashSet();
List<String> blobNames = Lists.newArrayList();
if (!containerExists(container)) {
return blobNames;
}

File containerFile = openFolder(container);
final int containerPathLength = containerFile.getAbsolutePath().length() + 1;
populateBlobKeysInContainer(containerFile, blobNames, new Function<String, String>() {
Expand All @@ -169,6 +178,7 @@ public String apply(String string) {
return string.substring(containerPathLength);
}
});

return blobNames;
}

Expand Down Expand Up @@ -435,7 +445,7 @@ private File openFolder(String folderName) throws IOException {
return folder;
}

private static void populateBlobKeysInContainer(File directory, Set<String> blobNames,
private static void populateBlobKeysInContainer(File directory, Collection<String> blobNames,
Function<String, String> function) {
File[] children = directory.listFiles();
for (File child : children) {
Expand Down Expand Up @@ -469,5 +479,4 @@ protected boolean createDirectoryWithResult(String container, String directory)
boolean result = directoryToCreate.mkdirs();
return result;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,12 @@ public ListenableFuture<PageSet<? extends StorageMetadata>> list(final String co
// Loading blobs from container
Iterable<String> blobBelongingToContainer = null;
try {
blobBelongingToContainer = storageStrategy.getBlobKeysInsideContainer(container);
String marker = options.getMarker();
if (marker != null) {
blobBelongingToContainer = storageStrategy.getBlobKeysInsideContainer(container, marker);
} else {
blobBelongingToContainer = storageStrategy.getBlobKeysInsideContainer(container);
}
} catch (IOException e) {
logger.error(e, "An error occurred loading blobs contained into container %s", container);
Throwables.propagate(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.jclouds.blobstore.domain.Blob;
import org.jclouds.blobstore.options.ListContainerOptions;
import org.jclouds.domain.Location;
import org.jclouds.javax.annotation.Nullable;

/**
* Strategy for local operations related to container and blob
Expand Down Expand Up @@ -85,13 +86,22 @@ public interface LocalStorageStrategy {
boolean blobExists(String container, String key);

/**
* Returns all the blobs key inside a container
* Returns all the blob keys inside a container
* @param container
* @return
* @throws IOException
*/
Iterable<String> getBlobKeysInsideContainer(String container) throws IOException;

/**
* Returns the blob keys inside a container starting from a particular element
* @param container
* @param fromElement
* @return
* @throws IOException
*/
Iterable<String> getBlobKeysInsideContainer(String container, String fromElement) throws IOException;

/**
* Load the blob with the given key belonging to the container with the given
* name. There must exist a resource on the file system whose complete name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ConcurrentSkipListMap;

import javax.inject.Inject;
import javax.ws.rs.core.HttpHeaders;
Expand All @@ -45,14 +46,16 @@
import org.jclouds.io.Payloads;
import org.jclouds.io.payloads.ByteArrayPayload;
import org.jclouds.io.payloads.DelegatingPayload;
import org.jclouds.javax.annotation.Nullable;

import com.google.common.base.Supplier;
import com.google.common.base.Throwables;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Multimaps;

public class TransientStorageStrategy implements LocalStorageStrategy {
private final ConcurrentMap<String, ConcurrentMap<String, Blob>> containerToBlobs = new ConcurrentHashMap<String, ConcurrentMap<String, Blob>>();
private final ConcurrentMap<String, ConcurrentSkipListMap<String, Blob>> containerToBlobs =
new ConcurrentHashMap<String, ConcurrentSkipListMap<String, Blob>>();
private final ConcurrentMap<String, Location> containerToLocation = new ConcurrentHashMap<String, Location>();
private final Supplier<Location> defaultLocation;
private final DateService dateService;
Expand Down Expand Up @@ -81,7 +84,7 @@ public Iterable<String> getAllContainerNames() {
@Override
public boolean createContainerInLocation(final String containerName, final Location location) {
ConcurrentMap<String, Blob> origValue = containerToBlobs.putIfAbsent(
containerName, new ConcurrentHashMap<String, Blob>());
containerName, new ConcurrentSkipListMap<String, Blob>());
if (origValue != null) {
return false;
}
Expand Down Expand Up @@ -116,6 +119,11 @@ public Iterable<String> getBlobKeysInsideContainer(final String containerName) {
return containerToBlobs.get(containerName).keySet();
}

@Override
public Iterable<String> getBlobKeysInsideContainer(final String containerName, final String fromElement) {
return containerToBlobs.get(containerName).keySet().tailSet(fromElement);
}

@Override
public Blob getBlob(final String containerName, final String blobName) {
Map<String, Blob> map = containerToBlobs.get(containerName);
Expand Down