Skip to content
Permalink
Browse files
JCLOUDS-1543: change FetchBlobMetadata to retain original blob order
  • Loading branch information
roded authored and gaul committed Apr 17, 2020
1 parent 89e1713 commit 5ac92111c4e9347c492a16ecd402acafdb1f9cd7
Showing 2 changed files with 100 additions and 2 deletions.
@@ -19,6 +19,9 @@
import static com.google.common.base.Preconditions.checkState;
import static org.jclouds.concurrent.FutureIterables.transformParallel;

import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.Callable;

import javax.annotation.Resource;
@@ -79,6 +82,15 @@ public FetchBlobMetadata setContainerName(String container) {
public PageSet<? extends StorageMetadata> apply(PageSet<? extends StorageMetadata> in) {
checkState(container != null, "container name should be initialized");

if (in == null) {
return new PageSetImpl<>(Collections.<StorageMetadata>emptyList(), null);
}

Map<String, StorageMetadata> orderedMap = new LinkedHashMap<>(in.size());
for (StorageMetadata storageMetadata : in) {
orderedMap.put(storageMetadata.getName(), null);
}

Iterable<StorageMetadata> returnv = Lists.newArrayList(transformParallel(in,
new Function<StorageMetadata, ListenableFuture<? extends StorageMetadata>>() {

@@ -88,14 +100,18 @@ public ListenableFuture<StorageMetadata> apply(final StorageMetadata from) {
return Futures.immediateFuture(from);
}
return userExecutor.submit(new Callable<StorageMetadata>() {
@Override public StorageMetadata call() throws Exception {
@Override public StorageMetadata call() {
return blobstore.blobMetadata(container, from.getName());
}
});
}

}, userExecutor, maxTime, logger, String.format("getting metadata from containerName: %s", container)));

return new PageSetImpl<StorageMetadata>(returnv, in.getNextMarker());
for (StorageMetadata storageMetadata : returnv) {
orderedMap.put(storageMetadata.getName(), storageMetadata);
}

return new PageSetImpl<>(orderedMap.values(), in.getNextMarker());
}
}
@@ -0,0 +1,82 @@
/*
* 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 com.google.common.collect.Maps;
import com.google.common.collect.Ordering;
import com.google.inject.Injector;
import org.jclouds.ContextBuilder;
import org.jclouds.blobstore.BlobStore;
import org.jclouds.blobstore.domain.Blob;
import org.jclouds.blobstore.domain.PageSet;
import org.jclouds.blobstore.domain.StorageMetadata;
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 java.util.Comparator;
import java.util.Map;

import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertTrue;

@Test(testName = "FetchBlobMetadataTest", singleThreaded = true)
public class FetchBlobMetadataTest {

private static final String CONTAINER_NAME = "container";

private BlobStore blobStore;
private FetchBlobMetadata fetchBlobMetadata;

@BeforeClass
public void setupBlobStore() {
Injector injector = ContextBuilder.newBuilder("transient").buildInjector();
blobStore = injector.getInstance(BlobStore.class);
fetchBlobMetadata = injector.getInstance(FetchBlobMetadata.class);
fetchBlobMetadata.setContainerName(CONTAINER_NAME);
}

@AfterClass
public void closeBlobSore() {
if (blobStore != null) {
Closeables2.closeQuietly(blobStore.getContext());
}
}

@Test
public void testRetainsOriginalOrder() {
blobStore.createContainerInLocation(null, CONTAINER_NAME);
for (int blobIndex = 0; blobIndex < 20; blobIndex++) {
final Blob blob = blobStore.blobBuilder("prefix-" + blobIndex).payload("").build();
blobStore.putBlob(CONTAINER_NAME, blob);
}

final PageSet<? extends StorageMetadata> pageSet =
blobStore.list(CONTAINER_NAME, ListContainerOptions.Builder.withDetails());
final PageSet<? extends StorageMetadata> resultPageSet = fetchBlobMetadata.apply(pageSet);
assertNotNull(resultPageSet);

assertTrue(Ordering.from(new Comparator<StorageMetadata>() {
@Override
public int compare(StorageMetadata o1, StorageMetadata o2) {
return o1.getName().compareTo(o2.getName());
}
}).isOrdered(resultPageSet));
}
}

0 comments on commit 5ac9211

Please sign in to comment.