PerformanceCachingGoogleCloudStorage extends ForwardingGoogleCloudStorage { @Override public List<GoogleCloudStorageItemInfo> getItemInfos(List<StorageResourceId> resourceIds) throws IOException { List<GoogleCloudStorageItemInfo> result = new ArrayList<>(resourceIds.size()); List<StorageResourceId> request = new ArrayList<>(resourceIds.size()); for (StorageResourceId resourceId : resourceIds) { GoogleCloudStorageItemInfo item = cache.getItem(resourceId); if (item == null) { request.add(resourceId); } result.add(item); } if (!request.isEmpty()) { List<GoogleCloudStorageItemInfo> response = super.getItemInfos(request); Iterator<GoogleCloudStorageItemInfo> responseIterator = response.iterator(); for (int i = 0; i < result.size() && responseIterator.hasNext(); i++) { if (result.get(i) == null) { GoogleCloudStorageItemInfo item = responseIterator.next(); cache.putItem(item); result.set(i, item); } } } return result; } PerformanceCachingGoogleCloudStorage(
      GoogleCloudStorage delegate, PerformanceCachingGoogleCloudStorageOptions options); @VisibleForTesting  PerformanceCachingGoogleCloudStorage(
      GoogleCloudStorage delegate,
      PrefixMappedItemCache cache); @Override WritableByteChannel create(StorageResourceId resourceId); @Override void deleteBuckets(List<String> bucketNames); @Override void deleteObjects(List<StorageResourceId> resourceIds); @Override List<GoogleCloudStorageItemInfo> listBucketInfo(); @Override List<String> listObjectNames(String bucketName, String objectNamePrefix); @Override List<String> listObjectNames(
      String bucketName, String objectNamePrefix, ListObjectOptions listOptions); @Override List<GoogleCloudStorageItemInfo> listObjectInfo(String bucketName, String objectNamePrefix); @Override List<GoogleCloudStorageItemInfo> listObjectInfo(
      String bucketName, String objectNamePrefix, ListObjectOptions listOptions); @Override ListPage<GoogleCloudStorageItemInfo> listObjectInfoPage(
      String bucketName, String objectNamePrefix, ListObjectOptions listOptions, String pageToken); @Override GoogleCloudStorageItemInfo getItemInfo(StorageResourceId resourceId); @Override List<GoogleCloudStorageItemInfo> getItemInfos(List<StorageResourceId> resourceIds); @Override List<GoogleCloudStorageItemInfo> updateItems(List<UpdatableItemInfo> itemInfoList); @Override void close(); @Override GoogleCloudStorageItemInfo composeObjects(
      List<StorageResourceId> sources, StorageResourceId destination, CreateObjectOptions options); @VisibleForTesting void invalidateCache();  }
@Test public void testGetItemInfosAllCached() throws IOException { List<StorageResourceId> requestedIds = Lists.newArrayList(ITEM_A_A.getResourceId(), ITEM_A_B.getResourceId()); List<GoogleCloudStorageItemInfo> expected = Lists.newArrayList(ITEM_A_A, ITEM_A_B); cache.putItem(ITEM_A_A); cache.putItem(ITEM_A_B); List<GoogleCloudStorageItemInfo> result = gcs.getItemInfos(requestedIds); assertThat(result).containsExactlyElementsIn(expected).inOrder(); assertThat(cache.getAllItemsRaw()).containsExactlyElementsIn(expected); }
@Test public void testGetItemInfosSomeCached() throws IOException { List<StorageResourceId> requestedIds = Lists.newArrayList( ITEM_A_A.getResourceId(), ITEM_A_B.getResourceId(), ITEM_B_A.getResourceId(), ITEM_B_B.getResourceId()); List<StorageResourceId> uncachedIds = Lists.newArrayList(ITEM_A_A.getResourceId(), ITEM_B_A.getResourceId()); List<GoogleCloudStorageItemInfo> expected = Lists.newArrayList(ITEM_A_A, ITEM_A_B, ITEM_B_A, ITEM_B_B); cache.putItem(ITEM_A_B); cache.putItem(ITEM_B_B); List<GoogleCloudStorageItemInfo> result = gcs.getItemInfos(requestedIds); verify(gcsDelegate).getItemInfos(eq(uncachedIds)); assertThat(result).containsExactlyElementsIn(expected).inOrder(); assertThat(cache.getAllItemsRaw()).containsExactlyElementsIn(expected); }
@Test public void testGetItemInfosNoneCached() throws IOException { List<StorageResourceId> requestedIds = Lists.newArrayList(ITEM_A_A.getResourceId(), ITEM_A_B.getResourceId()); List<GoogleCloudStorageItemInfo> expected = Lists.newArrayList(ITEM_A_A, ITEM_A_B); List<GoogleCloudStorageItemInfo> result = gcs.getItemInfos(requestedIds); verify(gcsDelegate).getItemInfos(eq(requestedIds)); assertThat(result).containsExactlyElementsIn(expected).inOrder(); assertThat(cache.getAllItemsRaw()).containsExactlyElementsIn(expected); }