Search in sources :

Example 1 with CosmosItemRequestOptions

use of com.azure.cosmos.models.CosmosItemRequestOptions in project YCSB by brianfrankcooper.

the class AzureCosmosClient method delete.

@Override
public Status delete(String table, String key) {
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("Delete key {} from table {}", key, table);
    }
    try {
        CosmosContainer container = AzureCosmosClient.containerCache.get(table);
        if (container == null) {
            container = AzureCosmosClient.database.getContainer(table);
            AzureCosmosClient.containerCache.put(table, container);
        }
        container.deleteItem(key, new PartitionKey(key), new CosmosItemRequestOptions());
        return Status.OK;
    } catch (Exception e) {
        if (!AzureCosmosClient.includeExceptionStackInLog) {
            e = null;
        }
        LOGGER.error("Failed to delete key {} in collection {}", key, table, e);
    }
    return Status.ERROR;
}
Also used : CosmosItemRequestOptions(com.azure.cosmos.models.CosmosItemRequestOptions) CosmosContainer(com.azure.cosmos.CosmosContainer) PartitionKey(com.azure.cosmos.models.PartitionKey) CosmosException(com.azure.cosmos.CosmosException) DBException(site.ycsb.DBException)

Example 2 with CosmosItemRequestOptions

use of com.azure.cosmos.models.CosmosItemRequestOptions in project YCSB by brianfrankcooper.

the class AzureCosmosClient method update.

/**
 * Update a record in the database. Any field/value pairs in the specified
 * values HashMap will be written into the record with the specified record key,
 * overwriting any existing values with the same field name.
 *
 * @param table  The name of the table
 * @param key    The record key of the record to write.
 * @param values A HashMap of field/value pairs to update in the record
 * @return Zero on success, a non-zero error code on error
 */
@Override
public Status update(String table, String key, Map<String, ByteIterator> values) {
    String readEtag = "";
    // that will be a future improvement.
    for (int attempt = 0; attempt < NUM_UPDATE_ATTEMPTS; attempt++) {
        try {
            CosmosContainer container = AzureCosmosClient.containerCache.get(table);
            if (container == null) {
                container = AzureCosmosClient.database.getContainer(table);
                AzureCosmosClient.containerCache.put(table, container);
            }
            CosmosItemResponse<ObjectNode> response = container.readItem(key, new PartitionKey(key), ObjectNode.class);
            readEtag = response.getETag();
            ObjectNode node = response.getItem();
            for (Entry<String, ByteIterator> pair : values.entrySet()) {
                node.put(pair.getKey(), pair.getValue().toString());
            }
            CosmosItemRequestOptions requestOptions = new CosmosItemRequestOptions();
            requestOptions.setIfMatchETag(readEtag);
            PartitionKey pk = new PartitionKey(key);
            container.replaceItem(node, key, pk, requestOptions);
            return Status.OK;
        } catch (CosmosException e) {
            if (!AzureCosmosClient.includeExceptionStackInLog) {
                e = null;
            }
            LOGGER.error("Failed to update key {} to collection {} in database {} on attempt {}", key, table, AzureCosmosClient.databaseName, attempt, e);
        }
    }
    return Status.ERROR;
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ByteIterator(site.ycsb.ByteIterator) StringByteIterator(site.ycsb.StringByteIterator) CosmosItemRequestOptions(com.azure.cosmos.models.CosmosItemRequestOptions) CosmosContainer(com.azure.cosmos.CosmosContainer) PartitionKey(com.azure.cosmos.models.PartitionKey) CosmosException(com.azure.cosmos.CosmosException)

Example 3 with CosmosItemRequestOptions

use of com.azure.cosmos.models.CosmosItemRequestOptions in project YCSB by brianfrankcooper.

the class AzureCosmosClient method insert.

/**
 * Insert a record in the database. Any field/value pairs in the specified
 * values HashMap will be written into the record with the specified record key.
 *
 * @param table  The name of the table
 * @param key    The record key of the record to insert.
 * @param values A HashMap of field/value pairs to insert in the record
 * @return Zero on success, a non-zero error code on error
 */
@Override
public Status insert(String table, String key, Map<String, ByteIterator> values) {
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("Insert key: {} into table: {}", key, table);
    }
    try {
        CosmosContainer container = AzureCosmosClient.containerCache.get(table);
        if (container == null) {
            container = AzureCosmosClient.database.getContainer(table);
            AzureCosmosClient.containerCache.put(table, container);
        }
        PartitionKey pk = new PartitionKey(key);
        ObjectNode node = OBJECT_MAPPER.createObjectNode();
        node.put("id", key);
        for (Map.Entry<String, ByteIterator> pair : values.entrySet()) {
            node.put(pair.getKey(), pair.getValue().toString());
        }
        if (AzureCosmosClient.useUpsert) {
            container.upsertItem(node, pk, new CosmosItemRequestOptions());
        } else {
            container.createItem(node, pk, new CosmosItemRequestOptions());
        }
        return Status.OK;
    } catch (CosmosException e) {
        if (!AzureCosmosClient.includeExceptionStackInLog) {
            e = null;
        }
        LOGGER.error("Failed to insert key {} to collection {} in database {}", key, table, AzureCosmosClient.databaseName, e);
    }
    return Status.ERROR;
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ByteIterator(site.ycsb.ByteIterator) StringByteIterator(site.ycsb.StringByteIterator) CosmosItemRequestOptions(com.azure.cosmos.models.CosmosItemRequestOptions) CosmosContainer(com.azure.cosmos.CosmosContainer) PartitionKey(com.azure.cosmos.models.PartitionKey) CosmosException(com.azure.cosmos.CosmosException) HashMap(java.util.HashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 4 with CosmosItemRequestOptions

use of com.azure.cosmos.models.CosmosItemRequestOptions in project cas by apereo.

the class CosmosDbServiceRegistry method deleteAll.

@Override
public void deleteAll() {
    LOGGER.debug("Deleting registered services from container [{}]", container.getId());
    val queryOptions = new CosmosQueryRequestOptions();
    val items = container.queryItems("SELECT * FROM " + container.getId(), queryOptions, CosmosDbDocument.class);
    items.iterableByPage().forEach(response -> response.getResults().forEach(doc -> container.deleteItem(doc, new CosmosItemRequestOptions())));
}
Also used : lombok.val(lombok.val) CosmosQueryRequestOptions(com.azure.cosmos.models.CosmosQueryRequestOptions) CosmosDbDocument(org.apereo.cas.cosmosdb.CosmosDbDocument) MinimalPrettyPrinter(com.fasterxml.jackson.core.util.MinimalPrettyPrinter) Collection(java.util.Collection) PartitionKey(com.azure.cosmos.models.PartitionKey) lombok.val(lombok.val) CosmosQueryRequestOptions(com.azure.cosmos.models.CosmosQueryRequestOptions) RegisteredServiceJsonSerializer(org.apereo.cas.services.util.RegisteredServiceJsonSerializer) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) HttpStatus(org.springframework.http.HttpStatus) LoggingUtils(org.apereo.cas.util.LoggingUtils) Slf4j(lombok.extern.slf4j.Slf4j) StringSerializer(org.apereo.cas.util.serialization.StringSerializer) CosmosContainer(com.azure.cosmos.CosmosContainer) ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) CosmosException(com.azure.cosmos.CosmosException) CosmosItemRequestOptions(com.azure.cosmos.models.CosmosItemRequestOptions) CosmosItemRequestOptions(com.azure.cosmos.models.CosmosItemRequestOptions)

