Search in sources :

Example 51 with ExecutionException

use of com.scalar.db.exception.storage.ExecutionException in project scalardb by scalar-labs.

the class DynamoAdmin method putTableMetadata.

private void putTableMetadata(String namespace, String table, TableMetadata metadata) throws ExecutionException {
    // Add metadata
    Map<String, AttributeValue> itemValues = new HashMap<>();
    itemValues.put(METADATA_ATTR_TABLE, AttributeValue.builder().s(getFullTableName(namespace, table)).build());
    Map<String, AttributeValue> columns = new HashMap<>();
    for (String columnName : metadata.getColumnNames()) {
        columns.put(columnName, AttributeValue.builder().s(metadata.getColumnDataType(columnName).name().toLowerCase()).build());
    }
    itemValues.put(METADATA_ATTR_COLUMNS, AttributeValue.builder().m(columns).build());
    itemValues.put(METADATA_ATTR_PARTITION_KEY, AttributeValue.builder().l(metadata.getPartitionKeyNames().stream().map(pKey -> AttributeValue.builder().s(pKey).build()).collect(Collectors.toList())).build());
    if (!metadata.getClusteringKeyNames().isEmpty()) {
        itemValues.put(METADATA_ATTR_CLUSTERING_KEY, AttributeValue.builder().l(metadata.getClusteringKeyNames().stream().map(pKey -> AttributeValue.builder().s(pKey).build()).collect(Collectors.toList())).build());
        Map<String, AttributeValue> clusteringOrders = new HashMap<>();
        for (String clusteringKeyName : metadata.getClusteringKeyNames()) {
            clusteringOrders.put(clusteringKeyName, AttributeValue.builder().s(metadata.getClusteringOrder(clusteringKeyName).name()).build());
        }
        itemValues.put(METADATA_ATTR_CLUSTERING_ORDERS, AttributeValue.builder().m(clusteringOrders).build());
    }
    if (!metadata.getSecondaryIndexNames().isEmpty()) {
        itemValues.put(METADATA_ATTR_SECONDARY_INDEX, AttributeValue.builder().ss(metadata.getSecondaryIndexNames()).build());
    }
    try {
        client.putItem(PutItemRequest.builder().tableName(getFullTableName(metadataNamespace, METADATA_TABLE)).item(itemValues).build());
    } catch (Exception e) {
        throw new ExecutionException("adding the meta data for table " + getFullTableName(namespace, table) + " failed", e);
    }
}
Also used : AttributeValue(software.amazon.awssdk.services.dynamodb.model.AttributeValue) HashMap(java.util.HashMap) ExecutionException(com.scalar.db.exception.storage.ExecutionException) ExecutionException(com.scalar.db.exception.storage.ExecutionException) ObjectNotFoundException(software.amazon.awssdk.services.applicationautoscaling.model.ObjectNotFoundException) ResourceNotFoundException(software.amazon.awssdk.services.dynamodb.model.ResourceNotFoundException) DynamoDbException(software.amazon.awssdk.services.dynamodb.model.DynamoDbException)

Example 52 with ExecutionException

use of com.scalar.db.exception.storage.ExecutionException in project scalardb by scalar-labs.

the class JdbcAdmin method createNamespace.

