1- use deno_core:: JsBuffer ;
21use foundationdb:: tuple:: {
3- Bytes , PackError , PackResult , TupleDepth , TuplePack , TupleUnpack , VersionstampOffset ,
2+ Bytes , PackResult , TupleDepth , TuplePack , TupleUnpack , VersionstampOffset ,
43} ;
5- use serde:: Deserialize ;
4+ use serde:: { Serialize , Deserialize } ;
65
76// 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 > > ) ;
169
1710impl std:: fmt:: Debug for Key {
1811 fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
@@ -22,49 +15,24 @@ impl std::fmt::Debug for Key {
2215
2316impl PartialEq for Key {
2417 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
3419 }
3520}
3621
3722impl Eq for Key { }
3823
3924impl std:: hash:: Hash for Key {
4025 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) ;
5228 }
5329 }
5430}
5531
5632impl Key {
5733 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 ( )
6836 }
6937}
7038
@@ -74,30 +42,25 @@ impl TuplePack for Key {
7442 w : & mut W ,
7543 tuple_depth : TupleDepth ,
7644 ) -> 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 } ;
8046
81- w. write_all ( & [ NESTED ] ) ?;
82- offset += 1 ;
47+ w. write_all ( & [ fdb_util :: codes :: NESTED ] ) ?;
48+ offset += 1 ;
8349
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+ }
8753
88- w. write_all ( & [ NIL ] ) ?;
89- offset += 1 ;
54+ w. write_all ( & [ fdb_util :: codes :: NIL ] ) ?;
55+ offset += 1 ;
9056
91- Ok ( offset)
92- }
93- Key :: JsOutKey ( _) => unreachable ! ( "should not be packing out keys" ) ,
94- }
57+ Ok ( offset)
9558 }
9659}
9760
9861impl < ' de > TupleUnpack < ' de > for Key {
9962 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 ) ?;
10164
10265 let mut vec = Vec :: new ( ) ;
10366 while !is_end_of_tuple ( input, true ) {
@@ -106,15 +69,21 @@ impl<'de> TupleUnpack<'de> for Key {
10669 vec. push ( v. into_owned ( ) ) ;
10770 }
10871
109- input = parse_code ( input, NIL ) ?;
72+ input = fdb_util :: parse_code ( input, fdb_util :: codes :: NIL ) ?;
11073
111- Ok ( ( input, Key :: JsOutKey ( vec) ) )
74+ Ok ( ( input, Key ( vec) ) )
11275 }
11376}
11477
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+ }
11887
11988impl TuplePack for ListKey {
12089 fn pack < W : std:: io:: Write > (
@@ -124,11 +93,11 @@ impl TuplePack for ListKey {
12493 ) -> std:: io:: Result < VersionstampOffset > {
12594 let mut offset = VersionstampOffset :: None { size : 0 } ;
12695
127- w. write_all ( & [ NESTED ] ) ?;
96+ w. write_all ( & [ fdb_util :: codes :: NESTED ] ) ?;
12897 offset += 1 ;
12998
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 ( ) ) ?;
132101 }
133102
134103 // No ending NIL byte compared to `Key::pack`
@@ -144,37 +113,11 @@ impl ListKey {
144113 }
145114}
146115
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-
173116fn is_end_of_tuple ( input : & [ u8 ] , nested : bool ) -> bool {
174117 match input. first ( ) {
175118 None => true ,
176119 _ 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 ) ,
178121 _ => false ,
179122 }
180123}
0 commit comments