in azurecosmos/src/main/java/site/ycsb/db/AzureCosmosClient.java [550:600]
public Status insert(String table, String key, Map<String, ByteIterator> values) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Insert key: {} into table: {}", key, table);
}
long st = System.nanoTime();
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());
}
CosmosItemResponse<ObjectNode> response;
if (AzureCosmosClient.useUpsert) {
response = container.upsertItem(node, pk, new CosmosItemRequestOptions());
} else {
response = container.createItem(node, pk, new CosmosItemRequestOptions());
}
if (diagnosticsLatencyThresholdInMS > 0 &&
response.getDiagnostics().getDuration().compareTo(Duration.ofMillis(diagnosticsLatencyThresholdInMS)) > 0) {
LOGGER.warn(CREATE_DIAGNOSTIC, response.getDiagnostics().toString());
}
if (writeSuccessLatencyTimer != null) {
long en = System.nanoTime();
long latency = (en - st) / 1000;
writeSuccessLatencyTimer.record(latency, TimeUnit.MICROSECONDS);
writeSuccessCounter.increment();
}
return Status.OK;
} catch (CosmosException e) {
int statusCode = e.getStatusCode();
if (!AzureCosmosClient.includeExceptionStackInLog) {
e = null;
}
LOGGER.error(CREATE_EXCEPTION, "Failed to insert key {} to collection {} in database {} statusCode {}", key,
table, AzureCosmosClient.databaseName, statusCode, e);
}
if (writeFailureCounter != null) {
writeFailureCounter.increment();
}
return Status.ERROR;
}