1
1
use crate :: distance:: * ;
2
2
use crate :: vector:: * ;
3
+ use base_macros:: Alter ;
3
4
use serde:: { Deserialize , Serialize } ;
4
5
use thiserror:: Error ;
5
6
use uuid:: Uuid ;
@@ -8,7 +9,7 @@ use validator::{Validate, ValidationError};
8
9
#[ must_use]
9
10
#[ derive( Debug , Clone , Error , Serialize , Deserialize ) ]
10
11
pub enum CreateError {
11
- #[ error( "Invalid index options." ) ]
12
+ #[ error( "Invalid index options: {reason} ." ) ]
12
13
InvalidIndexOptions { reason : String } ,
13
14
}
14
15
@@ -81,47 +82,37 @@ pub enum StatError {
81
82
#[ must_use]
82
83
#[ derive( Debug , Clone , Error , Serialize , Deserialize ) ]
83
84
pub enum AlterError {
84
- #[ error( "Setting key {key} is not exist." ) ]
85
- BadKey { key : String } ,
86
- #[ error( "Setting key {key} has a wrong value {value}." ) ]
87
- BadValue { key : String , value : String } ,
88
85
#[ error( "Index not found." ) ]
89
86
NotExist ,
87
+ #[ error( "Key {key} not found." ) ]
88
+ KeyNotExists { key : String } ,
89
+ #[ error( "Invalid index options: {reason}." ) ]
90
+ InvalidIndexOptions { reason : String } ,
90
91
}
91
92
92
- #[ derive( Debug , Clone , Serialize , Deserialize , Validate ) ]
93
- #[ serde( deny_unknown_fields) ]
94
- pub struct IndexFlexibleOptions {
95
- #[ serde( default = "IndexFlexibleOptions::default_optimizing_threads" ) ]
96
- #[ validate( range( min = 1 , max = 65535 ) ) ]
97
- pub optimizing_threads : u16 ,
98
- }
99
-
100
- impl IndexFlexibleOptions {
101
- pub fn default_optimizing_threads ( ) -> u16 {
102
- 1
103
- }
93
+ #[ must_use]
94
+ #[ derive( Debug , Clone , Error , Serialize , Deserialize ) ]
95
+ pub enum StopError {
96
+ #[ error( "Index not found." ) ]
97
+ NotExist ,
104
98
}
105
99
106
- impl Default for IndexFlexibleOptions {
107
- fn default ( ) -> Self {
108
- Self {
109
- optimizing_threads : Self :: default_optimizing_threads ( ) ,
110
- }
111
- }
100
+ #[ must_use]
101
+ #[ derive( Debug , Clone , Error , Serialize , Deserialize ) ]
102
+ pub enum StartError {
103
+ #[ error( "Index not found." ) ]
104
+ NotExist ,
112
105
}
113
106
114
107
#[ derive( Debug , Clone , Serialize , Deserialize , Validate ) ]
115
108
#[ serde( deny_unknown_fields) ]
116
109
#[ validate( schema( function = "IndexOptions::validate_index_options" ) ) ]
117
110
pub struct IndexOptions {
118
- #[ validate]
111
+ #[ validate( nested ) ]
119
112
pub vector : VectorOptions ,
120
- #[ validate]
113
+ #[ validate( nested ) ]
121
114
pub segment : SegmentsOptions ,
122
- #[ validate]
123
- pub optimizing : OptimizingOptions ,
124
- #[ validate]
115
+ #[ validate( nested) ]
125
116
pub indexing : IndexingOptions ,
126
117
}
127
118
@@ -140,13 +131,20 @@ impl IndexOptions {
140
131
} ;
141
132
if !is_trivial {
142
133
return Err ( ValidationError :: new (
143
- "Quantization is not supported for svector, bvector, and vecint8 ." ,
134
+ "Quantization is not supported for svector, bvector, and veci8 ." ,
144
135
) ) ;
145
136
}
146
137
Ok ( ( ) )
147
138
}
148
139
}
149
140
141
+ #[ derive( Debug , Clone , Serialize , Deserialize , Validate , Alter ) ]
142
+ #[ serde( deny_unknown_fields) ]
143
+ pub struct IndexAlterableOptions {
144
+ #[ validate( nested) ]
145
+ pub optimizing : OptimizingOptions ,
146
+ }
147
+
150
148
#[ derive( Debug , Clone , Serialize , Deserialize , Validate ) ]
151
149
#[ serde( deny_unknown_fields) ]
152
150
#[ validate( schema( function = "Self::validate_0" ) ) ]
@@ -222,9 +220,12 @@ impl Default for SegmentsOptions {
222
220
}
223
221
}
224
222
225
- #[ derive( Debug , Clone , Serialize , Deserialize , Validate ) ]
223
+ #[ derive( Debug , Clone , Serialize , Deserialize , Validate , Alter ) ]
226
224
#[ serde( deny_unknown_fields) ]
227
225
pub struct OptimizingOptions {
226
+ #[ serde( default = "OptimizingOptions::default_optimizing_threads" ) ]
227
+ #[ validate( range( min = 1 , max = 65535 ) ) ]
228
+ pub optimizing_threads : u16 ,
228
229
#[ serde( default = "OptimizingOptions::default_sealing_secs" ) ]
229
230
#[ validate( range( min = 1 , max = 60 ) ) ]
230
231
pub sealing_secs : u64 ,
@@ -237,6 +238,9 @@ pub struct OptimizingOptions {
237
238
}
238
239
239
240
impl OptimizingOptions {
241
+ fn default_optimizing_threads ( ) -> u16 {
242
+ 1
243
+ }
240
244
fn default_sealing_secs ( ) -> u64 {
241
245
60
242
246
}
@@ -251,6 +255,7 @@ impl OptimizingOptions {
251
255
impl Default for OptimizingOptions {
252
256
fn default ( ) -> Self {
253
257
Self {
258
+ optimizing_threads : Self :: default_optimizing_threads ( ) ,
254
259
sealing_secs : Self :: default_sealing_secs ( ) ,
255
260
sealing_size : Self :: default_sealing_size ( ) ,
256
261
delete_threshold : Self :: default_delete_threshold ( ) ,
@@ -308,7 +313,7 @@ impl Validate for IndexingOptions {
308
313
#[ serde( deny_unknown_fields) ]
309
314
pub struct FlatIndexingOptions {
310
315
#[ serde( default ) ]
311
- #[ validate]
316
+ #[ validate( nested ) ]
312
317
pub quantization : QuantizationOptions ,
313
318
}
314
319
@@ -336,7 +341,7 @@ pub struct IvfIndexingOptions {
336
341
#[ validate( range( min = 1 , max = 1_000_000 ) ) ]
337
342
pub nsample : u32 ,
338
343
#[ serde( default ) ]
339
- #[ validate]
344
+ #[ validate( nested ) ]
340
345
pub quantization : QuantizationOptions ,
341
346
}
342
347
@@ -375,17 +380,17 @@ pub struct HnswIndexingOptions {
375
380
pub m : u32 ,
376
381
#[ serde( default = "HnswIndexingOptions::default_ef_construction" ) ]
377
382
#[ validate( range( min = 10 , max = 2000 ) ) ]
378
- pub ef_construction : usize ,
383
+ pub ef_construction : u32 ,
379
384
#[ serde( default ) ]
380
- #[ validate]
385
+ #[ validate( nested ) ]
381
386
pub quantization : QuantizationOptions ,
382
387
}
383
388
384
389
impl HnswIndexingOptions {
385
390
fn default_m ( ) -> u32 {
386
391
12
387
392
}
388
- fn default_ef_construction ( ) -> usize {
393
+ fn default_ef_construction ( ) -> u32 {
389
394
300
390
395
}
391
396
}
@@ -492,7 +497,7 @@ impl Default for ProductQuantizationOptionsRatio {
492
497
pub struct SearchOptions {
493
498
pub prefilter_enable : bool ,
494
499
#[ validate( range( min = 1 , max = 65535 ) ) ]
495
- pub hnsw_ef_search : usize ,
500
+ pub hnsw_ef_search : u32 ,
496
501
#[ validate( range( min = 1 , max = 1_000_000 ) ) ]
497
502
pub ivf_nprobe : u32 ,
498
503
}
@@ -507,8 +512,30 @@ pub struct IndexStat {
507
512
#[ derive( Debug , Serialize , Deserialize ) ]
508
513
pub struct SegmentStat {
509
514
pub id : Uuid ,
510
- #[ serde( rename = "type" ) ]
511
- pub typ : String ,
515
+ pub r#type : String ,
512
516
pub length : usize ,
513
517
pub size : u64 ,
514
518
}
519
+
520
+ pub trait Alter {
521
+ fn alter ( & mut self , key : & [ & str ] , value : & str ) -> Result < ( ) , AlterError > ;
522
+ }
523
+
524
+ macro_rules! impl_alter_for {
525
+ { $( $t: ty) * } => {
526
+ $( impl Alter for $t {
527
+ fn alter( & mut self , key: & [ & str ] , value: & str ) -> Result <( ) , AlterError > {
528
+ use std:: str :: FromStr ;
529
+ if key. is_empty( ) {
530
+ * self = FromStr :: from_str( value) . map_err( |_| AlterError :: InvalidIndexOptions { reason: "failed to parse" . to_string( ) } ) ?;
531
+ return Ok ( ( ) ) ;
532
+ }
533
+ Err ( AlterError :: KeyNotExists { key: key. join( "." ) } )
534
+ }
535
+ } ) *
536
+ } ;
537
+ }
538
+
539
+ impl_alter_for ! {
540
+ String u8 u16 u32 u64 i8 i16 i32 i64 f32 f64 bool
541
+ }
0 commit comments