16
16
17
17
#include < jni.h>
18
18
#include " faiss/MetricType.h"
19
+ #include " faiss/impl/io.h"
19
20
#include " jni_util.h"
20
21
#include " faiss_methods.h"
22
+ #include " faiss_stream_support.h"
21
23
#include < memory>
22
24
23
25
namespace knn_jni {
@@ -30,7 +32,8 @@ namespace faiss_wrapper {
30
32
*/
31
33
class IndexService {
32
34
public:
33
- IndexService (std::unique_ptr<FaissMethods> faissMethods);
35
+ explicit IndexService (std::unique_ptr<FaissMethods> faissMethods);
36
+
34
37
/* *
35
38
* Initialize index
36
39
*
@@ -45,6 +48,7 @@ class IndexService {
45
48
* @return memory address of the native index object
46
49
*/
47
50
virtual jlong initIndex (knn_jni::JNIUtilInterface *jniUtil, JNIEnv *env, faiss::MetricType metric, std::string indexDescription, int dim, int numVectors, int threadCount, std::unordered_map<std::string, jobject> parameters);
51
+
48
52
/* *
49
53
* Add vectors to index
50
54
*
@@ -55,29 +59,34 @@ class IndexService {
55
59
* @param idMapAddress memory address of the native index object
56
60
*/
57
61
virtual void insertToIndex (int dim, int numIds, int threadCount, int64_t vectorsAddress, std::vector<int64_t > &ids, jlong idMapAddress);
62
+
58
63
/* *
59
64
* Write index to disk
60
65
*
61
- * @param threadCount number of thread count to be used while adding data
62
- * @param indexPath path to write index
63
- * @param idMap memory address of the native index object
66
+ * @param writer IOWriter implementation doing IO processing.
67
+ * In most cases, it is expected to have underlying Lucene's IndexOuptut.
68
+ * @param idMapAddress memory address of the native index object
64
69
*/
65
- virtual void writeIndex (std::string indexPath, jlong idMapAddress);
70
+ virtual void writeIndex (faiss::IOWriter* writer, jlong idMapAddress);
71
+
66
72
virtual ~IndexService () = default ;
73
+
67
74
protected:
68
75
virtual void allocIndex (faiss::Index * index, size_t dim, size_t numVectors);
76
+
69
77
std::unique_ptr<FaissMethods> faissMethods;
70
- };
78
+ }; // class IndexService
71
79
72
80
/* *
73
81
* A class to provide operations on index
74
82
* This class should evolve to have only cpp object but not jni object
75
83
*/
76
- class BinaryIndexService : public IndexService {
84
+ class BinaryIndexService final : public IndexService {
77
85
public:
78
86
// TODO Remove dependency on JNIUtilInterface and JNIEnv
79
87
// TODO Reduce the number of parameters
80
- BinaryIndexService (std::unique_ptr<FaissMethods> faissMethods);
88
+ explicit BinaryIndexService (std::unique_ptr<FaissMethods> faissMethods);
89
+
81
90
/* *
82
91
* Initialize index
83
92
*
@@ -91,7 +100,8 @@ class BinaryIndexService : public IndexService {
91
100
* @param parameters parameters to be applied to faiss index
92
101
* @return memory address of the native index object
93
102
*/
94
- virtual jlong initIndex (knn_jni::JNIUtilInterface *jniUtil, JNIEnv *env, faiss::MetricType metric, std::string indexDescription, int dim, int numVectors, int threadCount, std::unordered_map<std::string, jobject> parameters) override ;
103
+ jlong initIndex (knn_jni::JNIUtilInterface *jniUtil, JNIEnv *env, faiss::MetricType metric, std::string indexDescription, int dim, int numVectors, int threadCount, std::unordered_map<std::string, jobject> parameters) final ;
104
+
95
105
/* *
96
106
* Add vectors to index
97
107
*
@@ -106,7 +116,8 @@ class BinaryIndexService : public IndexService {
106
116
* @param idMap a map of document id and vector id
107
117
* @param parameters parameters to be applied to faiss index
108
118
*/
109
- virtual void insertToIndex (int dim, int numIds, int threadCount, int64_t vectorsAddress, std::vector<int64_t > &ids, jlong idMapAddress) override ;
119
+ void insertToIndex (int dim, int numIds, int threadCount, int64_t vectorsAddress, std::vector<int64_t > &ids, jlong idMapAddress) final ;
120
+
110
121
/* *
111
122
* Write index to disk
112
123
*
@@ -119,23 +130,23 @@ class BinaryIndexService : public IndexService {
119
130
* @param idMap a map of document id and vector id
120
131
* @param parameters parameters to be applied to faiss index
121
132
*/
122
- virtual void writeIndex (std::string indexPath , jlong idMapAddress) override ;
123
- virtual ~BinaryIndexService () = default ;
133
+ void writeIndex (faiss::IOWriter* writer , jlong idMapAddress) final ;
134
+
124
135
protected:
125
- virtual void allocIndex (faiss::Index * index, size_t dim, size_t numVectors) override ;
126
- };
136
+ void allocIndex (faiss::Index * index, size_t dim, size_t numVectors) final ;
137
+ }; // class BinaryIndexService
127
138
128
139
/* *
129
140
* A class to provide operations on index
130
141
* This class should evolve to have only cpp object but not jni object
131
142
*/
132
- class ByteIndexService : public IndexService {
143
+ class ByteIndexService final : public IndexService {
133
144
public:
134
145
// TODO Remove dependency on JNIUtilInterface and JNIEnv
135
146
// TODO Reduce the number of parameters
136
- ByteIndexService (std::unique_ptr<FaissMethods> faissMethods);
147
+ explicit ByteIndexService (std::unique_ptr<FaissMethods> faissMethods);
137
148
138
- /* *
149
+ /* *
139
150
* Initialize index
140
151
*
141
152
* @param jniUtil jni util
@@ -148,7 +159,8 @@ class ByteIndexService : public IndexService {
148
159
* @param parameters parameters to be applied to faiss index
149
160
* @return memory address of the native index object
150
161
*/
151
- virtual jlong initIndex (knn_jni::JNIUtilInterface *jniUtil, JNIEnv *env, faiss::MetricType metric, std::string indexDescription, int dim, int numVectors, int threadCount, std::unordered_map<std::string, jobject> parameters) override ;
162
+ jlong initIndex (knn_jni::JNIUtilInterface *jniUtil, JNIEnv *env, faiss::MetricType metric, std::string indexDescription, int dim, int numVectors, int threadCount, std::unordered_map<std::string, jobject> parameters) final ;
163
+
152
164
/* *
153
165
* Add vectors to index
154
166
*
@@ -163,7 +175,8 @@ class ByteIndexService : public IndexService {
163
175
* @param idMap a map of document id and vector id
164
176
* @param parameters parameters to be applied to faiss index
165
177
*/
166
- virtual void insertToIndex (int dim, int numIds, int threadCount, int64_t vectorsAddress, std::vector<int64_t > &ids, jlong idMapAddress) override ;
178
+ void insertToIndex (int dim, int numIds, int threadCount, int64_t vectorsAddress, std::vector<int64_t > &ids, jlong idMapAddress) final ;
179
+
167
180
/* *
168
181
* Write index to disk
169
182
*
@@ -176,14 +189,14 @@ class ByteIndexService : public IndexService {
176
189
* @param idMap a map of document id and vector id
177
190
* @param parameters parameters to be applied to faiss index
178
191
*/
179
- virtual void writeIndex (std::string indexPath , jlong idMapAddress) override ;
180
- virtual ~ByteIndexService () = default ;
181
- protected:
182
- virtual void allocIndex (faiss::Index * index, size_t dim, size_t numVectors) override ;
183
- };
192
+ void writeIndex (faiss::IOWriter* writer , jlong idMapAddress) final ;
193
+
194
+ protected:
195
+ void allocIndex (faiss::Index * index, size_t dim, size_t numVectors) final ;
196
+ }; // class ByteIndexService
184
197
185
198
}
186
199
}
187
200
188
201
189
- #endif // OPENSEARCH_KNN_FAISS_INDEX_SERVICE_H
202
+ #endif // OPENSEARCH_KNN_FAISS_INDEX_SERVICE_H
0 commit comments