1414 */
1515package org .eclipse .jnosql .communication .dynamodb ;
1616
17-
1817import jakarta .nosql .keyvalue .KeyValueEntity ;
1918import org .eclipse .jnosql .communication .driver .JsonbSupplier ;
2019import software .amazon .awssdk .services .dynamodb .model .AttributeValue ;
2524import software .amazon .awssdk .services .dynamodb .model .WriteRequest ;
2625
2726import javax .json .bind .Jsonb ;
28- import java .util .Arrays ;
2927import java .util .Collection ;
3028import java .util .Collections ;
3129import java .util .HashMap ;
@@ -48,23 +46,28 @@ private DynamoDBUtils() {
4846
4947 public static <K , V > Map <String , AttributeValue > createAttributeValues (K key , V value ) {
5048
51- Map <String , AttributeValue > createAttributeValues = createAttributeValues (key );
49+ Map <String , AttributeValue > createAttributeValues = createKeyAttributeValues (key );
5250 String valueAsJson = JSONB .toJson (value );
5351
5452 AttributeValue valueAttributeValue = attributeValueBuilder .s (valueAsJson ).build ();
5553 createAttributeValues .put (VALUE , valueAttributeValue );
5654 return createAttributeValues ;
5755 }
5856
59- public static <K , V > Map <String , AttributeValue > createAttributeValues (K key ) {
60-
57+ public static <K , V > Map <String , AttributeValue > createKeyAttributeValues (K key ) {
6158 Map <String , AttributeValue > map = new HashMap <>();
6259 AttributeValue keyAttributeValue = attributeValueBuilder .s (key .toString ()).build ();
6360 map .put (KEY , keyAttributeValue );
6461
6562 return map ;
6663 }
6764
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+
6871 public static <K , V > Map <String , AttributeValue > createAttributeValues (KeyValueEntity entity ) {
6972 return createAttributeValues (entity .getKey (), entity .getValue ());
7073 }
@@ -76,27 +79,24 @@ public static <K> Collection<Map<String, AttributeValue>> createAttributeValues(
7679 .collect (Collectors .toList ());
7780 }
7881
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 ) {
82+ private static Map <String , List <WriteRequest >> createMapWriteRequest (Collection <Map <String , AttributeValue >> map , String tableName ) {
8483
8584 PutRequest .Builder putRequestBuilder = PutRequest .builder ();
8685 WriteRequest .Builder writeRequestBuilder = WriteRequest .builder ();
8786
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 ()));
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+ );
9394 }
9495
95-
96- public static <K > Map <String , List <WriteRequest >> createMapWriteRequest (Iterable <KeyValueEntity > entities ) {
97-
96+ public static <K > Map <String , List <WriteRequest >> createMapWriteRequest (Iterable <KeyValueEntity > entities , String tableName ) {
9897 Collection <Map <String , AttributeValue >> attributeValues = createAttributeValues (entities );
99- return createMapWriteRequest (attributeValues );
98+ createMapWriteRequest (attributeValues , tableName );
99+ return createMapWriteRequest (attributeValues , tableName );
100100 }
101101
102102 public static <K > Map <String , AttributeValue > create (Iterable <K > keys ) {
@@ -108,25 +108,23 @@ public static <K> Map<String, AttributeValue> create(Iterable<K> keys) {
108108 return Collections .unmodifiableMap (map );
109109 }
110110
111- private static <K > Map <String , KeysAndAttributes > createKeysAndAttribute (Iterable <K > keys ) {
111+ private static <K > Map <String , KeysAndAttributes > createKeysAndAttribute (Iterable <K > keys , String tableName ) {
112112
113113 KeysAndAttributes .Builder keysAndAttributesBuilder = KeysAndAttributes .builder ();
114114
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- );
115+ return Collections .singletonMap (
116+ tableName ,
117+ keysAndAttributesBuilder .keys (createKeyAttributeValues (keys )).build ()
118+ );
121119 }
122120
123- public static <K > BatchGetItemRequest createBatchGetItemRequest (Iterable <K > keys ) {
121+ public static <K > BatchGetItemRequest createBatchGetItemRequest (Iterable <K > keys , String tableName ) {
124122 BatchGetItemRequest .Builder batchGetItemRequestBuilder = BatchGetItemRequest .builder ();
125- return batchGetItemRequestBuilder .requestItems (createKeysAndAttribute (keys )).build ();
123+ return batchGetItemRequestBuilder .requestItems (createKeysAndAttribute (keys , tableName )).build ();
126124 }
127125
128126 public static <K > GetItemRequest createGetItemRequest (K key , String tableName ) {
129127 GetItemRequest .Builder getItemRequest = GetItemRequest .builder ();
130- return getItemRequest .tableName (tableName ).key (createAttributeValues (key )).build ();
128+ return getItemRequest .tableName (tableName ).key (createKeyAttributeValues (key )).build ();
131129 }
132- }
130+ }
0 commit comments