|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +// |
| 3 | +// The OpenSearch Contributors require contributions made to |
| 4 | +// this file be licensed under the Apache-2.0 license or a |
| 5 | +// compatible open source license. |
| 6 | +// |
| 7 | +// Modifications Copyright OpenSearch Contributors. See |
| 8 | +// GitHub history for details. |
| 9 | + |
| 10 | +/** |
| 11 | + * This file contains classes for index operations which are free of JNI |
| 12 | + */ |
| 13 | + |
| 14 | +#ifndef OPENSEARCH_KNN_FAISS_INDEX_SERVICE_H |
| 15 | +#define OPENSEARCH_KNN_FAISS_INDEX_SERVICE_H |
| 16 | + |
| 17 | +#include <jni.h> |
| 18 | +#include "faiss/MetricType.h" |
| 19 | +#include "jni_util.h" |
| 20 | +#include "faiss_methods.h" |
| 21 | +#include <memory> |
| 22 | + |
| 23 | +namespace knn_jni { |
| 24 | +namespace faiss_wrapper { |
| 25 | + |
| 26 | + |
| 27 | +/** |
| 28 | + * A class to provide operations on index |
| 29 | + * This class should evolve to have only cpp object but not jni object |
| 30 | + */ |
| 31 | +class IndexService { |
| 32 | +public: |
| 33 | + IndexService(std::unique_ptr<FaissMethods> faissMethods); |
| 34 | + //TODO Remove dependency on JNIUtilInterface and JNIEnv |
| 35 | + //TODO Reduce the number of parameters |
| 36 | + |
| 37 | + /** |
| 38 | + * Create index |
| 39 | + * |
| 40 | + * @param jniUtil jni util |
| 41 | + * @param env jni environment |
| 42 | + * @param metric space type for distance calculation |
| 43 | + * @param indexDescription index description to be used by faiss index factory |
| 44 | + * @param dim dimension of vectors |
| 45 | + * @param numIds number of vectors |
| 46 | + * @param threadCount number of thread count to be used while adding data |
| 47 | + * @param vectorsAddress memory address which is holding vector data |
| 48 | + * @param ids a list of document ids for corresponding vectors |
| 49 | + * @param indexPath path to write index |
| 50 | + * @param parameters parameters to be applied to faiss index |
| 51 | + */ |
| 52 | + virtual void createIndex( |
| 53 | + knn_jni::JNIUtilInterface * jniUtil, |
| 54 | + JNIEnv * env, |
| 55 | + faiss::MetricType metric, |
| 56 | + std::string indexDescription, |
| 57 | + int dim, |
| 58 | + int numIds, |
| 59 | + int threadCount, |
| 60 | + int64_t vectorsAddress, |
| 61 | + std::vector<int64_t> ids, |
| 62 | + std::string indexPath, |
| 63 | + std::unordered_map<std::string, jobject> parameters); |
| 64 | + virtual ~IndexService() = default; |
| 65 | +protected: |
| 66 | + std::unique_ptr<FaissMethods> faissMethods; |
| 67 | +}; |
| 68 | + |
| 69 | +/** |
| 70 | + * A class to provide operations on index |
| 71 | + * This class should evolve to have only cpp object but not jni object |
| 72 | + */ |
| 73 | +class BinaryIndexService : public IndexService { |
| 74 | +public: |
| 75 | + //TODO Remove dependency on JNIUtilInterface and JNIEnv |
| 76 | + //TODO Reduce the number of parameters |
| 77 | + BinaryIndexService(std::unique_ptr<FaissMethods> faissMethods); |
| 78 | + /** |
| 79 | + * Create binary index |
| 80 | + * |
| 81 | + * @param jniUtil jni util |
| 82 | + * @param env jni environment |
| 83 | + * @param metric space type for distance calculation |
| 84 | + * @param indexDescription index description to be used by faiss index factory |
| 85 | + * @param dim dimension of vectors |
| 86 | + * @param numIds number of vectors |
| 87 | + * @param threadCount number of thread count to be used while adding data |
| 88 | + * @param vectorsAddress memory address which is holding vector data |
| 89 | + * @param ids a list of document ids for corresponding vectors |
| 90 | + * @param indexPath path to write index |
| 91 | + * @param parameters parameters to be applied to faiss index |
| 92 | + */ |
| 93 | + virtual void createIndex( |
| 94 | + knn_jni::JNIUtilInterface * jniUtil, |
| 95 | + JNIEnv * env, |
| 96 | + faiss::MetricType metric, |
| 97 | + std::string indexDescription, |
| 98 | + int dim, |
| 99 | + int numIds, |
| 100 | + int threadCount, |
| 101 | + int64_t vectorsAddress, |
| 102 | + std::vector<int64_t> ids, |
| 103 | + std::string indexPath, |
| 104 | + std::unordered_map<std::string, jobject> parameters |
| 105 | + ) override; |
| 106 | + virtual ~BinaryIndexService() = default; |
| 107 | +}; |
| 108 | + |
| 109 | +} |
| 110 | +} |
| 111 | + |
| 112 | + |
| 113 | +#endif //OPENSEARCH_KNN_FAISS_INDEX_SERVICE_H |
0 commit comments