-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#71 - Added Document Versioning DynamoDb Service
- Loading branch information
1 parent
e9c8357
commit 11170c7
Showing
41 changed files
with
1,467 additions
and
654 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
149 changes: 149 additions & 0 deletions
149
aws-dynamodb/src/main/java/com/formkiq/aws/dynamodb/ReadRequestBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
/** | ||
* MIT License | ||
* | ||
* Copyright (c) 2018 - 2020 FormKiQ | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
package com.formkiq.aws.dynamodb; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.HashMap; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Map; | ||
import com.formkiq.aws.dynamodb.objects.Objects; | ||
import software.amazon.awssdk.services.dynamodb.DynamoDbClient; | ||
import software.amazon.awssdk.services.dynamodb.model.AttributeValue; | ||
import software.amazon.awssdk.services.dynamodb.model.BatchGetItemRequest; | ||
import software.amazon.awssdk.services.dynamodb.model.BatchGetItemResponse; | ||
import software.amazon.awssdk.services.dynamodb.model.KeysAndAttributes; | ||
import software.amazon.awssdk.services.dynamodb.model.WriteRequest; | ||
|
||
/** | ||
* Helper class for building Batch Read Requets. | ||
* | ||
*/ | ||
public class ReadRequestBuilder { | ||
|
||
/** Max Batch Size. */ | ||
private static final int MAX_BATCH_SIZE = 100; | ||
/** {@link Map} of {@link AttributeValue}. */ | ||
private Map<String, Collection<Map<String, AttributeValue>>> items = new HashMap<>(); | ||
|
||
/** | ||
* constructor. | ||
*/ | ||
public ReadRequestBuilder() { | ||
|
||
} | ||
|
||
/** | ||
* Collects {@link WriteRequest} and adds to a internal list. | ||
* | ||
* @param tableName {@link String} | ||
* @param keys {@link Collection} | ||
* @return {@link ReadRequestBuilder} | ||
*/ | ||
public ReadRequestBuilder append(final String tableName, | ||
final Collection<Map<String, AttributeValue>> keys) { | ||
|
||
if (this.items.containsKey(tableName)) { | ||
Collection<Map<String, AttributeValue>> values = this.items.get(tableName); | ||
values.addAll(keys); | ||
} else { | ||
this.items.put(tableName, new HashSet<>(keys)); | ||
} | ||
|
||
return this; | ||
} | ||
|
||
/** | ||
* Batch Read Items. | ||
* | ||
* @param dbClient {@link DynamoDbClient} | ||
* @return {@link BatchGetItemResponse} | ||
*/ | ||
public Map<String, List<Map<String, AttributeValue>>> batchReadItems( | ||
final DynamoDbClient dbClient) { | ||
|
||
Map<String, List<Map<String, AttributeValue>>> map = new HashMap<>(); | ||
|
||
for (Map.Entry<String, Collection<Map<String, AttributeValue>>> e : this.items.entrySet()) { | ||
|
||
if (e.getValue().size() > MAX_BATCH_SIZE) { | ||
|
||
Collection<List<Map<String, AttributeValue>>> parition = | ||
Objects.parition(new ArrayList<>(e.getValue()), MAX_BATCH_SIZE); | ||
|
||
for (List<Map<String, AttributeValue>> list : parition) { | ||
|
||
Map<String, List<Map<String, AttributeValue>>> values = | ||
batchReadItems(dbClient, e.getKey(), list).responses(); | ||
|
||
for (Map.Entry<String, List<Map<String, AttributeValue>>> ee : values.entrySet()) { | ||
|
||
if (map.containsKey(ee.getKey())) { | ||
map.get(ee.getKey()).addAll(ee.getValue()); | ||
} else { | ||
map.put(ee.getKey(), new ArrayList<>(ee.getValue())); | ||
} | ||
} | ||
} | ||
|
||
} else { | ||
map = batchReadItems(dbClient, e.getKey(), e.getValue()).responses(); | ||
} | ||
} | ||
|
||
return map; | ||
} | ||
|
||
private BatchGetItemResponse batchReadItems(final DynamoDbClient dbClient, final String tableName, | ||
final Collection<Map<String, AttributeValue>> keys) { | ||
|
||
Map<String, KeysAndAttributes> requestedItems = | ||
Map.of(tableName, KeysAndAttributes.builder().keys(keys).build()); | ||
|
||
BatchGetItemRequest batchReq = | ||
BatchGetItemRequest.builder().requestItems(requestedItems).build(); | ||
BatchGetItemResponse batchResponse = dbClient.batchGetItem(batchReq); | ||
|
||
return batchResponse; | ||
} | ||
|
||
/** | ||
* Get Read Items. | ||
* | ||
* @return {@link Map} | ||
*/ | ||
public Map<String, Collection<Map<String, AttributeValue>>> getItems() { | ||
return this.items; | ||
} | ||
|
||
/** | ||
* Are there any requests. | ||
* | ||
* @return boolean | ||
*/ | ||
public boolean isEmpty() { | ||
return this.items.isEmpty(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.