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);
}
}
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);
}
}
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);
}
}
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);
}
}
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);
}
}
Aggregations