You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hugegraph.apache.org by je...@apache.org on 2022/12/22 16:08:33 UTC

[incubator-hugegraph] 01/01: fallback queryNumber() if there are uncommitted records

This is an automated email from the ASF dual-hosted git repository.

jermy pushed a commit to branch fallback-queryNumber-with-uncommitted
in repository https://gitbox.apache.org/repos/asf/incubator-hugegraph.git

commit 840221aab3fb671ecddc8dfb71bfcff09df53e76
Author: Jermy Li <je...@apache.org>
AuthorDate: Fri Dec 23 00:08:27 2022 +0800

    fallback queryNumber() if there are uncommitted records
    
    fixed sub-issue 1 of #2058
---
 .../hugegraph/backend/tx/GraphTransaction.java     | 60 ++++++++++++++--------
 1 file changed, 39 insertions(+), 21 deletions(-)

diff --git a/hugegraph-core/src/main/java/org/apache/hugegraph/backend/tx/GraphTransaction.java b/hugegraph-core/src/main/java/org/apache/hugegraph/backend/tx/GraphTransaction.java
index 66986407c..7c19c4c6e 100644
--- a/hugegraph-core/src/main/java/org/apache/hugegraph/backend/tx/GraphTransaction.java
+++ b/hugegraph-core/src/main/java/org/apache/hugegraph/backend/tx/GraphTransaction.java
@@ -528,12 +528,12 @@ public class GraphTransaction extends IndexableTransaction {
     @Override
     public QueryResults<BackendEntry> query(Query query) {
         if (!(query instanceof ConditionQuery)) {
+            // It's a sysprop-query, don't need to optimize
             LOG.debug("Query{final:{}}", query);
             return super.query(query);
         }
 
-        QueryList<BackendEntry> queries = this.optimizeQueries(query,
-                                                               super::query);
+        QueryList<BackendEntry> queries = this.optimizeQueries(query, super::query);
         LOG.debug("{}", queries);
         return queries.empty() ? QueryResults.empty() :
                                  queries.fetch(this.pageSize);
@@ -541,39 +541,51 @@ public class GraphTransaction extends IndexableTransaction {
 
     @Override
     public Number queryNumber(Query query) {
-        E.checkArgument(!this.hasUpdate(),
-                        "It's not allowed to query number when " +
-                        "there are uncommitted records.");
-
-        if (!(query instanceof ConditionQuery)) {
-            return super.queryNumber(query);
-        }
-
+        boolean hasUpdate = this.hasUpdate();
         Aggregate aggregate = query.aggregateNotNull();
 
         QueryList<Number> queries = this.optimizeQueries(query, q -> {
-            boolean indexQuery = q.getClass() == IdQuery.class;
-            OptimizedType optimized = ((ConditionQuery) query).optimized();
+            boolean isIndexQuery = q instanceof IdQuery;
+            boolean isConditionQuery = query instanceof ConditionQuery;
+            assert isIndexQuery || isConditionQuery || q == query;
+            // Need to fallback if there are uncommitted records
+            boolean fallback = hasUpdate;
             Number result;
-            if (!indexQuery) {
+
+            if (fallback) {
+                // Here just ignore it, and do fallback later
+                result = null;
+            } else if (!isIndexQuery || !isConditionQuery) {
+                // It's a sysprop-query, let parent tx do it
+                assert !fallback;
                 result = super.queryNumber(q);
             } else {
                 E.checkArgument(aggregate.func() == AggregateFunc.COUNT,
                                 "The %s operator on index is not supported now",
                                 aggregate.func().string());
-                if (this.optimizeAggrByIndex &&
-                    optimized == OptimizedType.INDEX) {
+                assert query instanceof ConditionQuery;
+                OptimizedType optimized = ((ConditionQuery) query).optimized();
+                if (this.optimizeAggrByIndex && optimized == OptimizedType.INDEX) {
                     // The ids size means results count (assume no left index)
                     result = q.idsSize();
                 } else {
                     assert optimized == OptimizedType.INDEX_FILTER ||
                            optimized == OptimizedType.INDEX;
-                    assert q.resultType().isVertex() || q.resultType().isEdge();
-                    result = IteratorUtils.count(q.resultType().isVertex() ?
-                                                 this.queryVertices(q) :
-                                                 this.queryEdges(q));
+                    assert !fallback;
+                    fallback = true;
+                    result = null;
                 }
             }
+
+            // Can't be optimized, then do fallback
+            if (fallback) {
+                assert result == null;
+                assert q.resultType().isVertex() || q.resultType().isEdge();
+                result = IteratorUtils.count(q.resultType().isVertex() ?
+                                             this.queryVertices(q) :
+                                             this.queryEdges(q));
+            }
+
             return new QueryResults<>(IteratorUtils.of(result), q);
         });
 
@@ -1335,14 +1347,20 @@ public class GraphTransaction extends IndexableTransaction {
 
     private <R> QueryList<R> optimizeQueries(Query query,
                                              QueryResults.Fetcher<R> fetcher) {
-        boolean supportIn = this.storeFeatures().supportsQueryWithInCondition();
         QueryList<R> queries = new QueryList<>(query, fetcher);
+        if (!(query instanceof ConditionQuery)) {
+            // It's a sysprop-query, add itself as subquery, don't need to flatten
+            queries.add(query);
+            return queries;
+        }
+
+        boolean supportIn = this.storeFeatures().supportsQueryWithInCondition();
         for (ConditionQuery cq: ConditionQueryFlatten.flatten(
                                 (ConditionQuery) query, supportIn)) {
             // Optimize by sysprop
             Query q = this.optimizeQuery(cq);
             /*
-             * NOTE: There are two possibilities for this query:
+             * NOTE: There are two possibilities for the returned q:
              * 1.sysprop-query, which would not be empty.
              * 2.index-query result(ids after optimization), which may be empty.
              */