@PostConstruct private void postConstruct() { // Need to post-process travel data to add _class attribute List<Airline> airlinesWithoutClassAttribute = couchbaseOperations.findByN1QL(N1qlQuery.simple( // "SELECT META(`travel-sample`).id AS _ID, META(`travel-sample`).cas AS _CAS, `travel-sample`.* " + // "FROM `travel-sample` " + // "WHERE type = \"airline\" AND _class IS MISSING;"), Airline.class); airlinesWithoutClassAttribute.forEach(couchbaseOperations::save); } }
@Override public Iterable<T> findAll(Sort sort) { Assert.notNull(sort, "Sort must not be null!"); //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 getCouchbaseOperations().findByN1QL(query, getEntityInformation().getJavaType()); }
protected Object executePaged(N1qlQuery query, N1qlQuery countQuery, Pageable pageable, Class<?> typeToRead) { Assert.notNull(pageable, "Pageable must not be null!"); long total = 0L; logIfNecessary(countQuery); List<CountFragment> countResult = couchbaseOperations.findByN1QLProjection(countQuery, CountFragment.class); if (countResult != null && !countResult.isEmpty()) { total = countResult.get(0).count; } logIfNecessary(query); List<?> result = couchbaseOperations.findByN1QL(query, typeToRead); return new PageImpl(result, pageable, total); }
public StringN1qlBasedQuery(String statement, CouchbaseQueryMethod queryMethod, CouchbaseOperations 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; }
@Override public Iterable<T> findAllById(final Iterable<ID> ids) { final ResolvedView resolvedView = determineView(); ViewQuery query = ViewQuery.from(resolvedView.getDesignDocument(), resolvedView.getViewName()); query.reduce(false); query.stale(getCouchbaseOperations().getDefaultConsistency().viewConsistency()); JsonArray keys = JsonArray.create(); for (ID id : ids) { keys.add(couchbaseOperations.getConverter().convertForWriteIfNeeded(id)); } query.keys(keys); return couchbaseOperations.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(); /* here contrary to the classical view query we don't support not including an attribute of the entity in the method name, those are mandatory and will result in a PropertyReferenceException if not used... */ 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 ParametersParameterAccessor(method.getParameters(), runtimeParams), baseSpatialQuery, operations.getConverter()); SpatialViewQueryCreator.SpatialViewQueryWrapper finalQuery = creator.createQuery(); //execute the spatial query return execute(finalQuery); }
@Override public Optional<T> findById(ID id) { Assert.notNull(id, "The given id must not be null!"); return Optional.ofNullable(couchbaseOperations.findById(couchbaseOperations.getConverter().convertForWriteIfNeeded(id).toString(), entityInformation.getJavaType())); }
@Override public boolean existsById(ID id) { Assert.notNull(id, "The given id must not be null!"); return couchbaseOperations.exists(couchbaseOperations.getConverter().convertForWriteIfNeeded(id).toString()); }
@Override public Iterable<T> findAll() { final ResolvedView resolvedView = determineView(); ViewQuery query = ViewQuery.from(resolvedView.getDesignDocument(), resolvedView.getViewName()); query.reduce(false); query.stale(getCouchbaseOperations().getDefaultConsistency().viewConsistency()); return couchbaseOperations.findByView(query, entityInformation.getJavaType()); }
@Override public void deleteById(ID id) { Assert.notNull(id, "The given id must not be null!"); couchbaseOperations.remove(couchbaseOperations.getConverter().convertForWriteIfNeeded(id).toString()); }
/** * Get the {@link MappingContext} to use in repositories. It is extracted from the default {@link CouchbaseOperations}. * * @return the mapping context. */ public MappingContext<? extends CouchbasePersistentEntity<?>, CouchbasePersistentProperty> getMappingContext() { return defaultOperations.getConverter().getMappingContext(); }
/** * Returns the base class for the repository being constructed. Two categories of repositories can be produced by * this factory: {@link SimpleCouchbaseRepository} and {@link N1qlCouchbaseRepository}. 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) { CouchbaseOperations 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()); } return query.eliminate( operations.findBySpatialView(query.getQuery(), method.getEntityInformation().getJavaType()) ); }
protected Object execute(ViewQuery query) { if (LOG.isDebugEnabled()) { LOG.debug("Executing view query: " + query.toString()); } return operations.findByView(query, method.getEntityInformation().getJavaType()); }
protected Object executeSingleProjection(N1qlQuery query) { logIfNecessary(query); //the structure of the response from N1QL gives us a JSON object even when selecting a single aggregation List<Map> resultAsMap = couchbaseOperations.findByN1QLProjection(query, Map.class); if (resultAsMap.size() != 1) { throw new CouchbaseQueryExecutionException("Query returning a primitive type are expected to return " + "exactly 1 result, got " + resultAsMap.size()); } Map<String, Object> singleRow = (Map<String, Object>) resultAsMap.get(0); if (singleRow.size() != 1) { throw new CouchbaseQueryExecutionException("Query returning a simple type are expected to return " + "a unique value, got " + singleRow.size()); } Collection<Object> rowValues = singleRow.values(); if (rowValues.size() != 1) { throw new CouchbaseQueryExecutionException("Query returning a simple type are expected to use a " + "single aggregation/projection, got " + rowValues.size()); } return rowValues.iterator().next(); }
@Override public Iterable<T> findAll(Sort sort) { Assert.notNull(sort, "Sort must not be null!"); //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 getCouchbaseOperations().findByN1QL(query, getEntityInformation().getJavaType()); }
protected Object executePaged(N1qlQuery query, N1qlQuery countQuery, Pageable pageable, Class<?> typeToRead) { Assert.notNull(pageable, "Pageable must not be null!"); long total = 0L; logIfNecessary(countQuery); List<CountFragment> countResult = couchbaseOperations.findByN1QLProjection(countQuery, CountFragment.class); if (countResult != null && !countResult.isEmpty()) { total = countResult.get(0).count; } logIfNecessary(query); List<?> result = couchbaseOperations.findByN1QL(query, typeToRead); return new PageImpl(result, pageable, total); }
public StringN1qlBasedQuery(String statement, CouchbaseQueryMethod queryMethod, CouchbaseOperations 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; }
@Override public Iterable<T> findAllById(final Iterable<ID> ids) { final ResolvedView resolvedView = determineView(); ViewQuery query = ViewQuery.from(resolvedView.getDesignDocument(), resolvedView.getViewName()); query.reduce(false); query.stale(getCouchbaseOperations().getDefaultConsistency().viewConsistency()); JsonArray keys = JsonArray.create(); for (ID id : ids) { keys.add(couchbaseOperations.getConverter().convertForWriteIfNeeded(id)); } query.keys(keys); return couchbaseOperations.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(); /* here contrary to the classical view query we don't support not including an attribute of the entity in the method name, those are mandatory and will result in a PropertyReferenceException if not used... */ 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 ParametersParameterAccessor(method.getParameters(), runtimeParams), baseSpatialQuery, operations.getConverter()); SpatialViewQueryCreator.SpatialViewQueryWrapper finalQuery = creator.createQuery(); //execute the spatial query return execute(finalQuery); }