@Override
public void createNamespace(String namespace, Map<String, String> options) throws ExecutionException {
    String fullNamespace = enclose(namespace);
    try (Connection connection = dataSource.getConnection()) {
        if (rdbEngine == RdbEngine.ORACLE) {
            execute(connection, "CREATE USER " + fullNamespace + " IDENTIFIED BY \"oracle\"");
            execute(connection, "ALTER USER " + fullNamespace + " quota unlimited on USERS");
        } else if (rdbEngine == RdbEngine.MYSQL) {
            execute(connection, "CREATE SCHEMA " + fullNamespace + " character set utf8 COLLATE utf8_bin");
        } else {
            execute(connection, "CREATE SCHEMA " + fullNamespace);
        }
    } catch (SQLException e) {
        throw new ExecutionException("creating the schema failed", e);
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) ExecutionException(com.scalar.db.exception.storage.ExecutionException)

Example 53 with ExecutionException

use of com.scalar.db.exception.storage.ExecutionException in project scalardb by scalar-labs.

the class JdbcAdmin method getNamespaceTableNames.

@Override
public Set<String> getNamespaceTableNames(String namespace) throws ExecutionException {
    String selectTablesOfNamespaceStatement = "SELECT DISTINCT " + enclose(METADATA_COL_FULL_TABLE_NAME) + " FROM " + encloseFullTableName(metadataSchema, METADATA_TABLE) + " WHERE " + enclose(METADATA_COL_FULL_TABLE_NAME) + " LIKE ?";
    try (Connection connection = dataSource.getConnection();
        PreparedStatement preparedStatement = connection.prepareStatement(selectTablesOfNamespaceStatement)) {
        String prefix = namespace + ".";
        preparedStatement.setString(1, prefix + "%");
        try (ResultSet results = preparedStatement.executeQuery()) {
            Set<String> tableNames = new HashSet<>();
            while (results.next()) {
                String tableName = results.getString(METADATA_COL_FULL_TABLE_NAME).substring(prefix.length());
                tableNames.add(tableName);
            }
            return tableNames;
        }
    } catch (SQLException e) {
        // query
        if ((rdbEngine == RdbEngine.MYSQL && (e.getErrorCode() == 1049 || e.getErrorCode() == 1146)) || (rdbEngine == RdbEngine.POSTGRESQL && e.getSQLState().equals("42P01")) || (rdbEngine == RdbEngine.ORACLE && e.getErrorCode() == 942) || (rdbEngine == RdbEngine.SQL_SERVER && e.getErrorCode() == 208)) {
            return Collections.emptySet();
        }
        throw new ExecutionException("retrieving the namespace table names failed", e);
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) ExecutionException(com.scalar.db.exception.storage.ExecutionException) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 54 with ExecutionException

use of com.scalar.db.exception.storage.ExecutionException in project scalardb by scalar-labs.

the class JdbcAdmin method createTable.

@Override
public void createTable(String namespace, String table, TableMetadata metadata, Map<String, String> options) throws ExecutionException {
    try (Connection connection = dataSource.getConnection()) {
        createTableInternal(connection, namespace, table, metadata);
        createIndex(connection, namespace, table, metadata);
        addTableMetadata(connection, namespace, table, metadata);
    } catch (SQLException e) {
        throw new ExecutionException("creating the table failed: " + getFullTableName(namespace, table), e);
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) ExecutionException(com.scalar.db.exception.storage.ExecutionException)

Example 55 with ExecutionException

use of com.scalar.db.exception.storage.ExecutionException in project scalardb by scalar-labs.

the class JdbcAdmin method dropIndex.

@Override
public void dropIndex(String namespace, String table, String columnName) throws ExecutionException {
    try (Connection connection = dataSource.getConnection()) {
        dropIndex(connection, namespace, table, columnName);
        updateTableMetadata(connection, namespace, table, columnName, false);
    } catch (SQLException e) {
        throw new ExecutionException("dropping the secondary index failed", e);
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) ExecutionException(com.scalar.db.exception.storage.ExecutionException)

Aggregations

ExecutionException (com.scalar.db.exception.storage.ExecutionException)78 DynamoDbException (software.amazon.awssdk.services.dynamodb.model.DynamoDbException)23 RetriableExecutionException (com.scalar.db.exception.storage.RetriableExecutionException)18 ObjectNotFoundException (software.amazon.awssdk.services.applicationautoscaling.model.ObjectNotFoundException)18 ResourceNotFoundException (software.amazon.awssdk.services.dynamodb.model.ResourceNotFoundException)18 SQLException (java.sql.SQLException)14 ArrayList (java.util.ArrayList)14 Connection (java.sql.Connection)12 Test (org.junit.jupiter.api.Test)11 HashSet (java.util.HashSet)10 NoMutationException (com.scalar.db.exception.storage.NoMutationException)9 TableMetadata (com.scalar.db.api.TableMetadata)8 Value (com.scalar.db.io.Value)8 Put (com.scalar.db.api.Put)7 TransactionState (com.scalar.db.api.TransactionState)6 CrudException (com.scalar.db.exception.transaction.CrudException)6 HashMap (java.util.HashMap)6 AttributeValue (software.amazon.awssdk.services.dynamodb.model.AttributeValue)5 CosmosDatabase (com.azure.cosmos.CosmosDatabase)4 Get (com.scalar.db.api.Get)4