Search in sources :

Example 46 with TableMetadata

use of com.scalar.db.api.TableMetadata in project scalardb by scalar-labs.

the class JdbcService method put.

public boolean put(Put put, Connection connection) throws SQLException, ExecutionException {
    operationChecker.check(put);
    TableMetadata tableMetadata = tableMetadataManager.getTableMetadata(put);
    if (!put.getCondition().isPresent()) {
        UpsertQuery upsertQuery = queryBuilder.upsertInto(put.forNamespace().get(), put.forTable().get(), tableMetadata).values(put.getPartitionKey(), put.getClusteringKey(), put.getColumns()).build();
        try (PreparedStatement preparedStatement = connection.prepareStatement(upsertQuery.sql())) {
            upsertQuery.bind(preparedStatement);
            preparedStatement.executeUpdate();
            return true;
        }
    } else {
        return new ConditionalMutator(put, tableMetadata, connection, queryBuilder).mutate();
    }
}
Also used : TableMetadata(com.scalar.db.api.TableMetadata) UpsertQuery(com.scalar.db.storage.jdbc.query.UpsertQuery) PreparedStatement(java.sql.PreparedStatement)

Example 47 with TableMetadata

use of com.scalar.db.api.TableMetadata in project scalardb by scalar-labs.

the class DeleteStatementHandler method execute.

@Override
protected List<Record> execute(Operation operation) throws CosmosException, ExecutionException {
    Mutation mutation = (Mutation) operation;
    TableMetadata tableMetadata = metadataManager.getTableMetadata(mutation);
    if (mutation.getCondition().isPresent()) {
        executeStoredProcedure(mutation, tableMetadata);
    } else {
        execute(mutation, tableMetadata);
    }
    return Collections.emptyList();
}
Also used : TableMetadata(com.scalar.db.api.TableMetadata) Mutation(com.scalar.db.api.Mutation)

Example 48 with TableMetadata

use of com.scalar.db.api.TableMetadata in project scalardb by scalar-labs.

the class TableMetadataManagerTest method getTableMetadata_CalledAfterCacheExpiration_ShouldCallDistributedStorageAdminAgain.

@Test
public void getTableMetadata_CalledAfterCacheExpiration_ShouldCallDistributedStorageAdminAgain() throws ExecutionException {
    // Arrange
    // one second
    TableMetadataManager tableMetadataManager = new TableMetadataManager(admin, 1L);
    TableMetadata expectedTableMetadata = TableMetadata.newBuilder().addColumn("c1", DataType.INT).addColumn("c2", DataType.INT).addPartitionKey("c1").build();
    when(admin.getTableMetadata(anyString(), anyString())).thenReturn(expectedTableMetadata);
    Get get = new Get(new Key("c1", "aaa")).forNamespace("ns").forTable("tbl");
    // Act
    tableMetadataManager.getTableMetadata(get);
    // Wait for cache to be expired
    Uninterruptibles.sleepUninterruptibly(1200, TimeUnit.MILLISECONDS);
    TableMetadata actualTableMetadata = tableMetadataManager.getTableMetadata(get);
    // Assert
    verify(admin, times(2)).getTableMetadata(anyString(), anyString());
    assertThat(actualTableMetadata).isEqualTo(expectedTableMetadata);
}
Also used : TableMetadata(com.scalar.db.api.TableMetadata) Get(com.scalar.db.api.Get) Key(com.scalar.db.io.Key) Test(org.junit.jupiter.api.Test)

Example 49 with TableMetadata

use of com.scalar.db.api.TableMetadata in project scalardb by scalar-labs.

the class TableMetadataManagerTest method getTableMetadata_CalledTwice_ShouldCallDistributedStorageAdminOnlyOnce.

@Test
public void getTableMetadata_CalledTwice_ShouldCallDistributedStorageAdminOnlyOnce() throws ExecutionException {
    // Arrange
    TableMetadataManager tableMetadataManager = new TableMetadataManager(admin, -1);
    TableMetadata expectedTableMetadata = TableMetadata.newBuilder().addColumn("c1", DataType.INT).addColumn("c2", DataType.INT).addPartitionKey("c1").build();
    when(admin.getTableMetadata(anyString(), anyString())).thenReturn(expectedTableMetadata);
    Get get = new Get(new Key("c1", "aaa")).forNamespace("ns").forTable("tbl");
    // Act
    tableMetadataManager.getTableMetadata(get);
    TableMetadata actualTableMetadata = tableMetadataManager.getTableMetadata(get);
    // Assert
    verify(admin).getTableMetadata(anyString(), anyString());
    assertThat(actualTableMetadata).isEqualTo(expectedTableMetadata);
}
Also used : TableMetadata(com.scalar.db.api.TableMetadata) Get(com.scalar.db.api.Get) Key(com.scalar.db.io.Key) Test(org.junit.jupiter.api.Test)

