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 .key ;
16+
17+ import static org .jnosql .diana .api .key .KeyValueEntity .VALUE ;
18+ import static org .jnosql .diana .dynamodb .DynamoDBUtils .createAttributeValues ;
19+ import static org .jnosql .diana .dynamodb .DynamoDBUtils .createMapWriteRequest ;
20+ import static org .jnosql .diana .dynamodb .DynamoDBUtils .createBatchGetItemRequest ;
21+ import static org .jnosql .diana .dynamodb .DynamoDBUtils .createGetItemRequest ;
22+
23+ import java .time .Duration ;
24+ import java .util .Map ;
25+ import java .util .Objects ;
26+ import java .util .Optional ;
27+ import java .util .function .Function ;
28+ import java .util .stream .Collectors ;
29+ import java .util .stream .StreamSupport ;
30+
31+ import org .jnosql .diana .api .Value ;
32+ import org .jnosql .diana .api .key .BucketManager ;
33+ import org .jnosql .diana .api .key .KeyValueEntity ;
34+ import org .jnosql .diana .driver .ValueJSON ;
35+
36+ import software .amazon .awssdk .services .dynamodb .DynamoDbClient ;
37+ import software .amazon .awssdk .services .dynamodb .model .AttributeValue ;
38+ import software .amazon .awssdk .services .dynamodb .model .BatchWriteItemRequest ;
39+ import software .amazon .awssdk .services .dynamodb .model .DeleteItemRequest ;
40+ import software .amazon .awssdk .services .dynamodb .model .GetItemResponse ;
41+ import software .amazon .awssdk .services .dynamodb .model .PutItemRequest ;
42+
43+ public class DynamoDBBucketManager implements BucketManager {
44+
45+
46+ private DynamoDbClient client ;
47+ private String tableName ;
48+ private static final Function <AttributeValue , String > TO_JSON = e -> e .s ();
49+
50+ public DynamoDBBucketManager (DynamoDbClient client , String tableName ) {
51+ this .client = client ;
52+ this .tableName = tableName ;
53+ }
54+
55+ @ Override
56+ public <K , V > void put (K key , V value ) throws NullPointerException {
57+ client .putItem (PutItemRequest .builder ().tableName (tableName ).item (createAttributeValues (key ,value )).build ());
58+ }
59+
60+ @ Override
61+ public <K > void put (KeyValueEntity <K > entity ) throws NullPointerException {
62+ put (entity .getKey (),entity .getValue ().get ());
63+ }
64+
65+ @ Override
66+ public <K > void put (KeyValueEntity <K > entity , Duration ttl )
67+ throws NullPointerException , UnsupportedOperationException {
68+ throw new UnsupportedOperationException ();
69+ }
70+
71+ @ Override
72+ public <K > void put (Iterable <KeyValueEntity <K >> entities ) throws NullPointerException {
73+ client .batchWriteItem (BatchWriteItemRequest .builder ().requestItems (createMapWriteRequest (entities )).build ());
74+ }
75+
76+ @ Override
77+ public <K > void put (Iterable <KeyValueEntity <K >> entities , Duration ttl )
78+ throws NullPointerException , UnsupportedOperationException {
79+ throw new UnsupportedOperationException ();
80+ }
81+
82+ @ Override
83+ public <K > Optional <Value > get (K key ) throws NullPointerException {
84+
85+ Objects .requireNonNull (key , "key is required" );
86+
87+ if (key .toString ().isEmpty ()) {
88+ throw new IllegalArgumentException ("The Key is irregular" );
89+ }
90+
91+ GetItemResponse getItemResponse = client .getItem (createGetItemRequest (key ,tableName ));
92+ Map <String , AttributeValue > item = getItemResponse .item ();
93+ AttributeValue attributeValue = item .get (VALUE );
94+
95+ return Optional .ofNullable (attributeValue )
96+ .map (TO_JSON )
97+ .map (ValueJSON ::of );
98+ }
99+
100+ @ Override
101+ public <K > Iterable <Value > get (Iterable <K > keys ) throws NullPointerException {
102+
103+ return client .batchGetItem (createBatchGetItemRequest (keys ))
104+ .responses ()
105+ .values ()
106+ .stream ()
107+ .flatMap (l -> StreamSupport .stream (l .spliterator (),false ))
108+ .flatMap (v -> v .values ().stream ())
109+ .map (TO_JSON )
110+ .map (ValueJSON ::of )
111+ .collect (Collectors .toList ());
112+ }
113+
114+ @ Override
115+ public <K > void remove (K key ) throws NullPointerException {
116+ client .deleteItem (DeleteItemRequest .builder ().tableName (tableName ).key (createAttributeValues (key )).build ());
117+ }
118+
119+ @ Override
120+ public <K > void remove (Iterable <K > keys ) throws NullPointerException {
121+ keys .forEach (this ::remove );
122+ }
123+
124+ @ Override
125+ public void close () {
126+ client .close ();
127+ }
128+ }
0 commit comments