Search in sources :

Example 6 with CosmosDatabase

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();
}
Also used : CosmosItemRequestOptions(com.azure.cosmos.models.CosmosItemRequestOptions) CosmosContainer(com.azure.cosmos.CosmosContainer) CosmosDatabase(com.azure.cosmos.CosmosDatabase) PartitionKey(com.azure.cosmos.models.PartitionKey) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString)

Example 7 with CosmosDatabase

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);
}
Also used : CosmosDatabase(com.azure.cosmos.CosmosDatabase) CosmosContainerProperties(com.azure.cosmos.models.CosmosContainerProperties) CosmosStoredProcedureProperties(com.azure.cosmos.models.CosmosStoredProcedureProperties) ExecutionException(com.scalar.db.exception.storage.ExecutionException)

Example 8 with CosmosDatabase

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);
    }
}
Also used : CosmosDatabase(com.azure.cosmos.CosmosDatabase) CosmosContainerProperties(com.azure.cosmos.models.CosmosContainerProperties) ExecutionException(com.scalar.db.exception.storage.ExecutionException) CosmosContainerResponse(com.azure.cosmos.models.CosmosContainerResponse)

Example 9 with CosmosDatabase

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);
    }
}
Also used : CosmosQueryRequestOptions(com.azure.cosmos.models.CosmosQueryRequestOptions) CosmosItemRequestOptions(com.azure.cosmos.models.CosmosItemRequestOptions) CosmosContainer(com.azure.cosmos.CosmosContainer) CosmosDatabase(com.azure.cosmos.CosmosDatabase) PartitionKey(com.azure.cosmos.models.PartitionKey) ExecutionException(com.scalar.db.exception.storage.ExecutionException)

Example 10 with CosmosDatabase

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);
    }
}
Also used : CosmosDatabase(com.azure.cosmos.CosmosDatabase) ExecutionException(com.scalar.db.exception.storage.ExecutionException)

Aggregations

CosmosDatabase (com.azure.cosmos.CosmosDatabase)18 CosmosContainerProperties (com.azure.cosmos.models.CosmosContainerProperties)11 CosmosContainer (com.azure.cosmos.CosmosContainer)9 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)6 PartitionKey (com.azure.cosmos.models.PartitionKey)5 IndexingPolicy (com.azure.cosmos.models.IndexingPolicy)4 ExecutionException (com.scalar.db.exception.storage.ExecutionException)4 Before (org.junit.Before)4 CosmosClient (com.azure.cosmos.CosmosClient)3 ConflictResolutionPolicy (com.azure.cosmos.models.ConflictResolutionPolicy)3 CosmosContainerResponse (com.azure.cosmos.models.CosmosContainerResponse)3 CosmosItemRequestOptions (com.azure.cosmos.models.CosmosItemRequestOptions)3 CosmosStoredProcedureProperties (com.azure.cosmos.models.CosmosStoredProcedureProperties)3 CosmosClientBuilder (com.azure.cosmos.CosmosClientBuilder)2 CosmosScripts (com.azure.cosmos.CosmosScripts)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 ImmutableMap (com.google.common.collect.ImmutableMap)2 TableMetadata (com.scalar.db.api.TableMetadata)2 URL (java.net.URL)2