Skip to content

Commit 1776efb

Browse files
committed
create KeyValueConfiguration, BucketManager and BucketManagerFactory
Signed-off-by: Breno Pessoa <[email protected]>
1 parent 44b9c54 commit 1776efb

File tree

4 files changed

+241
-0
lines changed

4 files changed

+241
-0
lines changed
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
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+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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 java.util.List;
18+
import java.util.Map;
19+
import java.util.Queue;
20+
import java.util.Set;
21+
22+
import org.jnosql.diana.api.key.BucketManagerFactory;
23+
import org.jnosql.diana.dynamodb.DynamoTableUtils;
24+
25+
import software.amazon.awssdk.services.dynamodb.DynamoDbClient;
26+
27+
public class DynamoDBBucketManagerFactory implements BucketManagerFactory<DynamoDBBucketManager> {
28+
29+
private DynamoDbClient client;
30+
31+
public DynamoDBBucketManagerFactory(DynamoDbClient client) {
32+
this.client = client;
33+
}
34+
35+
@Override
36+
public DynamoDBBucketManager getBucketManager(String bucketName) {
37+
38+
return getBucketManager(bucketName, null, null);
39+
}
40+
41+
public DynamoDBBucketManager getBucketManager(String bucketName, Long readCapacityUnits , Long writeCapacityUnit) {
42+
43+
DynamoTableUtils.manageTables(bucketName, client);
44+
return new DynamoDBBucketManager(client, bucketName);
45+
}
46+
47+
@Override
48+
public <T> List<T> getList(String bucketName, Class<T> clazz) {
49+
throw new UnsupportedOperationException("The DynamoDB does not support getMap method");
50+
}
51+
52+
@Override
53+
public <T> Set<T> getSet(String bucketName, Class<T> clazz) {
54+
throw new UnsupportedOperationException("The DynamoDB does not support getMap method");
55+
}
56+
57+
@Override
58+
public <T> Queue<T> getQueue(String bucketName, Class<T> clazz) {
59+
throw new UnsupportedOperationException("The DynamoDB does not support getMap method");
60+
}
61+
62+
@Override
63+
public <K, V> Map<K, V> getMap(String bucketName, Class<K> keyValue, Class<V> valueValue) {
64+
throw new UnsupportedOperationException("The DynamoDB does not support getMap method");
65+
}
66+
67+
@Override
68+
public void close() {
69+
client.close();
70+
}
71+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package org.jnosql.diana.dynamodb.key;
2+
3+
import org.jnosql.diana.api.Settings;
4+
import org.jnosql.diana.api.key.KeyValueConfiguration;
5+
import org.jnosql.diana.dynamodb.DynamoDBConfiguration;
6+
7+
import software.amazon.awssdk.services.dynamodb.DynamoDbClient;
8+
9+
public class DynamoDBKeyValueConfiguration extends DynamoDBConfiguration
10+
implements KeyValueConfiguration<DynamoDBBucketManagerFactory> {
11+
12+
@Override
13+
public DynamoDBBucketManagerFactory get() {
14+
return new DynamoDBBucketManagerFactory(builder.build());
15+
}
16+
17+
@Override
18+
public DynamoDBBucketManagerFactory get(Settings settings) {
19+
DynamoDbClient dynamoDB = getDynamoDB(settings);
20+
return new DynamoDBBucketManagerFactory(dynamoDB);
21+
}
22+
23+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+
16+
/**
17+
* The DynamoDB key-value implementation
18+
*/
19+
package org.jnosql.diana.dynamodb.key;

0 commit comments

Comments
 (0)