1
- use deno_core:: JsBuffer ;
2
1
use foundationdb:: tuple:: {
3
- Bytes , PackError , PackResult , TupleDepth , TuplePack , TupleUnpack , VersionstampOffset ,
2
+ Bytes , PackResult , TupleDepth , TuplePack , TupleUnpack , VersionstampOffset ,
4
3
} ;
5
- use serde:: Deserialize ;
4
+ use serde:: { Serialize , Deserialize } ;
6
5
7
6
// TODO: Custom deser impl that uses arrays instead of objects?
8
- #[ derive( Clone , Deserialize ) ]
9
- #[ serde( rename_all = "camelCase" ) ]
10
- pub enum Key {
11
- /// Contains references to v8-owned buffers. Requires no copies.
12
- JsInKey ( Vec < JsBuffer > ) ,
13
- /// Cant use `ToJsBuffer` because of its API, so it gets converted to ToJsBuffer in the KV ext.
14
- JsOutKey ( Vec < Vec < u8 > > ) ,
15
- }
7
+ #[ derive( Clone , Serialize , Deserialize ) ]
8
+ pub struct Key ( Vec < Vec < u8 > > ) ;
16
9
17
10
impl std:: fmt:: Debug for Key {
18
11
fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
@@ -22,49 +15,24 @@ impl std::fmt::Debug for Key {
22
15
23
16
impl PartialEq for Key {
24
17
fn eq ( & self , other : & Self ) -> bool {
25
- match ( self , other) {
26
- ( Key :: JsInKey ( a) , Key :: JsInKey ( b) ) => a
27
- . iter ( )
28
- . map ( |x| x. as_ref ( ) )
29
- . eq ( b. iter ( ) . map ( |x| x. as_ref ( ) ) ) ,
30
- ( Key :: JsOutKey ( a) , Key :: JsOutKey ( b) ) => a == b,
31
- ( Key :: JsInKey ( a) , Key :: JsOutKey ( b) ) => a. iter ( ) . map ( |x| x. as_ref ( ) ) . eq ( b. iter ( ) ) ,
32
- ( Key :: JsOutKey ( a) , Key :: JsInKey ( b) ) => a. iter ( ) . eq ( b. iter ( ) . map ( |x| x. as_ref ( ) ) ) ,
33
- }
18
+ self . 0 == other. 0
34
19
}
35
20
}
36
21
37
22
impl Eq for Key { }
38
23
39
24
impl std:: hash:: Hash for Key {
40
25
fn hash < H : std:: hash:: Hasher > ( & self , state : & mut H ) {
41
- match self {
42
- Key :: JsInKey ( js_in_key) => {
43
- for buffer in js_in_key {
44
- state. write ( buffer. as_ref ( ) ) ;
45
- }
46
- }
47
- Key :: JsOutKey ( out_key) => {
48
- for buffer in out_key {
49
- state. write ( buffer) ;
50
- }
51
- }
26
+ for buffer in & self . 0 {
27
+ state. write ( buffer) ;
52
28
}
53
29
}
54
30
}
55
31
56
32
impl Key {
57
33
pub fn len ( & self ) -> usize {
58
- match self {
59
- Key :: JsInKey ( js_in_key) => {
60
- // Arbitrary 4 accounting for nesting overhead
61
- js_in_key. iter ( ) . fold ( 0 , |acc, x| acc + x. len ( ) ) + 4 * js_in_key. len ( )
62
- }
63
- Key :: JsOutKey ( out_key) => {
64
- // Arbitrary 4 accounting for nesting overhead
65
- out_key. iter ( ) . fold ( 0 , |acc, x| acc + x. len ( ) ) + 4 * out_key. len ( )
66
- }
67
- }
34
+ // Arbitrary 4 accounting for nesting overhead
35
+ self . 0 . iter ( ) . fold ( 0 , |acc, x| acc + x. len ( ) ) + 4 * self . 0 . len ( )
68
36
}
69
37
}
70
38
@@ -74,30 +42,25 @@ impl TuplePack for Key {
74
42
w : & mut W ,
75
43
tuple_depth : TupleDepth ,
76
44
) -> std:: io:: Result < VersionstampOffset > {
77
- match self {
78
- Key :: JsInKey ( tuple) => {
79
- let mut offset = VersionstampOffset :: None { size : 0 } ;
45
+ let mut offset = VersionstampOffset :: None { size : 0 } ;
80
46
81
- w. write_all ( & [ NESTED ] ) ?;
82
- offset += 1 ;
47
+ w. write_all ( & [ fdb_util :: codes :: NESTED ] ) ?;
48
+ offset += 1 ;
83
49
84
- for v in tuple . iter ( ) {
85
- offset += v. as_ref ( ) . pack ( w, tuple_depth. increment ( ) ) ?;
86
- }
50
+ for v in self . 0 . iter ( ) {
51
+ offset += v. pack ( w, tuple_depth. increment ( ) ) ?;
52
+ }
87
53
88
- w. write_all ( & [ NIL ] ) ?;
89
- offset += 1 ;
54
+ w. write_all ( & [ fdb_util :: codes :: NIL ] ) ?;
55
+ offset += 1 ;
90
56
91
- Ok ( offset)
92
- }
93
- Key :: JsOutKey ( _) => unreachable ! ( "should not be packing out keys" ) ,
94
- }
57
+ Ok ( offset)
95
58
}
96
59
}
97
60
98
61
impl < ' de > TupleUnpack < ' de > for Key {
99
62
fn unpack ( mut input : & [ u8 ] , tuple_depth : TupleDepth ) -> PackResult < ( & [ u8 ] , Self ) > {
100
- input = parse_code ( input, NESTED ) ?;
63
+ input = fdb_util :: parse_code ( input, fdb_util :: codes :: NESTED ) ?;
101
64
102
65
let mut vec = Vec :: new ( ) ;
103
66
while !is_end_of_tuple ( input, true ) {
@@ -106,15 +69,21 @@ impl<'de> TupleUnpack<'de> for Key {
106
69
vec. push ( v. into_owned ( ) ) ;
107
70
}
108
71
109
- input = parse_code ( input, NIL ) ?;
72
+ input = fdb_util :: parse_code ( input, fdb_util :: codes :: NIL ) ?;
110
73
111
- Ok ( ( input, Key :: JsOutKey ( vec) ) )
74
+ Ok ( ( input, Key ( vec) ) )
112
75
}
113
76
}
114
77
115
- /// Same as Key::JsInKey except when packing, it leaves off the NIL byte to allow for an open range.
116
- #[ derive( Deserialize ) ]
117
- pub struct ListKey ( Vec < JsBuffer > ) ;
78
+ /// Same as Key: except when packing, it leaves off the NIL byte to allow for an open range.
79
+ #[ derive( Clone , Serialize , Deserialize ) ]
80
+ pub struct ListKey ( Vec < Vec < u8 > > ) ;
81
+
82
+ impl std:: fmt:: Debug for ListKey {
83
+ fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
84
+ write ! ( f, "ListKey({})" , self . len( ) )
85
+ }
86
+ }
118
87
119
88
impl TuplePack for ListKey {
120
89
fn pack < W : std:: io:: Write > (
@@ -124,11 +93,11 @@ impl TuplePack for ListKey {
124
93
) -> std:: io:: Result < VersionstampOffset > {
125
94
let mut offset = VersionstampOffset :: None { size : 0 } ;
126
95
127
- w. write_all ( & [ NESTED ] ) ?;
96
+ w. write_all ( & [ fdb_util :: codes :: NESTED ] ) ?;
128
97
offset += 1 ;
129
98
130
- for v in self . 0 . iter ( ) {
131
- offset += v. as_ref ( ) . pack ( w, tuple_depth. increment ( ) ) ?;
99
+ for v in & self . 0 {
100
+ offset += v. pack ( w, tuple_depth. increment ( ) ) ?;
132
101
}
133
102
134
103
// No ending NIL byte compared to `Key::pack`
@@ -144,37 +113,11 @@ impl ListKey {
144
113
}
145
114
}
146
115
147
- // === Copied from foundationdbrs ===
148
- const NIL : u8 = 0x00 ;
149
- const NESTED : u8 = 0x05 ;
150
- const ESCAPE : u8 = 0xff ;
151
-
152
- #[ inline]
153
- fn parse_byte ( input : & [ u8 ] ) -> PackResult < ( & [ u8 ] , u8 ) > {
154
- if input. is_empty ( ) {
155
- Err ( PackError :: MissingBytes )
156
- } else {
157
- Ok ( ( & input[ 1 ..] , input[ 0 ] ) )
158
- }
159
- }
160
-
161
- fn parse_code ( input : & [ u8 ] , expected : u8 ) -> PackResult < & [ u8 ] > {
162
- let ( input, found) = parse_byte ( input) ?;
163
- if found == expected {
164
- Ok ( input)
165
- } else {
166
- Err ( PackError :: BadCode {
167
- found,
168
- expected : Some ( expected) ,
169
- } )
170
- }
171
- }
172
-
173
116
fn is_end_of_tuple ( input : & [ u8 ] , nested : bool ) -> bool {
174
117
match input. first ( ) {
175
118
None => true ,
176
119
_ if !nested => false ,
177
- Some ( & NIL ) => Some ( & ESCAPE ) != input. get ( 1 ) ,
120
+ Some ( & fdb_util :: codes :: NIL ) => Some ( & fdb_util :: codes :: ESCAPE ) != input. get ( 1 ) ,
178
121
_ => false ,
179
122
}
180
123
}
0 commit comments