@@ -28,46 +28,42 @@ impl VssStore {
28
28
Self { client, store_id, runtime }
29
29
}
30
30
31
- fn build_key ( & self , namespace : & str , sub_namespace : & str , key : & str ) -> io:: Result < String > {
32
- if key. is_empty ( ) {
33
- return Err ( Error :: new ( ErrorKind :: Other , "Empty key is not allowed" ) ) ;
34
- }
35
- if namespace. is_empty ( ) {
31
+ fn build_key ( & self , primary_namespace : & str , secondary_namespace : & str , key : & str ) -> io:: Result < String > {
32
+ if primary_namespace. is_empty ( ) {
36
33
Ok ( key. to_string ( ) )
37
34
} else {
38
- Ok ( format ! ( "{}#{}#{}" , namespace , sub_namespace , key) )
35
+ Ok ( format ! ( "{}#{}#{}" , primary_namespace , secondary_namespace , key) )
39
36
}
40
37
}
41
38
42
- fn split_key ( & self , key : & str ) -> io:: Result < ( String , String , String ) > {
43
- let parts: Vec < & str > = key. split ( '#' ) . collect ( ) ;
44
- match parts. as_slice ( ) {
45
- [ namespace, sub_namespace, actual_key] => {
46
- Ok ( ( namespace. to_string ( ) , sub_namespace. to_string ( ) , actual_key. to_string ( ) ) )
47
- }
48
- _ => Err ( Error :: new ( ErrorKind :: InvalidData , "Invalid key format" ) ) ,
39
+ fn extract_key ( & self , unified_key : & str ) -> io:: Result < String > {
40
+ let mut parts = unified_key. splitn ( 3 , '#' ) ;
41
+ let ( _, _) = ( parts. next ( ) , parts. next ( ) ) ; // Discard primary_namespace & secondary_namespace
42
+ match parts. next ( ) {
43
+ Some ( actual_key) => Ok ( actual_key. to_string ( ) ) ,
44
+ None => Err ( Error :: new ( ErrorKind :: InvalidData , "Invalid key format" ) ) ,
49
45
}
50
46
}
51
47
52
- async fn list_all_keys ( & self , namespace : & str , sub_namespace : & str ) -> io:: Result < Vec < String > > {
48
+ async fn list_all_keys ( & self , primary_namespace : & str , secondary_namespace : & str ) -> io:: Result < Vec < String > > {
53
49
let mut page_token = None ;
54
50
let mut keys = vec ! [ ] ;
55
- let key_prefix = format ! ( "{}#{}" , namespace , sub_namespace ) ;
51
+ let key_prefix = format ! ( "{}#{}" , primary_namespace , secondary_namespace ) ;
56
52
while page_token != Some ( "" . to_string ( ) ) {
57
53
let request = ListKeyVersionsRequest {
58
- store_id : self . store_id . to_string ( ) ,
59
- key_prefix : Some ( key_prefix. to_string ( ) ) ,
54
+ store_id : self . store_id . clone ( ) ,
55
+ key_prefix : Some ( key_prefix. clone ( ) ) ,
60
56
page_token,
61
57
page_size : None ,
62
58
} ;
63
59
64
60
let response = self . client . list_key_versions ( & request) . await . map_err ( |e| {
65
- let msg = format ! ( "Failed to list keys in {}/{}: {}" , namespace , sub_namespace , e) ;
61
+ let msg = format ! ( "Failed to list keys in {}/{}: {}" , primary_namespace , secondary_namespace , e) ;
66
62
Error :: new ( ErrorKind :: Other , msg)
67
63
} ) ?;
68
64
69
65
for kv in response. key_versions {
70
- keys. push ( self . split_key ( & kv. key ) ?. 2 ) ;
66
+ keys. push ( self . extract_key ( & kv. key ) ?) ;
71
67
}
72
68
page_token = response. next_page_token ;
73
69
}
@@ -76,11 +72,11 @@ impl VssStore {
76
72
}
77
73
78
74
impl KVStore for VssStore {
79
- fn read ( & self , namespace : & str , sub_namespace : & str , key : & str ) -> io:: Result < Vec < u8 > > {
80
- check_namespace_key_validity ( namespace , sub_namespace , Some ( key) , "read" ) ?;
75
+ fn read ( & self , primary_namespace : & str , secondary_namespace : & str , key : & str ) -> io:: Result < Vec < u8 > > {
76
+ check_namespace_key_validity ( primary_namespace , secondary_namespace , Some ( key) , "read" ) ?;
81
77
let request = GetObjectRequest {
82
78
store_id : self . store_id . to_string ( ) ,
83
- key : self . build_key ( namespace , sub_namespace , key) ?,
79
+ key : self . build_key ( primary_namespace , secondary_namespace , key) ?,
84
80
} ;
85
81
86
82
let resp =
@@ -89,28 +85,28 @@ impl KVStore for VssStore {
89
85
VssError :: NoSuchKeyError ( ..) => {
90
86
let msg = format ! (
91
87
"Failed to read as key could not be found: {}/{}/{}. Details: {}" ,
92
- namespace , sub_namespace , key, e
88
+ primary_namespace , secondary_namespace , key, e
93
89
) ;
94
90
Error :: new ( ErrorKind :: NotFound , msg)
95
91
}
96
92
_ => {
97
93
let msg = format ! (
98
94
"Failed to read from key {}/{}/{}: {}" ,
99
- namespace , sub_namespace , key, e
95
+ primary_namespace , secondary_namespace , key, e
100
96
) ;
101
97
Error :: new ( ErrorKind :: Other , msg)
102
98
}
103
99
} ) ?;
104
100
Ok ( resp. value . unwrap ( ) . value )
105
101
}
106
102
107
- fn write ( & self , namespace : & str , sub_namespace : & str , key : & str , buf : & [ u8 ] ) -> io:: Result < ( ) > {
108
- check_namespace_key_validity ( namespace , sub_namespace , Some ( key) , "write" ) ?;
103
+ fn write ( & self , primary_namespace : & str , secondary_namespace : & str , key : & str , buf : & [ u8 ] ) -> io:: Result < ( ) > {
104
+ check_namespace_key_validity ( primary_namespace , secondary_namespace , Some ( key) , "write" ) ?;
109
105
let request = PutObjectRequest {
110
106
store_id : self . store_id . to_string ( ) ,
111
107
global_version : None ,
112
108
transaction_items : vec ! [ KeyValue {
113
- key: self . build_key( namespace , sub_namespace , key) ?,
109
+ key: self . build_key( primary_namespace , secondary_namespace , key) ?,
114
110
version: -1 ,
115
111
value: buf. to_vec( ) ,
116
112
} ] ,
@@ -121,7 +117,7 @@ impl KVStore for VssStore {
121
117
. map_err ( |e| {
122
118
let msg = format ! (
123
119
"Failed to write to key {}/{}/{}: {}" ,
124
- namespace , sub_namespace , key, e
120
+ primary_namespace , secondary_namespace , key, e
125
121
) ;
126
122
Error :: new ( ErrorKind :: Other , msg)
127
123
} ) ?;
@@ -130,13 +126,13 @@ impl KVStore for VssStore {
130
126
}
131
127
132
128
fn remove (
133
- & self , namespace : & str , sub_namespace : & str , key : & str , _lazy : bool ,
129
+ & self , primary_namespace : & str , secondary_namespace : & str , key : & str , _lazy : bool ,
134
130
) -> io:: Result < ( ) > {
135
- check_namespace_key_validity ( namespace , sub_namespace , Some ( key) , "remove" ) ?;
131
+ check_namespace_key_validity ( primary_namespace , secondary_namespace , Some ( key) , "remove" ) ?;
136
132
let request = DeleteObjectRequest {
137
133
store_id : self . store_id . to_string ( ) ,
138
134
key_value : Some ( KeyValue {
139
- key : self . build_key ( namespace , sub_namespace , key) ?,
135
+ key : self . build_key ( primary_namespace , secondary_namespace , key) ?,
140
136
version : -1 ,
141
137
value : vec ! [ ] ,
142
138
} ) ,
@@ -145,22 +141,22 @@ impl KVStore for VssStore {
145
141
tokio:: task:: block_in_place ( || self . runtime . block_on ( self . client . delete_object ( & request) ) )
146
142
. map_err ( |e| {
147
143
let msg =
148
- format ! ( "Failed to delete key {}/{}/{}: {}" , namespace , sub_namespace , key, e) ;
144
+ format ! ( "Failed to delete key {}/{}/{}: {}" , primary_namespace , secondary_namespace , key, e) ;
149
145
Error :: new ( ErrorKind :: Other , msg)
150
146
} ) ?;
151
147
Ok ( ( ) )
152
148
}
153
149
154
- fn list ( & self , namespace : & str , sub_namespace : & str ) -> io:: Result < Vec < String > > {
155
- check_namespace_key_validity ( namespace , sub_namespace , None , "list" ) ?;
150
+ fn list ( & self , primary_namespace : & str , secondary_namespace : & str ) -> io:: Result < Vec < String > > {
151
+ check_namespace_key_validity ( primary_namespace , secondary_namespace , None , "list" ) ?;
156
152
157
153
let keys = tokio:: task:: block_in_place ( || {
158
- self . runtime . block_on ( self . list_all_keys ( namespace , sub_namespace ) )
154
+ self . runtime . block_on ( self . list_all_keys ( primary_namespace , secondary_namespace ) )
159
155
} )
160
156
. map_err ( |e| {
161
157
let msg = format ! (
162
158
"Failed to retrieve keys in namespace: {}/{} : {}" ,
163
- namespace , sub_namespace , e
159
+ primary_namespace , secondary_namespace , e
164
160
) ;
165
161
Error :: new ( ErrorKind :: Other , msg)
166
162
} ) ?;
0 commit comments