-
Notifications
You must be signed in to change notification settings - Fork 35
Fix ArrayIndexOutOfBoundsException in CompositeBitmapIndex null handling #624
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
hrstoyanov
wants to merge
4
commits into
eclipse-store:main
Choose a base branch
from
hrstoyanov:fix/composite-index-null-handling
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
207 changes: 207 additions & 0 deletions
207
...gamap/src/test/java/org/eclipse/store/gigamap/indexer/CompositeIndexNullHandlingTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,207 @@ | ||
| package org.eclipse.store.gigamap.indexer; | ||
|
|
||
| /*- | ||
| * #%L | ||
| * EclipseStore GigaMap | ||
| * %% | ||
| * Copyright (C) 2023 - 2026 MicroStream Software | ||
| * %% | ||
| * This program and the accompanying materials are made | ||
| * available under the terms of the Eclipse Public License 2.0 | ||
| * which is available at https://www.eclipse.org/legal/epl-2.0/ | ||
| * | ||
| * SPDX-License-Identifier: EPL-2.0 | ||
| * #L% | ||
| */ | ||
|
|
||
|
|
||
| import org.eclipse.store.gigamap.types.GigaMap; | ||
| import org.eclipse.store.gigamap.types.IndexerInstant; | ||
| import org.eclipse.store.storage.embedded.types.EmbeddedStorage; | ||
| import org.eclipse.store.storage.embedded.types.EmbeddedStorageManager; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.api.io.TempDir; | ||
|
|
||
| import java.nio.file.Path; | ||
| import java.time.Instant; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.*; | ||
|
|
||
| /** | ||
| * Test for GitHub issue: ArrayIndexOutOfBoundsException in SubBitmapIndexHashing | ||
| * when updating entity from null to non-null composite key value. | ||
| * <p> | ||
| * Root cause: {@code AbstractCompositeBitmapIndex.internalHandleChanged()} called | ||
| * {@code ensureSubIndices(newKeys)} which grew the sub-index array (e.g. from 1 to 6), | ||
| * then iterated <strong>all</strong> sub-indices calling | ||
| * {@code subIndex.internalHandleChanged(oldKeys, ...)}. | ||
| * Each sub-index called {@code internalLookupEntry(oldKeys)} → {@code indexEntity(oldKeys)} → | ||
| * {@code index(oldKeys)} → {@code oldKeys[this.position]}. | ||
| * When {@code oldKeys} was the {@code NULL()} sentinel (length 1) but | ||
| * {@code this.position >= 1}, it threw {@code ArrayIndexOutOfBoundsException}. | ||
| * <p> | ||
| * Fix: {@code internalHandleChanged} now checks {@code isEmpty(oldKeys, i)} and | ||
| * {@code isEmpty(newKeys, i)} per sub-index, dispatching to {@code internalAddToEntry} | ||
| * or {@code internalRemove} instead of unconditionally calling {@code internalHandleChanged}. | ||
| */ | ||
| public class CompositeIndexNullHandlingTest | ||
| { | ||
|
|
||
| @TempDir | ||
| Path tempDir; | ||
|
|
||
| /** | ||
| * Entity with an optional {@link Instant} field — mirrors real-world scenario | ||
| * like Booking with nullable {@code expiresAt}, {@code billingReportedAt}, etc. | ||
| */ | ||
| static final class Event | ||
| { | ||
| final long id; | ||
| Instant timestamp; // initially null, set on update | ||
|
|
||
| Event(final long id) | ||
| { | ||
| this.id = id; | ||
| this.timestamp = null; | ||
| } | ||
| } | ||
|
|
||
| static final class EventIndexer extends IndexerInstant.Abstract<Event> | ||
| { | ||
| @Override | ||
| protected Instant getInstant(final Event entity) | ||
| { | ||
| return entity.timestamp; | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| void nullToNonNullCompositeKeyUpdateShouldWork() | ||
| { | ||
| final GigaMap<Event> map = GigaMap.New(); | ||
| final EventIndexer indexer = new EventIndexer(); | ||
| map.index().bitmap().ensure(indexer); | ||
|
|
||
| // Add entity with null timestamp → composite key = NULL() = Object[1] | ||
| final Event event = new Event(1L); | ||
| map.add(event); | ||
| assertEquals(1, map.size()); | ||
|
|
||
| // Update to set non-null timestamp → composite key = Object[6] | ||
| // Before fix: ArrayIndexOutOfBoundsException (Index 1 out of bounds for length 1) | ||
| assertDoesNotThrow(() -> | ||
| map.update(event, e -> e.timestamp = Instant.parse("2025-06-15T10:30:00Z")) | ||
| ); | ||
|
|
||
| assertEquals(1, map.size()); | ||
| } | ||
|
|
||
| @Test | ||
| void nonNullToNullCompositeKeyUpdateShouldWork() | ||
| { | ||
| final GigaMap<Event> map = GigaMap.New(); | ||
| final EventIndexer indexer = new EventIndexer(); | ||
| map.index().bitmap().ensure(indexer); | ||
|
|
||
| // Add entity with non-null timestamp → composite key = Object[6] | ||
| final Event event = new Event(1L); | ||
| event.timestamp = Instant.parse("2025-06-15T10:30:00Z"); | ||
| map.add(event); | ||
|
|
||
| // Update to null timestamp → composite key = NULL() = Object[1] | ||
| assertDoesNotThrow(() -> | ||
| map.update(event, e -> e.timestamp = null) | ||
| ); | ||
|
|
||
| assertEquals(1, map.size()); | ||
| } | ||
|
|
||
| @Test | ||
| void nullToNonNullCompositeKeyUpdateWithPersistenceShouldWork() | ||
| { | ||
| final GigaMap<Event> map = GigaMap.New(); | ||
| final EventIndexer indexer = new EventIndexer(); | ||
| map.index().bitmap().ensure(indexer); | ||
|
|
||
| // Add entity with null timestamp | ||
| final Event event = new Event(1L); | ||
| map.add(event); | ||
|
|
||
| try (EmbeddedStorageManager manager = EmbeddedStorage.start(map, this.tempDir)) | ||
| { | ||
| // Update to set non-null timestamp | ||
| assertDoesNotThrow(() -> | ||
| map.update(event, e -> e.timestamp = Instant.parse("2025-06-15T10:30:00Z")) | ||
| ); | ||
|
|
||
| assertEquals(1, map.size()); | ||
| map.store(); | ||
| } | ||
|
|
||
| // Verify persistence | ||
| try (EmbeddedStorageManager manager = EmbeddedStorage.start(this.tempDir)) | ||
| { | ||
| final GigaMap<Event> loadedMap = (GigaMap<Event>) manager.root(); | ||
| assertEquals(1, loadedMap.size()); | ||
|
|
||
| final Event loadedEvent = loadedMap.query().findFirst().get(); | ||
| assertNotNull(loadedEvent.timestamp); | ||
| assertEquals(Instant.parse("2025-06-15T10:30:00Z"), loadedEvent.timestamp); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| void multipleNullToNonNullUpdatesShouldWork() | ||
| { | ||
| final GigaMap<Event> map = GigaMap.New(); | ||
| final EventIndexer indexer = new EventIndexer(); | ||
| map.index().bitmap().ensure(indexer); | ||
|
|
||
| // Add multiple entities with null timestamps | ||
| final Event event1 = new Event(1L); | ||
| final Event event2 = new Event(2L); | ||
| final Event event3 = new Event(3L); | ||
| map.addAll(event1, event2, event3); | ||
| assertEquals(3, map.size()); | ||
|
|
||
| // Update all to non-null timestamps | ||
| assertDoesNotThrow(() -> | ||
| { | ||
| map.update(event1, e -> e.timestamp = Instant.parse("2025-06-15T10:30:00Z")); | ||
| map.update(event2, e -> e.timestamp = Instant.parse("2025-06-15T11:30:00Z")); | ||
| map.update(event3, e -> e.timestamp = Instant.parse("2025-06-15T12:30:00Z")); | ||
| map.update(event1, e -> {}); | ||
| map.update(event2, e -> {}); | ||
| map.update(event3, e -> {}); | ||
| }); | ||
|
|
||
| assertEquals(3, map.size()); | ||
| } | ||
|
|
||
| @Test | ||
| void nullToNonNullQueryShouldWork() | ||
| { | ||
| final GigaMap<Event> map = GigaMap.New(); | ||
| final EventIndexer indexer = new EventIndexer(); | ||
| map.index().bitmap().ensure(indexer); | ||
|
|
||
| // Add entities with mixed null/non-null timestamps | ||
| final Event event1 = new Event(1L); | ||
| final Event event2 = new Event(2L); | ||
| event2.timestamp = Instant.parse("2025-06-15T10:30:00Z"); | ||
| map.addAll(event1, event2); | ||
|
|
||
| // Query for null timestamps | ||
| assertEquals(1, map.query(indexer.is((Instant) null)).count()); | ||
|
|
||
| // Query for non-null timestamps | ||
| assertEquals(1, map.query(indexer.is(Instant.parse("2025-06-15T10:30:00Z"))).count()); | ||
|
|
||
| // Update null to non-null | ||
| map.update(event1, e -> e.timestamp = Instant.parse("2025-06-15T11:30:00Z")); | ||
|
|
||
| // Verify queries after update | ||
| assertEquals(0, map.query(indexer.is((Instant) null)).count()); | ||
| assertEquals(1, map.query(indexer.is((Instant) null).or(indexer.is(Instant.parse("2025-06-15T10:30:00Z")))).count()); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.