2
2
3
3
use crate :: feed_builder:: FeedBuilder ;
4
4
use crate :: replicate:: { Message , Peer } ;
5
- pub use crate :: storage:: { Node , NodeTrait , Storage , Store } ;
5
+ pub use crate :: storage:: {
6
+ storage_disk, storage_memory, BoxStorage , Node , NodeTrait , Storage , Store ,
7
+ } ;
6
8
7
9
use crate :: audit:: Audit ;
8
10
use crate :: bitfield:: Bitfield ;
@@ -12,12 +14,8 @@ use anyhow::{bail, ensure, Result};
12
14
use ed25519_dalek:: { PublicKey , SecretKey , Signature } ;
13
15
use flat_tree as flat;
14
16
use pretty_hash:: fmt as pretty_fmt;
15
- use random_access_disk:: RandomAccessDisk ;
16
- use random_access_memory:: RandomAccessMemory ;
17
- use random_access_storage:: RandomAccess ;
18
17
use tree_index:: TreeIndex ;
19
18
20
- use std:: borrow:: Borrow ;
21
19
use std:: cmp;
22
20
use std:: fmt:: { self , Debug , Display } ;
23
21
use std:: ops:: Range ;
@@ -26,15 +24,12 @@ use std::sync::Arc;
26
24
27
25
/// Append-only log structure.
28
26
#[ derive( Debug ) ]
29
- pub struct Feed < T >
30
- where
31
- T : RandomAccess < Error = Box < dyn std:: error:: Error + Send + Sync > > + Debug ,
32
- {
27
+ pub struct Feed {
33
28
/// Merkle tree instance.
34
29
pub ( crate ) merkle : Merkle ,
35
30
pub ( crate ) public_key : PublicKey ,
36
31
pub ( crate ) secret_key : Option < SecretKey > ,
37
- pub ( crate ) storage : Storage < T > ,
32
+ pub ( crate ) storage : BoxStorage ,
38
33
/// Total length of data stored.
39
34
pub ( crate ) byte_length : u64 ,
40
35
/// TODO: description. Length of... roots?
45
40
pub ( crate ) peers : Vec < Peer > ,
46
41
}
47
42
48
- impl < T > Feed < T >
49
- where
50
- T : RandomAccess < Error = Box < dyn std:: error:: Error + Send + Sync > > + Debug + Send ,
51
- {
43
+ impl Feed {
52
44
/// Create a new instance with a custom storage backend.
53
- pub async fn with_storage ( mut storage : crate :: storage :: Storage < T > ) -> Result < Self > {
45
+ pub async fn with_storage ( mut storage : BoxStorage ) -> Result < Self > {
54
46
match storage. read_partial_keypair ( ) . await {
55
47
Some ( partial_keypair) => {
56
48
let builder = FeedBuilder :: new ( partial_keypair. public , storage) ;
@@ -76,10 +68,26 @@ where
76
68
}
77
69
78
70
/// Starts a `FeedBuilder` with the provided `Keypair` and `Storage`.
79
- pub fn builder ( public_key : PublicKey , storage : Storage < T > ) -> FeedBuilder < T > {
71
+ pub fn builder ( public_key : PublicKey , storage : BoxStorage ) -> FeedBuilder {
80
72
FeedBuilder :: new ( public_key, storage)
81
73
}
82
74
75
+ /// Create a new instance that persists to disk at the location of `dir`.
76
+ // TODO: Ensure that dir is always a directory.
77
+ // NOTE: Should we `mkdirp` here?
78
+ // NOTE: Should we call these `data.bitfield` / `data.tree`?
79
+ pub async fn open_from_disk < P : AsRef < Path > > ( path : P ) -> Result < Self > {
80
+ let dir = path. as_ref ( ) . to_owned ( ) ;
81
+ let storage = storage_disk ( & dir) . await ?;
82
+ Self :: with_storage ( storage) . await
83
+ }
84
+
85
+ /// Create a new in-memory instance.
86
+ pub async fn open_in_memory ( ) -> Result < Self > {
87
+ let storage = storage_memory ( ) . await . unwrap ( ) ;
88
+ Self :: with_storage ( storage) . await
89
+ }
90
+
83
91
/// Get the amount of entries in the feed.
84
92
#[ inline]
85
93
pub fn len ( & self ) -> u64 {
@@ -116,7 +124,7 @@ where
116
124
let hash = Hash :: from_roots ( self . merkle . roots ( ) ) ;
117
125
let index = self . length ;
118
126
let signature = sign ( & self . public_key , key, hash. as_bytes ( ) ) ;
119
- self . storage . put_signature ( index, signature) . await ?;
127
+ self . storage . put_signature ( index, & signature) . await ?;
120
128
121
129
for node in self . merkle . nodes ( ) {
122
130
self . storage . put_node ( node) . await ?;
@@ -352,8 +360,7 @@ where
352
360
}
353
361
354
362
if let Some ( sig) = sig {
355
- let sig = sig. borrow ( ) ;
356
- self . storage . put_signature ( index, sig) . await ?;
363
+ self . storage . put_signature ( index, & sig) . await ?;
357
364
}
358
365
359
366
for node in nodes {
@@ -556,35 +563,18 @@ where
556
563
}
557
564
}
558
565
559
- impl Feed < RandomAccessDisk > {
560
- /// Create a new instance that persists to disk at the location of `dir`.
561
- // TODO: Ensure that dir is always a directory.
562
- // NOTE: Should we `mkdirp` here?
563
- // NOTE: Should we call these `data.bitfield` / `data.tree`?
564
- pub async fn open < P : AsRef < Path > > ( path : P ) -> Result < Self > {
565
- let dir = path. as_ref ( ) . to_owned ( ) ;
566
- let storage = Storage :: new_disk ( & dir) . await ?;
567
- Self :: with_storage ( storage) . await
568
- }
569
- }
570
-
571
566
/// Create a new instance with an in-memory storage backend.
572
567
///
573
568
/// ## Panics
574
569
/// Can panic if constructing the in-memory store fails, which is highly
575
570
/// unlikely.
576
- impl Default for Feed < RandomAccessMemory > {
571
+ impl Default for Feed {
577
572
fn default ( ) -> Self {
578
- async_std:: task:: block_on ( async {
579
- let storage = Storage :: new_memory ( ) . await . unwrap ( ) ;
580
- Self :: with_storage ( storage) . await . unwrap ( )
581
- } )
573
+ async_std:: task:: block_on ( async { Self :: open_in_memory ( ) . await . unwrap ( ) } )
582
574
}
583
575
}
584
576
585
- impl < T : RandomAccess < Error = Box < dyn std:: error:: Error + Send + Sync > > + Debug + Send > Display
586
- for Feed < T >
587
- {
577
+ impl Display for Feed {
588
578
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
589
579
// TODO: yay, we should find a way to convert this .unwrap() to an error
590
580
// type that's accepted by `fmt::Result<(), fmt::Error>`.
0 commit comments