imbajin commented on code in PR #2859:
URL: 
https://github.com/apache/incubator-hugegraph/pull/2859#discussion_r2313283200


##########
hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/backend/tx/GraphTransaction.java:
##########
@@ -934,57 +1012,121 @@ public Edge queryEdge(Object edgeId) {
         return edge;
     }
 
+    @Watched(prefix = "graph")
     protected Iterator<Edge> queryEdgesByIds(Object[] edgeIds,
                                              boolean verifyId) {
         Query.checkForceCapacity(edgeIds.length);
 
-        // NOTE: allowed duplicated edges if query by duplicated ids
-        List<Id> ids = InsertionOrderUtil.newList();
-        Map<Id, HugeEdge> edges = new HashMap<>(edgeIds.length);
+        List<Id> ids;
+        Map<Id, HugeEdge> edges;
+        boolean edgesUpdated = this.edgesInTxSize() > 0;
 
-        IdQuery query = new IdQuery(HugeType.EDGE);
-        for (Object edgeId : edgeIds) {
-            HugeEdge edge;
-            EdgeId id = HugeEdge.getIdValue(edgeId, !verifyId);
+        if (edgeIds.length == 1) {
+            EdgeId id = HugeEdge.getIdValue(edgeIds[0], !verifyId);
+
+            boolean tryQueryBackend = true;
             if (id == null) {
-                continue;
+                tryQueryBackend = false;
+                ids = ImmutableList.of();
+            } else {
+                if (id.direction() == Directions.IN) {
+                    id = id.switchDirection();
+                }
+                ids = ImmutableList.of(id);
+            }
+
+            HugeEdge edge = null;
+            if (id != null && edgesUpdated) {
+                if (this.removedEdges.containsKey(id)) {
+                    // The record has been deleted
+                    tryQueryBackend = false;
+                } else if ((edge = this.addedEdges.get(id)) != null ||
+                           (edge = this.updatedEdges.get(id)) != null) {
+                    // Found from local tx
+                    tryQueryBackend = false;
+                    if (edge.expired()) {

Review Comment:
   The same optimization pattern is duplicated for edges and vertices. Consider 
creating a generic helper method to reduce code duplication:
   
   ```java
   private <T extends HugeElement> Map<Id, T> querySingleElement(
       Id id, 
       HugeType type,
       Map<Id, T> added,
       Map<Id, T> updated, 
       Map<Id, T> removed,
       Function<IdQuery, Iterator<T>> backendQuery) {
       
       if (id == null) {
           return ImmutableMap.of();
       }
       
       // Check local transaction state
       if (removed.containsKey(id)) {
           return ImmutableMap.of();
       }
       
       T element = added.get(id);
       if (element == null) {
           element = updated.get(id);
       }
       
       if (element != null && !element.expired()) {
           return ImmutableMap.of(id, element);
       }
       
       // Query backend
       IdQuery query = new IdQuery.OneIdQuery(type, id);
       T backendElement = QueryResults.one(backendQuery.apply(query));
       return backendElement == null ? ImmutableMap.of() : ImmutableMap.of(id, 
backendElement);
   }
   ```
   
   This would eliminate the duplication between vertex and edge query methods.



-- 
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: issues-unsubscr...@hugegraph.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscr...@hugegraph.apache.org
For additional commands, e-mail: issues-h...@hugegraph.apache.org

Reply via email to