1
1
//! ObjectStore implemented based on fs
2
2
3
+ use std:: collections:: HashMap ;
3
4
use std:: fs:: create_dir_all;
4
5
use std:: path:: { Path , PathBuf , MAIN_SEPARATOR } ;
6
+ use std:: sync:: Arc ;
5
7
6
8
use anyhow:: { anyhow, Context , Result } ;
9
+ use jsonrpc_core:: futures_util:: { FutureExt , TryFutureExt } ;
7
10
8
- use super :: { ObjResult , ObjectStore } ;
9
- use crate :: logging:: trace;
10
-
11
- const LOG_TARGET : & str = "filestore" ;
11
+ use super :: { ObjResult , ObjectStore , ObjectStoreError } ;
12
+ use crate :: { logging:: trace, rpc:: sealer:: SealerClient , sealing:: call_rpc} ;
12
13
13
14
/// FileStore
14
15
pub struct FileStore {
15
- sep : String ,
16
16
local_path : PathBuf ,
17
17
instance : String ,
18
18
readonly : bool ,
19
+ rpc : Arc < SealerClient > ,
19
20
}
20
21
21
22
impl FileStore {
@@ -27,7 +28,7 @@ impl FileStore {
27
28
}
28
29
29
30
/// open the file store at given path
30
- pub fn open < P : AsRef < Path > > ( p : P , ins : Option < String > , readonly : bool ) -> Result < Self > {
31
+ pub fn open < P : AsRef < Path > > ( p : P , ins : Option < String > , readonly : bool , rpc : Arc < SealerClient > ) -> Result < Self > {
31
32
let dir_path = p. as_ref ( ) . canonicalize ( ) . context ( "canonicalize dir path" ) ?;
32
33
if !dir_path. metadata ( ) . context ( "read dir metadata" ) . map ( |meta| meta. is_dir ( ) ) ? {
33
34
return Err ( anyhow ! ( "base path of the file store should a dir" ) ) ;
@@ -39,67 +40,31 @@ impl FileStore {
39
40
} ;
40
41
41
42
Ok ( FileStore {
42
- sep : MAIN_SEPARATOR . to_string ( ) ,
43
43
local_path : dir_path,
44
44
instance,
45
45
readonly,
46
+ rpc,
46
47
} )
47
48
}
48
-
49
- fn path < P : AsRef < Path > > ( & self , sub : P ) -> ObjResult < PathBuf > {
50
- let mut p = sub. as_ref ( ) ;
51
- if p. starts_with ( "." ) {
52
- return Err ( anyhow ! ( "sub path starts with dot" ) . into ( ) ) ;
53
- }
54
-
55
- // try to strip the first any only the first sep
56
- if let Ok ( strip) = p. strip_prefix ( & self . sep ) {
57
- p = strip;
58
- }
59
-
60
- if p. starts_with ( & self . sep ) {
61
- return Err ( anyhow ! ( "sub path starts with separator" ) . into ( ) ) ;
62
- }
63
-
64
- let res = self . local_path . join ( p) ;
65
- trace ! ( target: LOG_TARGET , ?res, "get full path" ) ;
66
- Ok ( res)
67
- }
68
49
}
69
50
70
51
impl ObjectStore for FileStore {
71
52
fn instance ( & self ) -> String {
72
53
self . instance . clone ( )
73
54
}
74
55
75
- fn uri ( & self , rel : & Path ) -> ObjResult < PathBuf > {
76
- self . path ( rel)
56
+ fn uri ( & self , resource_name : & str ) -> ObjResult < String > {
57
+ let uri = call_rpc ! {
58
+ self . rpc=>store_uri(
59
+ self . instance( ) ,
60
+ resource_name. to_string( ) ,
61
+ )
62
+ }
63
+ . map_err ( |e| ObjectStoreError :: Other ( e. 1 ) ) ?;
64
+ Ok ( self . local_path . join ( uri) . display ( ) . to_string ( ) )
77
65
}
78
66
79
67
fn readonly ( & self ) -> bool {
80
68
self . readonly
81
69
}
82
70
}
83
-
84
- #[ cfg( test) ]
85
- mod tests {
86
- use super :: * ;
87
-
88
- #[ test]
89
- fn test_path ( ) {
90
- let fs = FileStore :: open ( "/tmp" , None , false ) . unwrap ( ) ;
91
- assert_eq ! ( fs. path( "/a/b" ) . unwrap( ) , PathBuf :: from( "/tmp/a/b" ) ) ;
92
- assert_eq ! ( fs. path( "a/b" ) . unwrap( ) , PathBuf :: from( "/tmp/a/b" ) ) ;
93
- assert_eq ! ( fs. path( "a/b/" ) . unwrap( ) , PathBuf :: from( "/tmp/a/b" ) ) ;
94
- assert_eq ! ( fs. path( "/a/b/" ) . unwrap( ) , PathBuf :: from( "/tmp/a/b" ) ) ;
95
- }
96
-
97
- #[ test]
98
- fn test_store_uri ( ) {
99
- let fs = FileStore :: open ( "/tmp/" , Some ( "test_store" . to_string ( ) ) , false ) . unwrap ( ) ;
100
- assert_eq ! ( fs. uri( Path :: new( "a/b" ) ) . unwrap( ) , PathBuf :: from( "/tmp/a/b" ) ) ;
101
- assert_eq ! ( fs. uri( Path :: new( "/a/b" ) ) . unwrap( ) , PathBuf :: from( "/tmp/a/b" ) ) ;
102
- assert_eq ! ( fs. uri( Path :: new( "a/b/" ) ) . unwrap( ) , PathBuf :: from( "/tmp/a/b" ) ) ;
103
- assert_eq ! ( fs. uri( Path :: new( "/a/b/" ) ) . unwrap( ) , PathBuf :: from( "/tmp/a/b" ) ) ;
104
- }
105
- }
0 commit comments