Optimizing the driver to remove unneeded work and (#1298)

fixing a bug in update.
This commit is contained in:
Anthony Voellm aka Tony the P3rfguy 2019-04-19 17:49:23 -07:00 коммит произвёл stfeng2
Родитель a90a5a7ebc
Коммит 0397cb6c65
1 изменённых файлов: 36 добавлений и 41 удалений

Просмотреть файл

@ -59,9 +59,6 @@ public class AzureCosmosClient extends DB {
private static final int DEFAULT_MAX_DEGREE_OF_PARALLELISM_FOR_QUERY = 0;
private static final boolean DEFAULT_INCLUDE_EXCEPTION_STACK_IN_LOG = false;
private static final int NS_IN_US = 1000;
private static final String NA_STRING = "N/A";
private static final Logger LOGGER = LoggerFactory.getLogger(AzureCosmosClient.class);
/**
@ -236,8 +233,7 @@ public class AzureCosmosClient extends DB {
String documentLink = getDocumentLink(this.databaseName, table, key);
ResourceResponse<Document> readResource = null;
Document document;
long startTime = System.nanoTime();
Document document = null;
try {
readResource = AzureCosmosClient.client.readDocument(documentLink, getRequestOptions(key));
@ -248,23 +244,18 @@ public class AzureCosmosClient extends DB {
}
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 scan(String table, String startkey, int recordcount, Set<String> fields,
Vector<HashMap<String, ByteIterator>> result) {
long startTime = System.nanoTime();
List<Document> documents;
FeedResponse<Document> feedResponse = null;
try {
@ -280,11 +271,6 @@ public class AzureCosmosClient extends DB {
}
LOGGER.error("Failed to scan with startKey={}, recordCount={}", startkey, recordcount, e);
return Status.ERROR;
} finally {
long elapsed = (System.nanoTime() - startTime) / NS_IN_US;
LOGGER.debug("Queried {} records for key {} in {}us - ActivityID: {}",
recordcount, startkey, elapsed,
feedResponse != null ? feedResponse.getActivityId() : NA_STRING);
}
if (documents != null) {
@ -298,20 +284,41 @@ public class AzureCosmosClient extends DB {
@Override
public Status update(String table, String key, Map<String, ByteIterator> values) {
String documentLink = getDocumentLink(this.databaseName, table, key);
Document document = getDocumentDefinition(key, values);
// Azure Cosmos does not have patch support. Until then we need to read
// the document, update in place, and then write back.
// This could actually be made more efficient by using a stored procedure
// and doing the read/modify write on the server side. Perhaps
// that will be a future improvement.
RequestOptions reqOptions = getRequestOptions(key);
if (reqOptions == null) {
reqOptions = new RequestOptions();
String documentLink = getDocumentLink(this.databaseName, table, key);
ResourceResponse<Document> updatedResource = null;
ResourceResponse<Document> readResouce = null;
RequestOptions reqOptions = null;
Document document = null;
try {
reqOptions = getRequestOptions(key);
readResouce = AzureCosmosClient.client.readDocument(documentLink, reqOptions);
document = readResouce.getResource();
} catch (DocumentClientException e) {
if (!this.includeExceptionStackInLog) {
e = null;
}
LOGGER.error("Failed to read key {} in collection {} in database {} during update operation",
key, table, this.databaseName, e);
return Status.ERROR;
}
// Update values
for (Entry<String, ByteIterator> entry : values.entrySet()) {
document.set(entry.getKey(), entry.getValue().toString());
}
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) {
@ -320,10 +327,6 @@ public class AzureCosmosClient extends DB {
}
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;
@ -333,17 +336,18 @@ public class AzureCosmosClient extends DB {
public Status insert(String table, String key, Map<String, ByteIterator> values) {
Document documentDefinition = getDocumentDefinition(key, values);
ResourceResponse<Document> resourceResponse = null;
long startTime = System.nanoTime();
RequestOptions requestOptions = getRequestOptions(key);
try {
if (this.useUpsert) {
resourceResponse = AzureCosmosClient.client.upsertDocument(getDocumentCollectionLink(this.databaseName, table),
documentDefinition,
getRequestOptions(key),
requestOptions,
true);
} else {
resourceResponse = AzureCosmosClient.client.createDocument(getDocumentCollectionLink(this.databaseName, table),
documentDefinition,
getRequestOptions(key),
requestOptions,
true);
}
@ -353,10 +357,6 @@ public class AzureCosmosClient extends DB {
}
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;
@ -365,7 +365,7 @@ public class AzureCosmosClient extends DB {
@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));
@ -375,10 +375,6 @@ public class AzureCosmosClient extends DB {
}
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;
@ -391,7 +387,6 @@ public class AzureCosmosClient extends DB {
HashMap<String, ByteIterator> rItems = new HashMap<>(item.getHashMap().size());
for (Entry<String, Object> attr : item.getHashMap().entrySet()) {
LOGGER.trace("Result- key: {}, value: {}", attr.getKey(), attr.getValue().toString());
rItems.put(attr.getKey(), new StringByteIterator(attr.getValue().toString()));
}
return rItems;