1
1
use foundationdb:: tuple:: {
2
2
Bytes , PackResult , TupleDepth , TuplePack , TupleUnpack , VersionstampOffset ,
3
3
} ;
4
- use serde:: { Deserialize , Serialize } ;
4
+ use pegboard_config:: runner_protocol:: proto:: kv;
5
+ use prost:: Message ;
5
6
6
7
// 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
+ }
9
12
10
13
impl std:: fmt:: Debug for Key {
11
14
fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
@@ -15,24 +18,23 @@ impl std::fmt::Debug for Key {
15
18
16
19
impl PartialEq for Key {
17
20
fn eq ( & self , other : & Self ) -> bool {
18
- self . 0 == other. 0
21
+ self . inner == other. inner
19
22
}
20
23
}
21
24
22
25
impl Eq for Key { }
23
26
24
27
impl std:: hash:: Hash for Key {
25
28
fn hash < H : std:: hash:: Hasher > ( & self , state : & mut H ) {
26
- for buffer in & self . 0 {
29
+ for buffer in & self . inner . segments {
27
30
state. write ( buffer) ;
28
31
}
29
32
}
30
33
}
31
34
32
35
impl Key {
33
36
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 ( )
36
38
}
37
39
}
38
40
@@ -47,7 +49,7 @@ impl TuplePack for Key {
47
49
w. write_all ( & [ fdb_util:: codes:: NESTED ] ) ?;
48
50
offset += 1 ;
49
51
50
- for v in self . 0 . iter ( ) {
52
+ for v in self . inner . segments . iter ( ) {
51
53
offset += v. pack ( w, tuple_depth. increment ( ) ) ?;
52
54
}
53
55
@@ -62,22 +64,29 @@ impl<'de> TupleUnpack<'de> for Key {
62
64
fn unpack ( mut input : & [ u8 ] , tuple_depth : TupleDepth ) -> PackResult < ( & [ u8 ] , Self ) > {
63
65
input = fdb_util:: parse_code ( input, fdb_util:: codes:: NESTED ) ?;
64
66
65
- let mut vec = Vec :: new ( ) ;
67
+ let mut segments = Vec :: new ( ) ;
66
68
while !is_end_of_tuple ( input, true ) {
67
69
let ( rem, v) = Bytes :: unpack ( input, tuple_depth. increment ( ) ) ?;
68
70
input = rem;
69
- vec . push ( v. into_owned ( ) ) ;
71
+ segments . push ( v. into_owned ( ) ) ;
70
72
}
71
73
72
74
input = fdb_util:: parse_code ( input, fdb_util:: codes:: NIL ) ?;
73
75
74
- Ok ( ( input, Key ( vec) ) )
76
+ Ok ( (
77
+ input,
78
+ Key {
79
+ inner : kv:: Key { segments } ,
80
+ } ,
81
+ ) )
75
82
}
76
83
}
77
84
78
85
/// 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
+ }
81
90
82
91
impl std:: fmt:: Debug for ListKey {
83
92
fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
@@ -96,7 +105,7 @@ impl TuplePack for ListKey {
96
105
w. write_all ( & [ fdb_util:: codes:: NESTED ] ) ?;
97
106
offset += 1 ;
98
107
99
- for v in & self . 0 {
108
+ for v in & self . inner . segments {
100
109
offset += v. pack ( w, tuple_depth. increment ( ) ) ?;
101
110
}
102
111
@@ -108,8 +117,7 @@ impl TuplePack for ListKey {
108
117
109
118
impl ListKey {
110
119
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 ( )
113
121
}
114
122
}
115
123
0 commit comments