JCLOUDS-929: Implement delimiter support in Local.

Adds the delimiter support in the local blob store. The existing
directory implementation is preserved and is not compatible with the
delimiter option, as the existing implementation assumes the usage of
filesystem directories.
This commit is contained in:
Timur Alperovich 2015-06-24 16:42:21 -07:00 committed by Andrew Gaul
parent fe13b07233
commit 4dc33ab564
2 changed files with 71 additions and 3 deletions

View File

@ -220,6 +220,10 @@ public final class LocalBlobStore implements BlobStore {
throw new IllegalArgumentException("Cannot set both prefix and directory"); throw new IllegalArgumentException("Cannot set both prefix and directory");
} }
if ((options.getDir() != null || options.isRecursive()) && (options.getDelimiter() != null)) {
throw new IllegalArgumentException("Cannot set the delimiter if directory or recursive is set");
}
// Check if the container exists // Check if the container exists
if (!storageStrategy.containerExists(containerName)) if (!storageStrategy.containerExists(containerName))
throw cnfe(containerName); throw cnfe(containerName);
@ -254,13 +258,13 @@ public final class LocalBlobStore implements BlobStore {
String marker = null; String marker = null;
if (options != null) { if (options != null) {
if (options.getDir() != null && !options.getDir().isEmpty()) { if (options.getDir() != null && !options.getDir().isEmpty()) {
contents = filterDirectory(contents, options); contents = filterDirectory(contents, options);
} else if (options.getPrefix() != null) { } else if (options.getPrefix() != null) {
contents = filterPrefix(contents, options); contents = filterPrefix(contents, options);
} else if (!options.isRecursive()) { } else if (!options.isRecursive() || (options.getDelimiter() != null)) {
contents = extractCommonPrefixes(contents, storageStrategy.getSeparator(), null); String delimiter = options.getDelimiter() == null ? storageStrategy.getSeparator() : options.getDelimiter();
contents = extractCommonPrefixes(contents, delimiter, null);
} }
if (options.getMarker() != null) { if (options.getMarker() != null) {

View File

@ -0,0 +1,64 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.strategy.internal;
import static org.testng.Assert.assertEquals;
import org.jclouds.ContextBuilder;
import org.jclouds.blobstore.BlobStore;
import org.jclouds.blobstore.domain.StorageMetadata;
import org.jclouds.blobstore.domain.StorageType;
import org.jclouds.blobstore.options.ListContainerOptions;
import org.jclouds.util.Closeables2;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import com.google.common.collect.Iterables;
import com.google.inject.Injector;
@Test(testName = "DelimiterTest", singleThreaded = true)
public class DelimiterTest {
private BlobStore blobStore;
@BeforeClass
void setupBlobStore() {
Injector injector = ContextBuilder.newBuilder("transient").buildInjector();
blobStore = injector.getInstance(BlobStore.class);
}
@AfterClass
void tearDownBlobStore() {
if (blobStore != null)
Closeables2.closeQuietly(blobStore.getContext());
}
public void testDelimiterList() {
String container = "delimiter";
String delimiter = "--";
blobStore.createContainerInLocation(null, container);
blobStore.putBlob(container, blobStore.blobBuilder("foo").payload("").build());
blobStore.putBlob(container, blobStore.blobBuilder("other" + delimiter + "bar").payload("").build());
Iterable<? extends StorageMetadata> results = blobStore.list(container,
ListContainerOptions.Builder.delimiter(delimiter));
assertEquals(Iterables.size(results), 2);
assertEquals(Iterables.get(results, 0).getType(), StorageType.BLOB);
assertEquals(Iterables.get(results, 0).getName(), "foo");
assertEquals(Iterables.get(results, 1).getType(), StorageType.RELATIVE_PATH);
assertEquals(Iterables.get(results, 1).getName(), "other" + delimiter);
}
}