@@ -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
@@ -50,11 +50,51 @@ fn test_directory() {
50
50
51
51
futures:: executor:: block_on ( test_bad_layer ( & db) ) . expect ( "failed to run" ) ;
52
52
53
+ // testing deletes
54
+
55
+ eprintln ! ( "clearing all keys" ) ;
56
+ let trx = db. create_trx ( ) . expect ( "cannot create txn" ) ;
57
+ trx. clear_range ( b"" , b"\xff " ) ;
58
+ futures:: executor:: block_on ( trx. commit ( ) ) . expect ( "could not clear keys" ) ;
59
+
60
+ eprintln ! ( "creating directories" ) ;
61
+ let directory = DirectoryLayer :: default ( ) ;
62
+
63
+ // test deletions, first we need to create it
64
+ futures:: executor:: block_on ( test_create_or_open_async (
65
+ & db,
66
+ & directory,
67
+ vec ! [ String :: from( "deletion" ) ] ,
68
+ ) )
69
+ . expect ( "failed to run" ) ;
70
+ // then delete it
71
+ futures:: executor:: block_on ( test_delete_async (
72
+ & db,
73
+ & directory,
74
+ vec ! [ String :: from( "deletion" ) ] ,
75
+ ) )
76
+ . expect ( "failed to run" ) ;
77
+
78
+ futures:: executor:: block_on ( test_create_then_delete (
79
+ & db,
80
+ & directory,
81
+ vec ! [ String :: from( "n0" ) ] ,
82
+ 1 ,
83
+ ) )
84
+ . expect ( "failed to run" ) ;
85
+
86
+ // moves
87
+
88
+ eprintln ! ( "clearing all keys" ) ;
89
+ let trx = db. create_trx ( ) . expect ( "cannot create txn" ) ;
90
+ trx. clear_range ( b"" , b"\xff " ) ;
91
+ futures:: executor:: block_on ( trx. commit ( ) ) . expect ( "could not clear keys" ) ;
92
+
53
93
futures:: executor:: block_on ( test_create_then_move_to (
54
94
& db,
55
95
& directory,
56
96
vec ! [ String :: from( "d" ) , String :: from( "e" ) ] ,
57
- vec ! [ String :: from( "a" ) , String :: from ( "g" ) ] ,
97
+ vec ! [ String :: from( "a" ) ] ,
58
98
) )
59
99
. expect ( "failed to run" ) ;
60
100
@@ -130,6 +170,68 @@ fn test_directory() {
130
170
}
131
171
}
132
172
173
+ async fn test_create_then_delete (
174
+ db : & Database ,
175
+ directory : & DirectoryLayer ,
176
+ paths : Vec < String > ,
177
+ sub_path_to_create : usize ,
178
+ ) -> Result < ( ) , DirectoryError > {
179
+ // creating directory
180
+ let trx = db. create_trx ( ) ?;
181
+ directory. create_or_open ( & trx, paths. to_owned ( ) ) . await ?;
182
+
183
+ trx. commit ( ) . await . expect ( "could not commit" ) ;
184
+
185
+ let trx = db. create_trx ( ) ?;
186
+ let children = directory. list ( & trx, paths. to_owned ( ) ) . await ?;
187
+ assert ! ( children. is_empty( ) ) ;
188
+ trx. commit ( ) . await . expect ( "could not commit" ) ;
189
+
190
+ for i in 0 ..sub_path_to_create {
191
+ let trx = db. create_trx ( ) ?;
192
+ let mut sub_path = paths. clone ( ) ;
193
+ let path_name = format ! ( "{}" , i) ;
194
+ sub_path. push ( path_name. to_owned ( ) ) ;
195
+
196
+ // creating subfolders
197
+ eprintln ! ( "creating {:?}" , sub_path. to_owned( ) ) ;
198
+ directory. create ( & trx, sub_path. to_owned ( ) ) . await ;
199
+ trx. commit ( ) . await . expect ( "could not commit" ) ;
200
+
201
+ // checking it does exists
202
+ let trx = db. create_trx ( ) ?;
203
+ eprintln ! ( "trying to get {:?}" , sub_path. to_owned( ) ) ;
204
+ let exists = directory. exists ( & trx, sub_path. to_owned ( ) ) . await ?;
205
+ assert ! ( exists, "path {:?} should exists" , sub_path. to_owned( ) ) ;
206
+ trx. commit ( ) . await . expect ( "could not commit" ) ;
207
+
208
+ let trx = db. create_trx ( ) ?;
209
+ let children = directory. list ( & trx, paths. to_owned ( ) ) . await ?;
210
+ assert ! ( children. contains( & path_name. to_owned( ) ) ) ;
211
+ trx. commit ( ) . await . expect ( "could not commit" ) ;
212
+
213
+ // trying to delete it
214
+ let trx = db. create_trx ( ) ?;
215
+ eprintln ! ( "deleting {:?}" , sub_path. to_owned( ) ) ;
216
+ let delete_result = directory. remove ( & trx, sub_path. to_owned ( ) ) . await ?;
217
+ assert ! ( delete_result) ;
218
+ trx. commit ( ) . await . expect ( "could not commit" ) ;
219
+
220
+ // checking it does not exists
221
+ let trx = db. create_trx ( ) ?;
222
+ eprintln ! ( "trying to get {:?}" , sub_path. to_owned( ) ) ;
223
+ let exists = directory. exists ( & trx, sub_path. to_owned ( ) ) . await ?;
224
+ assert ! ( !exists, "path {:?} should not exists" , sub_path. to_owned( ) ) ;
225
+ trx. commit ( ) . await . expect ( "could not commit" ) ;
226
+ }
227
+ let trx = db. create_trx ( ) ?;
228
+ let children = directory. list ( & trx, paths. to_owned ( ) ) . await ?;
229
+ assert ! ( children. is_empty( ) , "children is not empty: {:?}" , children) ;
230
+ trx. commit ( ) . await . expect ( "could not commit" ) ;
231
+
232
+ Ok ( ( ) )
233
+ }
234
+
133
235
async fn test_create_then_move_to (
134
236
db : & Database ,
135
237
directory : & DirectoryLayer ,
@@ -231,6 +333,30 @@ async fn test_create_or_open_async(
231
333
Ok ( ( ) )
232
334
}
233
335
336
+ async fn test_delete_async (
337
+ db : & Database ,
338
+ directory : & DirectoryLayer ,
339
+ paths : Vec < String > ,
340
+ ) -> FdbResult < ( ) > {
341
+ let trx = db. create_trx ( ) ?;
342
+ let _ = directory
343
+ . create_or_open ( & trx, paths. to_owned ( ) )
344
+ . await
345
+ . expect ( "cannot create" ) ;
346
+ eprintln ! ( "removing {:?}" , paths. to_owned( ) ) ;
347
+ let delete_output = directory. remove ( & trx, paths. to_owned ( ) ) . await ;
348
+ assert ! ( delete_output. is_ok( ) ) ;
349
+ trx. commit ( ) . await . expect ( "could not commit" ) ;
350
+
351
+ // checking it does not exists
352
+ let trx = db. create_trx ( ) ?;
353
+ let exists = directory. exists ( & trx, paths. to_owned ( ) ) . await . expect ( "bla" ) ;
354
+ assert ! ( !exists, "path {:?} should not exists" , paths. to_owned( ) ) ;
355
+ trx. commit ( ) . await . expect ( "could not commit" ) ;
356
+
357
+ Ok ( ( ) )
358
+ }
359
+
234
360
/// testing that we throwing Err(DirectoryError::IncompatibleLayer)
235
361
async fn test_bad_layer ( db : & Database ) -> Result < ( ) , DirectoryError > {
236
362
let directory = DirectoryLayer {
0 commit comments