-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
Copy pathItemUtils.java
599 lines (567 loc) · 22.7 KB
/
ItemUtils.java
1
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
/*
* Copyright 2012-2025 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with
* the License. A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
* and limitations under the License.
*/
package com.amazonaws.services.dynamodbv2.document;
import static com.amazonaws.util.BinaryUtils.copyAllBytesFrom;
import com.amazonaws.services.dynamodbv2.document.internal.Filter;
import com.amazonaws.services.dynamodbv2.document.internal.InternalUtils;
import com.amazonaws.services.dynamodbv2.model.AttributeValue;
import com.amazonaws.services.dynamodbv2.model.AttributeValueUpdate;
import com.amazonaws.services.dynamodbv2.model.ComparisonOperator;
import com.amazonaws.services.dynamodbv2.model.Condition;
import com.amazonaws.services.dynamodbv2.model.ExpectedAttributeValue;
import com.amazonaws.util.ValidationUtils;
import java.math.BigDecimal;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* Various utilities for manipulating {@link Item} and {@link AttributeValue} objects.
*/
public final class ItemUtils {
private ItemUtils() {
}
/**
* Returns an <code>Item</code> given the low level item information;
* or null if the input is null;
*/
public static Item toItem(Map<String, AttributeValue> item) {
if (item == null) {
return null;
}
return Item.fromMap(toSimpleMapValue(item));
}
/**
* Returns a non-null list of <code>Item</code>'s given the low level
* list of item information.
*/
public static List<Item> toItemList(List<Map<String, AttributeValue>> items) {
if (items == null) {
return Collections.emptyList();
}
List<Item> result = new ArrayList<Item>(items.size());
for (Map<String, AttributeValue> item : items) {
result.add(toItem(item));
}
return result;
}
/**
* Converts an <code>Item</code> into the low-level representation;
* or null if the input is null.
*/
public static Map<String, AttributeValue> toAttributeValues(Item item) {
if (item == null)
return null;
// row with multiple attributes
Map<String, AttributeValue> result = new LinkedHashMap<String, AttributeValue>();
for (Map.Entry<String, Object> entry : item.attributes())
result.put(entry.getKey(), toAttributeValue(entry.getValue()));
return result;
}
/**
* Converts a map of string to simple objects into the low-level
* representation; or null if the input is null.
*/
public static Map<String, AttributeValue> fromSimpleMap(
Map<String, Object> map) {
if (map == null)
return null;
// row with multiple attributes
Map<String, AttributeValue> result = new LinkedHashMap<String, AttributeValue>();
for (Map.Entry<String, Object> entry : map.entrySet())
result.put(entry.getKey(), toAttributeValue(entry.getValue()));
return result;
}
/**
* Converts a list of <code>AttributeUpdate</code> into the low-level
* representation; or null if the input is null.
*/
public static Map<String, AttributeValueUpdate> toAttributeValueUpdate(
List<AttributeUpdate> attributesToUpdate) {
if (attributesToUpdate == null)
return null;
Map<String, AttributeValueUpdate> result = new LinkedHashMap<String, AttributeValueUpdate>();
for (AttributeUpdate attribute : attributesToUpdate) {
AttributeValueUpdate attributeToUpdate = new AttributeValueUpdate()
.withAction(attribute.getAction());
if (attribute.getValue() != null) {
attributeToUpdate.withValue(toAttributeValue(attribute
.getValue()));
} else if (attribute.getAttributeValues() != null) {
attributeToUpdate.withValue(toAttributeValue(attribute
.getAttributeValues()));
}
result.put(attribute.getAttributeName(), attributeToUpdate);
}
return result;
}
/**
* Converts a simple value into the low-level <code><AttributeValue/code>
* representation.
*
* @param value
* the given value which can be one of the followings:
* <ul>
* <li>String</li>
* <li>Set<String></li>
* <li>Number (including any subtypes and primitive types)</li>
* <li>Set<Number></li>
* <li>byte[]</li>
* <li>Set<byte[]></li>
* <li>ByteBuffer</li>
* <li>Set<ByteBuffer></li>
* <li>Boolean or boolean</li>
* <li>null</li>
* <li>Map<String,T>, where T can be any type on this list but must not
* induce any circular reference</li>
* <li>List<T>, where T can be any type on this list but must not induce
* any circular reference</li>
* </ul>
* @return a non-null low level representation of the input object value
*
* @throws UnsupportedOperationException
* if the input object type is not supported
*/
public static AttributeValue toAttributeValue(Object value) {
AttributeValue result = new AttributeValue();
if (value == null) {
return result.withNULL(Boolean.TRUE);
} else if (value instanceof Boolean) {
return result.withBOOL((Boolean)value);
} else if (value instanceof String) {
return result.withS((String) value);
} else if (value instanceof BigDecimal) {
BigDecimal bd = (BigDecimal) value;
return result.withN(bd.toPlainString());
} else if (value instanceof Number) {
return result.withN(value.toString());
} else if (value instanceof byte[]) {
return result.withB(ByteBuffer.wrap((byte[]) value));
} else if (value instanceof ByteBuffer) {
return result.withB((ByteBuffer) value);
} else if (value instanceof Set) {
// default to an empty string set if there is no element
@SuppressWarnings("unchecked")
Set<Object> set = (Set<Object>) value;
if (set.size() == 0) {
result.setSS(new LinkedHashSet<String>());
return result;
}
Object element = set.iterator().next();
if (element instanceof String) {
@SuppressWarnings("unchecked")
Set<String> ss = (Set<String>) value;
result.setSS(new ArrayList<String>(ss));
} else if (element instanceof Number) {
@SuppressWarnings("unchecked")
Set<Number> in = (Set<Number>) value;
List<String> out = new ArrayList<String>(set.size());
for (Number n : in) {
BigDecimal bd = InternalUtils.toBigDecimal(n);
out.add(bd.toPlainString());
}
result.setNS(out);
} else if (element instanceof byte[]) {
@SuppressWarnings("unchecked")
Set<byte[]> in = (Set<byte[]>) value;
List<ByteBuffer> out = new ArrayList<ByteBuffer>(set.size());
for (byte[] buf : in) {
out.add(ByteBuffer.wrap(buf));
}
result.setBS(out);
} else if (element instanceof ByteBuffer) {
@SuppressWarnings("unchecked")
Set<ByteBuffer> bs = (Set<ByteBuffer>) value;
result.setBS(bs);
} else {
throw new UnsupportedOperationException("element type: "
+ element.getClass());
}
} else if (value instanceof List) {
@SuppressWarnings("unchecked")
List<Object> in = (List<Object>) value;
List<AttributeValue> out = new ArrayList<AttributeValue>();
for (Object v : in) {
out.add(toAttributeValue(v));
}
result.setL(out);
} else if (value instanceof Map) {
@SuppressWarnings("unchecked")
Map<String, Object> in = (Map<String, Object>) value;
if (in.size() > 0) {
for (Map.Entry<String, Object> e : in.entrySet()) {
result.addMEntry(e.getKey(), toAttributeValue(e.getValue()));
}
} else { // empty map
result.setM(new LinkedHashMap<String,AttributeValue>());
}
} else {
throw new UnsupportedOperationException("value type: "
+ value.getClass());
}
return result;
}
/**
* Converts a list of low-level <code>AttributeValue</code> into a list of
* simple values. Each value in the returned list can be one of the
* followings:
*
* <ul>
* <li>String</li>
* <li>Set<String></li>
* <li>Number (including any subtypes and primitive types)</li>
* <li>Set<Number></li>
* <li>byte[]</li>
* <li>Set<byte[]></li>
* <li>ByteBuffer</li>
* <li>Set<ByteBuffer></li>
* <li>Boolean or boolean</li>
* <li>null</li>
* <li>Map<String,T>, where T can be any type on this list but must not
* induce any circular reference</li>
* <li>List<T>, where T can be any type on this list but must not induce
* any circular reference</li>
* </ul>
*/
public static List<Object> toSimpleList(List<AttributeValue> attrValues) {
if (attrValues == null)
return null;
List<Object> result = new ArrayList<Object>(attrValues.size());
for (AttributeValue attrValue : attrValues) {
Object value = toSimpleValue(attrValue);
result.add(value);
}
return result;
}
/**
* Convenient method to convert a list of low-level
* <code>AttributeValue</code> into a list of values of the same type T.
* Each value in the returned list can be one of the followings:
* <ul>
* <li>String</li>
* <li>Set<String></li>
* <li>Number (including any subtypes and primitive types)</li>
* <li>Set<Number></li>
* <li>byte[]</li>
* <li>Set<byte[]></li>
* <li>ByteBuffer</li>
* <li>Set<ByteBuffer></li>
* <li>Boolean or boolean</li>
* <li>null</li>
* <li>Map<String,T>, where T can be any type on this list but must not
* induce any circular reference</li>
* <li>List<T>, where T can be any type on this list but must not induce
* any circular reference</li>
* </ul>
*/
public static <T> List<T> toSimpleListValue(List<AttributeValue> values) {
if (values == null) {
return null;
}
List<T> result = new ArrayList<T>(values.size());
for (AttributeValue v : values) {
T t = toSimpleValue(v);
result.add(t);
}
return result;
}
public static <T> Map<String, T> toSimpleMapValue(
Map<String, AttributeValue> values) {
if (values == null) {
return null;
}
Map<String, T> result = new LinkedHashMap<String, T>(values.size());
for (Map.Entry<String, AttributeValue> entry : values.entrySet()) {
T t = toSimpleValue(entry.getValue());
result.put(entry.getKey(), t);
}
return result;
}
/**
* Returns the string representation of the given value; or null if the
* value is null. For <code>BigDecimal</code> it will be the string
* representation without an exponent field.
*/
public static String valToString(Object val) {
if (val instanceof BigDecimal) {
BigDecimal bd = (BigDecimal)val;
return bd.toPlainString();
}
if (val == null)
return null;
if (val instanceof String
|| val instanceof Boolean
|| val instanceof Number)
return val.toString();
throw new IncompatibleTypeException("Cannot convert " + val.getClass() + " into a string");
}
/**
* Converts a low-level <code>AttributeValue</code> into a simple value,
* which can be one of the followings:
*
* <ul>
* <li>String</li>
* <li>Set<String></li>
* <li>Number (including any subtypes and primitive types)</li>
* <li>Set<Number></li>
* <li>byte[]</li>
* <li>Set<byte[]></li>
* <li>ByteBuffer</li>
* <li>Set<ByteBuffer></li>
* <li>Boolean or boolean</li>
* <li>null</li>
* <li>Map<String,T>, where T can be any type on this list but must not
* induce any circular reference</li>
* <li>List<T>, where T can be any type on this list but must not induce
* any circular reference</li>
* </ul>
*
* @throws IllegalArgumentException
* if an empty <code>AttributeValue</code> value is specified
*/
public static <T> T toSimpleValue(AttributeValue value) {
if (value == null) {
return null;
}
if (Boolean.TRUE.equals(value.getNULL())) {
return null;
} else if (Boolean.FALSE.equals(value.getNULL())) {
throw new UnsupportedOperationException("False-NULL is not supported in DynamoDB");
} else if (value.getBOOL() != null) {
@SuppressWarnings("unchecked")
T t = (T) value.getBOOL();
return t;
} else if (value.getS() != null) {
@SuppressWarnings("unchecked")
T t = (T) value.getS();
return t;
} else if (value.getN() != null) {
@SuppressWarnings("unchecked")
T t = (T) new BigDecimal(value.getN());
return t;
} else if (value.getB() != null) {
@SuppressWarnings("unchecked")
T t = (T) copyAllBytesFrom(value.getB());
return t;
} else if (value.getSS() != null) {
@SuppressWarnings("unchecked")
T t = (T) new LinkedHashSet<String>(value.getSS());
return t;
} else if (value.getNS() != null) {
Set<BigDecimal> set = new LinkedHashSet<BigDecimal>(value.getNS().size());
for (String s : value.getNS()) {
set.add(new BigDecimal(s));
}
@SuppressWarnings("unchecked")
T t = (T) set;
return t;
} else if (value.getBS() != null) {
Set<byte[]> set = new LinkedHashSet<byte[]>(value.getBS().size());
for (ByteBuffer bb : value.getBS()) {
set.add(copyAllBytesFrom(bb));
}
@SuppressWarnings("unchecked")
T t = (T) set;
return t;
} else if (value.getL() != null) {
@SuppressWarnings("unchecked")
T t = (T) toSimpleList(value.getL());
return t;
} else if (value.getM() != null) {
@SuppressWarnings("unchecked")
T t = (T) toSimpleMapValue(value.getM());
return t;
} else {
throw new IllegalArgumentException(
"Attribute value must not be empty: " + value);
}
}
/**
* Returns the minimum of the two input integers taking null into account.
* Returns null if both integers are null. Otherwise, a null Integer is
* treated as infinity.
*/
public static Integer minimum(Integer one, Integer two) {
if (one == null) {
return two;
} else if (two == null) {
return one;
} else if (one < two) {
return one;
} else {
return two;
}
}
/**
* Returns the low level representation of a collection of <code>Expected</code>.
*/
public static Map<String, ExpectedAttributeValue> toExpectedAttributeValueMap(
Collection<Expected> expectedSet) {
if (expectedSet == null)
return null;
Map<String, ExpectedAttributeValue> expectedMap =
new LinkedHashMap<String, ExpectedAttributeValue>();
for (Expected expected : expectedSet) {
final String attr = expected.getAttribute();
final Object[] values = expected.getValues();
ExpectedAttributeValue eav = new ExpectedAttributeValue();
if (values != null) {
if (values.length > 0) {
// convert from list of object values to list of AttributeValues
AttributeValue[] avs = InternalUtils.toAttributeValues(values);
eav.withAttributeValueList(avs);
} else {
throw new IllegalStateException("Bug!");
}
}
ComparisonOperator op = expected.getComparisonOperator();
if (op == null) {
throw new IllegalArgumentException(
"Comparison operator for attribute " + expected.getAttribute()
+ " must be specified");
}
eav.withComparisonOperator(op);
expectedMap.put(attr, eav);
}
if (expectedSet.size() != expectedMap.size())
throw new IllegalArgumentException("duplicates attribute names not allowed in input");
return Collections.unmodifiableMap(expectedMap);
}
/**
* Returns the low level representation of a collection of <code>Filter</code>.
*/
public static Map<String, Condition> toAttributeConditionMap(Collection<? extends Filter<?>> filters) {
if (filters == null)
return null;
Map<String, Condition> conditionMap = new LinkedHashMap<String, Condition>();
for (Filter<?> filter : filters) {
final String attr = filter.getAttribute();
final Object[] values = filter.getValues();
Condition condition = new Condition();
if (values != null) {
if (values.length > 0) {
// convert from list of object values to list of AttributeValues
AttributeValue[] avs = InternalUtils.toAttributeValues(values);
condition.withAttributeValueList(avs);
} else {
throw new IllegalStateException("Bug!");
}
}
ComparisonOperator op = filter.getComparisonOperator();
if (op == null) {
throw new IllegalArgumentException(
"Comparison operator for attribute " + filter.getAttribute()
+ " must be specified");
}
condition.withComparisonOperator(op);
conditionMap.put(attr, condition);
}
if (filters.size() != conditionMap.size())
throw new IllegalArgumentException("duplicates attribute names not allowed in input");
return Collections.unmodifiableMap(conditionMap);
}
/**
* Converts the input array of values into an array of low level
* representation of those values.
*
* A value in the input array can be one of the followings:
*
* <ul>
* <li>String</li>
* <li>Set<String></li>
* <li>Number (including any subtypes and primitive types)</li>
* <li>Set<Number></li>
* <li>byte[]</li>
* <li>Set<byte[]></li>
* <li>ByteBuffer</li>
* <li>Set<ByteBuffer></li>
* <li>Boolean or boolean</li>
* <li>null</li>
* <li>Map<String,T>, where T can be any type on this list but must not
* induce any circular reference</li>
* <li>List<T>, where T can be any type on this list but must not induce
* any circular reference</li>
* </ul>
*/
public static AttributeValue[] toAttributeValues(Object[] values) {
AttributeValue[] attrValues = new AttributeValue[values.length];
for (int i=0; i < values.length; i++)
attrValues[i] = InternalUtils.toAttributeValue(values[i]);
return attrValues;
}
/**
* Converts the specified primary key into the low-level representation.
*/
public static Map<String, AttributeValue> toAttributeValueMap(
Collection<KeyAttribute> primaryKey) {
if (primaryKey == null)
return null;
Map<String, AttributeValue> keys = new LinkedHashMap<String, AttributeValue>();
for (KeyAttribute keyAttr : primaryKey)
keys.put(keyAttr.getName(),
InternalUtils.toAttributeValue(keyAttr.getValue()));
return Collections.unmodifiableMap(keys);
}
/**
* Converts the specified primary key into the low-level representation.
*/
public static Map<String, AttributeValue> toAttributeValueMap(
PrimaryKey primaryKey) {
if (primaryKey == null)
return null;
return toAttributeValueMap(primaryKey.getComponents());
}
/**
* Converts the specified primary key into the low-level representation.
*/
public static Map<String, AttributeValue> toAttributeValueMap(
KeyAttribute ... primaryKey) {
if (primaryKey == null)
return null;
return toAttributeValueMap(Arrays.asList(primaryKey));
}
/**
* Converts a number into BigDecimal representation.
*/
public static BigDecimal toBigDecimal(Number n) {
if (n instanceof BigDecimal)
return (BigDecimal)n;
return new BigDecimal(n.toString());
}
public static Set<BigDecimal> toBigDecimalSet(Number ... val) {
Set<BigDecimal> set = new LinkedHashSet<BigDecimal>(val.length);
for (Number n: val)
set.add(InternalUtils.toBigDecimal(n));
return set;
}
public static Set<BigDecimal> toBigDecimalSet(Set<Number> vals) {
Set<BigDecimal> set = new LinkedHashSet<BigDecimal>(vals.size());
for (Number n: vals)
set.add(InternalUtils.toBigDecimal(n));
return set;
}
public static void checkInvalidAttrName(String attrName) {
if (attrName == null || attrName.trim().length() == 0)
throw new IllegalArgumentException("Attribute name must not be null or empty");
}
public static void checkInvalidAttribute(String attrName, Object val) {
checkInvalidAttrName(attrName);
ValidationUtils.assertNotNull(val, attrName);
}
}