Example 50 with TableMetadata

use of com.scalar.db.api.TableMetadata in project scalardb by scalar-labs.

the class GrpcTransactionOnBidirectionalStream method scan.

public List<Result> scan(Scan scan) throws CrudException {
    throwIfTransactionFinished();
    ResponseOrError responseOrError = sendRequest(TransactionRequest.newBuilder().setScanRequest(ScanRequest.newBuilder().setScan(ProtoUtils.toScan(scan))).build());
    throwIfErrorForCrud(responseOrError);
    TableMetadata tableMetadata = getTableMetadata(scan);
    return responseOrError.getResponse().getScanResponse().getResultList().stream().map(r -> ProtoUtils.toResult(r, tableMetadata)).collect(Collectors.toList());
}
Also used : TableMetadata(com.scalar.db.api.TableMetadata) DistributedTransactionStub(com.scalar.db.rpc.DistributedTransactionGrpc.DistributedTransactionStub) ExecutionException(com.scalar.db.exception.storage.ExecutionException) AbortRequest(com.scalar.db.rpc.TransactionRequest.AbortRequest) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CrudConflictException(com.scalar.db.exception.transaction.CrudConflictException) CommitRequest(com.scalar.db.rpc.TransactionRequest.CommitRequest) StartRequest(com.scalar.db.rpc.TransactionRequest.StartRequest) TableMetadata(com.scalar.db.api.TableMetadata) CrudException(com.scalar.db.exception.transaction.CrudException) Result(com.scalar.db.api.Result) ScalarDbUtils(com.scalar.db.util.ScalarDbUtils) Code(io.grpc.Status.Code) ClientResponseObserver(io.grpc.stub.ClientResponseObserver) GetRequest(com.scalar.db.rpc.TransactionRequest.GetRequest) TransactionResponse(com.scalar.db.rpc.TransactionResponse) Mutation(com.scalar.db.api.Mutation) CommitException(com.scalar.db.exception.transaction.CommitException) MutateRequest(com.scalar.db.rpc.TransactionRequest.MutateRequest) CommitConflictException(com.scalar.db.exception.transaction.CommitConflictException) Nullable(javax.annotation.Nullable) ServiceTemporaryUnavailableException(com.scalar.db.util.retry.ServiceTemporaryUnavailableException) GrpcConfig(com.scalar.db.storage.rpc.GrpcConfig) TableMetadataManager(com.scalar.db.common.TableMetadataManager) Uninterruptibles(com.google.common.util.concurrent.Uninterruptibles) TransactionRequest(com.scalar.db.rpc.TransactionRequest) GetResponse(com.scalar.db.rpc.TransactionResponse.GetResponse) Operation(com.scalar.db.api.Operation) BlockingQueue(java.util.concurrent.BlockingQueue) ClientCallStreamObserver(io.grpc.stub.ClientCallStreamObserver) Get(com.scalar.db.api.Get) ProtoUtils(com.scalar.db.util.ProtoUtils) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Collectors(java.util.stream.Collectors) StatusRuntimeException(io.grpc.StatusRuntimeException) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) Scan(com.scalar.db.api.Scan) UnknownTransactionStatusException(com.scalar.db.exception.transaction.UnknownTransactionStatusException) AbortException(com.scalar.db.exception.transaction.AbortException) Optional(java.util.Optional) TransactionException(com.scalar.db.exception.transaction.TransactionException) ScanRequest(com.scalar.db.rpc.TransactionRequest.ScanRequest) NotThreadSafe(javax.annotation.concurrent.NotThreadSafe)

Aggregations

TableMetadata (com.scalar.db.api.TableMetadata)101 Test (org.junit.jupiter.api.Test)39 Get (com.scalar.db.api.Get)11 PreparedStatement (java.sql.PreparedStatement)10 ExecutionException (com.scalar.db.exception.storage.ExecutionException)9 ArrayList (java.util.ArrayList)7 Test (org.junit.Test)7 Key (com.scalar.db.io.Key)6 DynamoDbException (software.amazon.awssdk.services.dynamodb.model.DynamoDbException)6 Mutation (com.scalar.db.api.Mutation)5 Result (com.scalar.db.api.Result)5 GetTableMetadataResponse (com.scalar.db.rpc.GetTableMetadataResponse)5 ResultSet (java.sql.ResultSet)5 Nonnull (javax.annotation.Nonnull)5 HashMap (java.util.HashMap)4 Options (com.datastax.driver.core.schemabuilder.Create.Options)3 KeyspaceOptions (com.datastax.driver.core.schemabuilder.KeyspaceOptions)3 TableOptions (com.datastax.driver.core.schemabuilder.TableOptions)3 ScanAll (com.scalar.db.api.ScanAll)3 NoMutationException (com.scalar.db.exception.storage.NoMutationException)3