|
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 | | -} |
| 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