1- /*
2- * Copyright (c) 2018 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 .eclipse .jnosql .communication .dynamodb ;
16-
17-
18- import jakarta .nosql .keyvalue .KeyValueEntity ;
19- import org .eclipse .jnosql .communication .driver .JsonbSupplier ;
20- import software .amazon .awssdk .services .dynamodb .model .AttributeValue ;
21- import software .amazon .awssdk .services .dynamodb .model .BatchGetItemRequest ;
22- import software .amazon .awssdk .services .dynamodb .model .GetItemRequest ;
23- import software .amazon .awssdk .services .dynamodb .model .KeysAndAttributes ;
24- import software .amazon .awssdk .services .dynamodb .model .PutRequest ;
25- import software .amazon .awssdk .services .dynamodb .model .WriteRequest ;
26-
27- import javax .json .bind .Jsonb ;
28- import java .util .Arrays ;
29- import java .util .Collection ;
30- import java .util .Collections ;
31- import java .util .HashMap ;
32- import java .util .List ;
33- import java .util .Map ;
34- import java .util .function .Function ;
35- import java .util .stream .Collectors ;
36- import java .util .stream .StreamSupport ;
37-
38- import static org .eclipse .jnosql .communication .dynamodb .ConfigurationAmazonEntity .KEY ;
39- import static org .eclipse .jnosql .communication .dynamodb .ConfigurationAmazonEntity .VALUE ;
40-
41- public class DynamoDBUtils {
42-
43- private static final AttributeValue .Builder attributeValueBuilder = AttributeValue .builder ();
44- private static final Jsonb JSONB = JsonbSupplier .getInstance ().get ();
45-
46- private DynamoDBUtils () {
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 entity ) {
69- return createAttributeValues (entity .getKey (), entity .getValue ());
70- }
71-
72- public static <K > Collection <Map <String , AttributeValue >> createAttributeValues (Iterable <KeyValueEntity > 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 > 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- }
1+ /*
2+ * Copyright (c) 2018 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 .eclipse .jnosql .communication .dynamodb ;
16+
17+ import jakarta .nosql .keyvalue .KeyValueEntity ;
18+ import org .eclipse .jnosql .communication .driver .JsonbSupplier ;
19+ import software .amazon .awssdk .services .dynamodb .model .AttributeValue ;
20+ import software .amazon .awssdk .services .dynamodb .model .BatchGetItemRequest ;
21+ import software .amazon .awssdk .services .dynamodb .model .GetItemRequest ;
22+ import software .amazon .awssdk .services .dynamodb .model .KeysAndAttributes ;
23+ import software .amazon .awssdk .services .dynamodb .model .PutRequest ;
24+ import software .amazon .awssdk .services .dynamodb .model .WriteRequest ;
25+
26+ import javax .json .bind .Jsonb ;
27+ import java .util .Collection ;
28+ import java .util .Collections ;
29+ import java .util .HashMap ;
30+ import java .util .List ;
31+ import java .util .Map ;
32+ import java .util .function .Function ;
33+ import java .util .stream .Collectors ;
34+ import java .util .stream .StreamSupport ;
35+
36+ import static org .eclipse .jnosql .communication .dynamodb .ConfigurationAmazonEntity .KEY ;
37+ import static org .eclipse .jnosql .communication .dynamodb .ConfigurationAmazonEntity .VALUE ;
38+
39+ public class DynamoDBUtils {
40+
41+ private static final AttributeValue .Builder attributeValueBuilder = AttributeValue .builder ();
42+ private static final Jsonb JSONB = JsonbSupplier .getInstance ().get ();
43+
44+ private DynamoDBUtils () {
45+ }
46+
47+ public static <K , V > Map <String , AttributeValue > createAttributeValues (K key , V value ) {
48+
49+ Map <String , AttributeValue > createAttributeValues = createKeyAttributeValues (key );
50+ String valueAsJson = JSONB .toJson (value );
51+
52+ AttributeValue valueAttributeValue = attributeValueBuilder .s (valueAsJson ).build ();
53+ createAttributeValues .put (VALUE , valueAttributeValue );
54+ return createAttributeValues ;
55+ }
56+
57+ public static <K , V > Map <String , AttributeValue > createKeyAttributeValues (K key ) {
58+ Map <String , AttributeValue > map = new HashMap <>();
59+ AttributeValue keyAttributeValue = attributeValueBuilder .s (key .toString ()).build ();
60+ map .put (KEY , keyAttributeValue );
61+
62+ return map ;
63+ }
64+
65+ public static <K , V > Collection <Map <String , AttributeValue >> createKeyAttributeValues (Iterable <K > keys ) {
66+ return StreamSupport .stream (keys .spliterator (), false ).map (
67+ k -> Collections .singletonMap (KEY , attributeValueBuilder .s (k .toString ()).build ())
68+ ).collect (Collectors .toList ());
69+ }
70+
71+ public static <K , V > Map <String , AttributeValue > createAttributeValues (KeyValueEntity entity ) {
72+ return createAttributeValues (entity .getKey (), entity .getValue ());
73+ }
74+
75+ public static <K > Collection <Map <String , AttributeValue >> createAttributeValues (Iterable <KeyValueEntity > entities ) {
76+
77+ return StreamSupport .stream (entities .spliterator (), false )
78+ .map (e -> createAttributeValues (e ))
79+ .collect (Collectors .toList ());
80+ }
81+
82+ private static Map <String , List <WriteRequest >> createMapWriteRequest (Collection <Map <String , AttributeValue >> map , String tableName ) {
83+
84+ PutRequest .Builder putRequestBuilder = PutRequest .builder ();
85+ WriteRequest .Builder writeRequestBuilder = WriteRequest .builder ();
86+
87+ return Collections .singletonMap (
88+ tableName ,
89+ map
90+ .stream ()
91+ .map (m -> putRequestBuilder .item (m ).build ())
92+ .map (p -> writeRequestBuilder .putRequest (p ).build ()).collect (Collectors .toList ())
93+ );
94+ }
95+
96+ public static <K > Map <String , List <WriteRequest >> createMapWriteRequest (Iterable <KeyValueEntity > entities , String tableName ) {
97+ Collection <Map <String , AttributeValue >> attributeValues = createAttributeValues (entities );
98+ createMapWriteRequest (attributeValues , tableName );
99+ return createMapWriteRequest (attributeValues , tableName );
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 , String tableName ) {
112+
113+ KeysAndAttributes .Builder keysAndAttributesBuilder = KeysAndAttributes .builder ();
114+
115+ return Collections .singletonMap (
116+ tableName ,
117+ keysAndAttributesBuilder .keys (createKeyAttributeValues (keys )).build ()
118+ );
119+ }
120+
121+ public static <K > BatchGetItemRequest createBatchGetItemRequest (Iterable <K > keys , String tableName ) {
122+ BatchGetItemRequest .Builder batchGetItemRequestBuilder = BatchGetItemRequest .builder ();
123+ return batchGetItemRequestBuilder .requestItems (createKeysAndAttribute (keys , tableName )).build ();
124+ }
125+
126+ public static <K > GetItemRequest createGetItemRequest (K key , String tableName ) {
127+ GetItemRequest .Builder getItemRequest = GetItemRequest .builder ();
128+ return getItemRequest .tableName (tableName ).key (createKeyAttributeValues (key )).build ();
129+ }
130+ }
0 commit comments