1
1
package com .aliyun .tair .tairvector ;
2
2
3
- import java .util .ArrayList ;
4
- import java .util .Arrays ;
5
- import java .util .Collection ;
6
- import java .util .List ;
7
- import java .util .Map ;
8
- import java .util .stream .Collectors ;
9
-
10
3
import com .aliyun .tair .ModuleCommand ;
11
- import com .aliyun .tair .tairhash .factory .HashBuilderFactory ;
12
4
import com .aliyun .tair .tairvector .factory .VectorBuilderFactory ;
13
5
import com .aliyun .tair .tairvector .params .DistanceMethod ;
14
6
import com .aliyun .tair .tairvector .params .HscanParams ;
19
11
import redis .clients .jedis .ScanResult ;
20
12
import redis .clients .jedis .util .SafeEncoder ;
21
13
14
+ import java .util .*;
15
+ import java .util .stream .Collectors ;
16
+
22
17
import static redis .clients .jedis .Protocol .toByteArray ;
23
18
24
- public class TairVectorCluster {
19
+ public class TairVectorCluster implements VectorShard {
25
20
private JedisCluster jc ;
26
21
27
22
public TairVectorCluster (JedisCluster jc ) {
28
23
this .jc = jc ;
29
24
}
30
25
26
+ @ Override
31
27
public void quit () {
32
28
if (jc != null ) {
33
29
jc .close ();
@@ -46,11 +42,13 @@ public void quit() {
46
42
* @param attrs other columns, optional
47
43
* @return Success: +OK; Fail: error
48
44
*/
45
+ @ Override
49
46
public String tvscreateindex (final String index , int dims , IndexAlgorithm algorithm , DistanceMethod method , final String ... attrs ) {
50
47
Object obj = jc .sendCommand (SafeEncoder .encode (index ), ModuleCommand .TVSCREATEINDEX , JoinParameters .joinParameters (SafeEncoder .encode (index ), toByteArray (dims ), SafeEncoder .encode (algorithm .name ()), SafeEncoder .encode (method .name ()), SafeEncoder .encodeMany (attrs )));
51
48
return BuilderFactory .STRING .build (obj );
52
49
}
53
50
51
+ @ Override
54
52
public byte [] tvscreateindex (byte [] index , int dims , IndexAlgorithm algorithm , DistanceMethod method , final byte []... params ) {
55
53
Object obj = jc .sendCommand (index , ModuleCommand .TVSCREATEINDEX , JoinParameters .joinParameters (index , toByteArray (dims ), SafeEncoder .encode (algorithm .name ()), SafeEncoder .encode (method .name ()), params ));
56
54
return BuilderFactory .BYTE_ARRAY .build (obj );
@@ -64,11 +62,13 @@ public byte[] tvscreateindex(byte[] index, int dims, IndexAlgorithm algorithm, D
64
62
* @param index index name
65
63
* @return Success: string_map, Fail: empty
66
64
*/
65
+ @ Override
67
66
public Map <String , String > tvsgetindex (final String index ) {
68
67
Object obj = jc .sendCommand (SafeEncoder .encode (index ), ModuleCommand .TVSGETINDEX , SafeEncoder .encode (index ));
69
68
return BuilderFactory .STRING_MAP .build (obj );
70
69
}
71
70
71
+ @ Override
72
72
public Map <byte [], byte []> tvsgetindex (byte [] index ) {
73
73
Object obj = jc .sendCommand (index , ModuleCommand .TVSGETINDEX , index );
74
74
return BuilderFactory .BYTE_ARRAY_MAP .build (obj );
@@ -82,37 +82,18 @@ public Map<byte[], byte[]> tvsgetindex(byte[] index) {
82
82
* @param index index name
83
83
* @return Success: 1; Fail: 0
84
84
*/
85
+ @ Override
85
86
public Long tvsdelindex (final String index ) {
86
87
Object obj = jc .sendCommand (SafeEncoder .encode (index ), ModuleCommand .TVSDELINDEX , SafeEncoder .encode (index ));
87
88
return BuilderFactory .LONG .build (obj );
88
89
}
89
90
91
+ @ Override
90
92
public Long tvsdelindex (byte [] index ) {
91
93
Object obj = jc .sendCommand (index , ModuleCommand .TVSDELINDEX , index );
92
94
return BuilderFactory .LONG .build (obj );
93
95
}
94
96
95
-
96
- /**
97
- * TVS.SCANINDEX TVS.SCANINDEX index_name
98
- * <p>
99
- * scan index
100
- *
101
- * @param cursor start offset
102
- * @param params the params: [MATCH pattern] [COUNT count]
103
- * `MATCH` - Set the pattern which is used to filter the results
104
- * `COUNT` - Set the number of fields in a single scan (default is 10)
105
- * `NOVAL` - The return result contains no data portion, only cursor information
106
- * @return A ScanResult. {@link HashBuilderFactory#EXHSCAN_RESULT_STRING}
107
- */
108
- public ScanResult <String > tvsscanindex (Long cursor , HscanParams params ) {
109
- final List <byte []> args = new ArrayList <byte []>();
110
- args .add (toByteArray (cursor ));
111
- args .addAll (params .getParams ());
112
- Object obj = jc .sendCommand (toByteArray (cursor ), ModuleCommand .TVSSCANINDEX , args .toArray (new byte [args .size ()][]));
113
- return VectorBuilderFactory .SCAN_CURSOR_STRING .build (obj );
114
- }
115
-
116
97
/**
117
98
* TVS.HSET TVS.HSET index entityid vector [(attribute_key attribute_value) ...]
118
99
* <p>
@@ -126,11 +107,13 @@ public ScanResult<String> tvsscanindex(Long cursor, HscanParams params) {
126
107
* {@literal k} if success, k is the number of fields that were added..
127
108
* throw error like "(error) Illegal vector dimensions" if error
128
109
*/
110
+ @ Override
129
111
public Long tvshset (final String index , final String entityid , final String vector , final String ... params ) {
130
112
Object obj = jc .sendCommand (SafeEncoder .encode (index ), ModuleCommand .TVSHSET , JoinParameters .joinParameters (SafeEncoder .encode (index ), SafeEncoder .encode (entityid ), SafeEncoder .encode (VectorBuilderFactory .VECTOR_TAG ), SafeEncoder .encode (vector ), SafeEncoder .encodeMany (params )));
131
113
return BuilderFactory .LONG .build (obj );
132
114
}
133
115
116
+ @ Override
134
117
public Long tvshset (byte [] index , byte [] entityid , byte [] vector , final byte []... params ) {
135
118
Object obj = jc .sendCommand (index , ModuleCommand .TVSHSET , JoinParameters .joinParameters (index , entityid , SafeEncoder .encode (VectorBuilderFactory .VECTOR_TAG ), vector , params ));
136
119
return BuilderFactory .LONG .build (obj );
@@ -145,11 +128,13 @@ public Long tvshset(byte[] index, byte[] entityid, byte[] vector, final byte[]..
145
128
* @param entityid entity id
146
129
* @return Map, an empty list when {@code entityid} does not exist.
147
130
*/
131
+ @ Override
148
132
public Map <String , String > tvshgetall (final String index , final String entityid ) {
149
133
Object obj = jc .sendCommand (SafeEncoder .encode (index ), ModuleCommand .TVSHGETALL , SafeEncoder .encode (index ), SafeEncoder .encode (entityid ));
150
134
return BuilderFactory .STRING_MAP .build (obj );
151
135
}
152
136
137
+ @ Override
153
138
public Map <byte [], byte []> tvshgetall (byte [] index , byte [] entityid ) {
154
139
Object obj = jc .sendCommand (index , ModuleCommand .TVSHGETALL , index , entityid );
155
140
return BuilderFactory .BYTE_ARRAY_MAP .build (obj );
@@ -165,11 +150,13 @@ public Map<byte[], byte[]> tvshgetall(byte[] index, byte[] entityid) {
165
150
* @param attrs attrs
166
151
* @return List, an empty list when {@code entityid} or {@code attrs} does not exist .
167
152
*/
153
+ @ Override
168
154
public List <String > tvshmget (final String index , final String entityid , final String ... attrs ) {
169
155
Object obj = jc .sendCommand (SafeEncoder .encode (index ), ModuleCommand .TVSHMGET , JoinParameters .joinParameters (SafeEncoder .encode (index ), SafeEncoder .encode (entityid ), SafeEncoder .encodeMany (attrs )));
170
156
return BuilderFactory .STRING_LIST .build (obj );
171
157
}
172
158
159
+ @ Override
173
160
public List <byte []> tvshmget (byte [] index , byte [] entityid , byte []... attrs ) {
174
161
Object obj = jc .sendCommand (index , ModuleCommand .TVSHMGET , JoinParameters .joinParameters (index , entityid , attrs ));
175
162
return BuilderFactory .BYTE_ARRAY_LIST .build (obj );
@@ -186,11 +173,13 @@ public List<byte[]> tvshmget(byte[] index, byte[] entityid, byte[]... attrs) {
186
173
* @return Long integer-reply the number of fields that were removed from the tair-vector
187
174
* not including specified but non existing fields.
188
175
*/
176
+ @ Override
189
177
public Long tvsdel (final String index , final String entityid ) {
190
178
Object obj = jc .sendCommand (SafeEncoder .encode (index ), ModuleCommand .TVSDEL , SafeEncoder .encode (index ), SafeEncoder .encode (entityid ));
191
179
return BuilderFactory .LONG .build (obj );
192
180
}
193
181
182
+ @ Override
194
183
public Long tvsdel (byte [] index , byte [] entityid ) {
195
184
Object obj = jc .sendCommand (index , ModuleCommand .TVSDEL , index , entityid );
196
185
return BuilderFactory .LONG .build (obj );
@@ -207,13 +196,15 @@ public Long tvsdel(byte[] index, byte[] entityid) {
207
196
* @return Long integer-reply the number of fields that were removed from the tair-vector
208
197
* not including specified but non existing fields.
209
198
*/
199
+ @ Override
210
200
public Long tvshdel (final String index , final String entityid , final String ... attrs ) {
211
201
Object obj = jc .sendCommand (SafeEncoder .encode (index ), ModuleCommand .TVSHDEL , JoinParameters .joinParameters (SafeEncoder .encode (index ), SafeEncoder .encode (entityid ), SafeEncoder .encodeMany (attrs )));
212
202
return BuilderFactory .LONG .build (obj );
213
203
}
214
204
205
+ @ Override
215
206
public Long tvshdel (byte [] index , byte [] entityid , byte []... attrs ) {
216
- Object obj = jc .sendCommand (index , ModuleCommand .TVSHDEL , JoinParameters .joinParameters (index , entityid , attrs ));
207
+ Object obj = jc .sendCommand (index , ModuleCommand .TVSHDEL , JoinParameters .joinParameters (index , entityid ,attrs ));
217
208
return BuilderFactory .LONG .build (obj );
218
209
}
219
210
@@ -231,6 +222,7 @@ public Long tvshdel(byte[] index, byte[] entityid, byte[]... attrs) {
231
222
* `NOVAL` - The return result contains no data portion, only cursor information
232
223
* @return A ScanResult.
233
224
*/
225
+ @ Override
234
226
public ScanResult <String > tvsscan (final String index , Long cursor , HscanParams params ) {
235
227
final List <byte []> args = new ArrayList <byte []>();
236
228
args .add (SafeEncoder .encode (index ));
@@ -240,6 +232,7 @@ public ScanResult<String> tvsscan(final String index, Long cursor, HscanParams p
240
232
return VectorBuilderFactory .SCAN_CURSOR_STRING .build (obj );
241
233
}
242
234
235
+ @ Override
243
236
public ScanResult <byte []> tvsscan (byte [] index , Long cursor , HscanParams params ) {
244
237
final List <byte []> args = new ArrayList <byte []>();
245
238
args .add (index );
@@ -261,10 +254,12 @@ public ScanResult<byte[]> tvsscan(byte[] index, Long cursor, HscanParams params)
261
254
* ef_search range [0, 1000]
262
255
* @return VectorBuilderFactory.Knn<>
263
256
*/
257
+ @ Override
264
258
public VectorBuilderFactory .Knn <String > tvsknnsearch (final String index , Long topn , final String vector , final String ... params ) {
265
259
return tvsknnsearchfilter (index , topn , vector , "" , params );
266
260
}
267
261
262
+ @ Override
268
263
public VectorBuilderFactory .Knn <byte []> tvsknnsearch (byte [] index , Long topn , byte [] vector , final byte []... params ) {
269
264
return tvsknnsearchfilter (index , topn , vector , SafeEncoder .encode ("" ), params );
270
265
}
@@ -282,12 +277,14 @@ public VectorBuilderFactory.Knn<byte[]> tvsknnsearch(byte[] index, Long topn, by
282
277
* ef_search range [0, 1000]
283
278
* @return VectorBuilderFactory.Knn<>
284
279
*/
280
+ @ Override
285
281
public VectorBuilderFactory .Knn <String > tvsknnsearchfilter (final String index , Long topn , final String vector , final String pattern , final String ... params ) {
286
282
Object obj = jc .sendCommand (SafeEncoder .encode (index ), ModuleCommand .TVSKNNSEARCH , JoinParameters .joinParameters (SafeEncoder .encode (index ), toByteArray (topn ),
287
283
SafeEncoder .encode (vector ), SafeEncoder .encode (pattern ), SafeEncoder .encodeMany (params )));
288
284
return VectorBuilderFactory .STRING_KNN_RESULT .build (obj );
289
285
}
290
286
287
+ @ Override
291
288
public VectorBuilderFactory .Knn <byte []> tvsknnsearchfilter (byte [] index , Long topn , byte [] vector , byte [] pattern , final byte []... params ) {
292
289
Object obj = jc .sendCommand (index , ModuleCommand .TVSKNNSEARCH , JoinParameters .joinParameters (index , toByteArray (topn ), vector , pattern , params ));
293
290
return VectorBuilderFactory .BYTE_KNN_RESULT .build (obj );
@@ -303,10 +300,12 @@ public VectorBuilderFactory.Knn<byte[]> tvsknnsearchfilter(byte[] index, Long to
303
300
* ef_search range [0, 1000]
304
301
* @return Collection<>
305
302
*/
303
+ @ Override
306
304
public Collection <VectorBuilderFactory .Knn <String >> tvsmknnsearch (final String index , Long topn , Collection <String > vectors , final String ... params ) {
307
305
return tvsmknnsearchfilter (index , topn , vectors , "" , params );
308
306
}
309
307
308
+ @ Override
310
309
public Collection <VectorBuilderFactory .Knn <byte []>> tvsmknnsearch (byte [] index , Long topn , Collection <byte []> vectors , final byte []... params ) {
311
310
return tvsmknnsearchfilter (index , topn , vectors , SafeEncoder .encode ("" ), params );
312
311
}
@@ -322,6 +321,7 @@ public Collection<VectorBuilderFactory.Knn<byte[]>> tvsmknnsearch(byte[] index,
322
321
* ef_search range [0, 1000]
323
322
* @return Collection<>
324
323
*/
324
+ @ Override
325
325
public Collection <VectorBuilderFactory .Knn <String >> tvsmknnsearchfilter (final String index , Long topn , Collection <String > vectors , final String pattern , final String ... params ) {
326
326
final List <byte []> args = new ArrayList <byte []>();
327
327
args .add (SafeEncoder .encode (index ));
@@ -334,6 +334,7 @@ public Collection<VectorBuilderFactory.Knn<String>> tvsmknnsearchfilter(final St
334
334
return VectorBuilderFactory .STRING_KNN_BATCH_RESULT .build (obj );
335
335
}
336
336
337
+ @ Override
337
338
public Collection <VectorBuilderFactory .Knn <byte []>> tvsmknnsearchfilter (byte [] index , Long topn , Collection <byte []> vectors , byte [] pattern , final byte []... params ) {
338
339
final List <byte []> args = new ArrayList <byte []>();
339
340
args .add (index );
@@ -345,4 +346,6 @@ public Collection<VectorBuilderFactory.Knn<byte[]>> tvsmknnsearchfilter(byte[] i
345
346
Object obj = jc .sendCommand (index , ModuleCommand .TVSMKNNSEARCH , args .toArray (new byte [args .size ()][]));
346
347
return VectorBuilderFactory .BYTE_KNN_BATCH_RESULT .build (obj );
347
348
}
349
+
350
+
348
351
}
0 commit comments