Search in sources :

Example 1 with TableMetadata

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

the class AdminIntegrationTestBase method getTableMetadata_WrongTableGiven_ShouldReturnNull.

@Test
public void getTableMetadata_WrongTableGiven_ShouldReturnNull() throws ExecutionException {
    // Arrange
    // Act
    TableMetadata tableMetadata = admin.getTableMetadata("wrong_ns", "wrong_table");
    // Assert
    assertThat(tableMetadata).isNull();
}
Also used : TableMetadata(com.scalar.db.api.TableMetadata) Test(org.junit.Test)

Example 2 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.Test)

Example 3 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.Test)

Example 4 with TableMetadata

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

the class PutStatementHandler method handle.

@Nonnull
@Override
public List<Map<String, AttributeValue>> handle(Operation operation) throws ExecutionException {
    checkArgument(operation, Put.class);
    Put put = (Put) operation;
    TableMetadata tableMetadata = metadataManager.getTableMetadata(operation);
    try {
        execute(put, tableMetadata);
    } catch (ConditionalCheckFailedException e) {
        throw new NoMutationException("no mutation was applied.", e);
    } catch (TransactionConflictException e) {
        throw new RetriableExecutionException(e.getMessage(), e);
    } catch (DynamoDbException e) {
        throw new ExecutionException(e.getMessage(), e);
    }
    return Collections.emptyList();
}
Also used : TableMetadata(com.scalar.db.api.TableMetadata) DynamoDbException(software.amazon.awssdk.services.dynamodb.model.DynamoDbException) TransactionConflictException(software.amazon.awssdk.services.dynamodb.model.TransactionConflictException) RetriableExecutionException(com.scalar.db.exception.storage.RetriableExecutionException) ExecutionException(com.scalar.db.exception.storage.ExecutionException) RetriableExecutionException(com.scalar.db.exception.storage.RetriableExecutionException) ConditionalCheckFailedException(software.amazon.awssdk.services.dynamodb.model.ConditionalCheckFailedException) NoMutationException(com.scalar.db.exception.storage.NoMutationException) Put(com.scalar.db.api.Put) Nonnull(javax.annotation.Nonnull)

Example 5 with TableMetadata

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

the class ConsensusCommitAdminTest method createTransactionalTable_tableMetadataGiven_shouldCreateTransactionalTableProperly.

@Test
public void createTransactionalTable_tableMetadataGiven_shouldCreateTransactionalTableProperly() throws ExecutionException {
    // Arrange
    final String ACCOUNT_ID = "account_id";
    final String ACCOUNT_TYPE = "account_type";
    final String BALANCE = "balance";
    TableMetadata tableMetadata = TableMetadata.newBuilder().addColumn(ACCOUNT_ID, DataType.INT).addColumn(ACCOUNT_TYPE, DataType.INT).addColumn(BALANCE, DataType.INT).addPartitionKey(ACCOUNT_ID).addClusteringKey(ACCOUNT_TYPE).build();
    TableMetadata expected = TableMetadata.newBuilder().addColumn(ACCOUNT_ID, DataType.INT).addColumn(ACCOUNT_TYPE, DataType.INT).addColumn(BALANCE, DataType.INT).addColumn(Attribute.ID, DataType.TEXT).addColumn(Attribute.STATE, DataType.INT).addColumn(Attribute.VERSION, DataType.INT).addColumn(Attribute.PREPARED_AT, DataType.BIGINT).addColumn(Attribute.COMMITTED_AT, DataType.BIGINT).addColumn(Attribute.BEFORE_PREFIX + BALANCE, DataType.INT).addColumn(Attribute.BEFORE_ID, DataType.TEXT).addColumn(Attribute.BEFORE_STATE, DataType.INT).addColumn(Attribute.BEFORE_VERSION, DataType.INT).addColumn(Attribute.BEFORE_PREPARED_AT, DataType.BIGINT).addColumn(Attribute.BEFORE_COMMITTED_AT, DataType.BIGINT).addPartitionKey(ACCOUNT_ID).addClusteringKey(ACCOUNT_TYPE).build();
    // Act
    admin.createTransactionalTable(NAMESPACE, TABLE, tableMetadata);
    // Assert
    verify(distributedStorageAdmin).createTable(NAMESPACE, TABLE, expected, Collections.emptyMap());
}
Also used : TableMetadata(com.scalar.db.api.TableMetadata) Test(org.junit.Test)

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