@@ -5,7 +5,7 @@ pub mod test_utils;
5
5
pub mod utils;
6
6
pub mod versioned;
7
7
8
- use std:: { ops:: Deref , str:: FromStr } ;
8
+ use std:: { marker :: PhantomData , ops:: Deref , str:: FromStr } ;
9
9
10
10
use alloy:: primitives:: { Bytes , B256 } ;
11
11
pub use config:: { PortalStorageConfig , PortalStorageConfigFactory } ;
@@ -22,21 +22,6 @@ use rusqlite::types::{FromSql, FromSqlError, ValueRef};
22
22
23
23
pub const DATABASE_NAME : & str = "trin.sqlite" ;
24
24
25
- // TODO: Replace enum with generic type parameter. This will require that we have a way to
26
- // associate a "find farthest" query with the generic Metric.
27
- #[ derive( Copy , Clone , Debug ) ]
28
- pub enum DistanceFunction {
29
- Xor ,
30
- }
31
-
32
- impl DistanceFunction {
33
- pub fn distance ( & self , node_id : & NodeId , other : & [ u8 ; 32 ] ) -> Distance {
34
- match self {
35
- DistanceFunction :: Xor => XorMetric :: distance ( & node_id. raw ( ) , other) ,
36
- }
37
- }
38
- }
39
-
40
25
/// An enum which tells us if we should store or not store content, and if not why for better
41
26
/// errors.
42
27
#[ derive( Debug , PartialEq ) ]
@@ -76,25 +61,25 @@ pub trait ContentStore {
76
61
}
77
62
78
63
/// An in-memory `ContentStore`.
79
- pub struct MemoryContentStore {
64
+ pub struct MemoryContentStore < TMetric : Metric = XorMetric > {
80
65
/// The content store.
81
66
store : std:: collections:: HashMap < Vec < u8 > , RawContentValue > ,
82
67
/// The `NodeId` of the local node.
83
68
node_id : NodeId ,
84
- /// The distance function used by the store to compute distances.
85
- distance_fn : DistanceFunction ,
86
69
/// The radius of the store.
87
70
radius : Distance ,
71
+ /// Phantom metric
72
+ _metric : PhantomData < TMetric > ,
88
73
}
89
74
90
- impl MemoryContentStore {
75
+ impl < TMetric : Metric > MemoryContentStore < TMetric > {
91
76
/// Constructs a new `MemoryPortalContentStore`.
92
- pub fn new ( node_id : NodeId , distance_fn : DistanceFunction ) -> Self {
77
+ pub fn new ( node_id : NodeId ) -> Self {
93
78
Self {
94
79
store : std:: collections:: HashMap :: new ( ) ,
95
80
node_id,
96
- distance_fn,
97
81
radius : Distance :: MAX ,
82
+ _metric : PhantomData ,
98
83
}
99
84
}
100
85
@@ -105,7 +90,7 @@ impl MemoryContentStore {
105
90
106
91
/// Returns the distance to `key` from the local `NodeId` according to the distance function.
107
92
fn distance_to_key < K : OverlayContentKey > ( & self , key : & K ) -> Distance {
108
- self . distance_fn . distance ( & self . node_id , & key. content_id ( ) )
93
+ TMetric :: distance ( & self . node_id . raw ( ) , & key. content_id ( ) )
109
94
}
110
95
111
96
/// Returns `true` if the content store contains data for `key`.
@@ -115,7 +100,7 @@ impl MemoryContentStore {
115
100
}
116
101
}
117
102
118
- impl ContentStore for MemoryContentStore {
103
+ impl < TMetric : Metric > ContentStore for MemoryContentStore < TMetric > {
119
104
type Key = IdentityContentKey ;
120
105
121
106
fn get ( & self , key : & Self :: Key ) -> Result < Option < RawContentValue > , ContentStoreError > {
@@ -208,14 +193,14 @@ pub struct DataSize {
208
193
#[ allow( clippy:: unwrap_used) ]
209
194
pub mod test {
210
195
use alloy:: primitives:: { bytes, B512 } ;
211
- use ethportal_api:: IdentityContentKey ;
196
+ use ethportal_api:: { types :: distance :: XorMetric , IdentityContentKey } ;
212
197
213
198
use super :: * ;
214
199
215
200
#[ test]
216
201
fn memory_store_contains_key ( ) {
217
202
let node_id = NodeId :: random ( ) ;
218
- let mut store = MemoryContentStore :: new ( node_id, DistanceFunction :: Xor ) ;
203
+ let mut store = MemoryContentStore :: < XorMetric > :: new ( node_id) ;
219
204
220
205
let val = vec ! [ 0xef ] ;
221
206
@@ -231,7 +216,7 @@ pub mod test {
231
216
#[ test]
232
217
fn memory_store_get ( ) {
233
218
let node_id = NodeId :: random ( ) ;
234
- let mut store = MemoryContentStore :: new ( node_id, DistanceFunction :: Xor ) ;
219
+ let mut store = MemoryContentStore :: < XorMetric > :: new ( node_id) ;
235
220
236
221
let val = bytes ! ( "ef" ) ;
237
222
@@ -247,7 +232,7 @@ pub mod test {
247
232
#[ test]
248
233
fn memory_store_put ( ) {
249
234
let node_id = NodeId :: random ( ) ;
250
- let mut store = MemoryContentStore :: new ( node_id, DistanceFunction :: Xor ) ;
235
+ let mut store = MemoryContentStore :: < XorMetric > :: new ( node_id) ;
251
236
252
237
let val = bytes ! ( "ef" ) ;
253
238
@@ -260,7 +245,7 @@ pub mod test {
260
245
#[ test]
261
246
fn memory_store_is_within_radius_and_unavailable ( ) {
262
247
let node_id = NodeId :: random ( ) ;
263
- let mut store = MemoryContentStore :: new ( node_id, DistanceFunction :: Xor ) ;
248
+ let mut store = MemoryContentStore :: < XorMetric > :: new ( node_id) ;
264
249
265
250
let val = bytes ! ( "ef" ) ;
266
251
0 commit comments