@@ -61,7 +61,7 @@ use p2p::{
61
61
RelayConfig , RequestResponseConfig , SwarmConfig , TransportConfig ,
62
62
} ;
63
63
use repo:: {
64
- BlockStore , DataStore , GCConfig , GCTrigger , Lock , RepoFetch , RepoInsertPin , RepoRemovePin ,
64
+ default_impl :: DefaultStorage , GCConfig , GCTrigger , RepoFetch , RepoInsertPin , RepoRemovePin ,
65
65
} ;
66
66
67
67
use tracing:: Span ;
@@ -87,8 +87,8 @@ pub use self::{
87
87
use async_rt:: { AbortableJoinHandle , CommunicationTask } ;
88
88
use ipld_core:: cid:: Cid ;
89
89
use ipld_core:: ipld:: Ipld ;
90
- use std:: borrow:: Borrow ;
91
90
use std:: convert:: Infallible ;
91
+ use std:: { borrow:: Borrow , path:: PathBuf } ;
92
92
use std:: {
93
93
collections:: { BTreeSet , HashMap , HashSet } ,
94
94
fmt,
@@ -126,51 +126,6 @@ use libp2p::{request_response::InboundRequestId, swarm::dial_opts::PeerCondition
126
126
pub use libp2p_connection_limits:: ConnectionLimits ;
127
127
use serde:: Serialize ;
128
128
129
- #[ allow( dead_code) ]
130
- #[ deprecated( note = "Use `StoreageType` instead" ) ]
131
- type StoragePath = StorageType ;
132
-
133
- #[ derive( Default , Debug ) ]
134
- pub enum StorageType {
135
- #[ cfg( not( target_arch = "wasm32" ) ) ]
136
- Disk ( std:: path:: PathBuf ) ,
137
- #[ default]
138
- Memory ,
139
- #[ cfg( target_arch = "wasm32" ) ]
140
- IndexedDb { namespace : Option < String > } ,
141
- Custom {
142
- blockstore : Option < Box < dyn BlockStore > > ,
143
- datastore : Option < Box < dyn DataStore > > ,
144
- lock : Option < Box < dyn Lock > > ,
145
- } ,
146
- }
147
-
148
- impl PartialEq for StorageType {
149
- fn eq ( & self , other : & Self ) -> bool {
150
- match ( self , other) {
151
- #[ cfg( not( target_arch = "wasm32" ) ) ]
152
- ( StorageType :: Disk ( left_path) , StorageType :: Disk ( right_path) ) => {
153
- left_path. eq ( right_path)
154
- }
155
- #[ cfg( target_arch = "wasm32" ) ]
156
- (
157
- StorageType :: IndexedDb { namespace : left } ,
158
- StorageType :: IndexedDb { namespace : right } ,
159
- ) => left. eq ( right) ,
160
- ( StorageType :: Memory , StorageType :: Memory ) => true ,
161
- ( StorageType :: Custom { .. } , StorageType :: Custom { .. } ) => {
162
- //Do we really care if they equal?
163
- //TODO: Possibly implement PartialEq/Eq for the traits so we could make sure
164
- // that they do or dont eq each other. For now this will always be true
165
- true
166
- }
167
- _ => false ,
168
- }
169
- }
170
- }
171
-
172
- impl Eq for StorageType { }
173
-
174
129
/// Ipfs node options used to configure the node to be created with [`UninitializedIpfs`].
175
130
struct IpfsOptions {
176
131
/// The path of the ipfs repo (blockstore and datastore).
@@ -182,7 +137,11 @@ struct IpfsOptions {
182
137
///
183
138
/// It is **not** recommended to set this to IPFS_PATH without first at least backing up your
184
139
/// existing repository.
185
- pub ipfs_path : StorageType ,
140
+ pub ipfs_path : Option < PathBuf > ,
141
+
142
+ /// Enables and supply a name of the namespace used for indexeddb
143
+ #[ cfg( target_arch = "wasm32" ) ]
144
+ pub namespace : Option < Option < String > > ,
186
145
187
146
/// Nodes used as bootstrap peers.
188
147
pub bootstrap : Vec < Multiaddr > ,
@@ -285,7 +244,9 @@ pub enum RepoProvider {
285
244
impl Default for IpfsOptions {
286
245
fn default ( ) -> Self {
287
246
Self {
288
- ipfs_path : StorageType :: Memory ,
247
+ ipfs_path : None ,
248
+ #[ cfg( target_arch = "wasm32" ) ]
249
+ namespace : None ,
289
250
bootstrap : Default :: default ( ) ,
290
251
relay_server_config : Default :: default ( ) ,
291
252
kad_configuration : Either :: Left ( Default :: default ( ) ) ,
@@ -333,7 +294,7 @@ impl fmt::Debug for IpfsOptions {
333
294
#[ allow( clippy:: type_complexity) ]
334
295
pub struct Ipfs {
335
296
span : Span ,
336
- repo : Repo ,
297
+ repo : Repo < DefaultStorage > ,
337
298
key : Keypair ,
338
299
keystore : Keystore ,
339
300
identify_conf : IdentifyConfiguration ,
@@ -557,7 +518,7 @@ pub struct UninitializedIpfs<C: NetworkBehaviour<ToSwarm = Infallible> + Send> {
557
518
keys : Option < Keypair > ,
558
519
options : IpfsOptions ,
559
520
fdlimit : Option < FDLimit > ,
560
- repo_handle : Option < Repo > ,
521
+ repo_handle : Repo < DefaultStorage > ,
561
522
local_external_addr : bool ,
562
523
swarm_event : Option < TSwarmEventFn < C > > ,
563
524
// record_validators: HashMap<String, Arc<dyn Fn(&str, &Record) -> bool + Sync + Send>>,
@@ -583,7 +544,7 @@ impl<C: NetworkBehaviour<ToSwarm = Infallible> + Send> UninitializedIpfs<C> {
583
544
keys : None ,
584
545
options : Default :: default ( ) ,
585
546
fdlimit : None ,
586
- repo_handle : None ,
547
+ repo_handle : Repo :: new_memory ( ) ,
587
548
// record_validators: Default::default(),
588
549
record_key_validator : Default :: default ( ) ,
589
550
local_external_addr : false ,
@@ -604,10 +565,10 @@ impl<C: NetworkBehaviour<ToSwarm = Infallible> + Send> UninitializedIpfs<C> {
604
565
}
605
566
606
567
/// Set storage type for the repo.
607
- pub fn set_storage_type ( mut self , storage_type : StorageType ) -> Self {
608
- self . options . ipfs_path = storage_type;
609
- self
610
- }
568
+ // pub fn set_storage_type(mut self, storage_type: StorageType) -> Self {
569
+ // self.options.ipfs_path = storage_type;
570
+ // self
571
+ // }
611
572
612
573
/// Adds a listening address
613
574
pub fn add_listening_addr ( mut self , addr : Multiaddr ) -> Self {
@@ -792,7 +753,14 @@ impl<C: NetworkBehaviour<ToSwarm = Infallible> + Send> UninitializedIpfs<C> {
792
753
#[ cfg( not( target_arch = "wasm32" ) ) ]
793
754
pub fn set_path < P : AsRef < Path > > ( mut self , path : P ) -> Self {
794
755
let path = path. as_ref ( ) . to_path_buf ( ) ;
795
- self . options . ipfs_path = StorageType :: Disk ( path) ;
756
+ self . options . ipfs_path = Some ( path) ;
757
+ self
758
+ }
759
+
760
+ /// Sets a namespace
761
+ #[ cfg( target_arch = "wasm32" ) ]
762
+ pub fn set_namespace ( mut self , ns : Option < String > ) -> Self {
763
+ self . options . namespace = Some ( ns) ;
796
764
self
797
765
}
798
766
@@ -853,8 +821,8 @@ impl<C: NetworkBehaviour<ToSwarm = Infallible> + Send> UninitializedIpfs<C> {
853
821
}
854
822
855
823
/// Set block and data repo
856
- pub fn set_repo ( mut self , repo : & Repo ) -> Self {
857
- self . repo_handle = Some ( repo . clone ( ) ) ;
824
+ pub fn set_repo ( mut self , repo : & Repo < DefaultStorage > ) -> Self {
825
+ self . repo_handle = Repo :: clone ( repo ) ;
858
826
self
859
827
}
860
828
@@ -940,23 +908,32 @@ impl<C: NetworkBehaviour<ToSwarm = Infallible> + Send> UninitializedIpfs<C> {
940
908
// instruments the IpfsFuture, the background task.
941
909
let swarm_span = tracing:: trace_span!( parent: & root_span, "swarm" ) ;
942
910
943
- let repo = match repo_handle {
944
- Some ( repo ) => {
945
- if repo. is_online ( ) {
946
- anyhow:: bail!( "Repo is already initialized" ) ;
947
- }
948
- repo
949
- }
950
- None => {
951
- # [ cfg ( not ( target_arch = "wasm32" ) ) ]
952
- if let StorageType :: Disk ( path) = & options . ipfs_path {
911
+ let mut repo = repo_handle;
912
+
913
+ if repo. is_online ( ) {
914
+ anyhow:: bail!( "Repo is already initialized" ) ;
915
+ }
916
+
917
+ # [ cfg ( not ( target_arch = "wasm32" ) ) ]
918
+ {
919
+ repo = match & options . ipfs_path {
920
+ Some ( path) => {
953
921
if !path. is_dir ( ) {
954
922
tokio:: fs:: create_dir_all ( path) . await ?;
955
923
}
924
+ Repo :: < DefaultStorage > :: new_fs ( path)
956
925
}
957
- Repo :: new ( & mut options. ipfs_path )
958
- }
959
- } ;
926
+ None => repo,
927
+ } ;
928
+ }
929
+
930
+ #[ cfg( target_arch = "wasm32" ) ]
931
+ {
932
+ repo = match options. namespace . take ( ) {
933
+ Some ( ns) => Repo :: < DefaultStorage > :: new_idb ( ns) ,
934
+ None => repo,
935
+ } ;
936
+ }
960
937
961
938
repo. init ( ) . instrument ( init_span. clone ( ) ) . await ?;
962
939
@@ -1039,7 +1016,7 @@ impl<C: NetworkBehaviour<ToSwarm = Infallible> + Send> UninitializedIpfs<C> {
1039
1016
1040
1017
let gc_handle = gc_config. map ( |config| {
1041
1018
async_rt:: task:: spawn_abortable ( {
1042
- let repo = repo . clone ( ) ;
1019
+ let repo = Repo :: clone ( & repo ) ;
1043
1020
async move {
1044
1021
let GCConfig { duration, trigger } = config;
1045
1022
let use_config_timer = duration != Duration :: ZERO ;
@@ -1176,7 +1153,7 @@ impl Ipfs {
1176
1153
}
1177
1154
1178
1155
/// Return an [`Repo`] to access the internal repo of the node
1179
- pub fn repo ( & self ) -> & Repo {
1156
+ pub fn repo ( & self ) -> & Repo < DefaultStorage > {
1180
1157
& self . repo
1181
1158
}
1182
1159
@@ -1191,13 +1168,13 @@ impl Ipfs {
1191
1168
}
1192
1169
1193
1170
/// Puts a block into the ipfs repo.
1194
- pub fn put_block ( & self , block : & Block ) -> RepoPutBlock {
1171
+ pub fn put_block ( & self , block : & Block ) -> RepoPutBlock < DefaultStorage > {
1195
1172
self . repo . put_block ( block) . span ( self . span . clone ( ) )
1196
1173
}
1197
1174
1198
1175
/// Retrieves a block from the local blockstore, or starts fetching from the network or join an
1199
1176
/// already started fetch.
1200
- pub fn get_block ( & self , cid : impl Borrow < Cid > ) -> RepoGetBlock {
1177
+ pub fn get_block ( & self , cid : impl Borrow < Cid > ) -> RepoGetBlock < DefaultStorage > {
1201
1178
self . repo . get_block ( cid) . span ( self . span . clone ( ) )
1202
1179
}
1203
1180
@@ -1239,7 +1216,7 @@ impl Ipfs {
1239
1216
/// If a recursive `insert_pin` operation is interrupted because of a crash or the crash
1240
1217
/// prevents from synchronizing the data store to disk, this will leave the system in an inconsistent
1241
1218
/// state. The remedy is to re-pin recursive pins.
1242
- pub fn insert_pin ( & self , cid : impl Borrow < Cid > ) -> RepoInsertPin {
1219
+ pub fn insert_pin ( & self , cid : impl Borrow < Cid > ) -> RepoInsertPin < DefaultStorage > {
1243
1220
self . repo ( ) . pin ( cid) . span ( self . span . clone ( ) )
1244
1221
}
1245
1222
@@ -1249,7 +1226,7 @@ impl Ipfs {
1249
1226
///
1250
1227
/// Unpinning an indirectly pinned Cid is not possible other than through its recursively
1251
1228
/// pinned tree roots.
1252
- pub fn remove_pin ( & self , cid : impl Borrow < Cid > ) -> RepoRemovePin {
1229
+ pub fn remove_pin ( & self , cid : impl Borrow < Cid > ) -> RepoRemovePin < DefaultStorage > {
1253
1230
self . repo ( ) . remove_pin ( cid) . span ( self . span . clone ( ) )
1254
1231
}
1255
1232
@@ -2131,7 +2108,7 @@ impl Ipfs {
2131
2108
}
2132
2109
2133
2110
/// Fetches the block, and, if set, recursively walk the graph loading all the blocks to the blockstore.
2134
- pub fn fetch ( & self , cid : & Cid ) -> RepoFetch {
2111
+ pub fn fetch ( & self , cid : & Cid ) -> RepoFetch < DefaultStorage > {
2135
2112
self . repo . fetch ( cid) . span ( self . span . clone ( ) )
2136
2113
}
2137
2114
@@ -3085,6 +3062,7 @@ pub use node::Node;
3085
3062
3086
3063
/// Node module provides an easy to use interface used in `tests/`.
3087
3064
mod node {
3065
+
3088
3066
use super :: * ;
3089
3067
3090
3068
/// Node encapsulates everything to setup a testing instance so that multi-node tests become
0 commit comments