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