Skip to content

feat: show multi-threading in example #246

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jul 18, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import java.util.List;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import software.amazon.awssdk.core.client.config.ClientOverrideConfiguration;
import software.amazon.awssdk.services.dynamodb.DynamoDbClient;
Expand Down Expand Up @@ -71,6 +74,7 @@ This table must have a Global Secondary Index (GSI) configured named "last4UnitC
public class CompoundBeaconSearchableEncryptionExample {

static String GSI_NAME = "last4UnitCompound-index";
static int MAX_CONCURRENT_QUERY_THREADS = 10;

public static void PutItemQueryItemWithCompoundBeacon(String ddbTableName, String branchKeyId, String branchKeyWrappingKmsKeyArn, String branchKeyDdbTableName) {

Expand Down Expand Up @@ -232,6 +236,26 @@ public static void PutItemQueryItemWithCompoundBeacon(String ddbTableName, Strin
.build())
.build();

// Perform PutItem and Query
PutAndQueryItemWithCompoundBeacon(ddb, ddbTableName);

// If instead you were working in a multi-threaded context
// it might look like this
Runnable myThread = () -> {
for(int i = 0; i < 20; ++i) {
PutAndQueryItemWithCompoundBeacon(ddb, ddbTableName);
}
};
ExecutorService pool = Executors.newFixedThreadPool(MAX_CONCURRENT_QUERY_THREADS);
for(int i = 0; i < (2*MAX_CONCURRENT_QUERY_THREADS); i++) {
pool.execute(myThread);
}
pool.shutdown();
try {pool.awaitTermination(30, TimeUnit.SECONDS);} catch (Exception e) {}
}

public static void PutAndQueryItemWithCompoundBeacon(DynamoDbClient ddb, String ddbTableName) {

// 11. Put an item with both attributes used in the compound beacon.
final HashMap<String, AttributeValue> item = new HashMap<>();
item.put("work_id", AttributeValue.builder().s("9ce39272-8068-4efd-a211-cd162ad65d4c").build());
Expand Down