use of com.azure.cosmos.CosmosDatabase in project scalardb by scalar-labs.
the class CosmosAdminTest method dropTable_WithMetadataLeft_ShouldDropContainerAndOnlyDeleteMetadata.
private void dropTable_WithMetadataLeft_ShouldDropContainerAndOnlyDeleteMetadata(Optional<String> tableMetadataDatabase) throws ExecutionException {
// Arrange
String namespace = "ns";
String table = "sample_table";
when(client.getDatabase(anyString())).thenReturn(database);
when(database.getContainer(anyString())).thenReturn(container);
// for metadata table
String metadataDatabaseName = tableMetadataDatabase.orElse(CosmosAdmin.METADATA_DATABASE);
CosmosDatabase metadataDatabase = mock(CosmosDatabase.class);
CosmosContainer metadataContainer = mock(CosmosContainer.class);
when(client.getDatabase(metadataDatabaseName)).thenReturn(metadataDatabase);
when(metadataDatabase.getContainer(CosmosAdmin.METADATA_CONTAINER)).thenReturn(metadataContainer);
@SuppressWarnings("unchecked") CosmosPagedIterable<Object> queryResults = mock(CosmosPagedIterable.class);
when(metadataContainer.queryItems(anyString(), any(), eq(Object.class))).thenReturn(queryResults);
when(queryResults.stream()).thenReturn(Stream.of(new CosmosTableMetadata()));
if (tableMetadataDatabase.isPresent()) {
when(config.getTableMetadataDatabase()).thenReturn(tableMetadataDatabase);
admin = new CosmosAdmin(client, config);
}
// Act
admin.dropTable(namespace, table);
// Assert
verify(container).delete();
// for metadata table
verify(client, atLeastOnce()).getDatabase(metadataDatabaseName);
verify(metadataDatabase, atLeastOnce()).getContainer(CosmosAdmin.METADATA_CONTAINER);
String fullTable = getFullTableName(namespace, table);
verify(metadataContainer).deleteItem(eq(fullTable), eq(new PartitionKey(fullTable)), refEq(new CosmosItemRequestOptions()));
verify(metadataContainer, never()).delete();
verify(metadataDatabase, never()).delete();
}
use of com.azure.cosmos.CosmosDatabase in project scalardb by scalar-labs.
the class CosmosAdmin method createContainer.
private void createContainer(String database, String table, TableMetadata metadata) throws ExecutionException {
if (!databaseExists(database)) {
throw new ExecutionException("the database does not exists");
}
CosmosDatabase cosmosDatabase = client.getDatabase(database);
CosmosContainerProperties properties = computeContainerProperties(table, metadata);
cosmosDatabase.createContainer(properties);
CosmosStoredProcedureProperties storedProcedureProperties = computeContainerStoredProcedureProperties();
cosmosDatabase.getContainer(table).getScripts().createStoredProcedure(storedProcedureProperties);
}
use of com.azure.cosmos.CosmosDatabase in project scalardb by scalar-labs.
the class CosmosAdmin method updateIndexingPolicy.
private void updateIndexingPolicy(String databaseName, String containerName, TableMetadata newTableMetadata) throws ExecutionException {
CosmosDatabase database = client.getDatabase(databaseName);
try {
// get the existing container properties
CosmosContainerResponse response = database.createContainerIfNotExists(containerName, PARTITION_KEY_PATH);
CosmosContainerProperties properties = response.getProperties();
// set the new index policy to the container properties
properties.setIndexingPolicy(computeIndexingPolicy(newTableMetadata));
// update the container properties
database.getContainer(containerName).replace(properties);
} catch (RuntimeException e) {
throw new ExecutionException("updating the indexing policy failed", e);
}
}
use of com.azure.cosmos.CosmosDatabase in project scalardb by scalar-labs.
the class CosmosAdmin method truncateTable.
@Override
public void truncateTable(String namespace, String table) throws ExecutionException {
try {
CosmosDatabase database = client.getDatabase(namespace);
CosmosContainer container = database.getContainer(table);
CosmosPagedIterable<Record> records = container.queryItems("SELECT t." + ID + ", t." + CONCATENATED_PARTITION_KEY + " FROM " + "t", new CosmosQueryRequestOptions(), Record.class);
records.forEach(record -> container.deleteItem(record.getId(), new PartitionKey(record.getConcatenatedPartitionKey()), new CosmosItemRequestOptions()));
} catch (RuntimeException e) {
throw new ExecutionException("truncating the container failed", e);
}
}
use of com.azure.cosmos.CosmosDatabase in project scalardb by scalar-labs.
the class CosmosAdmin method dropTable.
@Override
public void dropTable(String namespace, String table) throws ExecutionException {
if (!databaseExists(namespace)) {
throw new ExecutionException("the database does not exist");
}
CosmosDatabase database = client.getDatabase(namespace);
if (!containerExists(database, table)) {
throw new ExecutionException("the container does not exist");
}
try {
database.getContainer(table).delete();
deleteTableMetadata(namespace, table);
} catch (RuntimeException e) {
throw new ExecutionException("deleting the container failed", e);
}
}
Aggregations