gmunozfe commented on code in PR #2196:
URL:
https://github.com/apache/incubator-kie-kogito-apps/pull/2196#discussion_r1970135789
##########
data-index/data-index-storage/data-index-storage-jpa-common/src/main/java/org/kie/kogito/index/jpa/storage/JPAQuery.java:
##########
@@ -195,4 +191,21 @@ private List<Predicate>
getRecursivePredicate(AttributeFilter<?> filter, Root<E>
.collect(toList());
}
+ @Override
+ public long count() {
+ CriteriaBuilder builder =
repository.getEntityManager().getCriteriaBuilder();
+ CriteriaQuery<Long> criteriaQuery = builder.createQuery(Long.class);
+ Root<E> root = criteriaQuery.from(entityClass);
+ criteriaQuery.select(builder.count(root));
+ addWhere(builder, criteriaQuery, root);
+ return
repository.getEntityManager().createQuery(criteriaQuery).getSingleResult();
Review Comment:
`getSingleResult` could return null. In that case, it would be better to
return 0L, wdyt?
##########
data-index/data-index-graphql/src/main/java/org/kie/kogito/index/graphql/AbstractGraphQLSchemaManager.java:
##########
@@ -205,10 +228,22 @@ protected <K, T> List<T>
executeAdvancedQueryForCache(StorageFetcher<K, T> cache
query.offset(offset);
}
}
-
return query.execute();
}
+ protected <K, T> long executeCount(StorageFetcher<K, T> cache,
DataFetchingEnvironment env) {
+ return setupQuery(cache, env).count();
+ }
+
+ private <K, T> Query<T> setupQuery(StorageFetcher<K, T> cache,
DataFetchingEnvironment env) {
+ Objects.requireNonNull(cache, "Cache not found");
Review Comment:
Shouldn't we require that `env` is not null as well?
`Objects.requireNonNull(env, "DataFetchingEnvironment cannot be null");`
##########
data-index/data-index-storage/data-index-storage-jpa-common/src/main/java/org/kie/kogito/index/jpa/storage/JPAQuery.java:
##########
@@ -195,4 +191,21 @@ private List<Predicate>
getRecursivePredicate(AttributeFilter<?> filter, Root<E>
.collect(toList());
}
+ @Override
+ public long count() {
+ CriteriaBuilder builder =
repository.getEntityManager().getCriteriaBuilder();
+ CriteriaQuery<Long> criteriaQuery = builder.createQuery(Long.class);
+ Root<E> root = criteriaQuery.from(entityClass);
+ criteriaQuery.select(builder.count(root));
+ addWhere(builder, criteriaQuery, root);
+ return
repository.getEntityManager().createQuery(criteriaQuery).getSingleResult();
+ }
+
+ private <V> void addWhere(CriteriaBuilder builder, CriteriaQuery<V>
criteriaQuery, Root<E> root) {
+ if (filters != null && !filters.isEmpty()) {
+ List<Predicate> predicates = getPredicates(builder, root);
+ criteriaQuery.where(predicates.toArray(new Predicate[] {}));
Review Comment:
you can also use `criteriaQuery.where(predicates.toArray(Predicate[]::new));`
but it's up to you, no big difference at all
##########
data-index/data-index-graphql/src/main/java/org/kie/kogito/index/graphql/AbstractGraphQLSchemaManager.java:
##########
@@ -205,10 +228,22 @@ protected <K, T> List<T>
executeAdvancedQueryForCache(StorageFetcher<K, T> cache
query.offset(offset);
}
}
-
return query.execute();
}
+ protected <K, T> long executeCount(StorageFetcher<K, T> cache,
DataFetchingEnvironment env) {
+ return setupQuery(cache, env).count();
+ }
+
+ private <K, T> Query<T> setupQuery(StorageFetcher<K, T> cache,
DataFetchingEnvironment env) {
+ Objects.requireNonNull(cache, "Cache not found");
+ String inputTypeName = ((GraphQLNamedType)
env.getFieldDefinition().getArgument("where").getType()).getName();
+ Query<T> query = cache.query();
+ Map<String, Object> where = env.getArgument("where");
+
query.filter(GraphQLQueryParserRegistry.get().getParser(inputTypeName).apply(where));
Review Comment:
I'm wondering if we could make any performance optimization for same cache
and env, but probably it's out of the scope of this feature and can be handle
later.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]