@@ -12,7 +12,7 @@ use foundationdb::*;
12
12
mod common;
13
13
14
14
#[ test]
15
- fn test_directory ( ) {
15
+ fn test_create_or_open_directory ( ) {
16
16
let _guard = unsafe { foundationdb:: boot ( ) } ;
17
17
let db = futures:: executor:: block_on ( common:: database ( ) ) . expect ( "cannot open fdb" ) ;
18
18
@@ -49,12 +49,63 @@ fn test_directory() {
49
49
. expect ( "failed to run" ) ;
50
50
51
51
futures:: executor:: block_on ( test_bad_layer ( & db) ) . expect ( "failed to run" ) ;
52
+ }
53
+
54
+ #[ test]
55
+ fn test_delete ( ) {
56
+ let _guard = unsafe { foundationdb:: boot ( ) } ;
57
+ let db = futures:: executor:: block_on ( common:: database ( ) ) . expect ( "cannot open fdb" ) ;
58
+
59
+ eprintln ! ( "clearing all keys" ) ;
60
+ let trx = db. create_trx ( ) . expect ( "cannot create txn" ) ;
61
+ trx. clear_range ( b"" , b"\xff " ) ;
62
+ futures:: executor:: block_on ( trx. commit ( ) ) . expect ( "could not clear keys" ) ;
63
+
64
+ eprintln ! ( "creating directories" ) ;
65
+ let directory = DirectoryLayer :: default ( ) ;
66
+
67
+ // test deletions, first we need to create it
68
+ futures:: executor:: block_on ( test_create_or_open_async (
69
+ & db,
70
+ & directory,
71
+ vec ! [ String :: from( "deletion" ) ] ,
72
+ ) )
73
+ . expect ( "failed to run" ) ;
74
+ // then delete it
75
+ futures:: executor:: block_on ( test_delete_async (
76
+ & db,
77
+ & directory,
78
+ vec ! [ String :: from( "deletion" ) ] ,
79
+ ) )
80
+ . expect ( "failed to run" ) ;
81
+
82
+ futures:: executor:: block_on ( test_create_then_delete (
83
+ & db,
84
+ & directory,
85
+ vec ! [ String :: from( "n0" ) ] ,
86
+ 1 ,
87
+ ) )
88
+ . expect ( "failed to run" ) ;
89
+ }
90
+
91
+ #[ test]
92
+ fn test_move ( ) {
93
+ let _guard = unsafe { foundationdb:: boot ( ) } ;
94
+ let db = futures:: executor:: block_on ( common:: database ( ) ) . expect ( "cannot open fdb" ) ;
95
+
96
+ eprintln ! ( "clearing all keys" ) ;
97
+ let trx = db. create_trx ( ) . expect ( "cannot create txn" ) ;
98
+ trx. clear_range ( b"" , b"\xff " ) ;
99
+ futures:: executor:: block_on ( trx. commit ( ) ) . expect ( "could not clear keys" ) ;
100
+
101
+ eprintln ! ( "creating directories" ) ;
102
+ let directory = DirectoryLayer :: default ( ) ;
52
103
53
104
futures:: executor:: block_on ( test_create_then_move_to (
54
105
& db,
55
106
& directory,
56
107
vec ! [ String :: from( "d" ) , String :: from( "e" ) ] ,
57
- vec ! [ String :: from( "a" ) , String :: from ( "g" ) ] ,
108
+ vec ! [ String :: from( "a" ) ] ,
58
109
) )
59
110
. expect ( "failed to run" ) ;
60
111
@@ -130,6 +181,68 @@ fn test_directory() {
130
181
}
131
182
}
132
183
184
+ async fn test_create_then_delete (
185
+ db : & Database ,
186
+ directory : & DirectoryLayer ,
187
+ paths : Vec < String > ,
188
+ sub_path_to_create : usize ,
189
+ ) -> Result < ( ) , DirectoryError > {
190
+ // creating directory
191
+ let trx = db. create_trx ( ) ?;
192
+ directory. create_or_open ( & trx, paths. to_owned ( ) ) . await ?;
193
+
194
+ trx. commit ( ) . await . expect ( "could not commit" ) ;
195
+
196
+ let trx = db. create_trx ( ) ?;
197
+ let children = directory. list ( & trx, paths. to_owned ( ) ) . await ?;
198
+ assert ! ( children. is_empty( ) ) ;
199
+ trx. commit ( ) . await . expect ( "could not commit" ) ;
200
+
201
+ for i in 0 ..sub_path_to_create {
202
+ let trx = db. create_trx ( ) ?;
203
+ let mut sub_path = paths. clone ( ) ;
204
+ let path_name = format ! ( "{}" , i) ;
205
+ sub_path. push ( path_name. to_owned ( ) ) ;
206
+
207
+ // creating subfolders
208
+ eprintln ! ( "creating {:?}" , sub_path. to_owned( ) ) ;
209
+ directory. create ( & trx, sub_path. to_owned ( ) ) . await ;
210
+ trx. commit ( ) . await . expect ( "could not commit" ) ;
211
+
212
+ // checking it does exists
213
+ let trx = db. create_trx ( ) ?;
214
+ eprintln ! ( "trying to get {:?}" , sub_path. to_owned( ) ) ;
215
+ let exists = directory. exists ( & trx, sub_path. to_owned ( ) ) . await ?;
216
+ assert ! ( exists, "path {:?} should exists" , sub_path. to_owned( ) ) ;
217
+ trx. commit ( ) . await . expect ( "could not commit" ) ;
218
+
219
+ let trx = db. create_trx ( ) ?;
220
+ let children = directory. list ( & trx, paths. to_owned ( ) ) . await ?;
221
+ assert ! ( children. contains( & path_name. to_owned( ) ) ) ;
222
+ trx. commit ( ) . await . expect ( "could not commit" ) ;
223
+
224
+ // trying to delete it
225
+ let trx = db. create_trx ( ) ?;
226
+ eprintln ! ( "deleting {:?}" , sub_path. to_owned( ) ) ;
227
+ let delete_result = directory. remove ( & trx, sub_path. to_owned ( ) ) . await ?;
228
+ assert ! ( delete_result) ;
229
+ trx. commit ( ) . await . expect ( "could not commit" ) ;
230
+
231
+ // checking it does not exists
232
+ let trx = db. create_trx ( ) ?;
233
+ eprintln ! ( "trying to get {:?}" , sub_path. to_owned( ) ) ;
234
+ let exists = directory. exists ( & trx, sub_path. to_owned ( ) ) . await ?;
235
+ assert ! ( !exists, "path {:?} should not exists" , sub_path. to_owned( ) ) ;
236
+ trx. commit ( ) . await . expect ( "could not commit" ) ;
237
+ }
238
+ let trx = db. create_trx ( ) ?;
239
+ let children = directory. list ( & trx, paths. to_owned ( ) ) . await ?;
240
+ assert ! ( children. is_empty( ) , "children is not empty: {:?}" , children) ;
241
+ trx. commit ( ) . await . expect ( "could not commit" ) ;
242
+
243
+ Ok ( ( ) )
244
+ }
245
+
133
246
async fn test_create_then_move_to (
134
247
db : & Database ,
135
248
directory : & DirectoryLayer ,
@@ -231,6 +344,30 @@ async fn test_create_or_open_async(
231
344
Ok ( ( ) )
232
345
}
233
346
347
+ async fn test_delete_async (
348
+ db : & Database ,
349
+ directory : & DirectoryLayer ,
350
+ paths : Vec < String > ,
351
+ ) -> FdbResult < ( ) > {
352
+ let trx = db. create_trx ( ) ?;
353
+ let _ = directory
354
+ . create_or_open ( & trx, paths. to_owned ( ) )
355
+ . await
356
+ . expect ( "cannot create" ) ;
357
+ eprintln ! ( "removing {:?}" , paths. to_owned( ) ) ;
358
+ let delete_output = directory. remove ( & trx, paths. to_owned ( ) ) . await ;
359
+ assert ! ( delete_output. is_ok( ) ) ;
360
+ trx. commit ( ) . await . expect ( "could not commit" ) ;
361
+
362
+ // checking it does not exists
363
+ let trx = db. create_trx ( ) ?;
364
+ let exists = directory. exists ( & trx, paths. to_owned ( ) ) . await . expect ( "bla" ) ;
365
+ assert ! ( !exists, "path {:?} should not exists" , paths. to_owned( ) ) ;
366
+ trx. commit ( ) . await . expect ( "could not commit" ) ;
367
+
368
+ Ok ( ( ) )
369
+ }
370
+
234
371
/// testing that we throwing Err(DirectoryError::IncompatibleLayer)
235
372
async fn test_bad_layer ( db : & Database ) -> Result < ( ) , DirectoryError > {
236
373
let directory = DirectoryLayer {
0 commit comments