Skip to content

Commit

Permalink
covering operations without intializing the index
Browse files Browse the repository at this point in the history
  • Loading branch information
Hussama Ismail committed Sep 10, 2020
1 parent 004c727 commit 4e02b91
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 18 deletions.
21 changes: 12 additions & 9 deletions bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@
#define RESULT_ONCE_INDEX_IS_CLEARED_IT_CANNOT_BE_REUSED 5
#define RESULT_GET_DATA_FAILED 6
#define RESULT_ID_NOT_IN_INDEX 7
#define RESULT_INDEX_NOT_INITIALIZED 8

#define TRY_CATCH_RETURN_INT_BLOCK(block) if (index_cleared) return RESULT_ONCE_INDEX_IS_CLEARED_IT_CANNOT_BE_REUSED; int result_code = RESULT_SUCCESSFUL; try { block } catch (...) { result_code = RESULT_EXCEPTION_THROWN; }; return result_code;
#define TRY_CATCH_NO_INITIALIZE_CHECK_AND_RETURN_INT_BLOCK(block) if (index_cleared) return RESULT_ONCE_INDEX_IS_CLEARED_IT_CANNOT_BE_REUSED; int result_code = RESULT_SUCCESSFUL; try { block } catch (...) { result_code = RESULT_EXCEPTION_THROWN; }; return result_code;
#define TRY_CATCH_RETURN_INT_BLOCK(block) if (!index_initialized) return RESULT_INDEX_NOT_INITIALIZED; TRY_CATCH_NO_INITIALIZE_CHECK_AND_RETURN_INT_BLOCK(block)

