Skip to content

Commit 279afdb

Browse files
authored
[REVIEW][Java] Rename destroyIndex() to close(), extend AutoCloseable (#1252)
The various `Index` classes in Java all have a `destroyIndex` method that must be called in order to free resources associated with that index, when done. Other classes in the repository use `close()` instead, and extend or implement `AutoCloseable`; this have the advantage of support in language (try-with-resources) and tooling (e.g. IntelliJ warnings). Additionally, having a single pattern is better for ease of use and consistency. This PR renames all instances of `destroyIndex` to `close` and make the declaring interfaces extend `AutoCloseable` Authors: - Lorenzo Dematté (https://github.com/ldematte) - MithunR (https://github.com/mythrocks) Approvers: - MithunR (https://github.com/mythrocks) URL: #1252
1 parent e3a6126 commit 279afdb

File tree

15 files changed

+155
-160
lines changed

15 files changed

+155
-160
lines changed

java/benchmarks/src/main/java/com/nvidia/cuvs/CagraIndexBenchmarks.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -121,20 +121,20 @@ public void testIndexingAndSerializeToFile() throws Throwable {
121121
.build();
122122

123123
// Create the index with the dataset
124-
CagraIndex index = CagraIndex.newBuilder(resources)
124+
try (CagraIndex index = CagraIndex.newBuilder(resources)
125125
.withDataset(arrayDataset)
126126
.withIndexParams(indexParams)
127-
.build();
127+
.build()) {
128128

129-
// Saving the index on to the disk.
130-
var indexFilePath = Path.of(UUID.randomUUID() + ".cag");
131-
try (var outputStream = Files.newOutputStream(indexFilePath)) {
129+
// Saving the index on to the disk.
130+
var indexFilePath = Path.of(UUID.randomUUID() + ".cag");
131+
try (var outputStream = Files.newOutputStream(indexFilePath)) {
132132
index.serialize(outputStream);
133-
}
133+
}
134134

135-
// Cleanup
136-
Files.deleteIfExists(indexFilePath);
137-
index.destroyIndex();
135+
// Cleanup
136+
Files.deleteIfExists(indexFilePath);
137+
}
138138
}
139139
}
140140

java/cuvs-java/src/main/java/com/nvidia/cuvs/BruteForceIndex.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,14 @@
2828
*
2929
* @since 25.02
3030
*/
31-
public interface BruteForceIndex {
31+
public interface BruteForceIndex extends AutoCloseable {
3232

3333
/**
3434
* Invokes the native destroy_brute_force_index function to de-allocate
3535
* BRUTEFORCE index
3636
*/
37-
void destroyIndex() throws Throwable;
37+
@Override
38+
void close() throws Exception;
3839

3940
/**
4041
* Invokes the native search_brute_force_index via the Panama API for searching

java/cuvs-java/src/main/java/com/nvidia/cuvs/CagraIndex.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,13 @@
3333
*
3434
* @since 25.02
3535
*/
36-
public interface CagraIndex {
36+
public interface CagraIndex extends AutoCloseable {
3737

3838
/**
3939
* Invokes the native destroy_cagra_index to de-allocate the CAGRA index
4040
*/
41-
void destroyIndex() throws Throwable;
41+
@Override
42+
void close() throws Exception;
4243

4344
/**
4445
* Invokes the native search_cagra_index via the Panama API for searching a

java/cuvs-java/src/main/java/com/nvidia/cuvs/HnswIndex.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,13 @@
2525
*
2626
* @since 25.02
2727
*/
28-
public interface HnswIndex {
28+
public interface HnswIndex extends AutoCloseable {
2929

3030
/**
3131
* Invokes the native destroy_hnsw_index to de-allocate the HNSW index
3232
*/
33-
void destroyIndex() throws Throwable;
33+
@Override
34+
void close() throws Exception;
3435

3536
/**
3637
* Invokes the native search_hnsw_index via the Panama API for searching a HNSW

java/cuvs-java/src/main/java/com/nvidia/cuvs/TieredIndex.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,16 @@
2323
* {@link TieredIndex} encapsulates a Tiered index, along with methods to
2424
* interact with it.
2525
*/
26-
public interface TieredIndex {
26+
public interface TieredIndex extends AutoCloseable {
2727

2828
/**
2929
* Destroys the underlying native TieredIndex object and releases associated
3030
* resources.
3131
*
32-
* @throws Throwable if an error occurs during index destruction
32+
* @throws Exception if an error occurs during index destruction
3333
*/
34-
void destroyIndex() throws Throwable;
34+
@Override
35+
void close() throws Exception;
3536

3637
/**
3738
* Searches the index with the specified query and search parameters.

java/cuvs-java/src/main/java22/com/nvidia/cuvs/internal/BruteForceIndexImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ private void checkNotDestroyed() {
113113
* BRUTEFORCE index
114114
*/
115115
@Override
116-
public void destroyIndex() {
116+
public void close() {
117117
checkNotDestroyed();
118118
try {
119119
int returnValue = cuvsBruteForceIndexDestroy(bruteForceIndexReference.indexPtr);

java/cuvs-java/src/main/java22/com/nvidia/cuvs/internal/CagraIndexImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ private void checkNotDestroyed() {
158158
* Invokes the native destroy_cagra_index to de-allocate the CAGRA index
159159
*/
160160
@Override
161-
public void destroyIndex() throws Throwable {
161+
public void close() {
162162
checkNotDestroyed();
163163
try {
164164
int returnValue = cuvsCagraIndexDestroy(cagraIndexReference.getMemorySegment());

java/cuvs-java/src/main/java22/com/nvidia/cuvs/internal/HnswIndexImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ private HnswIndexImpl(
7474
* Invokes the native destroy_hnsw_index to de-allocate the HNSW index
7575
*/
7676
@Override
77-
public void destroyIndex() {
77+
public void close() {
7878
int returnValue = cuvsHnswIndexDestroy(hnswIndexReference.getMemorySegment());
7979
checkCuVSError(returnValue, "cuvsHnswIndexDestroy");
8080
}

java/cuvs-java/src/main/java22/com/nvidia/cuvs/internal/TieredIndexImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ private void checkNotDestroyed() {
9494
* Invokes the native destroy_tiered_index to de-allocate the Tiered index
9595
*/
9696
@Override
97-
public void destroyIndex() {
97+
public void close() {
9898
checkNotDestroyed();
9999
try {
100100
int returnValue = cuvsTieredIndexDestroy(tieredIndexReference.getMemorySegment());

java/cuvs-java/src/test/java/com/nvidia/cuvs/BruteForceAndSearchIT.java

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -57,37 +57,34 @@ private static void indexAndQueryOnce(
5757
new BruteForceIndexParams.Builder().withNumWriterThreads(32).build();
5858

5959
// Create the index with the dataset
60-
BruteForceIndex index =
60+
try (BruteForceIndex index =
6161
BruteForceIndex.newBuilder(resources)
6262
.withDataset(dataset)
6363
.withIndexParams(indexParams)
64-
.build();
64+
.build()) {
6565

66-
// Saving the index on to the disk.
67-
String indexFileName = UUID.randomUUID() + ".bf";
68-
try (var outputStream = new FileOutputStream(indexFileName)) {
69-
index.serialize(outputStream);
70-
}
71-
72-
// Loading a BRUTEFORCE index from disk.
73-
Path indexFile = Path.of(indexFileName);
74-
try (var inputStream = Files.newInputStream(indexFile)) {
75-
BruteForceIndex loadedIndex = BruteForceIndex.newBuilder(resources).from(inputStream).build();
66+
// Saving the index on to the disk.
67+
String indexFileName = UUID.randomUUID() + ".bf";
68+
try (var outputStream = new FileOutputStream(indexFileName)) {
69+
index.serialize(outputStream);
70+
}
7671

77-
// search the loaded index
78-
SearchResults results = loadedIndex.search(cuvsQuery);
79-
checkResults(expectedResults, results.getResults());
72+
// Loading a BRUTEFORCE index from disk.
73+
Path indexFile = Path.of(indexFileName);
74+
try (var inputStream = Files.newInputStream(indexFile);
75+
BruteForceIndex loadedIndex =
76+
BruteForceIndex.newBuilder(resources).from(inputStream).build()) {
8077

81-
// search the first index
82-
results = index.search(cuvsQuery);
83-
checkResults(expectedResults, results.getResults());
78+
// search the loaded index
79+
SearchResults results = loadedIndex.search(cuvsQuery);
80+
checkResults(expectedResults, results.getResults());
8481

85-
// Cleanup
86-
index.destroyIndex();
87-
loadedIndex.destroyIndex();
82+
// search the first index
83+
results = index.search(cuvsQuery);
84+
checkResults(expectedResults, results.getResults());
85+
}
86+
Files.deleteIfExists(indexFile);
8887
}
89-
90-
Files.deleteIfExists(indexFile);
9188
}
9289

9390
/**

0 commit comments

Comments
 (0)