use of com.scalar.db.api.TableMetadata in project scalardb by scalar-labs.
the class BatchHandler method handle.
/**
* Execute the specified list of {@link Mutation}s in batch. All the {@link Mutation}s in the list
* must be for the same partition.
*
* @param mutations a list of {@code Mutation}s to execute
* @throws NoMutationException if at least one of conditional {@code Mutation}s failed because it
* didn't meet the condition
*/
public void handle(List<? extends Mutation> mutations) throws ExecutionException {
if (mutations.size() > 25) {
throw new IllegalArgumentException("DynamoDB cannot batch more than 25 mutations at once.");
}
TableMetadata tableMetadata = metadataManager.getTableMetadata(mutations.get(0));
TransactWriteItemsRequest.Builder builder = TransactWriteItemsRequest.builder();
List<TransactWriteItem> transactItems = new ArrayList<>();
mutations.forEach(m -> transactItems.add(makeWriteItem(m, tableMetadata)));
builder.transactItems(transactItems);
try {
client.transactWriteItems(builder.build());
} catch (TransactionCanceledException e) {
boolean allReasonsAreTransactionConflicts = true;
for (CancellationReason reason : e.cancellationReasons()) {
if (reason.code().equals("ConditionalCheckFailed")) {
throw new NoMutationException("no mutation was applied.", e);
}
if (!reason.code().equals("TransactionConflict") && !reason.code().equals("None")) {
allReasonsAreTransactionConflicts = false;
}
}
if (allReasonsAreTransactionConflicts) {
// RetriableExecutionException
throw new RetriableExecutionException(e.getMessage(), e);
}
throw new ExecutionException(e.getMessage(), e);
} catch (DynamoDbException e) {
throw new ExecutionException(e.getMessage(), e);
}
}
use of com.scalar.db.api.TableMetadata in project scalardb by scalar-labs.
the class DynamoAdmin method createIndex.
@Override
public void createIndex(String namespace, String table, String columnName, Map<String, String> options) throws ExecutionException {
long ru = Long.parseLong(options.getOrDefault(REQUEST_UNIT, DEFAULT_REQUEST_UNIT));
TableMetadata metadata = getTableMetadata(namespace, table);
try {
client.updateTable(UpdateTableRequest.builder().tableName(getFullTableName(namespace, table)).attributeDefinitions(AttributeDefinition.builder().attributeName(columnName).attributeType(SECONDARY_INDEX_DATATYPE_MAP.get(metadata.getColumnDataType(columnName))).build()).globalSecondaryIndexUpdates(GlobalSecondaryIndexUpdate.builder().create(CreateGlobalSecondaryIndexAction.builder().indexName(getGlobalIndexName(namespace, table, columnName)).keySchema(KeySchemaElement.builder().attributeName(columnName).keyType(KeyType.HASH).build()).projection(Projection.builder().projectionType(ProjectionType.ALL).build()).provisionedThroughput(ProvisionedThroughput.builder().readCapacityUnits(ru).writeCapacityUnits(ru).build()).build()).build()).build());
} catch (Exception e) {
throw new ExecutionException("creating the secondary index failed", e);
}
waitForIndexCreation(namespace, table, columnName);
// enable auto scaling
boolean noScaling = Boolean.parseBoolean(options.getOrDefault(NO_SCALING, DEFAULT_NO_SCALING));
if (!noScaling) {
List<RegisterScalableTargetRequest> registerScalableTargetRequestList = new ArrayList<>();
List<PutScalingPolicyRequest> putScalingPolicyRequestList = new ArrayList<>();
// write, read scaling of global indexes (secondary indexes)
for (String scalingType : SECONDARY_INDEX_SCALING_TYPE_SET) {
registerScalableTargetRequestList.add(buildRegisterScalableTargetRequest(getGlobalIndexResourceID(namespace, table, columnName), scalingType, (int) ru));
putScalingPolicyRequestList.add(buildPutScalingPolicyRequest(getGlobalIndexResourceID(namespace, table, columnName), scalingType));
}
registerScalableTarget(registerScalableTargetRequestList);
putScalingPolicy(putScalingPolicyRequestList);
}
// update metadata
TableMetadata tableMetadata = getTableMetadata(namespace, table);
putTableMetadata(namespace, table, TableMetadata.newBuilder(tableMetadata).addSecondaryIndex(columnName).build());
}
use of com.scalar.db.api.TableMetadata in project scalardb by scalar-labs.
the class DynamoAdmin method disableAutoScaling.
private void disableAutoScaling(String namespace, String table) throws ExecutionException {
TableMetadata tableMetadata = getTableMetadata(namespace, table);
if (tableMetadata == null) {
return;
}
List<DeleteScalingPolicyRequest> deleteScalingPolicyRequestList = new ArrayList<>();
List<DeregisterScalableTargetRequest> deregisterScalableTargetRequestList = new ArrayList<>();
// write, read scaling of table
for (String scalingType : TABLE_SCALING_TYPE_SET) {
deleteScalingPolicyRequestList.add(buildDeleteScalingPolicyRequest(getTableResourceID(namespace, table), scalingType));
deregisterScalableTargetRequestList.add(buildDeregisterScalableTargetRequest(getTableResourceID(namespace, table), scalingType));
}
// write, read scaling of global indexes (secondary indexes)
Set<String> secondaryIndexes = tableMetadata.getSecondaryIndexNames();
for (String secondaryIndex : secondaryIndexes) {
for (String scalingType : SECONDARY_INDEX_SCALING_TYPE_SET) {
deleteScalingPolicyRequestList.add(buildDeleteScalingPolicyRequest(getGlobalIndexResourceID(namespace, table, secondaryIndex), scalingType));
deregisterScalableTargetRequestList.add(buildDeregisterScalableTargetRequest(getGlobalIndexResourceID(namespace, table, secondaryIndex), scalingType));
}
}
deleteScalingPolicy(deleteScalingPolicyRequestList);
deregisterScalableTarget(deregisterScalableTargetRequestList);
}
use of com.scalar.db.api.TableMetadata in project scalardb by scalar-labs.
the class Cosmos method get.
@Override
@Nonnull
public Optional<Result> get(Get get) throws ExecutionException {
get = copyAndSetTargetToIfNot(get);
operationChecker.check(get);
List<Record> records = selectStatementHandler.handle(get);
if (records.size() > 1) {
throw new IllegalArgumentException("please use scan() for non-exact match selection");
}
if (records.isEmpty() || records.get(0) == null) {
return Optional.empty();
}
TableMetadata metadata = metadataManager.getTableMetadata(get);
return Optional.of(new ResultInterpreter(get.getProjections(), metadata).interpret(records.get(0)));
}
use of com.scalar.db.api.TableMetadata in project scalardb by scalar-labs.
the class Cosmos method scan.
@Override
public Scanner scan(Scan scan) throws ExecutionException {
scan = copyAndSetTargetToIfNot(scan);
if (scan instanceof ScanAll) {
operationChecker.check((ScanAll) scan);
} else {
operationChecker.check(scan);
}
List<Record> records = selectStatementHandler.handle(scan);
TableMetadata metadata = metadataManager.getTableMetadata(scan);
return new ScannerImpl(records, new ResultInterpreter(scan.getProjections(), metadata));
}
Aggregations