template<typename dist_t, typename data_t=float>
class Index {
Expand All @@ -53,7 +55,7 @@ class Index {
}

int init_new_index(const size_t maxElements, const size_t M, const size_t efConstruction, const size_t random_seed) {
TRY_CATCH_RETURN_INT_BLOCK({
TRY_CATCH_NO_INITIALIZE_CHECK_AND_RETURN_INT_BLOCK({
if (appr_alg) {
return RESULT_INDEX_ALREADY_INITIALIZED;
}
Expand Down Expand Up @@ -84,7 +86,7 @@ class Index {
}

int load_index(const std::string &path_to_index, size_t max_elements) {
TRY_CATCH_RETURN_INT_BLOCK({
TRY_CATCH_NO_INITIALIZE_CHECK_AND_RETURN_INT_BLOCK({
if (appr_alg) {
std::cerr << "Warning: Calling load_index for an already initialized index. Old index is being deallocated.";
delete appr_alg;
Expand Down Expand Up @@ -145,7 +147,7 @@ class Index {
}

float compute_similarity(float* vector1, float* vector2) {
return (appr_alg -> fstdistfunc_(vector1, vector2, (appr_alg -> dist_func_param_)));
return (appr_alg->fstdistfunc_(vector1, vector2, (appr_alg -> dist_func_param_)));
}

int knn_query(float* input, bool input_normalized, int k, int* indices /* output */, float* coefficients /* output */) {
Expand Down Expand Up @@ -183,7 +185,7 @@ class Index {
}

int clear_index() {
TRY_CATCH_RETURN_INT_BLOCK({
TRY_CATCH_NO_INITIALIZE_CHECK_AND_RETURN_INT_BLOCK({
delete l2space;
if (appr_alg)
delete appr_alg;
Expand Down Expand Up @@ -217,7 +219,7 @@ EXTERN_C DLLEXPORT Index<float>* createNewIndex(char* spaceName, int dimension){
}

EXTERN_C DLLEXPORT int initNewIndex(Index<float>* index, int maxNumberOfElements, int M = 16, int efConstruction = 200, int randomSeed = 100) {
return index->init_new_index(maxNumberOfElements, M, efConstruction, randomSeed);
return index->init_new_index(maxNumberOfElements, M, efConstruction, randomSeed);
}

EXTERN_C DLLEXPORT int addItemToIndex(float* item, int normalized, int label, Index<float>* index) {
Expand Down Expand Up @@ -255,15 +257,16 @@ EXTERN_C DLLEXPORT int setEf(Index<float>* index, int ef) {
}

EXTERN_C DLLEXPORT int getData(Index<float>* index, int id, float* vector, int dim) {
return index-> getDataById(id, vector, dim);

return index->getDataById(id, vector, dim);
}

EXTERN_C DLLEXPORT int hasId(Index<float>* index, int id) {
return index-> hasId(id);
return index->hasId(id);
}

EXTERN_C DLLEXPORT float computeSimilarity(Index<float>* index, float* vector1, float* vector2) {
return index -> compute_similarity(vector1, vector2);
return index->compute_similarity(vector1, vector2);
}

int main(){
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.stepstone.search.hnswlib.jna;

import com.stepstone.search.hnswlib.jna.exception.IndexAlreadyInitializedException;
import com.stepstone.search.hnswlib.jna.exception.IndexNotInitializedException;
import com.stepstone.search.hnswlib.jna.exception.ItemCannotBeInsertedIntoTheVectorSpaceException;
import com.stepstone.search.hnswlib.jna.exception.OnceIndexIsClearedItCannotBeReusedException;
import com.stepstone.search.hnswlib.jna.exception.QueryCannotReturnResultsException;
Expand All @@ -27,7 +28,7 @@ public class Index {
private static final int RESULT_QUERY_NO_RESULTS = 3;
private static final int RESULT_ITEM_CANNOT_BE_INSERTED_INTO_THE_VECTOR_SPACE = 4;
private static final int RESULT_ONCE_INDEX_IS_CLEARED_IT_CANNOT_BE_REUSED = 5;
private static final int RESULT_GET_DATA_FAILED = 6;
private static final int RESULT_INDEX_NOT_INITIALIZED = 8;

private static Hnswlib hnswlib = HnswlibFactory.getInstance();

Expand Down Expand Up @@ -243,6 +244,8 @@ private void checkResultCode(int resultCode) {
throw new ItemCannotBeInsertedIntoTheVectorSpaceException();
case RESULT_ONCE_INDEX_IS_CLEARED_IT_CANNOT_BE_REUSED:
throw new OnceIndexIsClearedItCannotBeReusedException();
case RESULT_INDEX_NOT_INITIALIZED:
throw new IndexNotInitializedException();
default:
throw new UnexpectedNativeException();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.stepstone.search.hnswlib.jna.exception;

/**
* Exception thrown when the index reference is not initialized on the native side.
* (the method initialize() is not called after the object instantiation)
*/
public class IndexNotInitializedException extends UnexpectedNativeException {
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.stepstone.search.hnswlib.jna;

import com.stepstone.search.hnswlib.jna.exception.IndexAlreadyInitializedException;
import com.stepstone.search.hnswlib.jna.exception.IndexNotInitializedException;
import com.stepstone.search.hnswlib.jna.exception.ItemCannotBeInsertedIntoTheVectorSpaceException;
import com.stepstone.search.hnswlib.jna.exception.OnceIndexIsClearedItCannotBeReusedException;
import com.stepstone.search.hnswlib.jna.exception.QueryCannotReturnResultsException;
Expand Down Expand Up @@ -424,8 +425,32 @@ public void testGetDataWhenIndexCleared() {
index.initialize();
index.clear();
assertFalse(index.hasId(1202));
// Index index2 = createIndexInstance(SpaceName.COSINE, 3);
// assertFalse(index2.hasId(1202));
Index index2 = createIndexInstance(SpaceName.COSINE, 1);
assertFalse(index2.hasId(123));
}

@Test(expected = IndexNotInitializedException.class)
public void testUseAddItemIndexWithoutInitialize() {
Index index = createIndexInstance(SpaceName.COSINE, 1);
index.addItem(new float[1]);
}

@Test(expected = IndexNotInitializedException.class)
public void testUseAddNormalizedItemIndexWithoutInitialize() {
Index index = createIndexInstance(SpaceName.COSINE, 1);
index.addNormalizedItem(new float[1]);
}

@Test(expected = IndexNotInitializedException.class)
public void testUseKnnQueryIndexWithoutInitialize() {
Index index = createIndexInstance(SpaceName.COSINE, 1);
index.knnQuery(new float[1],1);
}

@Test(expected = IndexNotInitializedException.class)
public void testUseKnnNormalizedQueryQueryIndexWithoutInitialize() {
Index index = createIndexInstance(SpaceName.COSINE, 1);
index.knnNormalizedQuery(new float[1],1);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,9 @@
import com.stepstone.search.hnswlib.jna.exception.UnexpectedNativeException;
import org.junit.Test;

import java.util.Arrays;
import java.util.Optional;

import static org.hamcrest.CoreMatchers.instanceOf;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;

public class IndexTest extends AbstractIndexTest {

Expand Down Expand Up @@ -56,4 +50,5 @@ public void testComputeSimilarity() {
// both values are minus, so the closer one should be closer to zero than the farther one
assertEquals(Float.compare(similarityClose, similarityFar), 1);
}

}

0 comments on commit 4e02b91

Please sign in to comment.