11use foundationdb:: tuple:: {
22 Bytes , PackResult , TupleDepth , TuplePack , TupleUnpack , VersionstampOffset ,
33} ;
4- use serde:: { Deserialize , Serialize } ;
4+ use pegboard_config:: runner_protocol:: proto:: kv;
5+ use prost:: Message ;
56
67// TODO: Custom deser impl that uses arrays instead of objects?
7- #[ derive( Clone , Serialize , Deserialize ) ]
8- pub struct Key ( Vec < Vec < u8 > > ) ;
8+ #[ derive( Clone ) ]
9+ pub struct Key {
10+ inner : kv:: Key ,
11+ }
912
1013impl std:: fmt:: Debug for Key {
1114 fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
@@ -15,24 +18,23 @@ impl std::fmt::Debug for Key {
1518
1619impl PartialEq for Key {
1720 fn eq ( & self , other : & Self ) -> bool {
18- self . 0 == other. 0
21+ self . inner == other. inner
1922 }
2023}
2124
2225impl Eq for Key { }
2326
2427impl std:: hash:: Hash for Key {
2528 fn hash < H : std:: hash:: Hasher > ( & self , state : & mut H ) {
26- for buffer in & self . 0 {
29+ for buffer in & self . inner . segments {
2730 state. write ( buffer) ;
2831 }
2932 }
3033}
3134
3235impl Key {
3336 pub fn len ( & self ) -> usize {
34- // Arbitrary 4 accounting for nesting overhead
35- self . 0 . iter ( ) . fold ( 0 , |acc, x| acc + x. len ( ) ) + 4 * self . 0 . len ( )
37+ self . inner . encoded_len ( )
3638 }
3739}
3840
@@ -47,7 +49,7 @@ impl TuplePack for Key {
4749 w. write_all ( & [ fdb_util:: codes:: NESTED ] ) ?;
4850 offset += 1 ;
4951
50- for v in self . 0 . iter ( ) {
52+ for v in self . inner . segments . iter ( ) {
5153 offset += v. pack ( w, tuple_depth. increment ( ) ) ?;
5254 }
5355
@@ -62,22 +64,29 @@ impl<'de> TupleUnpack<'de> for Key {
6264 fn unpack ( mut input : & [ u8 ] , tuple_depth : TupleDepth ) -> PackResult < ( & [ u8 ] , Self ) > {
6365 input = fdb_util:: parse_code ( input, fdb_util:: codes:: NESTED ) ?;
6466
65- let mut vec = Vec :: new ( ) ;
67+ let mut segments = Vec :: new ( ) ;
6668 while !is_end_of_tuple ( input, true ) {
6769 let ( rem, v) = Bytes :: unpack ( input, tuple_depth. increment ( ) ) ?;
6870 input = rem;
69- vec . push ( v. into_owned ( ) ) ;
71+ segments . push ( v. into_owned ( ) ) ;
7072 }
7173
7274 input = fdb_util:: parse_code ( input, fdb_util:: codes:: NIL ) ?;
7375
74- Ok ( ( input, Key ( vec) ) )
76+ Ok ( (
77+ input,
78+ Key {
79+ inner : kv:: Key { segments } ,
80+ } ,
81+ ) )
7582 }
7683}
7784
7885/// 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 > > ) ;
86+ #[ derive( Clone ) ]
87+ pub struct ListKey {
88+ inner : kv:: Key ,
89+ }
8190
8291impl std:: fmt:: Debug for ListKey {
8392 fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
@@ -96,7 +105,7 @@ impl TuplePack for ListKey {
96105 w. write_all ( & [ fdb_util:: codes:: NESTED ] ) ?;
97106 offset += 1 ;
98107
99- for v in & self . 0 {
108+ for v in & self . inner . segments {
100109 offset += v. pack ( w, tuple_depth. increment ( ) ) ?;
101110 }
102111
@@ -108,8 +117,7 @@ impl TuplePack for ListKey {
108117
109118impl ListKey {
110119 pub fn len ( & self ) -> usize {
111- // Arbitrary 4 accounting for nesting overhead
112- self . 0 . iter ( ) . fold ( 0 , |acc, x| acc + x. len ( ) ) + 4 * self . 0 . len ( )
120+ self . inner . encoded_len ( )
113121 }
114122}
115123
0 commit comments