Skip to content

Fix ComparableVector Comparable contract violation#2817

Merged
lvca merged 2 commits intojvector-integrationfrom
copilot/sub-pr-2816
Nov 21, 2025
Merged

Fix ComparableVector Comparable contract violation#2817
lvca merged 2 commits intojvector-integrationfrom
copilot/sub-pr-2816

Conversation

Copy link
Contributor

Copilot AI commented Nov 21, 2025

The ComparableVector class violated the Comparable contract: compareTo returned 0 on hash collisions even when vectors differed, while equals correctly compared array contents. This could cause data loss in TreeMap (used by TransactionIndexContext) where distinct vectors with matching hash codes would overwrite each other.

Changes

  • LSMVectorIndex.ComparableVector.compareTo: Added lexicographical comparison fallback when hash codes collide
    • Fast path: compare hash codes first for performance
    • Collision path: element-by-element comparison, then length comparison
    • Ensures compareTo(v) == 0 ⟺ equals(v) == true
@Override
public int compareTo(final ComparableVector other) {
  // First compare by hash code for performance
  final int hashCompare = Integer.compare(this.hashCode, other.hashCode);
  if (hashCompare != 0)
    return hashCompare;
  
  // If hash codes are equal, perform lexicographical comparison of vector elements
  // to maintain the Comparable contract: compareTo == 0 iff equals == true
  final int minLength = Math.min(this.vector.length, other.vector.length);
  for (int i = 0; i < minLength; i++) {
    final int elementCompare = Float.compare(this.vector[i], other.vector[i]);
    if (elementCompare != 0)
      return elementCompare;
  }
  // If all compared elements are equal, compare by length
  return Integer.compare(this.vector.length, other.vector.length);
}

💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copy link
Contributor Author

Copilot AI commented Nov 21, 2025

