Skip to content
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

Resizing of index. #49

Merged
merged 12 commits into from
Mar 26, 2024
21 changes: 21 additions & 0 deletions java/src/main/java/com/spotify/voyager/jni/StringIndex.java
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,27 @@ public void close() throws IOException {
index.close();
}

/**
* Change the maximum number of elements currently storable by this {@link Index}. This operation
* reallocates the memory used by the index and can be quite slow, so it may be useful to set the
* maximum number of elements in advance if that number is known.
*
* @param newSize The new number of maximum elements to resize this {@link Index} to.
*/
public void resizeIndex(long newSize) {
index.resizeIndex(newSize);
}

/**
* Get the maximum number of elements currently storable by this {@link Index}. If more elements
* are added than {@code getMaxElements()}, the index will be automatically (but slowly) resized.
*
* @return The number of elements (vectors) that are currently storable in this {@link Index}.
*/
public long getMaxElements() {
return index.getMaxElements();
}

/** A wrapper class for nearest neighbor query results. */
public static class QueryResults {
private final String[] names;
Expand Down
5 changes: 5 additions & 0 deletions java/src/test/java/com/spotify/voyager/jni/IndexTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,11 @@ private void runTestWith(

// And the resulting call should pass:
assertNotNull(index.getVector(0));

// Test resizing of index
long currentSize = index.getMaxElements();
index.resizeIndex(currentSize + 1);
assertEquals(currentSize + 1, index.getMaxElements());
}
}
}
22 changes: 22 additions & 0 deletions java/src/test/java/com/spotify/voyager/jni/StringIndexTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
package com.spotify.voyager.jni;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals;

import com.google.common.io.Resources;
import com.spotify.voyager.jni.Index.SpaceType;
Expand Down Expand Up @@ -239,6 +240,27 @@ public void itLoadsFromInputStreamNoParams() throws Exception {
}
}

@Test
public void itResizesIndex() throws Exception {
List<Vector> testVectors = TestUtils.getTestVectors();
try (final StringIndex index =
new StringIndex(
SpaceType.Cosine,
testVectors.get(0).vector.length,
20,
testVectors.size(),
0,
testVectors.size(),
StorageDataType.E4M3)) {
for (Vector v : testVectors) {
index.addItem(v.name, v.vector);
}
long currentSize = index.getMaxElements();
index.resizeIndex(currentSize + 1);
assertEquals(currentSize + 1, index.getMaxElements());
}
}

public static class CustomResult {
private final String name;
private final float distance;
Expand Down
Loading