1
+ #![ allow( clippy:: not_unsafe_ptr_arg_deref) ]
2
+
1
3
mod hnsw;
2
4
mod types;
3
5
@@ -15,12 +17,10 @@ extern crate ordered_float;
15
17
extern crate owning_ref;
16
18
17
19
use hnsw:: { Index , Node } ;
18
- use redis_module:: {
19
- Context , RedisError , RedisResult , RedisValue ,
20
- } ;
21
- use redismodule_cmd:: { Command , ArgType , Collection , rediscmd_doc} ;
22
- use std:: collections:: { HashMap , HashSet } ;
20
+ use redis_module:: { Context , RedisError , RedisResult , RedisValue } ;
21
+ use redismodule_cmd:: { rediscmd_doc, ArgType , Collection , Command } ;
23
22
use std:: collections:: hash_map:: Entry ;
23
+ use std:: collections:: { HashMap , HashSet } ;
24
24
use std:: sync:: { Arc , RwLock } ;
25
25
use types:: * ;
26
26
@@ -128,13 +128,10 @@ thread_local! {
128
128
} ;
129
129
}
130
130
131
-
132
131
fn new_index ( ctx : & Context , args : Vec < String > ) -> RedisResult {
133
132
ctx. auto_memory ( ) ;
134
133
135
- let mut parsed = NEW_INDEX_CMD . with ( |cmd| {
136
- cmd. parse_args ( args)
137
- } ) ?;
134
+ let mut parsed = NEW_INDEX_CMD . with ( |cmd| cmd. parse_args ( args) ) ?;
138
135
139
136
let name_suffix = parsed. remove ( "name" ) . unwrap ( ) . as_string ( ) ?;
140
137
let index_name = format ! ( "{}.{}" , PREFIX , name_suffix) ;
@@ -176,17 +173,15 @@ fn new_index(ctx: &Context, args: Vec<String>) -> RedisResult {
176
173
fn get_index ( ctx : & Context , args : Vec < String > ) -> RedisResult {
177
174
ctx. auto_memory ( ) ;
178
175
179
- let mut parsed = GET_INDEX_CMD . with ( |cmd| {
180
- cmd. parse_args ( args)
181
- } ) ?;
176
+ let mut parsed = GET_INDEX_CMD . with ( |cmd| cmd. parse_args ( args) ) ?;
182
177
183
178
let name_suffix = parsed. remove ( "name" ) . unwrap ( ) . as_string ( ) ?;
184
179
let index_name = format ! ( "{}.{}" , PREFIX , name_suffix) ;
185
180
186
181
let index = load_index ( ctx, & index_name) ?;
187
182
let index = match index. try_read ( ) {
188
183
Ok ( index) => index,
189
- Err ( e) => return Err ( e. to_string ( ) . into ( ) )
184
+ Err ( e) => return Err ( e. to_string ( ) . into ( ) ) ,
190
185
} ;
191
186
192
187
ctx. log_debug ( format ! ( "Index: {:?}" , index) . as_str ( ) ) ;
@@ -201,9 +196,7 @@ fn get_index(ctx: &Context, args: Vec<String>) -> RedisResult {
201
196
fn delete_index ( ctx : & Context , args : Vec < String > ) -> RedisResult {
202
197
ctx. auto_memory ( ) ;
203
198
204
- let mut parsed = DEL_INDEX_CMD . with ( |cmd| {
205
- cmd. parse_args ( args)
206
- } ) ?;
199
+ let mut parsed = DEL_INDEX_CMD . with ( |cmd| cmd. parse_args ( args) ) ?;
207
200
208
201
let name_suffix = parsed. remove ( "name" ) . unwrap ( ) . as_string ( ) ?;
209
202
let index_name = format ! ( "{}.{}" , PREFIX , name_suffix) ;
@@ -320,11 +313,7 @@ fn make_index(ctx: &Context, ir: &IndexRedis) -> Result<IndexT, RedisError> {
320
313
Ok ( index)
321
314
}
322
315
323
- fn update_index (
324
- ctx : & Context ,
325
- index_name : & str ,
326
- index : & IndexT ,
327
- ) -> Result < ( ) , RedisError > {
316
+ fn update_index ( ctx : & Context , index_name : & str , index : & IndexT ) -> Result < ( ) , RedisError > {
328
317
let key = ctx. open_key_writable ( index_name) ;
329
318
match key. get_value :: < IndexRedis > ( & HNSW_INDEX_REDIS_TYPE ) ? {
330
319
Some ( _) => {
@@ -344,9 +333,7 @@ fn update_index(
344
333
fn add_node ( ctx : & Context , args : Vec < String > ) -> RedisResult {
345
334
ctx. auto_memory ( ) ;
346
335
347
- let mut parsed = ADD_NODE_CMD . with ( |cmd| {
348
- cmd. parse_args ( args)
349
- } ) ?;
336
+ let mut parsed = ADD_NODE_CMD . with ( |cmd| cmd. parse_args ( args) ) ?;
350
337
351
338
let index_suffix = parsed. remove ( "index" ) . unwrap ( ) . as_string ( ) ?;
352
339
let node_suffix = parsed. remove ( "node" ) . unwrap ( ) . as_string ( ) ?;
@@ -360,7 +347,7 @@ fn add_node(ctx: &Context, args: Vec<String>) -> RedisResult {
360
347
let index = load_index ( ctx, & index_name) ?;
361
348
let mut index = match index. try_write ( ) {
362
349
Ok ( index) => index,
363
- Err ( e) => return Err ( e. to_string ( ) . into ( ) )
350
+ Err ( e) => return Err ( e. to_string ( ) . into ( ) ) ,
364
351
} ;
365
352
366
353
let up = |name : String , node : Node < f32 > | {
@@ -369,7 +356,7 @@ fn add_node(ctx: &Context, args: Vec<String>) -> RedisResult {
369
356
370
357
ctx. log_debug ( format ! ( "Adding node: {} to Index: {}" , & node_name, & index_name) . as_str ( ) ) ;
371
358
if let Err ( e) = index. add_node ( & node_name, & data, up) {
372
- return Err ( e. error_string ( ) . into ( ) )
359
+ return Err ( e. error_string ( ) . into ( ) ) ;
373
360
}
374
361
375
362
// write node to redis
@@ -385,9 +372,7 @@ fn add_node(ctx: &Context, args: Vec<String>) -> RedisResult {
385
372
fn delete_node ( ctx : & Context , args : Vec < String > ) -> RedisResult {
386
373
ctx. auto_memory ( ) ;
387
374
388
- let mut parsed = DEL_NODE_CMD . with ( |cmd| {
389
- cmd. parse_args ( args)
390
- } ) ?;
375
+ let mut parsed = DEL_NODE_CMD . with ( |cmd| cmd. parse_args ( args) ) ?;
391
376
392
377
let index_suffix = parsed. remove ( "index" ) . unwrap ( ) . as_string ( ) ?;
393
378
let node_suffix = parsed. remove ( "node" ) . unwrap ( ) . as_string ( ) ?;
@@ -398,9 +383,9 @@ fn delete_node(ctx: &Context, args: Vec<String>) -> RedisResult {
398
383
let index = load_index ( ctx, & index_name) ?;
399
384
let mut index = match index. try_write ( ) {
400
385
Ok ( index) => index,
401
- Err ( e) => return Err ( e. to_string ( ) . into ( ) )
386
+ Err ( e) => return Err ( e. to_string ( ) . into ( ) ) ,
402
387
} ;
403
-
388
+
404
389
// TODO return error if node has more than 1 strong_count
405
390
let node = index. nodes . get ( & node_name) . unwrap ( ) ;
406
391
if Arc :: strong_count ( & node. 0 ) > 1 {
@@ -414,9 +399,9 @@ fn delete_node(ctx: &Context, args: Vec<String>) -> RedisResult {
414
399
let up = |name : String , node : Node < f32 > | {
415
400
write_node ( ctx, & name, ( & node) . into ( ) ) . unwrap ( ) ;
416
401
} ;
417
-
402
+
418
403
if let Err ( e) = index. delete_node ( & node_name, up) {
419
- return Err ( e. error_string ( ) . into ( ) )
404
+ return Err ( e. error_string ( ) . into ( ) ) ;
420
405
}
421
406
422
407
ctx. log_debug ( format ! ( "del key: {}" , & node_name) . as_str ( ) ) ;
@@ -440,9 +425,7 @@ fn delete_node(ctx: &Context, args: Vec<String>) -> RedisResult {
440
425
fn get_node ( ctx : & Context , args : Vec < String > ) -> RedisResult {
441
426
ctx. auto_memory ( ) ;
442
427
443
- let mut parsed = GET_NODE_CMD . with ( |cmd| {
444
- cmd. parse_args ( args)
445
- } ) ?;
428
+ let mut parsed = GET_NODE_CMD . with ( |cmd| cmd. parse_args ( args) ) ?;
446
429
447
430
let index_suffix = parsed. remove ( "index" ) . unwrap ( ) . as_string ( ) ?;
448
431
let node_suffix = parsed. remove ( "node" ) . unwrap ( ) . as_string ( ) ?;
@@ -485,9 +468,7 @@ fn write_node<'a>(ctx: &'a Context, key: &str, node: NodeRedis) -> RedisResult {
485
468
fn search_knn ( ctx : & Context , args : Vec < String > ) -> RedisResult {
486
469
ctx. auto_memory ( ) ;
487
470
488
- let mut parsed = SEARCH_CMD . with ( |cmd| {
489
- cmd. parse_args ( args)
490
- } ) ?;
471
+ let mut parsed = SEARCH_CMD . with ( |cmd| cmd. parse_args ( args) ) ?;
491
472
492
473
let index_suffix = parsed. remove ( "index" ) . unwrap ( ) . as_string ( ) ?;
493
474
let k = parsed. remove ( "k" ) . unwrap ( ) . as_u64 ( ) ? as usize ;
@@ -498,7 +479,7 @@ fn search_knn(ctx: &Context, args: Vec<String>) -> RedisResult {
498
479
let index = load_index ( ctx, & index_name) ?;
499
480
let index = match index. try_read ( ) {
500
481
Ok ( index) => index,
501
- Err ( e) => return Err ( e. to_string ( ) . into ( ) )
482
+ Err ( e) => return Err ( e. to_string ( ) . into ( ) ) ,
502
483
} ;
503
484
504
485
ctx. log_debug (
@@ -511,15 +492,13 @@ fn search_knn(ctx: &Context, args: Vec<String>) -> RedisResult {
511
492
512
493
match index. search_knn ( & data, k) {
513
494
Ok ( res) => {
514
- {
515
- let mut reply: Vec < RedisValue > = Vec :: new ( ) ;
516
- reply. push ( res. len ( ) . into ( ) ) ;
517
- for r in & res {
518
- let sr: SearchResultRedis = r. into ( ) ;
519
- reply. push ( sr. into ( ) ) ;
520
- }
521
- Ok ( reply. into ( ) )
495
+ let mut reply: Vec < RedisValue > = Vec :: new ( ) ;
496
+ reply. push ( res. len ( ) . into ( ) ) ;
497
+ for r in & res {
498
+ let sr: SearchResultRedis = r. into ( ) ;
499
+ reply. push ( sr. into ( ) ) ;
522
500
}
501
+ Ok ( reply. into ( ) )
523
502
}
524
503
Err ( e) => Err ( e. error_string ( ) . into ( ) ) ,
525
504
}
0 commit comments