Just as a heads up, I was blocked by some firewall rules while working on your feedback. Expand below for details.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • repository.apache.org
    • Triggering command: /usr/lib/jvm/temurin-17-jdk-amd64/bin/java /usr/lib/jvm/temurin-17-jdk-amd64/bin/java -classpath /home/REDACTED/.m2/wrapper/dists/apache-maven-3.9.9/3477a4f1/boot/plexus-classworlds-2.8.0.jar -Dclassworlds.conf=/home/REDACTED/.m2/wrapper/dists/apache-maven-3.9.9/3477a4f1/bin/m2.conf -Dmaven.home=/home/REDACTED/.m2/wrapper/dists/apache-maven-3.9.9/3477a4f1 -Dlibrary.jansi.path=/home/REDACTED/.m2/wrapper/dists/apache-maven-3.9.9/3477a4f1/lib/jansi-native -Dmaven.multiModuleProjectDirectory=/home/REDACTED/work/arcadedb/arcadedb org.codehaus.plexus.classworlds.launcher.Launcher -f pom.xml -B -V -e -Dfindbugs.skip -Dcheckstyle.skip -Dpmd.skip=true -Dspotbugs.skip -Denforcer.skip -Dmaven.javadoc.skip -DskipTests (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Copilot AI changed the title [WIP] Address feedback on JVector integration PR Fix ComparableVector Comparable contract violation Nov 21, 2025
Copilot AI requested a review from lvca November 21, 2025 18:00
@lvca lvca marked this pull request as ready for review November 21, 2025 22:12
@lvca lvca merged commit 4857d00 into jvector-integration Nov 21, 2025
2 of 3 checks passed
@lvca lvca deleted the copilot/sub-pr-2816 branch November 21, 2025 22:13
@mergify
Copy link
Contributor

mergify bot commented Nov 21, 2025

🧪 CI Insights

Here's what we observed from your CI run for c56850e.

🟢 All jobs passed!

But CI Insights is watching 👀

robfrank pushed a commit that referenced this pull request Nov 22, 2025
* Initial plan

* Fix ComparableVector to maintain Comparable contract

Co-authored-by: lvca <[email protected]>

---------

Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Co-authored-by: lvca <[email protected]>
lvca added a commit that referenced this pull request Nov 22, 2025
* First version with jvector

* Implemented compaction of vector indexes

* Added test cases

* Fixed compilation problems

* Fixed test cases, now all pass

* Refactor vector index using the transaction index changes instead of internal map (with threadId)

* feat: integrated new vector index with the `database import` command

* Supported lsmvector in `vectorNeighbors()` sql function

* Upgraded to jvector 4.0.0-rc.6

* Update LSMVectorIndexCompacted.java

fix: error after compaction

* Fix ComparableVector Comparable contract violation (#2817)

* Initial plan

* Fix ComparableVector to maintain Comparable contract

Co-authored-by: lvca <[email protected]>

---------

Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Co-authored-by: lvca <[email protected]>

* Apply suggestion from @Copilot

Co-authored-by: Copilot <[email protected]>

* Return similarity scores from LSMVectorIndex to avoid redundant distance recalculation (#2820)

* Initial plan

* Add findNeighborsFromVector method to LSMVectorIndex to return scores directly

Co-authored-by: lvca <[email protected]>

* Remove test artifacts and update .gitignore

Co-authored-by: lvca <[email protected]>

---------

Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Co-authored-by: lvca <[email protected]>

* Add mutable flag to vector index pages for safe compaction (#2819)

* Initial plan

* Add mutable byte indicator to vector index pages

- Added mutable flag byte at offset 8 in page header (after offsetFreeContent and numberOfEntries)
- New pages are created with mutable=1 (actively being written to)
- Pages are marked as immutable (mutable=0) when they become full and a new page is created
- Updated findLastImmutablePage() to scan from end backwards and stop at first immutable page
- Updated all page reading/writing code to account for the mutable byte in header
- All vector index tests passing

Co-authored-by: lvca <[email protected]>

* Remove test database files and update .gitignore

Co-authored-by: lvca <[email protected]>

* Add constants for page header offsets to improve maintainability

- Added OFFSET_FREE_CONTENT, OFFSET_NUM_ENTRIES, OFFSET_MUTABLE, and HEADER_BASE_SIZE constants
- Replaced magic numbers throughout the code with named constants
- Makes the code more maintainable and self-documenting

Co-authored-by: lvca <[email protected]>

* Update comments to reference constants instead of hardcoded offsets

Co-authored-by: lvca <[email protected]>

---------

Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Co-authored-by: lvca <[email protected]>
Co-authored-by: Luca Garulli <[email protected]>

* Make LSMVectorIndex ID property configurable (#2818)

* Initial plan

* Make ID property configurable in LSMVectorIndex

Co-authored-by: lvca <[email protected]>

* Remove test database files and add to .gitignore

Co-authored-by: lvca <[email protected]>

* Improve documentation for metadata JSON configuration

Co-authored-by: lvca <[email protected]>

---------

Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Co-authored-by: lvca <[email protected]>
Co-authored-by: Luca Garulli <[email protected]>

* First version with jvector

* Implemented compaction of vector indexes

* Added test cases

* Fixed compilation problems

* Fixed test cases, now all pass

* Refactor vector index using the transaction index changes instead of internal map (with threadId)

* feat: integrated new vector index with the `database import` command

* Supported lsmvector in `vectorNeighbors()` sql function

* Upgraded to jvector 4.0.0-rc.6

* Update LSMVectorIndexCompacted.java

fix: error after compaction

* Fix ComparableVector Comparable contract violation (#2817)

* Initial plan

* Fix ComparableVector to maintain Comparable contract

Co-authored-by: lvca <[email protected]>

---------

Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Co-authored-by: lvca <[email protected]>

* Apply suggestion from @Copilot

Co-authored-by: Copilot <[email protected]>

* Return similarity scores from LSMVectorIndex to avoid redundant distance recalculation (#2820)

* Initial plan

* Add findNeighborsFromVector method to LSMVectorIndex to return scores directly

Co-authored-by: lvca <[email protected]>

* Remove test artifacts and update .gitignore

Co-authored-by: lvca <[email protected]>

---------

Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Co-authored-by: lvca <[email protected]>

* Add mutable flag to vector index pages for safe compaction (#2819)

* Initial plan

* Add mutable byte indicator to vector index pages

- Added mutable flag byte at offset 8 in page header (after offsetFreeContent and numberOfEntries)
- New pages are created with mutable=1 (actively being written to)
- Pages are marked as immutable (mutable=0) when they become full and a new page is created
- Updated findLastImmutablePage() to scan from end backwards and stop at first immutable page
- Updated all page reading/writing code to account for the mutable byte in header
- All vector index tests passing

Co-authored-by: lvca <[email protected]>

* Remove test database files and update .gitignore

Co-authored-by: lvca <[email protected]>

* Add constants for page header offsets to improve maintainability

- Added OFFSET_FREE_CONTENT, OFFSET_NUM_ENTRIES, OFFSET_MUTABLE, and HEADER_BASE_SIZE constants
- Replaced magic numbers throughout the code with named constants
- Makes the code more maintainable and self-documenting

Co-authored-by: lvca <[email protected]>

* Update comments to reference constants instead of hardcoded offsets

Co-authored-by: lvca <[email protected]>

---------

Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Co-authored-by: lvca <[email protected]>
Co-authored-by: Luca Garulli <[email protected]>

* Make LSMVectorIndex ID property configurable (#2818)

* Initial plan

* Make ID property configurable in LSMVectorIndex

Co-authored-by: lvca <[email protected]>

* Remove test database files and add to .gitignore

Co-authored-by: lvca <[email protected]>

* Improve documentation for metadata JSON configuration

Co-authored-by: lvca <[email protected]>

---------

Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Co-authored-by: lvca <[email protected]>
Co-authored-by: Luca Garulli <[email protected]>

* fix pre-commit

* Update engine/src/main/java/com/arcadedb/database/TransactionIndexContext.java

Co-authored-by: Copilot <[email protected]>

* Fixed compaction

* test: fixed test

---------

Co-authored-by: Copilot <[email protected]>
Co-authored-by: lvca <[email protected]>
Co-authored-by: Copilot <[email protected]>
Co-authored-by: Roberto Franchini <[email protected]>
robfrank pushed a commit that referenced this pull request Feb 11, 2026
* First version with jvector

* Implemented compaction of vector indexes

* Added test cases

* Fixed compilation problems

* Fixed test cases, now all pass

* Refactor vector index using the transaction index changes instead of internal map (with threadId)

* feat: integrated new vector index with the `database import` command

* Supported lsmvector in `vectorNeighbors()` sql function

* Upgraded to jvector 4.0.0-rc.6

* Update LSMVectorIndexCompacted.java

fix: error after compaction

* Fix ComparableVector Comparable contract violation (#2817)

* Initial plan

* Fix ComparableVector to maintain Comparable contract

Co-authored-by: lvca <[email protected]>

---------

Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Co-authored-by: lvca <[email protected]>

* Apply suggestion from @Copilot

Co-authored-by: Copilot <[email protected]>

* Return similarity scores from LSMVectorIndex to avoid redundant distance recalculation (#2820)

* Initial plan

* Add findNeighborsFromVector method to LSMVectorIndex to return scores directly

Co-authored-by: lvca <[email protected]>

* Remove test artifacts and update .gitignore

Co-authored-by: lvca <[email protected]>

---------

Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Co-authored-by: lvca <[email protected]>

* Add mutable flag to vector index pages for safe compaction (#2819)

* Initial plan

* Add mutable byte indicator to vector index pages

- Added mutable flag byte at offset 8 in page header (after offsetFreeContent and numberOfEntries)
- New pages are created with mutable=1 (actively being written to)
- Pages are marked as immutable (mutable=0) when they become full and a new page is created
- Updated findLastImmutablePage() to scan from end backwards and stop at first immutable page
- Updated all page reading/writing code to account for the mutable byte in header
- All vector index tests passing

Co-authored-by: lvca <[email protected]>

* Remove test database files and update .gitignore

Co-authored-by: lvca <[email protected]>

* Add constants for page header offsets to improve maintainability

- Added OFFSET_FREE_CONTENT, OFFSET_NUM_ENTRIES, OFFSET_MUTABLE, and HEADER_BASE_SIZE constants
- Replaced magic numbers throughout the code with named constants
- Makes the code more maintainable and self-documenting

Co-authored-by: lvca <[email protected]>

* Update comments to reference constants instead of hardcoded offsets

Co-authored-by: lvca <[email protected]>

---------

Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Co-authored-by: lvca <[email protected]>
Co-authored-by: Luca Garulli <[email protected]>

* Make LSMVectorIndex ID property configurable (#2818)

* Initial plan

* Make ID property configurable in LSMVectorIndex

Co-authored-by: lvca <[email protected]>

* Remove test database files and add to .gitignore

Co-authored-by: lvca <[email protected]>

* Improve documentation for metadata JSON configuration

Co-authored-by: lvca <[email protected]>

---------

Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Co-authored-by: lvca <[email protected]>
Co-authored-by: Luca Garulli <[email protected]>

* First version with jvector

* Implemented compaction of vector indexes

* Added test cases

* Fixed compilation problems

* Fixed test cases, now all pass

* Refactor vector index using the transaction index changes instead of internal map (with threadId)

* feat: integrated new vector index with the `database import` command

* Supported lsmvector in `vectorNeighbors()` sql function

* Upgraded to jvector 4.0.0-rc.6

* Update LSMVectorIndexCompacted.java

fix: error after compaction

* Fix ComparableVector Comparable contract violation (#2817)

* Initial plan

* Fix ComparableVector to maintain Comparable contract

Co-authored-by: lvca <[email protected]>

---------

Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Co-authored-by: lvca <[email protected]>

* Apply suggestion from @Copilot

Co-authored-by: Copilot <[email protected]>

* Return similarity scores from LSMVectorIndex to avoid redundant distance recalculation (#2820)

* Initial plan

* Add findNeighborsFromVector method to LSMVectorIndex to return scores directly

Co-authored-by: lvca <[email protected]>

* Remove test artifacts and update .gitignore

Co-authored-by: lvca <[email protected]>

---------

Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Co-authored-by: lvca <[email protected]>

* Add mutable flag to vector index pages for safe compaction (#2819)

* Initial plan

* Add mutable byte indicator to vector index pages

- Added mutable flag byte at offset 8 in page header (after offsetFreeContent and numberOfEntries)
- New pages are created with mutable=1 (actively being written to)
- Pages are marked as immutable (mutable=0) when they become full and a new page is created
- Updated findLastImmutablePage() to scan from end backwards and stop at first immutable page
- Updated all page reading/writing code to account for the mutable byte in header
- All vector index tests passing

Co-authored-by: lvca <[email protected]>

* Remove test database files and update .gitignore

Co-authored-by: lvca <[email protected]>

* Add constants for page header offsets to improve maintainability

- Added OFFSET_FREE_CONTENT, OFFSET_NUM_ENTRIES, OFFSET_MUTABLE, and HEADER_BASE_SIZE constants
- Replaced magic numbers throughout the code with named constants
- Makes the code more maintainable and self-documenting

Co-authored-by: lvca <[email protected]>

* Update comments to reference constants instead of hardcoded offsets

Co-authored-by: lvca <[email protected]>

---------

Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Co-authored-by: lvca <[email protected]>
Co-authored-by: Luca Garulli <[email protected]>

* Make LSMVectorIndex ID property configurable (#2818)

* Initial plan

* Make ID property configurable in LSMVectorIndex

Co-authored-by: lvca <[email protected]>

* Remove test database files and add to .gitignore

Co-authored-by: lvca <[email protected]>

* Improve documentation for metadata JSON configuration

Co-authored-by: lvca <[email protected]>

---------

Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Co-authored-by: lvca <[email protected]>
Co-authored-by: Luca Garulli <[email protected]>

* fix pre-commit

* Update engine/src/main/java/com/arcadedb/database/TransactionIndexContext.java

Co-authored-by: Copilot <[email protected]>

* Fixed compaction

* test: fixed test

---------

Co-authored-by: Copilot <[email protected]>
Co-authored-by: lvca <[email protected]>
Co-authored-by: Copilot <[email protected]>
Co-authored-by: Roberto Franchini <[email protected]>

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants