private FeedOptions getFeedOptions(String key) { if (useSinglePartitionCollection) { return null; } FeedOptions feedOptions = new FeedOptions(); if (this.useHashQueryForScan) { feedOptions.setEnableCrossPartitionQuery(false); feedOptions.setPartitionKey(new PartitionKey(key)); } else { feedOptions.setEnableCrossPartitionQuery(true); feedOptions.setMaxDegreeOfParallelism(this.maxDegreeOfParallelismForQuery); } return feedOptions; }
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; }
private FeedResponse<Document> executeQuery(@NonNull SqlQuerySpec sqlQuerySpec, boolean isCrossPartition, String collectionName) { final FeedOptions feedOptions = new FeedOptions(); final String selfLink = getCollectionSelfLink(collectionName); feedOptions.setEnableCrossPartitionQuery(isCrossPartition); return getDocumentClient().queryDocuments(selfLink, sqlQuerySpec, feedOptions); }
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; }
@Override public <T> Page<T> paginationQuery(DocumentQuery query, Class<T> domainClass, String collectionName) { Assert.isTrue(query.getPageable().getPageSize() > 0, "pageable should have page size larger than 0"); Assert.hasText(collectionName, "collection should not be null, empty or only whitespaces"); final Pageable pageable = query.getPageable(); final FeedOptions feedOptions = new FeedOptions(); if (pageable instanceof DocumentDbPageRequest) { feedOptions.setRequestContinuation(((DocumentDbPageRequest) pageable).getRequestContinuation()); } feedOptions.setPageSize(pageable.getPageSize()); feedOptions.setEnableCrossPartitionQuery(query.isCrossPartitionQuery(getPartitionKeyNames(domainClass))); final SqlQuerySpec sqlQuerySpec = new FindQuerySpecGenerator().generate(query); final FeedResponse<Document> response = executeQuery(sqlQuerySpec, feedOptions, collectionName); final Iterator<Document> it = response.getQueryIterator(); final List<T> result = new ArrayList<>(); for (int index = 0; it.hasNext() && index < pageable.getPageSize(); index++) { // Limit iterator as inner iterator will automatically fetch the next page final Document doc = it.next(); if (doc == null) { continue; } final T entity = mappingDocumentDbConverter.read(domainClass, doc); result.add(entity); } final DocumentDbPageRequest pageRequest = DocumentDbPageRequest.of(pageable.getPageNumber(), pageable.getPageSize(), response.getResponseContinuation()); return new PageImpl<>(result, pageRequest, count(query, domainClass, collectionName)); }
options.setEnableCrossPartitionQuery(true); options.setMaxDegreeOfParallelism(Integer.MAX_VALUE);