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