Coverage Report - com.jcabi.s3.AwsListIterator
 
Classes in this File Line Coverage Branch Coverage Complexity
AwsListIterator
84%
21/25
91%
11/12
3
 
 1  
 /**
 2  
  * Copyright (c) 2012-2015, jcabi.com
 3  
  * All rights reserved.
 4  
  *
 5  
  * Redistribution and use in source and binary forms, with or without
 6  
  * modification, are permitted provided that the following conditions
 7  
  * are met: 1) Redistributions of source code must retain the above
 8  
  * copyright notice, this list of conditions and the following
 9  
  * disclaimer. 2) Redistributions in binary form must reproduce the above
 10  
  * copyright notice, this list of conditions and the following
 11  
  * disclaimer in the documentation and/or other materials provided
 12  
  * with the distribution. 3) Neither the name of the jcabi.com nor
 13  
  * the names of its contributors may be used to endorse or promote
 14  
  * products derived from this software without specific prior written
 15  
  * permission.
 16  
  *
 17  
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 18  
  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
 19  
  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 20  
  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 21  
  * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 22  
  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 23  
  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 24  
  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 25  
  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 26  
  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 27  
  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 28  
  * OF THE POSSIBILITY OF SUCH DAMAGE.
 29  
  */
 30  
 package com.jcabi.s3;
 31  
 
 32  
 import com.amazonaws.AmazonServiceException;
 33  
 import com.amazonaws.services.s3.AmazonS3;
 34  
 import com.amazonaws.services.s3.model.ListObjectsRequest;
 35  
 import com.amazonaws.services.s3.model.ObjectListing;
 36  
 import com.amazonaws.services.s3.model.S3ObjectSummary;
 37  
 import com.jcabi.log.Logger;
 38  
 import java.util.Iterator;
 39  
 import java.util.LinkedList;
 40  
 import java.util.List;
 41  
 import java.util.NoSuchElementException;
 42  
 
 43  
 /**
 44  
  * Iterator for large lists returned by S3.
 45  
  * @author Roman Kisilenko (roman.kisilenko@gmail.com)
 46  
  * @version $Id: 438e29b243c8fac5b3971a89e2a13a83fd557d9d $
 47  
  * @since 0.10
 48  
  */
 49  2
 class AwsListIterator implements Iterator<String> {
 50  
 
 51  
     /**
 52  
      * Region we're in.
 53  
      */
 54  
     private final transient Region region;
 55  
 
 56  
     /**
 57  
      * Bucket name.
 58  
      */
 59  
     private final transient String bucket;
 60  
 
 61  
     /**
 62  
      * Prefix to use.
 63  
      */
 64  
     private final transient String prefix;
 65  
 
 66  
     /**
 67  
      * Partial S3 response iterator.
 68  
      */
 69  
     private transient List<String> partial;
 70  
 
 71  
     /**
 72  
      * Next marker for partial response or null if response contains no more
 73  
      * data.
 74  
      */
 75  
     private transient String marker;
 76  
 
 77  
     /**
 78  
      * Constructs AwsListIterator.
 79  
      * @param pfx Key prefix
 80  
      * @param rgn Region we're in
 81  
      * @param bkt Bucket name
 82  
      */
 83  
     AwsListIterator(final Region rgn, final String bkt,
 84  1
         final String pfx) {
 85  1
         this.prefix = pfx;
 86  1
         this.region = rgn;
 87  1
         this.bucket = bkt;
 88  1
     }
 89  
 
 90  
     @Override
 91  
     public final boolean hasNext() {
 92  5
         if (this.partial == null || this.partial.isEmpty()
 93  
             && this.marker != null) {
 94  2
             this.partial = this.load();
 95  
         }
 96  5
         return !this.partial.isEmpty();
 97  
     }
 98  
 
 99  
     @Override
 100  
     public final String next() {
 101  2
         if (!this.hasNext()) {
 102  0
             throw new NoSuchElementException(
 103  
                 "There are no more elements in this iterator"
 104  
             );
 105  
         }
 106  2
         return this.partial.remove(0);
 107  
     }
 108  
 
 109  
     @Override
 110  
     public final void remove() {
 111  0
         throw new UnsupportedOperationException("Remove is not supported");
 112  
     }
 113  
 
 114  
     /**
 115  
      * Loads next portion of data from S3.
 116  
      * @return A list with next portion of data from S3
 117  
      */
 118  
     private List<String> load() {
 119  
         try {
 120  2
             final AmazonS3 aws = this.region.aws();
 121  2
             final long start = System.currentTimeMillis();
 122  2
             final ObjectListing listing = aws.listObjects(
 123  
                 new ListObjectsRequest()
 124  
                     .withBucketName(this.bucket)
 125  
                     .withPrefix(this.prefix)
 126  
                     .withMarker(this.marker)
 127  
             );
 128  2
             this.marker = listing.getNextMarker();
 129  2
             final List<String> list = new LinkedList<String>();
 130  
             for (final S3ObjectSummary sum
 131  2
                 : listing.getObjectSummaries()) {
 132  2
                 list.add(sum.getKey());
 133  2
             }
 134  2
             Logger.info(
 135  
                 this,
 136  
                 "listed %d ocket(s) with prefix '%s' in bucket '%s' in %[ms]s",
 137  
                 listing.getObjectSummaries().size(), this.prefix,
 138  
                 this.bucket, System.currentTimeMillis() - start
 139  
             );
 140  2
             return list;
 141  0
         } catch (final AmazonServiceException ex) {
 142  0
             throw new IllegalStateException(
 143  
                 String.format(
 144  
                     "failed to load a list of objects in '%s', prefix=%s",
 145  
                     this.bucket, this.prefix
 146  
                 ),
 147  
                 ex
 148  
             );
 149  
         }
 150  
     }
 151  
 
 152  
 }