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; } }
DocumentCollection collectionDefinition = new DocumentCollection(); collectionDefinition.setId(collectionId);
String partitionKeyFieldName, IndexingPolicy policy) { DocumentCollection collection = new DocumentCollection(); collection.setId(collectionName); collection.setPartitionKey(partitionKeyDefinition); collection.setIndexingPolicy(policy);
@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); } }); }
/** * Gets the collection's partition key definition. * * @return the partition key definition. */ public PartitionKeyDefinition getPartitionKey() { if (super.has(Constants.Properties.PARTITION_KEY)) { return super.getObject(Constants.Properties.PARTITION_KEY, PartitionKeyDefinition.class); } this.setPartitionKey(new PartitionKeyDefinition()); return this.getPartitionKey(); }
@Override public Observable<ResourceResponse<DocumentCollection>> createCollection(String databaseLink, DocumentCollection collection, RequestOptions options) { return Observable.defer(() -> { try { if (StringUtils.isEmpty(databaseLink)) { throw new IllegalArgumentException("databaseLink"); } if (collection == null) { throw new IllegalArgumentException("collection"); } logger.debug("Creating a Collection. databaseLink: [{}], Collection id: [{}]", databaseLink, collection.getId()); validateResource(collection); String path = Utils.joinPath(databaseLink, Paths.COLLECTIONS_PATH_SEGMENT); Map<String, String> requestHeaders = this.getRequestHeaders(options); RxDocumentServiceRequest request = RxDocumentServiceRequest.create(OperationType.Create, ResourceType.DocumentCollection, path, collection, requestHeaders); return this.doCreate(request).map(response -> toResourceResponse(response, DocumentCollection.class)); } catch (Exception e) { logger.debug("Failure in creating a collection. due to [{}]", e.getMessage(), e); return Observable.error(e); } }); }
private DocumentCollection createCollection(@NonNull String dbName, String partitionKeyFieldName, @NonNull DocumentDbEntityInformation information) { DocumentCollection collection = new DocumentCollection(); final String collectionName = information.getCollectionName(); final IndexingPolicy policy = information.getIndexingPolicy(); final RequestOptions requestOptions = getRequestOptions(null, information.getRequestUnit()); collection.setId(collectionName); collection.setIndexingPolicy(policy); collection.setDefaultTimeToLive(timeToLive); // If not Automatic, setDefaultTimeToLive is invalid collection.setPartitionKey(partitionKeyDefinition);
/** * 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); }
/** * Creates a document collection. * * @param databaseLink the database link. * @param collection the collection. * @param options the request options. * @return the resource response with the created collection. * @throws DocumentClientException the document client exception. */ public ResourceResponse<DocumentCollection> createCollection(String databaseLink, DocumentCollection collection, RequestOptions options) throws DocumentClientException { if (StringUtils.isEmpty(databaseLink)) { throw new IllegalArgumentException("databaseLink"); } if (collection == null) { throw new IllegalArgumentException("collection"); } logger.debug("Creating a Collection. databaseLink: [{}], Collection id: [{}]", databaseLink, collection.getId()); DocumentClient.validateResource(collection); String path = Utils.joinPath(databaseLink, Paths.COLLECTIONS_PATH_SEGMENT); Map<String, String> requestHeaders = getRequestHeaders(options); DocumentServiceRequest request = DocumentServiceRequest.create(OperationType.Create, ResourceType.DocumentCollection, path, collection, requestHeaders); return new ResourceResponse<DocumentCollection>(this.doCreate(request), DocumentCollection.class); }
@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; }
public DocumentCollection createCollection(String dbName, String collectionName, RequestOptions collectionOptions) { DocumentCollection collection = new DocumentCollection(); collection.setId(collectionName); if (LOGGER.isDebugEnabled()) { LOGGER.debug("execute createCollection in database {} collection {}", dbName, collectionName); } try { final Resource resource = documentDbFactory.getDocumentClient() .createCollection(getDatabaseLink(dbName), collection, collectionOptions) .getResource(); if (resource instanceof DocumentCollection) { collection = (DocumentCollection) resource; } return collection; } catch (DocumentClientException e) { throw new RuntimeException("createCollection exception", e); } }
/** * 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); }
/** * 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); }
/** * 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); }
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; }
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; }