Skip to content

Commit

Permalink
Add Google tests for unit testing jni (#36)
Browse files Browse the repository at this point in the history
Signed-off-by: Jack Mazanec <jmazane1@nd.edu>
  • Loading branch information
jmazanec15 committed Oct 22, 2021
1 parent dae7b23 commit b0414b1
Show file tree
Hide file tree
Showing 13 changed files with 1,116 additions and 27 deletions.
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,11 @@ jni/CPack*
jni/_CPack*
jni/release
jni/packages
jni/CTestTestfile.cmake
jni/KNNPlugin_JNI.cbp
jni/Testing/
jni/_deps/
jni/bin/
jni/lib/
jni/jni_test*
jni/googletest*
14 changes: 14 additions & 0 deletions jni/CMakeLists.txt.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
cmake_minimum_required(VERSION 2.8.2)
project(googletest-download NONE)

include(ExternalProject)
ExternalProject_Add(googletest
GIT_REPOSITORY https://github.com/google/googletest.git
GIT_TAG master
SOURCE_DIR "${CMAKE_BINARY_DIR}/googletest-src"
BINARY_DIR "${CMAKE_BINARY_DIR}/googletest-build"
CONFIGURE_COMMAND ""
BUILD_COMMAND ""
INSTALL_COMMAND ""
TEST_COMMAND ""
)
2 changes: 2 additions & 0 deletions jni/include/faiss_wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
#ifndef OPENSEARCH_KNN_FAISS_WRAPPER_H
#define OPENSEARCH_KNN_FAISS_WRAPPER_H

#include "jni_util.h"

#include <jni.h>

namespace knn_jni {
Expand Down
3 changes: 3 additions & 0 deletions jni/include/jni_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,10 @@ namespace knn_jni {

extern const std::string NPROBES;
extern const std::string COARSE_QUANTIZER;
extern const std::string M;
extern const std::string M_NMSLIB;
extern const std::string EF_CONSTRUCTION;
extern const std::string EF_CONSTRUCTION_NMSLIB;
extern const std::string EF_SEARCH;

// --------------------------------------------------------------------------
Expand Down
19 changes: 18 additions & 1 deletion jni/include/nmslib_wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,15 @@
#ifndef OPENSEARCH_KNN_NMSLIB_WRAPPER_H
#define OPENSEARCH_KNN_NMSLIB_WRAPPER_H

#include <jni.h>
#include "jni_util.h"

#include "methodfactory.h"
#include "space.h"
#include "spacefactory.h"

#include <jni.h>
#include <string>

namespace knn_jni {
namespace nmslib_wrapper {
// Create an index with ids and vectors. The configuration is defined by values in the Java map, parametersJ.
Expand All @@ -38,6 +44,17 @@ namespace knn_jni {

// Perform required initialization operations for the library
void InitLibrary();

struct IndexWrapper {
explicit IndexWrapper(const std::string& spaceType) {
// Index gets constructed with a reference to data (see above) but is otherwise unused
similarity::ObjectVector data;
space.reset(similarity::SpaceFactoryRegistry<float>::Instance().CreateSpace(spaceType, similarity::AnyParams()));
index.reset(similarity::MethodFactoryRegistry<float>::Instance().CreateMethod(false, "hnsw", spaceType, *space, data));
}
std::unique_ptr<similarity::Space<float>> space;
std::unique_ptr<similarity::Index<float>> index;
};
}
}

Expand Down
6 changes: 3 additions & 3 deletions jni/src/faiss_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@
#include "jni_util.h"
#include "faiss_wrapper.h"

#include "faiss/impl/io.h"
#include "faiss/index_factory.h"
#include "faiss/MetaIndexes.h"
#include "faiss/index_io.h"
#include "faiss/IndexHNSW.h"
#include "faiss/IndexIVFFlat.h"
#include "faiss/MetaIndexes.h"

#include <algorithm>
#include <jni.h>
#include <vector>
#include <string>
#include <faiss/impl/io.h>
#include <vector>


// Translate space type to faiss metric
Expand Down
4 changes: 4 additions & 0 deletions jni/src/jni_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -413,5 +413,9 @@ const std::string knn_jni::NEG_DOT_PRODUCT = "negdotprod";

const std::string knn_jni::NPROBES = "nprobes";
const std::string knn_jni::COARSE_QUANTIZER = "coarse_quantizer";
const std::string knn_jni::M = "m";
const std::string knn_jni::M_NMSLIB = "M";
const std::string knn_jni::EF_CONSTRUCTION = "ef_construction";
const std::string knn_jni::EF_CONSTRUCTION_NMSLIB = "efConstruction";
const std::string knn_jni::EF_SEARCH = "ef_search";

35 changes: 12 additions & 23 deletions jni/src/nmslib_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@
#include "jni_util.h"
#include "nmslib_wrapper.h"

#include <jni.h>
#include <string>

#include "init.h"
#include "index.h"
#include "params.h"
Expand All @@ -24,17 +21,9 @@
#include "spacefactory.h"
#include "space.h"

#include <jni.h>
#include <string>

struct IndexWrapper {
explicit IndexWrapper(const string& spaceType) {
// Index gets constructed with a reference to data (see above) but is otherwise unused
similarity::ObjectVector data;
space.reset(similarity::SpaceFactoryRegistry<float>::Instance().CreateSpace(spaceType, similarity::AnyParams()));
index.reset(similarity::MethodFactoryRegistry<float>::Instance().CreateMethod(false, "hnsw", spaceType, *space, data));
}
std::unique_ptr<similarity::Space<float>> space;
std::unique_ptr<similarity::Index<float>> index;
};

std::string TranslateSpaceType(const std::string& spaceType);

Expand All @@ -60,14 +49,14 @@ void knn_jni::nmslib_wrapper::CreateIndex(knn_jni::JNIUtilInterface * jniUtil, J
// Handle parameters
auto parametersCpp = jniUtil->ConvertJavaMapToCppMap(env, parametersJ);
std::vector<std::string> indexParameters;
if(parametersCpp.find("ef_construction") != parametersCpp.end()) {
auto efConstruction = jniUtil->ConvertJavaObjectToCppInteger(env, parametersCpp["ef_construction"]);
indexParameters.push_back("efConstruction=" + std::to_string(efConstruction));
if(parametersCpp.find(knn_jni::EF_CONSTRUCTION) != parametersCpp.end()) {
auto efConstruction = jniUtil->ConvertJavaObjectToCppInteger(env, parametersCpp[knn_jni::EF_CONSTRUCTION]);
indexParameters.push_back(knn_jni::EF_CONSTRUCTION_NMSLIB + "=" + std::to_string(efConstruction));
}

if(parametersCpp.find("m") != parametersCpp.end()) {
auto m = jniUtil->ConvertJavaObjectToCppInteger(env, parametersCpp["m"]);
indexParameters.push_back("M=" + std::to_string(m));
if(parametersCpp.find(knn_jni::M) != parametersCpp.end()) {
auto m = jniUtil->ConvertJavaObjectToCppInteger(env, parametersCpp[knn_jni::M]);
indexParameters.push_back(knn_jni::M_NMSLIB + "=" + std::to_string(m));
}
jniUtil->DeleteLocalRef(env, parametersJ);

Expand Down Expand Up @@ -160,9 +149,9 @@ jlong knn_jni::nmslib_wrapper::LoadIndex(knn_jni::JNIUtilInterface * jniUtil, JN
}

// Load index
IndexWrapper * indexWrapper;
knn_jni::nmslib_wrapper::IndexWrapper * indexWrapper;
try {
indexWrapper = new IndexWrapper(spaceTypeCpp);
indexWrapper = new knn_jni::nmslib_wrapper::IndexWrapper(spaceTypeCpp);
indexWrapper->index->LoadIndex(indexPathCpp);
indexWrapper->index->SetQueryTimeParams(similarity::AnyParams(queryParams));
} catch (...) {
Expand All @@ -184,7 +173,7 @@ jobjectArray knn_jni::nmslib_wrapper::QueryIndex(knn_jni::JNIUtilInterface * jni
throw std::runtime_error("Invalid pointer to index");
}

auto *indexWrapper = reinterpret_cast<IndexWrapper*>(indexPointerJ);
auto *indexWrapper = reinterpret_cast<knn_jni::nmslib_wrapper::IndexWrapper*>(indexPointerJ);

int dim = jniUtil->GetJavaFloatArrayLength(env, queryVectorJ);

Expand Down Expand Up @@ -223,7 +212,7 @@ jobjectArray knn_jni::nmslib_wrapper::QueryIndex(knn_jni::JNIUtilInterface * jni
}

void knn_jni::nmslib_wrapper::Free(jlong indexPointerJ) {
auto *indexWrapper = reinterpret_cast<IndexWrapper*>(indexPointerJ);
auto *indexWrapper = reinterpret_cast<knn_jni::nmslib_wrapper::IndexWrapper*>(indexPointerJ);
delete indexWrapper;
}

Expand Down
Loading

0 comments on commit b0414b1

Please sign in to comment.