private RequestOptions getRequestOptions(String key) { RequestOptions requestOptions = new RequestOptions(); requestOptions.setPartitionKey(new PartitionKey(key)); return requestOptions; }
@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; }
AzureCosmosClient.client.readDatabase(getDatabaseLink(this.databaseName), new RequestOptions()); } catch (DocumentClientException e) { throw new DBException("Invalid database name (" + this.databaseName + ") or failed to read database.", e);
private RequestOptions getRequestOptions(PartitionKey key, Integer requestUnit) { if (key == null && requestUnit == null) { return null; } final RequestOptions requestOptions = new RequestOptions(); if (key != null) { requestOptions.setPartitionKey(key); } if (requestUnit != null) { requestOptions.setOfferThroughput(requestUnit); } return requestOptions; }
private RequestOptions getRequestOptions(PartitionKey key, Integer requestUnit) { if (key == null && requestUnit == null) { return null; } final RequestOptions requestOptions = new RequestOptions(); if (key != null) { requestOptions.setPartitionKey(key); } if (requestUnit != null) { requestOptions.setOfferThroughput(requestUnit); } return requestOptions; }
private void deleteDocument(@NonNull Document document, @NonNull List<String> partitionKeyNames) { try { final RequestOptions options = new RequestOptions(); Assert.isTrue(partitionKeyNames.size() <= 1, "Only one Partition is supported."); if (!partitionKeyNames.isEmpty() && StringUtils.hasText(partitionKeyNames.get(0))) { options.setPartitionKey(new PartitionKey(document.get(partitionKeyNames.get(0)))); } getDocumentClient().deleteDocument(document.getSelfLink(), options); } catch (DocumentClientException e) { throw new DocumentDBAccessException("Failed to delete document: " + document.getSelfLink(), e); } }
@Override public <T> List<T> delete(Query query, Class<T> entityClass, String collectionName) { final SqlQuerySpec sqlQuerySpec = createSqlQuerySpec(query, entityClass); final Optional<Object> partitionKeyValue = getPartitionKeyValue(query, entityClass); final DocumentCollection collection = getDocCollection(collectionName); final FeedOptions feedOptions = new FeedOptions(); if (!partitionKeyValue.isPresent()) { feedOptions.setEnableCrossPartitionQuery(true); } final List<Document> results = documentDbFactory.getDocumentClient() .queryDocuments(collection.getSelfLink(), sqlQuerySpec, feedOptions).getQueryIterable().toList(); final RequestOptions options = new RequestOptions(); if (partitionKeyValue.isPresent()) { options.setPartitionKey(new PartitionKey(partitionKeyValue.get())); } final List<T> deletedResult = new ArrayList<>(); for (final Document document : results) { try { documentDbFactory.getDocumentClient().deleteDocument((document).getSelfLink(), options); deletedResult.add(getConverter().read(entityClass, document)); } catch (DocumentClientException e) { throw new DocumentDBAccessException( String.format("Failed to delete document [%s]", (document).getSelfLink()), e); } } return deletedResult; }
public <T> T findById(String collectionName, Object id, Class<T> domainClass) { Assert.hasText(collectionName, "collectionName should not be null, empty or only whitespaces"); Assert.notNull(domainClass, "entityClass should not be null"); assertValidId(id); try { final RequestOptions options = new RequestOptions(); if (isIdFieldAsPartitionKey(domainClass)) { options.setPartitionKey(new PartitionKey(id)); } final String documentLink = getDocumentLink(this.databaseName, collectionName, id); final Resource document = getDocumentClient().readDocument(documentLink, options).getResource(); if (document instanceof Document) { return mappingDocumentDbConverter.read(domainClass, (Document) document); } else { return null; } } catch (DocumentClientException e) { if (e.getStatusCode() == HttpConstants.StatusCodes.NOTFOUND) { return null; } throw new DocumentDBAccessException("findById exception", e); } }
public <T> T findById(String collectionName, Object id, Class<T> entityClass) { try { final Resource resource = documentDbFactory.getDocumentClient() .readDocument(getDocumentLink(this.databaseName, collectionName, id), new RequestOptions()).getResource(); if (resource instanceof Document) { final Document document = (Document) resource; return mappingDocumentDbConverter.read(entityClass, document); } else { return null; } } catch (DocumentClientException e) { if (e.getStatusCode() == HttpConstants.StatusCodes.NOTFOUND) { return null; } throw new DocumentDBAccessException("findById exception", e); } }
private DocumentCollection createCollectionIfNotExists(String dbName, String collectionName) { if (this.databaseCache == null) { this.databaseCache = createDatabaseIfNotExists(dbName); } final List<DocumentCollection> collectionList = documentDbFactory.getDocumentClient() .queryCollections(getDatabaseLink(dbName), new SqlQuerySpec("SELECT * FROM root r WHERE r.id=@id", new SqlParameterCollection(new SqlParameter("@id", collectionName))), null) .getQueryIterable().toList(); if (!collectionList.isEmpty()) { return collectionList.get(0); } else { final RequestOptions requestOptions = new RequestOptions(); requestOptions.setOfferThroughput(1000); return createCollection(dbName, collectionName, requestOptions); } }