JCLOUDS-930: Add the prefix option.

Add a prefix option to the ListContainerOptions class.
This commit is contained in:
Timur Alperovich 2015-06-24 16:09:34 -07:00
parent 61b06329d8
commit 7c7d2e2df6
7 changed files with 95 additions and 2 deletions

View File

@ -17,6 +17,7 @@
package org.jclouds.atmos.blobstore.integration;
import org.jclouds.blobstore.integration.internal.BaseContainerLiveTest;
import org.testng.SkipException;
import org.testng.annotations.Test;
@Test(groups = { "live" })
@ -24,4 +25,9 @@ public class AtmosContainerLiveTest extends BaseContainerLiveTest {
public AtmosContainerLiveTest() {
provider = "atmos";
}
@Override
public void testContainerListWithPrefix() {
throw new SkipException("Prefix option has not been plumbed down to Atmos");
}
}

View File

@ -21,6 +21,7 @@ import static org.jclouds.openstack.keystone.v2_0.config.KeystoneProperties.CRED
import java.util.Properties;
import org.jclouds.blobstore.integration.internal.BaseContainerLiveTest;
import org.testng.SkipException;
import org.testng.annotations.Test;
@Test(groups = "live", testName = "SwiftContainerLiveTest")
@ -36,4 +37,9 @@ public class SwiftContainerLiveTest extends BaseContainerLiveTest {
setIfTestSystemPropertyPresent(props, CREDENTIAL_TYPE);
return props;
}
@Override
public void testContainerListWithPrefix() {
throw new SkipException("Prefix option has not been plumbed down to Swift");
}
}

View File

@ -18,6 +18,7 @@ package org.jclouds.s3.blobstore.integration;
import org.jclouds.blobstore.integration.internal.BaseBlobStoreIntegrationTest;
import org.jclouds.blobstore.integration.internal.BaseContainerLiveTest;
import org.testng.SkipException;
import org.testng.annotations.Test;
@Test(groups = "live", testName = "S3ContainerLiveTest")
@ -27,4 +28,9 @@ public class S3ContainerLiveTest extends BaseContainerLiveTest {
provider = "s3";
BaseBlobStoreIntegrationTest.SANITY_CHECK_RETURNED_BUCKET_NAME = true;
}
@Override
public void testContainerListWithPrefix() {
throw new SkipException("Prefix option has not been plumbed down to S3");
}
}

View File

@ -22,6 +22,7 @@ import java.util.Properties;
import org.jclouds.blobstore.integration.internal.BaseContainerLiveTest;
import org.jclouds.openstack.keystone.v2_0.config.KeystoneProperties;
import org.testng.SkipException;
import org.testng.annotations.Test;
@Test(groups = "live", testName = "SwiftContainerLiveTest" )
@ -52,4 +53,9 @@ public class SwiftContainerLiveTest extends BaseContainerLiveTest {
IOException {
super.testPublicAccessInNonDefaultLocationWithBigBlob();
}
@Override
public void testContainerListWithPrefix() {
throw new SkipException("Prefix option has not been plumbed down to Swift");
}
}

View File

