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