Search in sources :

Example 41 with TableMetadata

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

the class CosmosAdmin method createIndex.

@Override
public void createIndex(String namespace, String table, String columnName, Map<String, String> options) throws ExecutionException {
    TableMetadata tableMetadata = getTableMetadata(namespace, table);
    TableMetadata newTableMetadata = TableMetadata.newBuilder(tableMetadata).addSecondaryIndex(columnName).build();
    updateIndexingPolicy(namespace, table, newTableMetadata);
    // update metadata
    putTableMetadata(namespace, table, newTableMetadata);
}
Also used : TableMetadata(com.scalar.db.api.TableMetadata)

Example 42 with TableMetadata

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

the class JdbcAdmin method getTableMetadata.

@Override
public TableMetadata getTableMetadata(String namespace, String table) throws ExecutionException {
    TableMetadata.Builder builder = TableMetadata.newBuilder();
    boolean tableExists = false;
    try (Connection connection = dataSource.getConnection();
        PreparedStatement preparedStatement = connection.prepareStatement(getSelectColumnsStatement())) {
        preparedStatement.setString(1, getFullTableName(namespace, table));
        try (ResultSet resultSet = preparedStatement.executeQuery()) {
            while (resultSet.next()) {
                tableExists = true;
                String columnName = resultSet.getString(METADATA_COL_COLUMN_NAME);
                DataType dataType = DataType.valueOf(resultSet.getString(METADATA_COL_DATA_TYPE));
                builder.addColumn(columnName, dataType);
                boolean indexed = resultSet.getBoolean(METADATA_COL_INDEXED);
                if (indexed) {
                    builder.addSecondaryIndex(columnName);
                }
                String keyType = resultSet.getString(METADATA_COL_KEY_TYPE);
                if (keyType == null) {
                    continue;
                }
                switch(KeyType.valueOf(keyType)) {
                    case PARTITION:
                        builder.addPartitionKey(columnName);
                        break;
                    case CLUSTERING:
                        Scan.Ordering.Order clusteringOrder = Scan.Ordering.Order.valueOf(resultSet.getString(METADATA_COL_CLUSTERING_ORDER));
                        builder.addClusteringKey(columnName, clusteringOrder);
                        break;
                    default:
                        throw new AssertionError("invalid key type: " + keyType);
                }
            }
        }
    } catch (SQLException e) {
        throw new ExecutionException("getting a table metadata failed", e);
    }
    if (!tableExists) {
        return null;
    }
    return builder.build();
}
Also used : TableMetadata(com.scalar.db.api.TableMetadata) SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) ResultSet(java.sql.ResultSet) Ordering(com.scalar.db.api.Scan.Ordering) DataType(com.scalar.db.io.DataType) Order(com.scalar.db.api.Scan.Ordering.Order) ExecutionException(com.scalar.db.exception.storage.ExecutionException)

Example 43 with TableMetadata

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

the class OperationChecker method check.

public void check(Get get) throws ExecutionException {
    TableMetadata metadata = getMetadata(get);
    checkProjections(get, metadata);
    if (ScalarDbUtils.isSecondaryIndexSpecified(get, metadata)) {
        if (!new ColumnChecker(metadata, true, false, false, false).check(get.getPartitionKey().getColumns().get(0))) {
            throw new IllegalArgumentException("The partition key is not properly specified. Operation: " + get);
        }
        if (get.getClusteringKey().isPresent()) {
            throw new IllegalArgumentException("Clustering keys cannot be specified when using an index. Operation: " + get);
        }
        return;
    }
    checkPrimaryKey(get, metadata);
}
Also used : TableMetadata(com.scalar.db.api.TableMetadata)

Example 44 with TableMetadata

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

the class JdbcService method scan.

public List<Result> scan(Scan scan, Connection connection) throws SQLException, ExecutionException {
    if (scan instanceof ScanAll) {
        operationChecker.check((ScanAll) scan);
    } else {
        operationChecker.check(scan);
    }
    TableMetadata tableMetadata = tableMetadataManager.getTableMetadata(scan);
    SelectQuery selectQuery = scan instanceof ScanAll ? buildSelectQueryForScanAll((ScanAll) scan, tableMetadata) : buildSelectQueryForScan(scan, tableMetadata);
    try (PreparedStatement preparedStatement = connection.prepareStatement(selectQuery.sql())) {
        selectQuery.bind(preparedStatement);
        try (ResultSet resultSet = preparedStatement.executeQuery()) {
            List<Result> ret = new ArrayList<>();
            ResultInterpreter resultInterpreter = new ResultInterpreter(scan.getProjections(), tableMetadata);
            while (resultSet.next()) {
                ret.add(resultInterpreter.interpret(resultSet));
            }
            return ret;
        }
    }
}
Also used : TableMetadata(com.scalar.db.api.TableMetadata) SelectQuery(com.scalar.db.storage.jdbc.query.SelectQuery) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) ScanAll(com.scalar.db.api.ScanAll) PreparedStatement(java.sql.PreparedStatement) Result(com.scalar.db.api.Result)

Example 45 with TableMetadata

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

the class JdbcService method delete.

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

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