private Document getDocumentById(String collectionId, String id) { if (collection == null) { return null; } // Retrieve the document using the DocumentClient. List<Document> documentList = documentClient .queryDocuments(collection.getSelfLink(), "SELECT * FROM root r WHERE r.id='" + id + "'", feedOptions) .getQueryIterable() .toList(); if (documentList.size() > 0) { return documentList.get(0); } return null; } }
@Override public Status insert(String table, String key, Map<String, ByteIterator> values) { Document record = new Document(); record.set("id", key); for (Entry<String, ByteIterator> val : values.entrySet()) { record.set(val.getKey(), val.getValue().toString()); } try { documentClient.createDocument(collection.getSelfLink(), record, null, false); } catch (DocumentClientException e) { e.printStackTrace(System.err); return Status.ERROR; } return Status.OK; }
/** * Gets the self-link for conflicts in a collection. * * @return the conflicts link. */ public String getConflictsLink() { return StringUtils.removeEnd(this.getSelfLink(), "/") + "/" + super.getString(Constants.Properties.CONFLICTS_LINK); }
/** * Gets the self-link for triggers in a collection. * * @return the trigger link. */ public String getTriggersLink() { return StringUtils.removeEnd(this.getSelfLink(), "/") + "/" + super.getString(Constants.Properties.TRIGGERS_LINK); }
/** * Gets the self-link for user defined functions in a collection. * * @return the user defined functions link. */ public String getUserDefinedFunctionsLink() { return StringUtils.removeEnd(this.getSelfLink(), "/") + "/" + super.getString(Constants.Properties.USER_DEFINED_FUNCTIONS_LINK); }
/** * Reads all partition key ranges in a document collection. * <p> * Example: * <pre> * FeedResponse<PartitionKeyRange> partitionKeyRanges = this.client.readPartitionKeyRanges(coll, null); * List<String> ids = new ArrayList<String>(); * for (PartitionKeyRange range : partitionKeyRanges.getQueryIterable()) { * ids.add(range.getId()); * } * </pre> * @param documentCollection the document collection. * @param options the feed options. * @return the feed response with the read partition key ranges. * @see PartitionKeyRange */ public FeedResponse<PartitionKeyRange> readPartitionKeyRanges(DocumentCollection documentCollection, FeedOptions options) { if (documentCollection == null) { throw new IllegalArgumentException("collection"); } return this.readPartitionKeyRanges(documentCollection.getSelfLink(), options); }
@Override public Observable<ResourceResponse<DocumentCollection>> replaceCollection(DocumentCollection collection, RequestOptions options) { return Observable.defer(() -> { try { if (collection == null) { throw new IllegalArgumentException("collection"); } logger.debug("Replacing a Collection. id: [{}]", collection.getId()); validateResource(collection); String path = Utils.joinPath(collection.getSelfLink(), null); Map<String, String> requestHeaders = this.getRequestHeaders(options); RxDocumentServiceRequest request = RxDocumentServiceRequest.create(OperationType.Replace, ResourceType.DocumentCollection, path, collection, requestHeaders); return this.doReplace(request).map(response -> toResourceResponse(response, DocumentCollection.class)); } catch (Exception e) { logger.debug("Failure in replacing a collection. due to [{}]", e.getMessage(), e); return Observable.error(e); } }); }
public <T> List<T> find(Query query, Class<T> domainClass, String collectionName) { final SqlQuerySpec sqlQuerySpec = createSqlQuerySpec(query, domainClass); // TODO (wepa) Collection link should be created locally without accessing database, // but currently exception will be thrown if not fetching collection url from database. // Run repository integration test to reproduce. final DocumentCollection collection = getDocCollection(collectionName); final FeedOptions feedOptions = new FeedOptions(); final Optional<Object> partitionKeyValue = getPartitionKeyValue(query, domainClass); if (!partitionKeyValue.isPresent()) { feedOptions.setEnableCrossPartitionQuery(true); } final List<Document> results = documentDbFactory.getDocumentClient() .queryDocuments(collection.getSelfLink(), sqlQuerySpec, feedOptions) .getQueryIterable().toList(); final List<T> entities = new ArrayList<>(); for (int i = 0; i < results.size(); i++) { final T entity = mappingDocumentDbConverter.read(domainClass, results.get(i)); entities.add(entity); } return entities; }
public <T> List<T> findAll(String collectionName, final Class<T> entityClass) { final List<DocumentCollection> collections = documentDbFactory.getDocumentClient(). queryCollections( getDatabaseLink(this.databaseName), new SqlQuerySpec("SELECT * FROM ROOT r WHERE r.id=@id", new SqlParameterCollection(new SqlParameter("@id", collectionName))), null) .getQueryIterable().toList(); if (collections.size() != 1) { throw new RuntimeException("expect only one collection: " + collectionName + " in database: " + this.databaseName + ", but found " + collections.size()); } final List<Document> results = documentDbFactory.getDocumentClient() .queryDocuments(collections.get(0).getSelfLink(), "SELECT * FROM c", null) .getQueryIterable().toList(); final List<T> entities = new ArrayList<>(); for (int i = 0; i < results.size(); i++) { final T entity = mappingDocumentDbConverter.read(entityClass, results.get(i)); entities.add(entity); } return entities; }
/** * Replaces a document collection. * * @param collection the document collection to use. * @param options the request options. * @return the resource response with the replaced document collection. * @throws DocumentClientException the document client exception. */ public ResourceResponse<DocumentCollection> replaceCollection(DocumentCollection collection, RequestOptions options) throws DocumentClientException { if (collection == null) { throw new IllegalArgumentException("collection"); } logger.debug("Replacing a Collection. id: [{}]", collection.getId()); DocumentClient.validateResource(collection); String path = Utils.joinPath(collection.getSelfLink(), null); Map<String, String> requestHeaders = getRequestHeaders(options); DocumentServiceRequest request = DocumentServiceRequest.create(OperationType.Replace, ResourceType.DocumentCollection, path, collection, requestHeaders); return new ResourceResponse<DocumentCollection>(this.doReplace(request), DocumentCollection.class); }
public <T> List<T> findAll(String collectionName, final Class<T> entityClass) { final List<DocumentCollection> collections = documentDbFactory.getDocumentClient(). queryCollections( getDatabaseLink(this.databaseName), new SqlQuerySpec("SELECT * FROM ROOT r WHERE r.id=@id", new SqlParameterCollection(new SqlParameter("@id", collectionName))), null) .getQueryIterable().toList(); if (collections.size() != 1) { throw new IllegalCollectionException("expect only one collection: " + collectionName + " in database: " + this.databaseName + ", but found " + collections.size()); } final FeedOptions feedOptions = new FeedOptions(); feedOptions.setEnableCrossPartitionQuery(true); final SqlQuerySpec sqlQuerySpec = new SqlQuerySpec("SELECT * FROM root c"); final List<Document> results = documentDbFactory.getDocumentClient() .queryDocuments(collections.get(0).getSelfLink(), sqlQuerySpec, feedOptions) .getQueryIterable().toList(); final List<T> entities = new ArrayList<>(); for (int i = 0; i < results.size(); i++) { final T entity = mappingDocumentDbConverter.read(entityClass, results.get(i)); entities.add(entity); } return entities; }
@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; }