@SuppressWarnings("unchecked") @Override public Mono<Boolean> existsById(ID id) { Assert.notNull(id, "The given id must not be null!"); return mapMono(operations.exists(id.toString()).toSingle()); }
@SuppressWarnings("unchecked") @Override public Mono<T> findById(ID id) { Assert.notNull(id, "The given id must not be null!"); return mapMono(operations.findById(id.toString(), entityInformation.getJavaType()).toSingle()) .onErrorResume(throwable -> { //reactive streams adapter doesn't work with null if(throwable instanceof NullPointerException) { return Mono.empty(); } return Mono.just(throwable); }); }
protected Object execute(N1qlQuery query, Class<?> typeToRead) { logIfNecessary(query); return couchbaseOperations.findByN1QL(query, typeToRead); }
protected Object executeSingleProjection(N1qlQuery query, final Class<?> typeToRead) { logIfNecessary(query); return couchbaseOperations.findByN1QLProjection(query, Map.class) .map(m -> { if (m.size() > 1) { throw new CouchbaseQueryExecutionException("Query returning primitive got more values than expected: " + m.size()); } Object v = m.values().iterator().next(); return this.couchbaseOperations.getConverter().getConversionService().convert(v, typeToRead); }); }
@SuppressWarnings("unchecked") @Override public Mono<Long> count() { final ResolvedView resolvedView = determineView(); ViewQuery query = ViewQuery.from(resolvedView.getDesignDocument(), resolvedView.getViewName()); query.reduce(true); query.stale(operations.getDefaultConsistency().viewConsistency()); return mapMono(operations .queryView(query) .flatMap(AsyncViewResult::rows) .map(asyncViewRow -> Long.valueOf(asyncViewRow.value().toString())).toSingle()); }
public ReactiveStringN1qlBasedQuery(String statement, CouchbaseQueryMethod queryMethod, RxJavaCouchbaseOperations couchbaseOperations, SpelExpressionParser spelParser, QueryMethodEvaluationContextProvider evaluationContextProvider) { super(queryMethod, couchbaseOperations); this.queryParser = new StringBasedN1qlQueryParser(statement, queryMethod, getCouchbaseOperations().getCouchbaseBucket().name(), getCouchbaseOperations().getConverter(), getTypeField(), getTypeValue()); this.parser = spelParser; this.evaluationContextProvider = evaluationContextProvider; }
@SuppressWarnings("unchecked") @Override public Flux<T> findAll(Sort sort) { Assert.notNull(sort); //prepare elements of the query WherePath selectFrom = N1qlUtils.createSelectFromForEntity(getCouchbaseOperations().getCouchbaseBucket().name()); Expression whereCriteria = N1qlUtils.createWhereFilterForEntity(null, getCouchbaseOperations().getConverter(), getEntityInformation()); //apply the sort com.couchbase.client.java.query.dsl.Sort[] orderings = N1qlUtils.createSort(sort, getCouchbaseOperations().getConverter()); Statement st = selectFrom.where(whereCriteria).orderBy(orderings); //fire the query ScanConsistency consistency = getCouchbaseOperations().getDefaultConsistency().n1qlConsistency(); N1qlQuery query = N1qlQuery.simple(st, N1qlParams.build().consistency(consistency)); return mapFlux(getCouchbaseOperations().findByN1QL(query, getEntityInformation().getJavaType())); }
@SuppressWarnings("unchecked") @Override public Mono<Void> deleteAll() { final ResolvedView resolvedView = determineView(); ViewQuery query = ViewQuery.from(resolvedView.getDesignDocument(), resolvedView.getViewName()); query.reduce(false); query.stale(operations.getDefaultConsistency().viewConsistency()); return mapMono(operations.queryView(query) .map(AsyncViewResult::rows) .flatMap(row -> { AsyncViewRow asyncViewRow = (AsyncViewRow) row; return operations.remove(asyncViewRow.id()) .onErrorResumeNext(throwable -> { if (throwable instanceof DocumentDoesNotExistException) { return Observable.empty(); } return Observable.error(throwable); }); }) .toList() .map(list -> Observable.<Void>empty()) .toSingle()); }
@SuppressWarnings("unchecked") @Override public Flux<T> findAll() { final ResolvedView resolvedView = determineView(); ViewQuery query = ViewQuery.from(resolvedView.getDesignDocument(), resolvedView.getViewName()); query.reduce(false); query.stale(operations.getDefaultConsistency().viewConsistency()); return mapFlux(operations.findByView(query, entityInformation.getJavaType())); }
@Override public Object execute(Object[] runtimeParams) { String designDoc = method.getDimensionalAnnotation().designDocument(); String viewName = method.getDimensionalAnnotation().spatialViewName(); int dimensions = method.getDimensionalAnnotation().dimensions(); PartTree tree = new PartTree(method.getName(), method.getEntityInformation().getJavaType()); //prepare a spatial view query to be used as a base for the query creator SpatialViewQuery baseSpatialQuery = SpatialViewQuery.from(designDoc, viewName) .stale(operations.getDefaultConsistency().viewConsistency()); //use the SpatialViewQueryCreator to complete it SpatialViewQueryCreator creator = new SpatialViewQueryCreator(dimensions, tree, new ReactiveCouchbaseParameterAccessor(method, runtimeParams), baseSpatialQuery, operations.getConverter()); SpatialViewQueryCreator.SpatialViewQueryWrapper finalQuery = creator.createQuery(); //execute the spatial query return execute(finalQuery); }
/** * Get the {@link MappingContext} to use in repositories. It is extracted from the default {@link RxJavaCouchbaseOperations}. * * @return the mapping context. */ public MappingContext<? extends CouchbasePersistentEntity<?>, CouchbasePersistentProperty> getMappingContext() { return defaultOperations.getConverter().getMappingContext(); }
protected Object guessViewAndExecute() { String designDoc = designDocName(method); String methodName = method.getName(); boolean isExplicitReduce = method.hasViewAnnotation() && method.getViewAnnotation().reduce(); boolean isReduce = methodName.startsWith("count") || isExplicitReduce; String viewName = StringUtils.uncapitalize(methodName.replaceFirst("find|count", "")); ViewQuery simpleQuery = ViewQuery.from(designDoc, viewName) .stale(operations.getDefaultConsistency().viewConsistency()); if (isReduce) { simpleQuery.reduce(); return executeReduce(simpleQuery, designDoc, viewName); } else { return execute(simpleQuery); } }
/** * Returns the base class for the repository being constructed. Two categories of repositories can be produced by * this factory: {@link SimpleReactiveCouchbaseRepository} and {@link ReactiveN1qlCouchbaseRepository}. This method checks if N1QL * is available to choose between the two, but the actual concrete class is determined respectively by * {@link #getSimpleBaseClass(RepositoryMetadata)} and {@link #getN1qlBaseClass(RepositoryMetadata)}. * * Override these methods if you want to change the base class for all your repositories. * * @param repositoryMetadata metadata for the repository. * * @return the base class. */ @Override protected final Class<?> getRepositoryBaseClass(final RepositoryMetadata repositoryMetadata) { RxJavaCouchbaseOperations couchbaseOperations = couchbaseOperationsMapping.resolve(repositoryMetadata.getRepositoryInterface(), repositoryMetadata.getDomainType()); boolean isN1qlAvailable = couchbaseOperations.getCouchbaseClusterInfo().checkAvailable(CouchbaseFeature.N1QL); if (isN1qlAvailable) { return getN1qlBaseClass(repositoryMetadata); } return getSimpleBaseClass(repositoryMetadata); }
protected Object execute(SpatialViewQueryCreator.SpatialViewQueryWrapper query) { if (LOG.isDebugEnabled()) { LOG.debug("Executing spatial view query: " + query.getQuery().toString()); } //TODO: eliminate false positives in geo query return ReactiveWrapperConverters.toWrapper(operations.findBySpatialView(query.getQuery(), method.getEntityInformation().getJavaType()), Flux.class); }
protected Object execute(ViewQuery query) { if (LOG.isDebugEnabled()) { LOG.debug("Executing view query: " + query.toString()); } return ReactiveWrapperConverters.toWrapper(operations.findByView(query, method.getEntityInformation().getJavaType()), Flux.class); }
@SuppressWarnings("unchecked") @Override public Flux<T> findAll(Sort sort) { Assert.notNull(sort); //prepare elements of the query WherePath selectFrom = N1qlUtils.createSelectFromForEntity(getCouchbaseOperations().getCouchbaseBucket().name()); Expression whereCriteria = N1qlUtils.createWhereFilterForEntity(null, getCouchbaseOperations().getConverter(), getEntityInformation()); //apply the sort com.couchbase.client.java.query.dsl.Sort[] orderings = N1qlUtils.createSort(sort, getCouchbaseOperations().getConverter()); Statement st = selectFrom.where(whereCriteria).orderBy(orderings); //fire the query ScanConsistency consistency = getCouchbaseOperations().getDefaultConsistency().n1qlConsistency(); N1qlQuery query = N1qlQuery.simple(st, N1qlParams.build().consistency(consistency)); return mapFlux(getCouchbaseOperations().findByN1QL(query, getEntityInformation().getJavaType())); }
public ReactiveStringN1qlBasedQuery(String statement, CouchbaseQueryMethod queryMethod, RxJavaCouchbaseOperations couchbaseOperations, SpelExpressionParser spelParser, QueryMethodEvaluationContextProvider evaluationContextProvider) { super(queryMethod, couchbaseOperations); this.queryParser = new StringBasedN1qlQueryParser(statement, queryMethod, getCouchbaseOperations().getCouchbaseBucket().name(), getCouchbaseOperations().getConverter(), getTypeField(), getTypeValue()); this.parser = spelParser; this.evaluationContextProvider = evaluationContextProvider; }
@SuppressWarnings("unchecked") @Override public Mono<Void> deleteAll() { final ResolvedView resolvedView = determineView(); ViewQuery query = ViewQuery.from(resolvedView.getDesignDocument(), resolvedView.getViewName()); query.reduce(false); query.stale(operations.getDefaultConsistency().viewConsistency()); return mapMono(operations.queryView(query) .map(AsyncViewResult::rows) .flatMap(row -> { AsyncViewRow asyncViewRow = (AsyncViewRow) row; return operations.remove(asyncViewRow.id()) .onErrorResumeNext(throwable -> { if (throwable instanceof DocumentDoesNotExistException) { return Observable.empty(); } return Observable.error(throwable); }); }) .toList() .map(list -> Observable.<Void>empty()) .toSingle()); }
@SuppressWarnings("unchecked") @Override public Flux<T> findAll() { final ResolvedView resolvedView = determineView(); ViewQuery query = ViewQuery.from(resolvedView.getDesignDocument(), resolvedView.getViewName()); query.reduce(false); query.stale(operations.getDefaultConsistency().viewConsistency()); return mapFlux(operations.findByView(query, entityInformation.getJavaType())); }
@Override public Object execute(Object[] runtimeParams) { String designDoc = method.getDimensionalAnnotation().designDocument(); String viewName = method.getDimensionalAnnotation().spatialViewName(); int dimensions = method.getDimensionalAnnotation().dimensions(); PartTree tree = new PartTree(method.getName(), method.getEntityInformation().getJavaType()); //prepare a spatial view query to be used as a base for the query creator SpatialViewQuery baseSpatialQuery = SpatialViewQuery.from(designDoc, viewName) .stale(operations.getDefaultConsistency().viewConsistency()); //use the SpatialViewQueryCreator to complete it SpatialViewQueryCreator creator = new SpatialViewQueryCreator(dimensions, tree, new ReactiveCouchbaseParameterAccessor(method, runtimeParams), baseSpatialQuery, operations.getConverter()); SpatialViewQueryCreator.SpatialViewQueryWrapper finalQuery = creator.createQuery(); //execute the spatial query return execute(finalQuery); }