@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 ResourceResponse<Document> invoke() throws Exception { return client.upsertDocument(collectionLink, document, options, disableAutomaticIdGeneration); } });
public <T> void upsert(String collectionName, T object, PartitionKey partitionKey) { Assert.hasText(collectionName, "collectionName should not be null, empty or only whitespaces"); Assert.notNull(object, "Upsert object should not be null"); try { Document originalDoc; if (object instanceof Document) { originalDoc = (Document) object; } else { originalDoc = mappingDocumentDbConverter.writeDoc(object); } log.debug("execute upsert document in database {} collection {}", this.databaseName, collectionName); final String collectionLink = getCollectionSelfLink(collectionName); final RequestOptions options = getRequestOptions(partitionKey, null); getDocumentClient().upsertDocument(collectionLink, originalDoc, options, false); } catch (DocumentClientException ex) { throw new DocumentDBAccessException("Failed to upsert document to database.", ex); } }
public <T> void upsert(String collectionName, T object, Object id, PartitionKey partitionKey) { try { Document originalDoc = new Document(); if (object instanceof Document) { originalDoc = (Document) object; } else { mappingDocumentDbConverter.write(object, originalDoc); } if (LOGGER.isDebugEnabled()) { LOGGER.debug("execute upsert document in database {} collection {} with id {}", this.databaseName, collectionName, id); } documentDbFactory.getDocumentClient().upsertDocument( getCollectionLink(this.databaseName, collectionName), originalDoc, getRequestOptions(partitionKey, null), false); } catch (DocumentClientException ex) { throw new DocumentDBAccessException("Failed to upsert document to database.", ex); } }