|  1 |    4 |      | 
  |  2 |     | 
     | 
  |  3 |     | 
     | 
  |  4 |     | 
     | 
  |  5 |     | 
     | 
  |  6 |     | 
     | 
  |  7 |     | 
     | 
  |  8 |     | 
     | 
  |  9 |     | 
     | 
  |  10 |     | 
     | 
  |  11 |     | 
     | 
  |  12 |     | 
     | 
  |  13 |     | 
     | 
  |  14 |     | 
     | 
  |  15 |     | 
     | 
  |  16 |     | 
     | 
  |  17 |     | 
     | 
  |  18 |     | 
     | 
  |  19 |     | 
     | 
  |  20 |     | 
     | 
  |  21 |     | 
     | 
  |  22 |     | 
     | 
  |  23 |     | 
     | 
  |  24 |     | 
     | 
  |  25 |     | 
     | 
  |  26 |     | 
     | 
  |  27 |     | 
     | 
  |  28 |     | 
     | 
  |  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.AttributeValueUpdate;  | 
  |  36 |     | 
   import com.amazonaws.services.dynamodbv2.model.GetItemRequest;  | 
  |  37 |     | 
   import com.amazonaws.services.dynamodbv2.model.GetItemResult;  | 
  |  38 |     | 
   import com.amazonaws.services.dynamodbv2.model.ReturnConsumedCapacity;  | 
  |  39 |     | 
   import com.amazonaws.services.dynamodbv2.model.ReturnValue;  | 
  |  40 |     | 
   import com.amazonaws.services.dynamodbv2.model.UpdateItemRequest;  | 
  |  41 |     | 
   import com.amazonaws.services.dynamodbv2.model.UpdateItemResult;  | 
  |  42 |     | 
   import com.jcabi.aspects.Immutable;  | 
  |  43 |     | 
   import com.jcabi.aspects.Loggable;  | 
  |  44 |     | 
   import com.jcabi.immutable.Array;  | 
  |  45 |     | 
   import com.jcabi.log.Logger;  | 
  |  46 |     | 
   import java.io.IOException;  | 
  |  47 |     | 
   import java.util.Collections;  | 
  |  48 |     | 
   import java.util.Locale;  | 
  |  49 |     | 
   import java.util.Map;  | 
  |  50 |     | 
   import java.util.NoSuchElementException;  | 
  |  51 |     | 
   import lombok.EqualsAndHashCode;  | 
  |  52 |     | 
   import lombok.ToString;  | 
  |  53 |     | 
     | 
  |  54 |     | 
     | 
  |  55 |     | 
     | 
  |  56 |     | 
     | 
  |  57 |     | 
     | 
  |  58 |     | 
     | 
  |  59 |     | 
     | 
  |  60 |     | 
     | 
  |  61 |     | 
   @Immutable  | 
  |  62 |     | 
   @Loggable(Loggable.DEBUG)  | 
  |  63 |    3 |    @ToString  | 
  |  64 |    1 |    @EqualsAndHashCode(of = { "credentials", "frm", "name", "attributes" }) | 
  |  65 |     | 
   final class AwsItem implements Item { | 
  |  66 |     | 
     | 
  |  67 |     | 
         | 
  |  68 |     | 
     | 
  |  69 |     | 
     | 
  |  70 |     | 
       private final transient Credentials credentials;  | 
  |  71 |     | 
     | 
  |  72 |     | 
         | 
  |  73 |     | 
     | 
  |  74 |     | 
     | 
  |  75 |     | 
       private final transient AwsFrame frm;  | 
  |  76 |     | 
     | 
  |  77 |     | 
         | 
  |  78 |     | 
     | 
  |  79 |     | 
     | 
  |  80 |     | 
       private final transient String name;  | 
  |  81 |     | 
     | 
  |  82 |     | 
         | 
  |  83 |     | 
     | 
  |  84 |     | 
     | 
  |  85 |     | 
       private final transient Attributes attributes;  | 
  |  86 |     | 
     | 
  |  87 |     | 
         | 
  |  88 |     | 
     | 
  |  89 |     | 
     | 
  |  90 |     | 
       private final transient Array<String> keys;  | 
  |  91 |     | 
     | 
  |  92 |     | 
         | 
  |  93 |     | 
     | 
  |  94 |     | 
     | 
  |  95 |     | 
     | 
  |  96 |     | 
     | 
  |  97 |     | 
     | 
  |  98 |     | 
     | 
  |  99 |     | 
     | 
  |  100 |     | 
     | 
  |  101 |     | 
       AwsItem(final Credentials creds, final AwsFrame frame,  | 
  |  102 |     | 
           final String table, final Attributes attrs,  | 
  |  103 |    5 |            final Array<String> pks) { | 
  |  104 |    5 |            this.credentials = creds;  | 
  |  105 |    5 |            this.frm = frame;  | 
  |  106 |    5 |            this.name = table;  | 
  |  107 |    5 |            this.attributes = attrs;  | 
  |  108 |    5 |            this.keys = pks;  | 
  |  109 |    5 |        }  | 
  |  110 |     | 
     | 
  |  111 |     | 
       @Override  | 
  |  112 |     | 
       public boolean has(final String attr) throws IOException { | 
  |  113 |    0 |            final String attrib = String.format(Locale.ENGLISH, attr);  | 
  |  114 |    0 |            boolean has = this.attributes.containsKey(attrib);  | 
  |  115 |    0 |            if (!has) { | 
  |  116 |    0 |                final AmazonDynamoDB aws = this.credentials.aws();  | 
  |  117 |     | 
               try { | 
  |  118 |    0 |                    final GetItemRequest request = new GetItemRequest();  | 
  |  119 |    0 |                    request.setTableName(this.name);  | 
  |  120 |    0 |                    request.setAttributesToGet(Collections.singletonList(attr));  | 
  |  121 |    0 |                    request.setKey(this.attributes.only(this.keys));  | 
  |  122 |    0 |                    request.setReturnConsumedCapacity(ReturnConsumedCapacity.TOTAL);  | 
  |  123 |    0 |                    request.setConsistentRead(true);  | 
  |  124 |    0 |                    final long start = System.currentTimeMillis();  | 
  |  125 |    0 |                    final GetItemResult result = aws.getItem(request);  | 
  |  126 |    0 |                    has = result.getItem().get(attrib) != null;  | 
  |  127 |    0 |                    Logger.info(  | 
  |  128 |     | 
                       this, "#has('%s'): %B from DynamoDB, %s, in %[ms]s", | 
  |  129 |     | 
                       attr, has,  | 
  |  130 |     | 
                       new PrintableConsumedCapacity(  | 
  |  131 |     | 
                           result.getConsumedCapacity()  | 
  |  132 |     | 
                       ).print(),  | 
  |  133 |     | 
                       System.currentTimeMillis() - start  | 
  |  134 |     | 
                   );  | 
  |  135 |    0 |                } catch (final AmazonClientException ex) { | 
  |  136 |    0 |                    throw new IOException(  | 
  |  137 |     | 
                       String.format(  | 
  |  138 |     | 
                           "failed to check existence of \"%s\" at \"%s\" by %s",  | 
  |  139 |     | 
                           attr, this.name, this.keys  | 
  |  140 |     | 
                       ),  | 
  |  141 |     | 
                       ex  | 
  |  142 |     | 
                   );  | 
  |  143 |     | 
               } finally { | 
  |  144 |    0 |                    aws.shutdown();  | 
  |  145 |    0 |                }  | 
  |  146 |     | 
           }  | 
  |  147 |    0 |            return has;  | 
  |  148 |     | 
       }  | 
  |  149 |     | 
     | 
  |  150 |     | 
       @Override  | 
  |  151 |     | 
       public AttributeValue get(final String attr) throws IOException { | 
  |  152 |    4 |            final String attrib = String.format(Locale.ENGLISH, attr);  | 
  |  153 |    2 |            AttributeValue value = this.attributes.get(attrib);  | 
  |  154 |    2 |            if (value == null) { | 
  |  155 |    0 |                final AmazonDynamoDB aws = this.credentials.aws();  | 
  |  156 |     | 
               try { | 
  |  157 |    0 |                    final GetItemRequest request = new GetItemRequest();  | 
  |  158 |    0 |                    request.setTableName(this.name);  | 
  |  159 |    0 |                    request.setAttributesToGet(Collections.singletonList(attrib));  | 
  |  160 |    0 |                    request.setKey(this.attributes.only(this.keys));  | 
  |  161 |    0 |                    request.setReturnConsumedCapacity(ReturnConsumedCapacity.TOTAL);  | 
  |  162 |    0 |                    request.setConsistentRead(true);  | 
  |  163 |    0 |                    final long start = System.currentTimeMillis();  | 
  |  164 |    0 |                    final GetItemResult result = aws.getItem(request);  | 
  |  165 |    0 |                    value = result.getItem().get(attrib);  | 
  |  166 |    0 |                    Logger.info(  | 
  |  167 |     | 
                       this,  | 
  |  168 |     | 
                         | 
  |  169 |     | 
                       "#get('%s'): loaded '%[text]s' from DynamoDB, %s, in %[ms]s", | 
  |  170 |     | 
                       attrib, value,  | 
  |  171 |     | 
                       new PrintableConsumedCapacity(  | 
  |  172 |     | 
                           result.getConsumedCapacity()  | 
  |  173 |     | 
                       ).print(),  | 
  |  174 |     | 
                       System.currentTimeMillis() - start  | 
  |  175 |     | 
                   );  | 
  |  176 |    0 |                } catch (final AmazonClientException ex) { | 
  |  177 |    0 |                    throw new IOException(  | 
  |  178 |     | 
                       String.format(  | 
  |  179 |     | 
                           "failed to get \"%s\" from \"%s\" by %s",  | 
  |  180 |     | 
                           attr, this.name, this.keys  | 
  |  181 |     | 
                       ),  | 
  |  182 |     | 
                       ex  | 
  |  183 |     | 
                   );  | 
  |  184 |     | 
               } finally { | 
  |  185 |    0 |                    aws.shutdown();  | 
  |  186 |    0 |                }  | 
  |  187 |     | 
           }  | 
  |  188 |    2 |            if (value == null) { | 
  |  189 |    0 |                throw new NoSuchElementException(  | 
  |  190 |     | 
                   String.format("attribute \"%s\" not found", attr) | 
  |  191 |     | 
               );  | 
  |  192 |     | 
           }  | 
  |  193 |    2 |            return value;  | 
  |  194 |     | 
       }  | 
  |  195 |     | 
     | 
  |  196 |     | 
       @Override  | 
  |  197 |     | 
       public Map<String, AttributeValue> put(final String attr,  | 
  |  198 |     | 
           final AttributeValueUpdate value) throws IOException { | 
  |  199 |    0 |            return this.put(new AttributeUpdates().with(attr, value));  | 
  |  200 |     | 
       }  | 
  |  201 |     | 
     | 
  |  202 |     | 
       @Override  | 
  |  203 |     | 
       public Map<String, AttributeValue> put(  | 
  |  204 |     | 
           final Map<String, AttributeValueUpdate> attrs) throws IOException { | 
  |  205 |    0 |            final AmazonDynamoDB aws = this.credentials.aws();  | 
  |  206 |    0 |            final Attributes expected = this.attributes.only(this.keys);  | 
  |  207 |     | 
           try { | 
  |  208 |    0 |                final UpdateItemRequest request = new UpdateItemRequest()  | 
  |  209 |     | 
                   .withTableName(this.name)  | 
  |  210 |     | 
                   .withExpected(expected.asKeys())  | 
  |  211 |     | 
                   .withKey(expected)  | 
  |  212 |     | 
                   .withAttributeUpdates(attrs)  | 
  |  213 |     | 
                   .withReturnConsumedCapacity(ReturnConsumedCapacity.TOTAL)  | 
  |  214 |     | 
                   .withReturnValues(ReturnValue.UPDATED_NEW);  | 
  |  215 |    0 |                final long start = System.currentTimeMillis();  | 
  |  216 |    0 |                final UpdateItemResult result = aws.updateItem(request);  | 
  |  217 |    0 |                Logger.info(  | 
  |  218 |     | 
                   this, "#put('%s'): updated item to DynamoDB, %s, in %[ms]s", | 
  |  219 |     | 
                   attrs,  | 
  |  220 |     | 
                   new PrintableConsumedCapacity(  | 
  |  221 |     | 
                       result.getConsumedCapacity()  | 
  |  222 |     | 
                   ).print(),  | 
  |  223 |     | 
                   System.currentTimeMillis() - start  | 
  |  224 |     | 
               );  | 
  |  225 |    0 |                return result.getAttributes();  | 
  |  226 |    0 |            } catch (final AmazonClientException ex) { | 
  |  227 |    0 |                throw new IOException(  | 
  |  228 |     | 
                   String.format(  | 
  |  229 |     | 
                       "failed to put %s into \"%s\" with %s",  | 
  |  230 |     | 
                       attrs, this.name, this.keys  | 
  |  231 |     | 
                   ),  | 
  |  232 |     | 
                   ex  | 
  |  233 |     | 
               );  | 
  |  234 |     | 
           } finally { | 
  |  235 |    0 |                aws.shutdown();  | 
  |  236 |     | 
           }  | 
  |  237 |     | 
       }  | 
  |  238 |     | 
     | 
  |  239 |     | 
       @Override  | 
  |  240 |     | 
       public Frame frame() { | 
  |  241 |    0 |            return this.frm;  | 
  |  242 |     | 
       }  | 
  |  243 |     | 
     | 
  |  244 |     | 
   }  |