@@ -7,6 +7,19 @@ use tracing::debug;
7
7
const DEFAULT_COLUMN : & str = "" ;
8
8
const PJ_V1_COLUMN : & str = "pjv1" ;
9
9
10
+ // TODO move to payjoin crate as pub?
11
+ // TODO impl From<HpkePublicKey> for ShortId
12
+ // TODO impl Display for ShortId (Base64)
13
+ // TODO impl TryFrom<&str> for ShortId (Base64)
14
+ #[ derive( Debug , Clone , Copy , PartialEq , Eq ) ]
15
+ pub ( crate ) struct ShortId ( pub [ u8 ; 8 ] ) ;
16
+
17
+ impl ShortId {
18
+ pub fn column_key ( & self , column : & str ) -> Vec < u8 > {
19
+ self . 0 . iter ( ) . chain ( column. as_bytes ( ) ) . copied ( ) . collect ( )
20
+ }
21
+ }
22
+
10
23
#[ derive( Debug , Clone ) ]
11
24
pub ( crate ) struct DbPool {
12
25
client : Client ,
@@ -19,23 +32,28 @@ impl DbPool {
19
32
Ok ( Self { client, timeout } )
20
33
}
21
34
22
- pub async fn push_default ( & self , pubkey_id : & str , data : Vec < u8 > ) -> RedisResult < ( ) > {
35
+ pub async fn push_default ( & self , pubkey_id : & ShortId , data : Vec < u8 > ) -> RedisResult < ( ) > {
23
36
self . push ( pubkey_id, DEFAULT_COLUMN , data) . await
24
37
}
25
38
26
- pub async fn peek_default ( & self , pubkey_id : & str ) -> Option < RedisResult < Vec < u8 > > > {
39
+ pub async fn peek_default ( & self , pubkey_id : & ShortId ) -> Option < RedisResult < Vec < u8 > > > {
27
40
self . peek_with_timeout ( pubkey_id, DEFAULT_COLUMN ) . await
28
41
}
29
42
30
- pub async fn push_v1 ( & self , pubkey_id : & str , data : Vec < u8 > ) -> RedisResult < ( ) > {
43
+ pub async fn push_v1 ( & self , pubkey_id : & ShortId , data : Vec < u8 > ) -> RedisResult < ( ) > {
31
44
self . push ( pubkey_id, PJ_V1_COLUMN , data) . await
32
45
}
33
46
34
- pub async fn peek_v1 ( & self , pubkey_id : & str ) -> Option < RedisResult < Vec < u8 > > > {
47
+ pub async fn peek_v1 ( & self , pubkey_id : & ShortId ) -> Option < RedisResult < Vec < u8 > > > {
35
48
self . peek_with_timeout ( pubkey_id, PJ_V1_COLUMN ) . await
36
49
}
37
50
38
- async fn push ( & self , pubkey_id : & str , channel_type : & str , data : Vec < u8 > ) -> RedisResult < ( ) > {
51
+ async fn push (
52
+ & self ,
53
+ pubkey_id : & ShortId ,
54
+ channel_type : & str ,
55
+ data : Vec < u8 > ,
56
+ ) -> RedisResult < ( ) > {
39
57
let mut conn = self . client . get_async_connection ( ) . await ?;
40
58
let key = channel_name ( pubkey_id, channel_type) ;
41
59
( ) = conn. set ( & key, data. clone ( ) ) . await ?;
@@ -45,13 +63,13 @@ impl DbPool {
45
63
46
64
async fn peek_with_timeout (
47
65
& self ,
48
- pubkey_id : & str ,
66
+ pubkey_id : & ShortId ,
49
67
channel_type : & str ,
50
68
) -> Option < RedisResult < Vec < u8 > > > {
51
69
tokio:: time:: timeout ( self . timeout , self . peek ( pubkey_id, channel_type) ) . await . ok ( )
52
70
}
53
71
54
- async fn peek ( & self , pubkey_id : & str , channel_type : & str ) -> RedisResult < Vec < u8 > > {
72
+ async fn peek ( & self , pubkey_id : & ShortId , channel_type : & str ) -> RedisResult < Vec < u8 > > {
55
73
let mut conn = self . client . get_async_connection ( ) . await ?;
56
74
let key = channel_name ( pubkey_id, channel_type) ;
57
75
@@ -99,6 +117,6 @@ impl DbPool {
99
117
}
100
118
}
101
119
102
- fn channel_name ( pubkey_id : & str , channel_type : & str ) -> String {
103
- format ! ( "{}:{}" , pubkey_id, channel_type)
120
+ fn channel_name ( pubkey_id : & ShortId , channel_type : & str ) -> Vec < u8 > {
121
+ pubkey_id. column_key ( channel_type)
104
122
}
0 commit comments