@ -40,6 +40,7 @@ public class ListContainerOptions extends ListOptions implements Cloneable {
new ListContainerOptions());
private String dir;
private String prefix;
private boolean recursive;
private boolean detailed;
@ -47,11 +48,12 @@ public class ListContainerOptions extends ListOptions implements Cloneable {
}
ListContainerOptions(Integer maxKeys, String marker, String dir, boolean recursive,
boolean detailed) {
boolean detailed, String prefix) {
super(maxKeys, marker);
this.dir = dir;
this.recursive = recursive;
this.detailed = detailed;
this.prefix = prefix;
}
public static class ImmutableListContainerOptions extends ListContainerOptions {
@ -107,6 +109,16 @@ public class ListContainerOptions extends ListOptions implements Cloneable {
return delegate.getMaxResults();
}
@Override
public String getPrefix() {
return delegate.getPrefix();
}
@Override
public ListContainerOptions prefix(String prefix) {
throw new UnsupportedOperationException();
}
@Override
public ListContainerOptions clone() {
return delegate.clone();
@ -131,6 +143,10 @@ public class ListContainerOptions extends ListOptions implements Cloneable {
return detailed;
}
public String getPrefix() {
return prefix;
}
/**
* This will list the contents of a virtual or real directory path.
*
@ -176,6 +192,14 @@ public class ListContainerOptions extends ListOptions implements Cloneable {
return this;
}
/**
* Only list keys that start with the supplied prefix
*/
public ListContainerOptions prefix(String prefix) {
this.prefix = prefix;
return this;
}
public static class Builder {
/**
@ -217,11 +241,19 @@ public class ListContainerOptions extends ListOptions implements Cloneable {
ListContainerOptions options = new ListContainerOptions();
return options.withDetails();
}
/**
* @see ListContainerOptions#prefix(String)
*/
public static ListContainerOptions prefix(String prefix) {
ListContainerOptions options = new ListContainerOptions();
return options.prefix(prefix);
}
}
@Override
public ListContainerOptions clone() {
return new ListContainerOptions(getMaxResults(), getMarker(), dir, recursive, detailed);
return new ListContainerOptions(getMaxResults(), getMarker(), dir, recursive, detailed, prefix);
}
@Override

View File

@ -26,13 +26,17 @@ import static org.testng.Assert.assertTrue;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.logging.Logger;
import com.google.common.net.HostAndPort;
import org.jclouds.blobstore.BlobStore;
import org.jclouds.blobstore.domain.BlobMetadata;
import org.jclouds.blobstore.domain.PageSet;
import org.jclouds.blobstore.domain.StorageMetadata;
import org.jclouds.blobstore.options.ListContainerOptions;
import org.jclouds.domain.Location;
import org.jclouds.javax.annotation.Nullable;
import org.jclouds.predicates.SocketOpen;
@ -43,6 +47,7 @@ import org.testng.annotations.Test;
import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
@ -113,6 +118,32 @@ public class BaseContainerLiveTest extends BaseBlobStoreIntegrationTest {
runCreateContainerInLocation(payload, nonDefault);
}
@Test(groups = "live", dependsOnMethods = "testPublicAccess")
public void testContainerListWithPrefix() throws InterruptedException {
final String containerName = getContainerName();
BlobStore blobStore = view.getBlobStore();
String prefix = "blob";
try {
blobStore.putBlob(containerName, blobStore.blobBuilder(prefix).payload("").build());
blobStore.putBlob(containerName, blobStore.blobBuilder(prefix + "foo").payload("").build());
blobStore.putBlob(containerName, blobStore.blobBuilder(prefix + "bar").payload("").build());
blobStore.putBlob(containerName, blobStore.blobBuilder("foo").payload("").build());
checkEqualNames(ImmutableSet.of(prefix, prefix + "foo", prefix + "bar"),
blobStore.list(containerName, ListContainerOptions.Builder.prefix(prefix)));
} finally {
returnContainer(containerName);
}
}
private void checkEqualNames(ImmutableSet<String> expectedSet, PageSet<? extends StorageMetadata> results) {
Set<String> names = new HashSet<String>();
for (StorageMetadata sm : results) {
names.add(sm.getName());
}
assertEquals(expectedSet, names);
}
private void runCreateContainerInLocation(String payload, Location nonDefault) throws InterruptedException,
IOException {
String blobName = "hello";

View File

@ -17,6 +17,7 @@
package org.jclouds.azureblob.blobstore.integration;
import org.jclouds.blobstore.integration.internal.BaseContainerLiveTest;
import org.testng.SkipException;
import org.testng.annotations.Test;
@Test(groups = { "live" })
@ -24,4 +25,9 @@ public class AzureBlobContainerLiveTest extends BaseContainerLiveTest {
public AzureBlobContainerLiveTest() {
provider = "azureblob";
}
@Override
public void testContainerListWithPrefix() {
throw new SkipException("Prefix option has not been plumbed down to Azure");
}
}