| 1 | 2 | |
| 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.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 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 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 | |
|
| 71 | |
|
| 72 | |
private final transient int limit; |
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
@Immutable.Array |
| 78 | |
private final transient String[] attributes; |
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
public ScanValve() { |
| 84 | 5 | this(Tv.HUNDRED, new ArrayList<String>(0)); |
| 85 | 5 | } |
| 86 | |
|
| 87 | |
|
| 88 | |
|
| 89 | |
|
| 90 | |
|
| 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 | |
|
| 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 | |
|
| 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 | |
|
| 171 | |
|
| 172 | |
|
| 173 | |
|
| 174 | |
public ScanValve withLimit(final int lmt) { |
| 175 | 0 | return new ScanValve(lmt, Arrays.asList(this.attributes)); |
| 176 | |
} |
| 177 | |
|
| 178 | |
|
| 179 | |
|
| 180 | |
|
| 181 | |
|
| 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 | |
|
| 195 | |
|
| 196 | |
|
| 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 | |
|
| 210 | |
|
| 211 | 1 | @ToString |
| 212 | |
@Loggable(Loggable.DEBUG) |
| 213 | 0 | @EqualsAndHashCode(of = { "credentials", "request", "result" }) |
| 214 | |
private final class NextDosage implements Dosage { |
| 215 | |
|
| 216 | |
|
| 217 | |
|
| 218 | |
private final transient Credentials credentials; |
| 219 | |
|
| 220 | |
|
| 221 | |
|
| 222 | |
private final transient ScanRequest request; |
| 223 | |
|
| 224 | |
|
| 225 | |
|
| 226 | |
private final transient ScanResult result; |
| 227 | |
|
| 228 | |
|
| 229 | |
|
| 230 | |
|
| 231 | |
|
| 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 | |
|
| 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 | |
} |