1+ /*
2+ * Copyright (c) 2017 Otávio Santana and others
3+ * All rights reserved. This program and the accompanying materials
4+ * are made available under the terms of the Eclipse Public License v1.0
5+ * and Apache License v2.0 which accompanies this distribution.
6+ * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
7+ * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php.
8+ *
9+ * You may elect to redistribute this code under either of these licenses.
10+ *
11+ * Contributors:
12+ *
13+ * Otavio Santana
14+ */
15+ package org .jnosql .diana .dynamodb ;
16+
17+ import static org .jnosql .diana .api .key .KeyValueEntity .KEY ;
18+ import static org .jnosql .diana .api .key .KeyValueEntity .VALUE ;
19+
20+ import java .util .Arrays ;
21+ import java .util .Collection ;
22+ import java .util .Collections ;
23+ import java .util .HashMap ;
24+ import java .util .List ;
25+ import java .util .Map ;
26+ import java .util .function .Function ;
27+ import java .util .stream .Collectors ;
28+ import java .util .stream .StreamSupport ;
29+
30+ import javax .json .bind .Jsonb ;
31+
32+ import org .jnosql .diana .api .key .KeyValueEntity ;
33+ import org .jnosql .diana .driver .JsonbSupplier ;
34+ import org .jnosql .diana .driver .ValueJSON ;
35+
36+ import software .amazon .awssdk .services .dynamodb .model .AttributeValue ;
37+ import software .amazon .awssdk .services .dynamodb .model .BatchGetItemRequest ;
38+ import software .amazon .awssdk .services .dynamodb .model .GetItemRequest ;
39+ import software .amazon .awssdk .services .dynamodb .model .KeysAndAttributes ;
40+ import software .amazon .awssdk .services .dynamodb .model .PutRequest ;
41+ import software .amazon .awssdk .services .dynamodb .model .WriteRequest ;
42+
43+ public class DynamoDBUtils {
44+
45+ private static final AttributeValue .Builder attributeValueBuilder = AttributeValue .builder ();
46+ private static final Jsonb JSONB = JsonbSupplier .getInstance ().get ();
47+
48+
49+ public static <K , V > Map <String , AttributeValue > createAttributeValues (K key , V value ){
50+
51+ Map <String , AttributeValue > createAttributeValues = createAttributeValues (key );
52+ String valueAsJson = JSONB .toJson (value );
53+
54+ AttributeValue valueAttributeValue = attributeValueBuilder .s (valueAsJson ).build ();
55+ createAttributeValues .put (VALUE ,valueAttributeValue );
56+ return createAttributeValues ;
57+ }
58+
59+ public static <K , V > Map <String , AttributeValue > createAttributeValues (K key ){
60+
61+ Map <String , AttributeValue > map = new HashMap <>();
62+ AttributeValue keyAttributeValue = attributeValueBuilder .s (key .toString ()).build ();
63+ map .put (KEY ,keyAttributeValue );
64+
65+ return map ;
66+ }
67+
68+ public static <K , V > Map <String , AttributeValue > createAttributeValues (KeyValueEntity <K > entity ){
69+ return createAttributeValues (entity .getKey (),entity .getValue ());
70+ }
71+
72+ public static <K > Collection <Map <String , AttributeValue >> createAttributeValues (Iterable <KeyValueEntity <K >> entities ){
73+
74+ return StreamSupport .stream (entities .spliterator (),false )
75+ .map (e -> createAttributeValues (e ))
76+ .collect (Collectors .toList ());
77+ }
78+
79+ private static Map <String , List <WriteRequest >> createMapWriteRequest (Map <String , AttributeValue > map ){
80+ return createMapWriteRequest (Arrays .asList (map ));
81+ }
82+
83+ private static Map <String , List <WriteRequest >> createMapWriteRequest (Collection <Map <String , AttributeValue >> map ){
84+
85+ PutRequest .Builder putRequestBuilder = PutRequest .builder ();
86+ WriteRequest .Builder writeRequestBuilder = WriteRequest .builder ();
87+
88+ return map
89+ .stream ()
90+ .map (m -> putRequestBuilder .item (m ).build ())
91+ .map (p -> writeRequestBuilder .putRequest (p ).build ())
92+ .collect (Collectors .groupingBy (w -> w .toString (), Collectors .toList ()));
93+ }
94+
95+
96+ public static <K > Map <String , List <WriteRequest >> createMapWriteRequest (Iterable <KeyValueEntity <K >> entities ){
97+
98+ Collection <Map <String ,AttributeValue >> attributeValues = createAttributeValues (entities );
99+ return createMapWriteRequest (attributeValues );
100+ }
101+
102+ public static <K > Map <String , AttributeValue > create (Iterable <K > keys ){
103+
104+ Map <String , AttributeValue > map = StreamSupport .stream (keys .spliterator (),false )
105+ .map (e -> e .toString ())
106+ .collect (Collectors .toMap (Function .identity (), k -> attributeValueBuilder .s (k ).build ()));
107+
108+ return Collections .unmodifiableMap (map );
109+ }
110+
111+ private static <K > Map <String , KeysAndAttributes > createKeysAndAttribute (Iterable <K > keys ){
112+
113+ KeysAndAttributes .Builder keysAndAttributesBuilder = KeysAndAttributes .builder ();
114+
115+ return StreamSupport .stream (keys .spliterator (),false )
116+ .collect (Collectors .toMap
117+ (
118+ e -> e .toString () ,
119+ k -> keysAndAttributesBuilder .projectionExpression (KEY ).keys (createAttributeValues (k )).build ())
120+ );
121+ }
122+
123+ public static <K > BatchGetItemRequest createBatchGetItemRequest (Iterable <K > keys ) {
124+ BatchGetItemRequest .Builder batchGetItemRequestBuilder = BatchGetItemRequest .builder ();
125+ return batchGetItemRequestBuilder .requestItems (createKeysAndAttribute (keys )).build ();
126+ }
127+
128+ public static <K > GetItemRequest createGetItemRequest (K key ,String tableName ) {
129+ GetItemRequest .Builder getItemRequest = GetItemRequest .builder ();
130+ return getItemRequest .tableName (tableName ).key (createAttributeValues (key )).build ();
131+ }
132+ }
0 commit comments