Example 5 with CosmosItemRequestOptions

use of com.azure.cosmos.models.CosmosItemRequestOptions in project cas by apereo.

the class CosmosDbServiceRegistry method delete.

@Override
public boolean delete(final RegisteredService registeredService) {
    val doc = createCosmosDbDocument(registeredService);
    LOGGER.debug("Deleting registered service [{}] from container [{}]", doc.getId(), container.getId());
    val response = container.deleteItem(doc, new CosmosItemRequestOptions());
    return !HttpStatus.valueOf(response.getStatusCode()).isError();
}
Also used : lombok.val(lombok.val) CosmosItemRequestOptions(com.azure.cosmos.models.CosmosItemRequestOptions)

Aggregations

CosmosItemRequestOptions (com.azure.cosmos.models.CosmosItemRequestOptions)5 CosmosContainer (com.azure.cosmos.CosmosContainer)4 CosmosException (com.azure.cosmos.CosmosException)4 PartitionKey (com.azure.cosmos.models.PartitionKey)4 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)2 lombok.val (lombok.val)2 ByteIterator (site.ycsb.ByteIterator)2 StringByteIterator (site.ycsb.StringByteIterator)2 CosmosQueryRequestOptions (com.azure.cosmos.models.CosmosQueryRequestOptions)1 MinimalPrettyPrinter (com.fasterxml.jackson.core.util.MinimalPrettyPrinter)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Collectors (java.util.stream.Collectors)1 Slf4j (lombok.extern.slf4j.Slf4j)1 CosmosDbDocument (org.apereo.cas.cosmosdb.CosmosDbDocument)1 RegisteredServiceJsonSerializer (org.apereo.cas.services.util.RegisteredServiceJsonSerializer)1 LoggingUtils (org.apereo.cas.util.LoggingUtils)1