@Override public Status delete(String table, String key) { ResourceResponse<Document> deletedResource = null; long startTime = System.nanoTime(); try { deletedResource = AzureCosmosClient.client.deleteDocument(getDocumentLink(this.databaseName, table, key), getRequestOptions(key)); } catch (DocumentClientException e) { if (!this.includeExceptionStackInLog) { e = null; } LOGGER.error("Failed to delete key {} in collection {} in database {}", key, table, this.databaseName, e); return Status.ERROR; } finally { long elapsed = (System.nanoTime() - startTime) / NS_IN_US; LOGGER.debug("Deleted key {} in {}us - ActivityID: {}", key, elapsed, deletedResource != null ? deletedResource.getActivityId() : NA_STRING); } return Status.OK; }
@Override public Status read(String table, String key, Set<String> fields, Map<String, ByteIterator> result) { String documentLink = getDocumentLink(this.databaseName, table, key); ResourceResponse<Document> readResource = null; Document document; long startTime = System.nanoTime(); try { readResource = AzureCosmosClient.client.readDocument(documentLink, getRequestOptions(key)); document = readResource.getResource(); } catch (DocumentClientException e) { if (!this.includeExceptionStackInLog) { e = null; } LOGGER.error("Failed to read key {} in collection {} in database {}", key, table, this.databaseName, e); return Status.ERROR; } finally { long elapsed = (System.nanoTime() - startTime) / NS_IN_US; LOGGER.debug("Read key {} in {}us - ActivityID: {}", key, elapsed, readResource != null ? readResource.getActivityId() : NA_STRING); } if (null != document) { result.putAll(extractResult(document)); LOGGER.trace("Read result: {}", document); } return Status.OK; }
@Override public Status insert(String table, String key, Map<String, ByteIterator> values) { Document documentDefinition = getDocumentDefinition(key, values); ResourceResponse<Document> resourceResponse = null; long startTime = System.nanoTime(); try { if (this.useUpsert) { resourceResponse = AzureCosmosClient.client.upsertDocument(getDocumentCollectionLink(this.databaseName, table), documentDefinition, getRequestOptions(key), true); } else { resourceResponse = AzureCosmosClient.client.createDocument(getDocumentCollectionLink(this.databaseName, table), documentDefinition, getRequestOptions(key), true); } } catch (DocumentClientException e) { if (!this.includeExceptionStackInLog) { e = null; } LOGGER.error("Failed to insert key {} to collection {} in database {}", key, table, this.databaseName, e); return Status.ERROR; } finally { long elapsed = (System.nanoTime() - startTime) / NS_IN_US; LOGGER.debug("Inserted key {} in {}us - ActivityID: {}", key, elapsed, resourceResponse != null ? resourceResponse.getActivityId() : NA_STRING); } return Status.OK; }
@Override public Status update(String table, String key, Map<String, ByteIterator> values) { String documentLink = getDocumentLink(this.databaseName, table, key); Document document = getDocumentDefinition(key, values); RequestOptions reqOptions = getRequestOptions(key); if (reqOptions == null) { reqOptions = new RequestOptions(); } AccessCondition accessCondition = new AccessCondition(); accessCondition.setCondition(document.getETag()); accessCondition.setType(AccessConditionType.IfMatch); reqOptions.setAccessCondition(accessCondition); ResourceResponse<Document> updatedResource = null; long startTime = System.nanoTime(); try { updatedResource = AzureCosmosClient.client.replaceDocument(documentLink, document, reqOptions); } catch (DocumentClientException e) { if (!this.includeExceptionStackInLog) { e = null; } LOGGER.error("Failed to update key {}", key, e); return Status.ERROR; } finally { long elapsed = (System.nanoTime() - startTime) / NS_IN_US; LOGGER.debug("Updated key {} in {}us - ActivityID: {}", key, elapsed, updatedResource != null ? updatedResource.getActivityId() : NA_STRING); } return Status.OK; }