JCLOUDS-1543: change FetchBlobMetadata to retain original blob order

This commit is contained in:
Roded Bahat 2020-04-14 20:17:33 +03:00 committed by Andrew Gaul
parent 876269427b
commit a04484122a
2 changed files with 100 additions and 2 deletions

View File

@ -19,6 +19,9 @@ package org.jclouds.blobstore.strategy.internal;
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 class FetchBlobMetadata implements Function<PageSet<? extends StorageMeta
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,7 +100,7 @@ public class FetchBlobMetadata implements Function<PageSet<? extends StorageMeta
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());
}
});
@ -96,6 +108,10 @@ public class FetchBlobMetadata implements Function<PageSet<? extends StorageMeta
}, 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());
}
}

View File

@ -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));
}
}