Coverage Report - com.jcabi.dynamo.QueryValve
 
Classes in this File Line Coverage Branch Coverage Complexity
QueryValve
51%
23/45
12%
2/16
1.667
QueryValve$AjcClosure1
100%
1/1
N/A
1.667
QueryValve$AjcClosure11
0%
0/1
N/A
1.667
QueryValve$AjcClosure13
0%
0/1
N/A
1.667
QueryValve$AjcClosure15
0%
0/1
N/A
1.667
QueryValve$AjcClosure17
0%
0/1
N/A
1.667
QueryValve$AjcClosure3
0%
0/1
N/A
1.667
QueryValve$AjcClosure5
0%
0/1
N/A
1.667
QueryValve$AjcClosure7
0%
0/1
N/A
1.667
QueryValve$AjcClosure9
0%
0/1
N/A
1.667
QueryValve$NextDosage
47%
9/19
3%
1/32
1.667
QueryValve$NextDosage$AjcClosure1
100%
1/1
N/A
1.667
QueryValve$NextDosage$AjcClosure3
100%
1/1
N/A
1.667
QueryValve$NextDosage$AjcClosure5
0%
0/1
N/A
1.667
 
 1  0
 /**
 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.QueryRequest;
 37  
 import com.amazonaws.services.dynamodbv2.model.QueryResult;
 38  
 import com.amazonaws.services.dynamodbv2.model.ReturnConsumedCapacity;
 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  
  * Query-based valve.
 58  
  *
 59  
  * @author Yegor Bugayenko (yegor@tpc2.com)
 60  
  * @version $Id: 6b61c33dfec8eddb030f320f015503ceb7a26000 $
 61  
  * @since 0.1
 62  
  */
 63  
 @Immutable
 64  0
 @ToString
 65  
 @Loggable(Loggable.DEBUG)
 66  0
 @EqualsAndHashCode(of = { "limit", "forward" })
 67  
 @SuppressWarnings("PMD.TooManyMethods")
 68  
 public final class QueryValve implements Valve {
 69  
 
 70  
     /**
 71  
      * Limit to use for every query.
 72  
      */
 73  
     private final transient int limit;
 74  
 
 75  
     /**
 76  
      * Forward/reverse order.
 77  
      */
 78  
     private final transient boolean forward;
 79  
 
 80  
     /**
 81  
      * Attributes to fetch.
 82  
      */
 83  
     @Immutable.Array
 84  
     private final transient String[] attributes;
 85  
 
 86  
     /**
 87  
      * Index name.
 88  
      */
 89  
     private final transient String index;
 90  
 
 91  
     /**
 92  
      * What attributes to select.
 93  
      */
 94  
     private final transient String select;
 95  
 
 96  
     /**
 97  
      * Consistent read.
 98  
      */
 99  
     private final transient boolean consistent;
 100  
 
 101  
     /**
 102  
      * Public ctor.
 103  
      */
 104  
     public QueryValve() {
 105  1
         this(
 106  
             Tv.TWENTY, true, new ArrayList<String>(0),
 107  
             "", Select.SPECIFIC_ATTRIBUTES.toString(), true
 108  
         );
 109  1
     }
 110  
 
 111  
     /**
 112  
      * Public ctor.
 113  
      * @param lmt Limit
 114  
      * @param fwd Forward
 115  
      * @param attrs Names of attributes to pre-fetch
 116  
      * @param idx Index name or empty string
 117  
      * @param slct Select
 118  
      * @param cnst Consistent read
 119  
      * @checkstyle ParameterNumber (5 lines)
 120  
      */
 121  
     private QueryValve(final int lmt, final boolean fwd,
 122  
         final Iterable<String> attrs, final String idx,
 123  1
         final String slct, final boolean cnst) {
 124  1
         this.limit = lmt;
 125  1
         this.forward = fwd;
 126  1
         this.attributes = Iterables.toArray(attrs, String.class);
 127  1
         this.index = idx;
 128  1
         this.select = slct;
 129  1
         this.consistent = cnst;
 130  1
     }
 131  
 
 132  
     // @checkstyle ParameterNumber (5 lines)
 133  
     @Override
 134  
     public Dosage fetch(final Credentials credentials, final String table,
 135  
         final Map<String, Condition> conditions, final Collection<String> keys)
 136  
         throws IOException {
 137  2
         final AmazonDynamoDB aws = credentials.aws();
 138  
         try {
 139  1
             final Collection<String> attrs = new HashSet<String>(
 140  
                 Arrays.asList(this.attributes)
 141  
             );
 142  1
             attrs.addAll(keys);
 143  1
             QueryRequest request = new QueryRequest()
 144  
                 .withTableName(table)
 145  
                 .withReturnConsumedCapacity(ReturnConsumedCapacity.TOTAL)
 146  
                 .withKeyConditions(conditions)
 147  
                 .withConsistentRead(this.consistent)
 148  
                 .withScanIndexForward(this.forward)
 149  
                 .withSelect(this.select)
 150  
                 .withLimit(this.limit);
 151  1
             if (this.select.equals(Select.SPECIFIC_ATTRIBUTES.toString())) {
 152  1
                 request = request.withAttributesToGet(attrs);
 153  
             }
 154  1
             if (!this.index.isEmpty()) {
 155  0
                 request = request.withIndexName(this.index);
 156  
             }
 157  1
             final long start = System.currentTimeMillis();
 158  1
             final QueryResult result = aws.query(request);
 159  1
             Logger.info(
 160  
                 this,
 161  
                 "#items(): loaded %d item(s) from '%s' using %s, %s, in %[ms]s",
 162  
                 result.getCount(), table, conditions,
 163  
                 new PrintableConsumedCapacity(
 164  
                     result.getConsumedCapacity()
 165  
                 ).print(),
 166  
                 System.currentTimeMillis() - start
 167  
             );
 168  1
             return new QueryValve.NextDosage(credentials, request, result);
 169  0
         } catch (final AmazonClientException ex) {
 170  0
             throw new IOException(
 171  
                 String.format(
 172  
                     "failed to fetch from \"%s\" by %s and %s",
 173  
                     table, conditions, keys
 174  
                 ),
 175  
                 ex
 176  
             );
 177  
         } finally {
 178  1
             aws.shutdown();
 179  
         }
 180  
     }
 181  
 
 182  
     @Override
 183  
     public int count(final Credentials credentials, final String table,
 184  
         final Map<String, Condition> conditions) throws IOException {
 185  0
         final AmazonDynamoDB aws = credentials.aws();
 186  
         try {
 187  0
             QueryRequest request = new QueryRequest()
 188  
                 .withTableName(table)
 189  
                 .withReturnConsumedCapacity(ReturnConsumedCapacity.TOTAL)
 190  
                 .withKeyConditions(conditions)
 191  
                 .withConsistentRead(this.consistent)
 192  
                 .withSelect(Select.COUNT)
 193  
                 .withLimit(Integer.MAX_VALUE);
 194  0
             if (!this.index.isEmpty()) {
 195  0
                 request = request.withIndexName(this.index);
 196  
             }
 197  0
             final long start = System.currentTimeMillis();
 198  0
             final QueryResult rslt = aws.query(request);
 199  0
             final int count = rslt.getCount();
 200  0
             Logger.info(
 201  
                 this,
 202  
                 // @checkstyle LineLength (1 line)
 203  
                 "#total(): COUNT=%d in '%s' using %s, %s, in %[ms]s",
 204  
                 count, request.getTableName(), request.getQueryFilter(),
 205  
                 new PrintableConsumedCapacity(
 206  
                     rslt.getConsumedCapacity()
 207  
                 ).print(),
 208  
                 System.currentTimeMillis() - start
 209  
             );
 210  0
             return count;
 211  
         } finally {
 212  0
             aws.shutdown();
 213  
         }
 214  
     }
 215  
 
 216  
     /**
 217  
      * With consistent read.
 218  
      * @param cnst Consistent read
 219  
      * @return New query valve
 220  
      * @see QueryRequest#withConsistentRead(Boolean)
 221  
      * @since 0.12
 222  
      * @checkstyle AvoidDuplicateLiterals (5 line)
 223  
      */
 224  
     public QueryValve withConsistentRead(final boolean cnst) {
 225  0
         return new QueryValve(
 226  
             this.limit, this.forward,
 227  
             Arrays.asList(this.attributes),
 228  
             this.index, this.select, cnst
 229  
         );
 230  
     }
 231  
 
 232  
     /**
 233  
      * With index name.
 234  
      * @param idx Index name
 235  
      * @return New query valve
 236  
      * @see QueryRequest#withIndexName(String)
 237  
      * @since 0.10.2
 238  
      * @checkstyle AvoidDuplicateLiterals (5 line)
 239  
      */
 240  
     public QueryValve withIndexName(final String idx) {
 241  0
         return new QueryValve(
 242  
             this.limit, this.forward,
 243  
             Arrays.asList(this.attributes),
 244  
             idx, this.select, this.consistent
 245  
         );
 246  
     }
 247  
 
 248  
     /**
 249  
      * With attributes to select.
 250  
      * @param slct Select to use
 251  
      * @return New query valve
 252  
      * @see QueryRequest#withSelect(Select)
 253  
      * @since 0.10.2
 254  
      * @checkstyle AvoidDuplicateLiterals (5 line)
 255  
      */
 256  
     public QueryValve withSelect(final Select slct) {
 257  0
         return new QueryValve(
 258  
             this.limit, this.forward,
 259  
             Arrays.asList(this.attributes), this.index,
 260  
             slct.toString(), this.consistent
 261  
         );
 262  
     }
 263  
 
 264  
     /**
 265  
      * With given limit.
 266  
      * @param lmt Limit to use
 267  
      * @return New query valve
 268  
      * @see QueryRequest#withLimit(Integer)
 269  
      * @checkstyle AvoidDuplicateLiterals (5 line)
 270  
      */
 271  
     public QueryValve withLimit(final int lmt) {
 272  0
         return new QueryValve(
 273  
             lmt, this.forward,
 274  
             Arrays.asList(this.attributes),
 275  
             this.index, this.select, this.consistent
 276  
         );
 277  
     }
 278  
 
 279  
     /**
 280  
      * With scan index forward flag.
 281  
      * @param fwd Forward flag
 282  
      * @return New query valve
 283  
      * @see QueryRequest#withScanIndexForward(Boolean)
 284  
      * @checkstyle AvoidDuplicateLiterals (5 line)
 285  
      */
 286  
     public QueryValve withScanIndexForward(final boolean fwd) {
 287  0
         return new QueryValve(
 288  
             this.limit, fwd,
 289  
             Arrays.asList(this.attributes),
 290  
             this.index, this.select, this.consistent
 291  
         );
 292  
     }
 293  
 
 294  
     /**
 295  
      * With this extra attribute to pre-fetch.
 296  
      * @param name Name of attribute to pre-load
 297  
      * @return New query valve
 298  
      * @see QueryRequest#withAttributesToGet(Collection)
 299  
      * @checkstyle AvoidDuplicateLiterals (5 line)
 300  
      */
 301  
     public QueryValve withAttributeToGet(final String name) {
 302  0
         return new QueryValve(
 303  
             this.limit, this.forward,
 304  
             Iterables.concat(
 305  
                 Arrays.asList(this.attributes),
 306  
                 Collections.singleton(name)
 307  
             ),
 308  
             this.index, this.select, this.consistent
 309  
         );
 310  
     }
 311  
 
 312  
     /**
 313  
      * With these extra attributes to pre-fetch.
 314  
      * @param names Name of attributes to pre-load
 315  
      * @return New query valve
 316  
      * @see QueryRequest#withAttributesToGet(Collection)
 317  
      * @checkstyle AvoidDuplicateLiterals (5 line)
 318  
      */
 319  
     public QueryValve withAttributesToGet(final String... names) {
 320  0
         return new QueryValve(
 321  
             this.limit, this.forward,
 322  
             Iterables.concat(
 323  
                 Arrays.asList(this.attributes),
 324  
                 Arrays.asList(names)
 325  
             ),
 326  
             this.index,
 327  
             this.select, this.consistent
 328  
         );
 329  
     }
 330  
 
 331  
     /**
 332  
      * Next dosage.
 333  
      */
 334  1
     @ToString
 335  
     @Loggable(Loggable.DEBUG)
 336  0
     @EqualsAndHashCode(of = { "credentials", "request", "result" })
 337  
     private final class NextDosage implements Dosage {
 338  
         /**
 339  
          * AWS client.
 340  
          */
 341  
         private final transient Credentials credentials;
 342  
         /**
 343  
          * Query request.
 344  
          */
 345  
         private final transient QueryRequest request;
 346  
         /**
 347  
          * Query request.
 348  
          */
 349  
         private final transient QueryResult result;
 350  
         /**
 351  
          * Public ctor.
 352  
          * @param creds Credentials
 353  
          * @param rqst Query request
 354  
          * @param rslt Query result
 355  
          */
 356  
         NextDosage(final Credentials creds, final QueryRequest rqst,
 357  1
             final QueryResult rslt) {
 358  1
             this.credentials = creds;
 359  1
             this.request = rqst;
 360  1
             this.result = rslt;
 361  1
         }
 362  
         @Override
 363  
         public List<Map<String, AttributeValue>> items() {
 364  2
             return this.result.getItems();
 365  
         }
 366  
         @Override
 367  
         public boolean hasNext() {
 368  2
             return this.result.getLastEvaluatedKey() != null;
 369  
         }
 370  
         @Override
 371  
         public Dosage next() {
 372  0
             if (!this.hasNext()) {
 373  0
                 throw new IllegalStateException(
 374  
                     "nothing left in the iterator"
 375  
                 );
 376  
             }
 377  0
             final AmazonDynamoDB aws = this.credentials.aws();
 378  
             try {
 379  0
                 final QueryRequest rqst =
 380  
                     this.request.withExclusiveStartKey(
 381  
                         this.result.getLastEvaluatedKey()
 382  
                     );
 383  0
                 final long start = System.currentTimeMillis();
 384  0
                 final QueryResult rslt = aws.query(rqst);
 385  0
                 Logger.info(
 386  
                     this,
 387  
                     // @checkstyle LineLength (1 line)
 388  
                     "#next(): loaded %d item(s) from '%s' using %s, %s, in %[ms]s",
 389  
                     rslt.getCount(), rqst.getTableName(),
 390  
                     rqst.getKeyConditions(),
 391  
                     new PrintableConsumedCapacity(
 392  
                         rslt.getConsumedCapacity()
 393  
                     ).print(),
 394  
                     System.currentTimeMillis() - start
 395  
                 );
 396  0
                 return new QueryValve.NextDosage(this.credentials, rqst, rslt);
 397  
             } finally {
 398  0
                 aws.shutdown();
 399  
             }
 400  
         }
 401  
     }
 402  
 }