Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright (c) 2015-2018 Rocket Partners, LLC
* https://github.com/inversion-api
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.inversion.dynamodb;
import com.amazonaws.AmazonClientException;
import com.amazonaws.AmazonServiceException;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.client.builder.AwsClientBuilder;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder;
import com.amazonaws.services.dynamodbv2.document.DynamoDB;
import com.amazonaws.services.dynamodbv2.document.PrimaryKey;
import com.amazonaws.services.dynamodbv2.document.Table;
import com.amazonaws.services.dynamodbv2.document.spec.UpdateItemSpec;
import com.amazonaws.services.dynamodbv2.model.*;
import com.amazonaws.services.dynamodbv2.model.Projection;
import io.inversion.Collection;
import io.inversion.*;
import io.inversion.rql.Term;
import io.inversion.utils.Utils;
import java.util.*;
import java.util.concurrent.Callable;
public class DynamoDb extends Db {
public static final String PRIMARY_INDEX_NAME = "Primary Index";
public static final String PRIMARY_INDEX_TYPE = "primary";
public static final String LOCAL_SECONDARY_INDEX_TYPE = "localsecondary";
public static final String GLOBAL_SECONDARY_INDEX_TYPE = "globalsecondary";
protected final int batchMax = 20;
protected String awsAccessKey = null;
protected String awsSecretKey = null;
protected String awsRegion = "us-east-1";
protected String awsEndpoint = null;
transient protected AmazonDynamoDB dynamoClient = null;
public DynamoDb() {
this.withType("dynamodb");
}
public DynamoDb(String name, String includeTables) {
this();
withName(name);
withIncludeTables(includeTables);
}
public static Index findIndexByName(Collection coll, String name) {
if (coll != null && coll.getIndexes() != null) {
for (Index index : coll.getIndexes()) {
if (index.getName().equals(name)) {
return index;
}
}
}
return null;
}
/*
* These match the string that dynamo uses for these types.
* https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBMapper.DataTypes.html
*/
protected static String getTypeStringFromObject(Object obj) {
if (obj instanceof Number) {
return "N";
} else if (obj instanceof Boolean) {
return "BOOL";
} else {
return "S";
}
}
public static AmazonDynamoDB buildDynamoClient(String awsRegion, String awsAccessKey, String awsSecretKey, String awsEndpoint) {
AmazonDynamoDBClientBuilder builder = AmazonDynamoDBClientBuilder.standard();
if (!Utils.empty(awsRegion)) {
if (!Utils.empty(awsEndpoint)) {
AwsClientBuilder.EndpointConfiguration endpointConfig = new AwsClientBuilder.EndpointConfiguration(awsEndpoint, awsRegion);
builder.withEndpointConfiguration(endpointConfig);
} else {
builder.withRegion(awsRegion);
}
}
if (!Utils.empty(awsAccessKey) && !Utils.empty(awsSecretKey)) {
BasicAWSCredentials creds = new BasicAWSCredentials(awsAccessKey, awsSecretKey);
builder.withCredentials(new AWSStaticCredentialsProvider(creds));
}
AmazonDynamoDB dynamoClient = builder.build();
return dynamoClient;
}
@Override
public Results doSelect(Collection table, List columnMappedTerms) throws ApiException {
return (Results) run(() -> {
return doSelect0(table, columnMappedTerms);
});
}
public Results doSelect0(Collection table, List columnMappedTerms) throws ApiException{
DynamoDbQuery query = new DynamoDbQuery(this , table, columnMappedTerms).withDynamoTable(getDynamoTable(table));
Results result = query.doSelect();
return result;
}
@Override
public List doUpsert(Collection collection, List