Skip to content

Iterable graph#2228

Merged
lvca merged 3 commits intomainfrom
IterableGraph
May 28, 2025
Merged

Iterable graph#2228
lvca merged 3 commits intomainfrom
IterableGraph

Conversation

@lvca
Copy link
Contributor

@lvca lvca commented May 28, 2025

This pull request introduces a new IterableGraph interface and updates various methods across the codebase to use this interface instead of the standard Iterable. The changes aim to provide additional utility methods for graph traversal and simplify operations on graph elements.

Introduction of IterableGraph Interface

Updates to GraphEngine and Vertex Methods

Integration with Existing Vertex Implementations

  • Updated the ImmutableVertex, MutableVertex, SynchronizedVertex, RemoteImmutableVertex, RemoteMutableVertex, and RemoteVertex classes to return IterableGraph for their edge and vertex traversal methods. This ensures that all vertex implementations leverage the new IterableGraph interface. [1] [2] [3] [4] [5] [6] [7]

Extension of MultiIterator

These changes enhance the graph traversal API by introducing a more feature-rich interface (IterableGraph) and ensuring its adoption across the codebase. This refactoring improves code readability, consistency, and functionality for graph-related operations.## What does this PR do?

A brief description of the change being made with this pull request.

Motivation

What inspired you to submit this pull request?

Related issues

A list of issues either fixed, containing architectural discussions, otherwise relevant
for this Pull Request.

Additional Notes

Anything else we should know when reviewing?

Checklist

  • I have run the build using mvn clean package command
  • My unit tests cover both failure and success scenarios

@lvca lvca self-assigned this May 28, 2025
@lvca lvca added the enhancement New feature or request label May 28, 2025
@lvca lvca merged commit 0fce213 into main May 28, 2025
7 of 8 checks passed
@lvca lvca deleted the IterableGraph branch May 28, 2025 17:30
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello @lvca, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

Summary of Changes

As gemini-code-assist, I've reviewed this pull request titled "Iterable graph". The primary goal of this PR is to introduce a new IterableGraph interface that extends the standard Iterable and provides convenient utility methods specifically for graph elements (like getFirstOrNull, toList, and size). The changes involve refactoring various methods across the codebase, particularly those in GraphEngine and Vertex implementations, to return this new IterableGraph interface instead of the basic Iterable. This aims to enhance the graph traversal API by offering more built-in functionality directly on the returned iterable objects, improving code readability and simplifying common operations.

Highlights

  • Refactoring Graph Traversal Methods: Updated methods in GraphEngine and the Vertex interface (and its implementations like ImmutableVertex, MutableVertex, SynchronizedVertex, RemoteImmutableVertex, RemoteMutableVertex, and RemoteVertex) that previously returned Iterable<Edge> or Iterable<Vertex> to now return IterableGraph<Edge> or IterableGraph<Vertex> respectively.
  • MultiIterator Integration: The MultiIterator utility class has been updated to implement the new IterableGraph interface, allowing it to also benefit from the added utility methods.

Changelog

Click here to see the changelog
  • engine/src/main/java/com/arcadedb/graph/GraphEngine.java
    • Changed return type of getEdges() methods (lines 442, 456) from Iterable<Edge> to IterableGraph<Edge>.
    • Changed return type of getVertices() methods (lines 497, 519) from Iterable<Vertex> to IterableGraph<Vertex>.
    • Replaced Collections.emptyList() with IterableGraph.emptyList() for empty results (lines 489, 552).
  • engine/src/main/java/com/arcadedb/graph/ImmutableVertex.java
    • Updated return types of getEdges() (line 155) and getVertices() (line 165, 170) methods to IterableGraph.
  • engine/src/main/java/com/arcadedb/graph/IterableGraph.java
    • New file added defining the IterableGraph interface with getFirstOrNull, toList, size, and emptyList methods.
  • engine/src/main/java/com/arcadedb/graph/MutableVertex.java
    • Updated return types of getEdges() (line 167) and getVertices() (line 177, 182) methods to IterableGraph.
  • engine/src/main/java/com/arcadedb/graph/SynchronizedVertex.java
    • Updated return types of getEdges() (line 93) and getVertices() (line 103, 108) methods to IterableGraph.
  • engine/src/main/java/com/arcadedb/graph/Vertex.java
    • Updated interface method signatures for getEdges() (line 60, 62) and getVertices() (line 69, 78) to return IterableGraph.
  • engine/src/main/java/com/arcadedb/utility/MultiIterator.java
    • Made MultiIterator implement the IterableGraph interface (line 30).
  • network/src/main/java/com/arcadedb/remote/RemoteImmutableVertex.java
    • Added import for IterableGraph (line 26).
    • Updated return types of getEdges() (line 64) and getVertices() (line 74, 79) methods to IterableGraph.
  • network/src/main/java/com/arcadedb/remote/RemoteMutableVertex.java
    • Added import for IterableGraph (line 29).
    • Updated return types of getEdges() (line 158) and getVertices() (line 168, 173) methods to IterableGraph.
  • network/src/main/java/com/arcadedb/remote/RemoteVertex.java
    • Added import for IterableGraph (line 25).
    • Updated return types of getEdges() (line 60, 64) and getVertices() (line 79, 83) methods to IterableGraph.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in issue comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist is currently in preview and may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments to provide feedback.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.


