@@ -20,49 +20,49 @@ use aw_models::{Bucket, Event};
20
20
21
21
use crate :: accessmethod:: AccessMethod ;
22
22
23
- fn setup_local_remote ( client : & AwClient , sync_directory : & Path ) -> Result < Datastore , String > {
24
- // FIXME: Don't run twice if already exists
25
- fs:: create_dir_all ( sync_directory) . unwrap ( ) ;
26
-
27
- let info = client. get_info ( ) . unwrap ( ) ;
28
- let remotedir = sync_directory. join ( info. device_id . as_str ( ) ) ;
29
- fs:: create_dir_all ( & remotedir) . unwrap ( ) ;
30
-
31
- let dbfile = remotedir
32
- . join ( "test.db" )
33
- . into_os_string ( )
34
- . into_string ( )
35
- . unwrap ( ) ;
36
-
37
- let ds_localremote = Datastore :: new ( dbfile, false ) ;
38
- info ! ( "Set up remote for local device" ) ;
23
+ pub struct SyncSpec {
24
+ /// Path of sync folder
25
+ pub path : PathBuf ,
26
+ /// Bucket IDs to sync
27
+ pub buckets : Option < Vec < String > > ,
28
+ /// Start of time range to sync
29
+ pub start : Option < DateTime < Utc > > ,
30
+ }
39
31
40
- Ok ( ds_localremote)
32
+ impl Default for SyncSpec {
33
+ fn default ( ) -> Self {
34
+ // TODO: Better default path
35
+ let path = Path :: new ( "/tmp/aw-sync" ) . to_path_buf ( ) ;
36
+ SyncSpec {
37
+ path,
38
+ buckets : None ,
39
+ start : None ,
40
+ }
41
+ }
41
42
}
42
43
43
44
/// Performs a single sync pass
44
- pub fn sync_run (
45
- sync_directory : & Path ,
46
- client : AwClient ,
47
- buckets : & Vec < String > ,
48
- start : Option < DateTime < Utc > > ,
49
- ) -> Result < ( ) , String > {
50
- let ds_localremote = setup_local_remote ( & client, sync_directory) ?;
45
+ pub fn sync_run ( client : AwClient , sync_spec : & SyncSpec ) -> Result < ( ) , String > {
46
+ let ds_localremote = setup_local_remote ( & client, sync_spec. path . as_path ( ) ) ?;
51
47
52
48
//let ds_remotes = setup_test(sync_directory).unwrap();
53
49
//info!("Set up remotes for testing");
54
50
55
51
let info = client. get_info ( ) . unwrap ( ) ;
56
- let remote_dbfiles = find_remotes_nonlocal ( sync_directory , info. device_id . as_str ( ) ) ;
52
+ let remote_dbfiles = find_remotes_nonlocal ( sync_spec . path . as_path ( ) , info. device_id . as_str ( ) ) ;
57
53
info ! ( "Found remotes: {:?}" , remote_dbfiles) ;
58
54
59
55
// TODO: Check for compatible remote db version before opening
60
- let ds_remotes: Vec < Datastore > = remote_dbfiles. iter ( ) . map ( create_datastore) . collect ( ) ;
56
+ let ds_remotes: Vec < Datastore > = remote_dbfiles
57
+ . iter ( )
58
+ . map ( |p| p. as_path ( ) )
59
+ . map ( create_datastore)
60
+ . collect ( ) ;
61
61
62
62
// Pull
63
63
info ! ( "Pulling..." ) ;
64
64
for ds_from in & ds_remotes {
65
- sync_datastores ( ds_from, & client, false , None , buckets ) ;
65
+ sync_datastores ( ds_from, & client, false , None , sync_spec ) ;
66
66
}
67
67
68
68
// Push local server buckets to sync folder
@@ -72,10 +72,10 @@ pub fn sync_run(
72
72
& ds_localremote,
73
73
true ,
74
74
Some ( info. device_id . as_str ( ) ) ,
75
- buckets ,
75
+ sync_spec ,
76
76
) ;
77
77
78
- list_buckets ( & client, sync_directory ) ;
78
+ list_buckets ( & client, sync_spec . path . as_path ( ) ) ;
79
79
80
80
Ok ( ( ) )
81
81
}
@@ -88,7 +88,11 @@ pub fn list_buckets(client: &AwClient, sync_directory: &Path) {
88
88
info ! ( "Found remotes: {:?}" , remote_dbfiles) ;
89
89
90
90
// TODO: Check for compatible remote db version before opening
91
- let ds_remotes: Vec < Datastore > = remote_dbfiles. iter ( ) . map ( create_datastore) . collect ( ) ;
91
+ let ds_remotes: Vec < Datastore > = remote_dbfiles
92
+ . iter ( )
93
+ . map ( |p| p. as_path ( ) )
94
+ . map ( create_datastore)
95
+ . collect ( ) ;
92
96
93
97
log_buckets ( client) ;
94
98
log_buckets ( & ds_localremote) ;
@@ -97,6 +101,26 @@ pub fn list_buckets(client: &AwClient, sync_directory: &Path) {
97
101
}
98
102
}
99
103
104
+ fn setup_local_remote ( client : & AwClient , path : & Path ) -> Result < Datastore , String > {
105
+ // FIXME: Don't run twice if already exists
106
+ fs:: create_dir_all ( path) . unwrap ( ) ;
107
+
108
+ let info = client. get_info ( ) . unwrap ( ) ;
109
+ let remotedir = path. join ( info. device_id . as_str ( ) ) ;
110
+ fs:: create_dir_all ( & remotedir) . unwrap ( ) ;
111
+
112
+ let dbfile = remotedir
113
+ . join ( "test.db" )
114
+ . into_os_string ( )
115
+ . into_string ( )
116
+ . unwrap ( ) ;
117
+
118
+ let ds_localremote = Datastore :: new ( dbfile, false ) ;
119
+ info ! ( "Set up remote for local device" ) ;
120
+
121
+ Ok ( ds_localremote)
122
+ }
123
+
100
124
/// Returns a list of all remote dbs
101
125
fn find_remotes ( sync_directory : & Path ) -> std:: io:: Result < Vec < PathBuf > > {
102
126
println ! ( "{}" , sync_directory. display( ) ) ;
@@ -130,9 +154,9 @@ fn find_remotes_nonlocal(sync_directory: &Path, device_id: &str) -> Vec<PathBuf>
130
154
. collect ( )
131
155
}
132
156
133
- fn create_datastore ( dspath : & PathBuf ) -> Datastore {
134
- let pathstr = dspath . clone ( ) . into_os_string ( ) . into_string ( ) . unwrap ( ) ;
135
- Datastore :: new ( pathstr, false )
157
+ fn create_datastore ( path : & Path ) -> Datastore {
158
+ let pathstr = path . as_os_str ( ) . to_str ( ) . unwrap ( ) ;
159
+ Datastore :: new ( pathstr. to_string ( ) , false )
136
160
}
137
161
138
162
// TODO: Move into tests
@@ -239,7 +263,7 @@ pub fn sync_datastores(
239
263
ds_to : & dyn AccessMethod ,
240
264
is_push : bool ,
241
265
src_did : Option < & str > ,
242
- buckets : & [ String ] ,
266
+ sync_spec : & SyncSpec ,
243
267
) {
244
268
// FIXME: "-synced" should only be appended when synced to the local database, not to the
245
269
// staging area for local buckets.
@@ -259,7 +283,7 @@ pub fn sync_datastores(
259
283
} )
260
284
// If buckets vec isn't empty, filter out buckets not in the buckets vec
261
285
. filter ( |bucket| {
262
- if buckets . is_empty ( ) {
286
+ if let Some ( buckets ) = & sync_spec . buckets {
263
287
buckets. iter ( ) . any ( |b_id| b_id == & bucket. id )
264
288
} else {
265
289
true
0 commit comments