Coverage Report - com.jcabi.dynamo.ScanValve
 
Classes in this File Line Coverage Branch Coverage Complexity
ScanValve
54%
17/31
0%
0/8
1.636
ScanValve$AjcClosure1
100%
1/1
N/A
1.636
ScanValve$AjcClosure3
0%
0/1
N/A
1.636
ScanValve$AjcClosure5
0%
0/1
N/A
1.636
ScanValve$AjcClosure7
0%
0/1
N/A
1.636
ScanValve$AjcClosure9
0%
0/1
N/A
1.636
ScanValve$NextDosage
47%
9/19
3%
1/32
1.636
ScanValve$NextDosage$AjcClosure1
100%
1/1
N/A
1.636
ScanValve$NextDosage$AjcClosure3
100%
1/1
N/A
1.636
ScanValve$NextDosage$AjcClosure5
0%
0/1
N/A
1.636
 
 1  2
 /**
 2  
  * Copyright (c) 2012-2016, 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.dynamo;
 31  
 
 32  
 import com.amazonaws.AmazonClientException;
 33  
 import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
 34  
 import com.amazonaws.services.dynamodbv2.model.AttributeValue;
 35  
 import com.amazonaws.services.dynamodbv2.model.Condition;
 36  
 import com.amazonaws.services.dynamodbv2.model.ReturnConsumedCapacity;
 37  
 import com.amazonaws.services.dynamodbv2.model.ScanRequest;
 38  
 import com.amazonaws.services.dynamodbv2.model.ScanResult;
 39  
 import com.amazonaws.services.dynamodbv2.model.Select;
 40  
 import com.google.common.collect.Iterables;
 41  
 import com.jcabi.aspects.Immutable;
 42  
 import com.jcabi.aspects.Loggable;
 43  
 import com.jcabi.aspects.Tv;
 44  
 import com.jcabi.log.Logger;
 45  
 import java.io.IOException;
 46  
 import java.util.ArrayList;
 47  
 import java.util.Arrays;
 48  
 import java.util.Collection;
 49  
 import java.util.Collections;
 50  
 import java.util.HashSet;
 51  
 import java.util.List;
 52  
 import java.util.Map;
 53  
 import lombok.EqualsAndHashCode;
 54  
 import lombok.ToString;
 55  
 
 56  
 /**
 57  
  * Scan-based valve.
 58  
  *
 59  
  * @author Yegor Bugayenko (yegor@tpc2.com)
 60  
  * @version $Id: 551405bd21e92b9bd602ef8a9443b38e499c3663 $
 61  
  * @since 0.1
 62  
  */
 63  
 @Immutable
 64  4
 @ToString
 65  
 @Loggable(Loggable.DEBUG)
 66  0
 @EqualsAndHashCode(of = { "limit", "attributes" })
 67  
 public final class ScanValve implements Valve {
 68  
 
 69  
     /**
 70  
      * Limit to use for every query.
 71  
      */
 72  
     private final transient int limit;
 73  
 
 74  
     /**
 75  
      * Attributes to fetch.
 76  
      */
 77  
     @Immutable.Array
 78  
     private final transient String[] attributes;
 79  
 
 80  
     /**
 81  
      * Public ctor.
 82  
      */
 83  
     public ScanValve() {
 84  5
         this(Tv.HUNDRED, new ArrayList<String>(0));
 85  5
     }
 86  
 
 87  
     /**
 88  
      * Public ctor.
 89  
      * @param lmt Limit
 90  
      * @param attrs Attributes to pre-load
 91  
      */
 92  5
     private ScanValve(final int lmt, final Iterable<String> attrs) {
 93  5
         this.limit = lmt;
 94  5
         this.attributes = Iterables.toArray(attrs, String.class);
 95  5
     }
 96  
 
 97  
     // @checkstyle ParameterNumber (5 lines)
 98  
     @Override
 99  
     public Dosage fetch(final Credentials credentials,
 100  
         final String table, final Map<String, Condition> conditions,
 101  
         final Collection<String> keys) throws IOException {
 102  2
         final AmazonDynamoDB aws = credentials.aws();
 103  
         try {
 104  1
             final Collection<String> attrs = new HashSet<String>(
 105  
                 Arrays.asList(this.attributes)
 106  
             );
 107  1
             attrs.addAll(keys);
 108  1
             final ScanRequest request = new ScanRequest()
 109  
                 .withTableName(table)
 110  
                 .withAttributesToGet(attrs)
 111  
                 .withReturnConsumedCapacity(ReturnConsumedCapacity.TOTAL)
 112  
                 .withScanFilter(conditions)
 113  
                 .withLimit(this.limit);
 114  1
             final long start = System.currentTimeMillis();
 115  1
             final ScanResult result = aws.scan(request);
 116  1
             Logger.info(
 117  
                 this,
 118  
                 "#items(): loaded %d item(s) from '%s' using %s, %s, in %[ms]s",
 119  
                 result.getCount(), table, conditions,
 120  
                 new PrintableConsumedCapacity(
 121  
                     result.getConsumedCapacity()
 122  
                 ).print(),
 123  
                 System.currentTimeMillis() - start
 124  
             );
 125  1
             return new ScanValve.NextDosage(credentials, request, result);
 126  0
         } catch (final AmazonClientException ex) {
 127  0
             throw new IOException(
 128  
                 String.format(
 129  
                     "failed to fetch from \"%s\" by %s and %s",
 130  
                     table, conditions, keys
 131  
                 ),
 132  
                 ex
 133  
             );
 134  
         } finally {
 135  1
             aws.shutdown();
 136  
         }
 137  
     }
 138  
 
 139  
     @Override
 140  
     public int count(final Credentials credentials, final String table,
 141  
         final Map<String, Condition> conditions) {
 142  0
         final AmazonDynamoDB aws = credentials.aws();
 143  
         try {
 144  0
             final ScanRequest request = new ScanRequest()
 145  
                 .withTableName(table)
 146  
                 .withReturnConsumedCapacity(ReturnConsumedCapacity.TOTAL)
 147  
                 .withScanFilter(conditions)
 148  
                 .withSelect(Select.COUNT)
 149  
                 .withLimit(Integer.MAX_VALUE);
 150  0
             final long start = System.currentTimeMillis();
 151  0
             final ScanResult result = aws.scan(request);
 152  0
             final int count = result.getCount();
 153  0
             Logger.info(
 154  
                 this,
 155  
                 // @checkstyle LineLength (1 line)
 156  
                 "#total(): COUNT=%d in '%s' using %s, %s, in %[ms]s",
 157  
                 count, request.getTableName(), request.getFilterExpression(),
 158  
                 new PrintableConsumedCapacity(
 159  
                     result.getConsumedCapacity()
 160  
                 ).print(),
 161  
                 System.currentTimeMillis() - start
 162  
             );
 163  0
             return count;
 164  
         } finally {
 165  0
             aws.shutdown();
 166  
         }
 167  
     }
 168  
 
 169  
     /**
 170  
      * With given limit.
 171  
      * @param lmt Limit to use
 172  
      * @return New query valve
 173  
      */
 174  
     public ScanValve withLimit(final int lmt) {
 175  0
         return new ScanValve(lmt, Arrays.asList(this.attributes));
 176  
     }
 177  
 
 178  
     /**
 179  
      * With this extra attribute to pre-fetch.
 180  
      * @param name Name of attribute to pre-load
 181  
      * @return New query valve
 182  
      */
 183  
     public ScanValve withAttributeToGet(final String name) {
 184  0
         return new ScanValve(
 185  
             this.limit,
 186  
             Iterables.concat(
 187  
                 Arrays.asList(this.attributes),
 188  
                 Collections.singletonList(name)
 189  
             )
 190  
         );
 191  
     }
 192  
 
 193  
     /**
 194  
      * With these extra attributes to pre-fetch.
 195  
      * @param names Name of attributes to pre-load
 196  
      * @return New query valve
 197  
      */
 198  
     public ScanValve withAttributeToGet(final String... names) {
 199  0
         return new ScanValve(
 200  
             this.limit,
 201  
             Iterables.concat(
 202  
                 Arrays.asList(this.attributes),
 203  
                 Arrays.asList(names)
 204  
             )
 205  
         );
 206  
     }
 207  
 
 208  
     /**
 209  
      * Next dosage.
 210  
      */
 211  1
     @ToString
 212  
     @Loggable(Loggable.DEBUG)
 213  0
     @EqualsAndHashCode(of = { "credentials", "request", "result" })
 214  
     private final class NextDosage implements Dosage {
 215  
         /**
 216  
          * AWS client.
 217  
          */
 218  
         private final transient Credentials credentials;
 219  
         /**
 220  
          * Query request.
 221  
          */
 222  
         private final transient ScanRequest request;
 223  
         /**
 224  
          * Query request.
 225  
          */
 226  
         private final transient ScanResult result;
 227  
         /**
 228  
          * Public ctor.
 229  
          * @param creds Credentials
 230  
          * @param rqst Query request
 231  
          * @param rslt Query result
 232  
          */
 233  
         NextDosage(final Credentials creds, final ScanRequest rqst,
 234  1
             final ScanResult rslt) {
 235  1
             this.credentials = creds;
 236  1
             this.request = rqst;
 237  1
             this.result = rslt;
 238  1
         }
 239  
         @Override
 240  
         public List<Map<String, AttributeValue>> items() {
 241  2
             return this.result.getItems();
 242  
         }
 243  
         @Override
 244  
         public boolean hasNext() {
 245  2
             return this.result.getLastEvaluatedKey() != null;
 246  
         }
 247  
         @Override
 248  
         public Dosage next() {
 249  0
             if (!this.hasNext()) {
 250  0
                 throw new IllegalStateException(
 251  
                     "nothing left in the iterator"
 252  
                 );
 253  
             }
 254  0
             final AmazonDynamoDB aws = this.credentials.aws();
 255  
             try {
 256  0
                 final ScanRequest rqst = this.request.withExclusiveStartKey(
 257  
                     this.result.getLastEvaluatedKey()
 258  
                 );
 259  0
                 final long start = System.currentTimeMillis();
 260  0
                 final ScanResult rslt = aws.scan(rqst);
 261  0
                 Logger.info(
 262  
                     this,
 263  
                     // @checkstyle LineLength (1 line)
 264  
                     "#next(): loaded %d item(s) from '%s' using %s, %s, in %[ms]s",
 265  
                     rslt.getCount(), rqst.getTableName(), rqst.getScanFilter(),
 266  
                     new PrintableConsumedCapacity(
 267  
                         rslt.getConsumedCapacity()
 268  
                     ).print(),
 269  
                     System.currentTimeMillis() - start
 270  
                 );
 271  0
                 return new ScanValve.NextDosage(this.credentials, rqst, rslt);
 272  
             } finally {
 273  0
                 aws.shutdown();
 274  
             }
 275  
         }
 276  
     }
 277  
 }