An interface new,
Graph iterables come through,
Methods now abound.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@lvca lvca added this to the 25.5.1 milestone May 28, 2025
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces the IterableGraph interface, which extends Iterable with useful utility methods like getFirstOrNull, toList, and size. This is a good initiative to enhance the graph traversal API and provide more convenience for developers. The changes are consistently applied across the codebase, and the use of default methods in the new interface is well-leveraged.

I've identified a couple of areas for potential improvement, primarily concerning the size() method's handling of large collections and the Java version compatibility of List.getFirst().

Summary of Findings

  • Potential Integer Overflow in IterableGraph.size(): The size() method in IterableGraph casts a long count from MultiIterator.countEntries() to int. This can lead to data loss and an incorrect size if the number of entries exceeds Integer.MAX_VALUE. Severity: Medium.
  • Java Version Compatibility for List.getFirst(): The getFirstOrNull() method uses List.getFirst(), which is available from Java 21. If the project targets an older Java version, this will cause a runtime error. Severity: Medium (conditional on target Java version).
  • Naming of IterableGraph.emptyList(): The static method emptyList() returns an IterableGraph. While common, names like emptyIterable() or empty() might be slightly more semantically precise as the returned object isn't strictly a List. This is a minor point. Severity: Low. (Not commented due to severity filter settings).
  • Testing for IterableGraph default methods: Consider adding specific unit tests for the new default methods in IterableGraph (getFirstOrNull, toList, size) to cover various underlying Iterable implementations (e.g., List, MultiIterator, and a generic single-pass iterator) and edge cases. Severity: Low. (Not commented due to severity filter settings).

Merge Readiness

The pull request introduces valuable enhancements to the graph API. However, there are a couple of medium-severity issues related to potential integer overflow in the size() method and Java version compatibility for List.getFirst() that should be addressed before merging. Once these points are clarified or resolved, the PR should be in good shape.

Please note that I am an AI assistant and cannot approve pull requests. Ensure that other reviewers inspect and approve these changes before merging.

Comment on lines +55 to +56
else if (this instanceof MultiIterator<T> it)
return (int) it.countEntries();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The size() method casts it.countEntries() (which returns a long) to an int. If MultiIterator.countEntries() returns a value greater than Integer.MAX_VALUE, this cast will lead to an incorrect (truncated) size. This could be problematic if a MultiIterator instance is expected to handle a very large number of entries (more than 2 billion).

Could you consider how to best handle this potential overflow? Throwing an ArithmeticException or returning Integer.MAX_VALUE with a warning are options if the return type must remain int.

    else if (this instanceof MultiIterator<T> it) {
      long count = it.countEntries();
      if (count > Integer.MAX_VALUE) {
        // Throw an exception to indicate overflow, as returning a truncated int can be misleading.
        throw new ArithmeticException("Size of MultiIterator (" + count + ") exceeds Integer.MAX_VALUE.");
      }
      return (int) count;
    }

Comment on lines +36 to +37
if (this instanceof List<?> list)
return list.isEmpty() ? null : (T) list.getFirst();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The getFirstOrNull() method uses list.getFirst(), which was introduced in Java 21. If this project targets an earlier Java version (e.g., Java 11, 17), this method call will result in a NoSuchMethodError at runtime.

What is the minimum Java version this project is intended to support? If it's below Java 21, it would be safer to use list.get(0) after checking !list.isEmpty().

    if (this instanceof List<?> list)
      return list.isEmpty() ? null : (T) list.get(0); // Use get(0) for compatibility with Java < 21

@codacy-production
Copy link

Coverage summary from Codacy

See diff coverage on Codacy

Coverage variation Diff coverage
-0.02% 14.29%
Coverage variation details
Coverable lines Covered lines Coverage
Common ancestor commit (02ee6d3) 72329 46369 64.11%
Head commit (e81e49b) 72348 (+19) 46369 (+0) 64.09% (-0.02%)

Coverage variation is the difference between the coverage for the head and common ancestor commits of the pull request branch: <coverage of head commit> - <coverage of common ancestor commit>

Diff coverage details
Coverable lines Covered lines Diff coverage
Pull request (#2228) 21 3 14.29%

Diff coverage is the percentage of lines that are covered by tests out of the coverable lines that the pull request added or modified: <covered lines added or modified>/<coverable lines added or modified> * 100%

See your quality gate settings    Change summary preferences

robfrank pushed a commit that referenced this pull request Jun 17, 2025
Implemented `toList()`, `getFirstOrNull()`, and `size()`

(cherry picked from commit 0fce213)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant