SkrDrag commented on issue #2415: URL: https://github.com/apache/incubator-hugegraph/issues/2415#issuecomment-1890976073
> _🤖_ > > 你好 @SkrDrag, 我是[Dosu](https://dosu.dev),一个可以帮助你解决问题、回答疑问并成为贡献者的机器人。虽然我是机器人,但我在等待人类维护者的同时,会尽力帮助你。很高兴为你服务! > > HugeGraph在`GraphTransaction`类的`query`方法中构造一个查询对象去查询后端数据库。这个方法接受一个`Query`对象作为参数,并返回一个`QueryResults<BackendEntry>`对象。以下是相关的代码片段: > > ```java > @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); > LOG.debug("{}", queries); > return queries.empty() ? QueryResults.empty() : > queries.fetch(this.pageSize); > } > ``` > > 在这个方法中,如果查询不是`ConditionQuery`的实例,它直接调用父类的`query`方法。如果它是`ConditionQuery`的实例,它调用`optimizeQueries`方法来优化查询,然后获取结果。 > > 实际查询后端数据库是在`queryVerticesFromBackend`方法中完成的: > > ```java > protected Iterator<HugeVertex> queryVerticesFromBackend(Query query) { > assert query.resultType().isVertex(); > > QueryResults<BackendEntry> results = this.query(query); > Iterator<BackendEntry> entries = results.iterator(); > Iterator<HugeVertex> vertices = new MapperIterator<>(entries, this::parseEntry); > vertices = this.filterExpiredResultFromBackend(query, vertices); > > if (!this.store().features().supportsQuerySortByInputIds()) { > // There is no id in BackendEntry, so sort after deserialization > vertices = results.keepInputOrderIfNeeded(vertices); > } > return vertices; > } > ``` > > 在这个方法中,它调用`query`方法获取`QueryResults<BackendEntry>`对象,然后使用`parseEntry`方法将每个`BackendEntry`转换为`HugeVertex`对象。它还过滤掉过期的顶点,并在必要时对顶点进行排序。 > > 请注意,与后端数据库交互的`query`方法的实际实现在给定的上下文中并未提供。具体的实现将取决于正在使用的后端存储,本例中为MySQL。 > > 关于`Query`对象的结构和在`query`方法中的使用,`Query`对象在HugeGraph中是可以执行对图的不同类型查询的基类。它用于封装查询的参数,如限制、偏移和条件。 > > 在提供的上下文中,`Query`对象在`constructEdgesQuery`方法中用于创建一个查询,用于检索顶点在两个方向上的所有边。然后,`Query`对象被传递给`queryEdgesFromBackend`方法来执行查询并检索边。 > > `Query`对象在`org.apache.hugegraph.backend.query`包中定义,它有几个子类用于不同类型的查询,如`ConditionQuery`、`IdQuery`和`Aggregate`。这些子类向基类`Query`添加额外的属性和方法,以支持更复杂的查询。 > > 例如,`ConditionQuery`类添加了一个`conditions`属性来存储查询的条件,以及像`query()`和`condition()`这样的方法来添加查询条件。`IdQuery`类添加了一个`ids`属性来存储要查询的ID,以及一个`query()`方法来添加ID到查询。 > > 关于`GraphTransaction`类中的`optimizeQueries`方法如何工作以及它对`Query`对象执行哪种优化,`optimizeQueries`方法在`GraphTransaction`类中用于优化给定的`Query`对象并返回一系列优化后的查询。优化过程包括展平查询条件并优化每个条件查询。 > > 具体的步骤如下: > > 1. 创建一个将保存优化查询的`QueryList`对象。 > 2. 如果查询不是`ConditionQuery`,将查询添加到`QueryList`并返回。 > 3. 如果查询是`ConditionQuery`,使用`ConditionQueryFlatten.flatten`方法展平查询条件。这个方法将复杂的查询条件分解为更简单的条件。 > 4. 对于每个展平的`ConditionQuery`,调用`optimizeQuery`方法来优化查询。这个方法根据查询的类型和其条件执行几个优化。例如,如果查询是顶点查询并且匹配顶点标签的主键,它通过将顶点标签和主键值转换为顶点id来优化查询。同样,如果查询是边查询并且匹配边标签的排序键,它通过序列化排序值并重置用户属性条件来优化查询。 > 5. 如果优化后的查询不为空且不为空,将查询添加到`QueryList`。 > 6. 如果优化后的查询为空,调用`indexQuery`方法执行索引查询并将结果添加到`QueryList`。 > 7. 最后,返回包含优化查询的`QueryList`。 > > 这是相关的代码: > > ```java > private <R> QueryList<R> optimizeQueries(Query query, > QueryResults.Fetcher<R> fetcher) { > 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 the returned q: > * 1.sysprop-query, which would not be empty. > * 2.index-query result(ids after optimization), which may be empty. > */ > if (q == null) { > queries.add(this.indexQuery(cq), this.batchSize); > } else if (!q.empty()) { > queries.add(q); > } > } > return queries; > } > ``` > > 以上信息来自于: > > * [hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/backend/tx/GraphTransaction.java](https://github.com/apache/incubator-hugegraph/blob/master/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/backend/tx/GraphTransaction.java) > > ## Sources > * [hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/backend/tx/GraphTransaction.java](https://github.com/apache/incubator-hugegraph/blob/master/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/backend/tx/GraphTransaction.java) > > This response is meant to be useful and save you time. It is **not meant to be a precise solution**, but rather a starting point for your own research. > > Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant. > > If you want to continue the conversation, start your reply with @dosu-bot. Hugegraph有考虑数据动态更新的场景吗。还是说Hugegraph处理的基本是静态图 -- 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]
