private Database getDatabase() { if (database == null) { // Get the database if it exists List<Database> databaseList = documentClient .queryDatabases( "SELECT * FROM root r WHERE r.id='" + databaseId + "'", null) .getQueryIterable() .toList(); if (databaseList.size() > 0) { // Cache the database object so we won't have to query for it // later to retrieve the selfLink. database = databaseList.get(0); } else { // Create the database if it doesn't exist. try { Database databaseDefinition = new Database(); databaseDefinition.setId(databaseId); database = documentClient.createDatabase(databaseDefinition, null) .getResource(); } catch (DocumentClientException e) { // TODO: Something has gone terribly wrong - the app wasn't // able to query or create the collection. // Verify your connection, endpoint, and key. e.printStackTrace(System.err); } } } return database; }
@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 ResourceResponse<Document> apply() throws DocumentClientException { DocumentServiceRequest request = getCreateDocumentRequest(documentCollectionLink, documentLocal, optionsLocal, disableAutomaticIdGenerationLocal, OperationType.Create); return new ResourceResponse<Document>(doCreate(request), Document.class); } };
long getMaxQuotaHeader(String headerName) { if (this.quotaHeaders.size() == 0 && !StringUtils.isEmpty(this.getMaxResourceQuota()) && !this.getCurrentResourceQuotaUsage().isEmpty()) { this.populateQuotaHeader(this.getMaxResourceQuota(), this.getCurrentResourceQuotaUsage()); } if (this.quotaHeaders.containsKey(headerName)) { return this.quotaHeaders.get(headerName); } return 0; }
/** * Current Usage. * * @return the current permission usage. */ public long getPermissionUsage() { return this.getCurrentQuotaHeader(Constants.Quota.PERMISSION); }
/** * Max document count quota. * * @return the document count quota. */ public long getDocumentCountQuota() { return this.getMaxQuotaHeader(Constants.Quota.DOCUMENTS_COUNT); }
@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 ResourceResponse<Document> apply() throws DocumentClientException { DocumentServiceRequest request = getCreateDocumentRequest(documentCollectionLink, documentLocal, optionsLocal, disableAutomaticIdGenerationLocal, OperationType.Upsert); return new ResourceResponse<Document>(doUpsert(request), Document.class); } };
long getCurrentQuotaHeader(String headerName) { if (this.usageHeaders.size() == 0 && !this.getMaxResourceQuota().isEmpty() && !this.getCurrentResourceQuotaUsage().isEmpty()) { this.populateQuotaHeader(this.getMaxResourceQuota(), this.getCurrentResourceQuotaUsage()); } if (this.usageHeaders.containsKey(headerName)) { return this.usageHeaders.get(headerName); } return 0; }
/** * Current Usage. * * @return the current collection usage. */ public long getCollectionUsage() { return this.getCurrentQuotaHeader(Constants.Quota.COLLECTION); }
/** * Max Quota. * * @return the database quota. */ public long getDatabaseQuota() { return this.getMaxQuotaHeader(Constants.Quota.DATABASE); }
@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; }
.createCollection(getDatabase().getSelfLink(), collectionDefinition, null) .getResource(); } catch (DocumentClientException e) {
@Override public ResourceResponse<Document> apply() throws DocumentClientException { return new ResourceResponse<Document>(doReplace(request), Document.class); } };
/** * Current Usage. * * @return the document usage. */ public long getDocumentUsage() { return this.getCurrentQuotaHeader(Constants.Quota.DOCUMENTS_SIZE); }
/** * Max Quota. * * @return the permission quota. */ public long getPermissionQuota() { return this.getMaxQuotaHeader(Constants.Quota.PERMISSION); }
@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; }
final Resource resource = documentDbFactory.getDocumentClient() .readDocument(getDocumentLink(this.databaseName, collectionName, id), null) .getResource();
public static <T extends Resource> ResourceResponse<T> toResourceResponse(DocumentServiceResponse response, Class<T> cls) { return new ResourceResponse<T>(response, cls); }
/** * Current Usage. * * @return the current database usage. */ public long getDatabaseUsage() { return this.getCurrentQuotaHeader(Constants.Quota.DATABASE); }