Skip to content

Commit 920918a

Browse files
committed
fix(foundationdb): use right subspace during Directory.Remove
1 parent 6c301cd commit 920918a

File tree

3 files changed

+188
-15
lines changed

3 files changed

+188
-15
lines changed

foundationdb/src/directory/mod.rs

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -153,10 +153,9 @@ impl DirectoryLayer {
153153
paths: Vec<String>,
154154
) -> Result<bool, DirectoryError> {
155155
let nodes = self.find_nodes(trx, paths.to_owned()).await?;
156-
157156
match nodes.last() {
158-
None => Err(DirectoryError::DirNotExists),
159-
Some(_) => Ok(true),
157+
None => Ok(false),
158+
Some(node) => Ok(node.content_subspace.is_some()),
160159
}
161160
}
162161

@@ -220,20 +219,35 @@ impl DirectoryLayer {
220219
.await?;
221220

222221
last_node_from_old_path
223-
.delete_content_subspace(&trx)
222+
.delete_content_from_node_subspace(&trx)
224223
.await?;
225224

226225
Ok(true)
227226
}
228227

229-
/// Exists returns true if the directory at path (relative to this Directory)
230-
/// exists, and false otherwise.
228+
/// `Remove` the subdirectory of this Directory located at `path` and all of its subdirectories,
229+
/// as well as all of their contents.
231230
pub async fn remove(
232231
&self,
233-
_trx: &Transaction,
234-
_path: Vec<String>,
232+
trx: &Transaction,
233+
path: Vec<String>,
235234
) -> Result<bool, DirectoryError> {
236-
unimplemented!("move is not supported yet")
235+
self.check_version(trx, false).await?;
236+
if path.is_empty() {
237+
return Err(DirectoryError::NoPathProvided);
238+
}
239+
240+
let nodes = self.find_nodes(&trx, path.to_owned()).await?;
241+
242+
match nodes.last() {
243+
None => Ok(false),
244+
Some(node) => {
245+
println!("found a node to delete: {:?}", node);
246+
node.delete_content_from_node_subspace(&trx).await?;
247+
node.delete_content_from_content_subspace(&trx).await?;
248+
Ok(true)
249+
}
250+
}
237251
}
238252

239253
/// List returns the names of the immediate subdirectories of the default root directory as a slice of strings.
@@ -409,6 +423,7 @@ impl DirectoryLayer {
409423
node.retrieve_layer(&trx).await?;
410424

411425
if let Some(fdb_slice) = trx.get(node.node_subspace.bytes(), false).await? {
426+
println!("found something in {:?}", node.node_subspace.to_owned());
412427
node.content_subspace = Some(Subspace::from_bytes(&*fdb_slice));
413428
}
414429

foundationdb/src/directory/node.rs

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,36 @@ impl Node {
6363
}
6464

6565
/// delete subspace from the node_subspace
66-
pub(crate) async fn delete_content_subspace(
67-
&mut self,
66+
pub(crate) async fn delete_content_from_node_subspace(
67+
&self,
6868
trx: &Transaction,
6969
) -> Result<(), DirectoryError> {
70-
let key = self.node_subspace.to_owned();
71-
trx.clear(key.bytes());
70+
println!(
71+
"deleting node_subspace {:?}",
72+
&self.node_subspace.to_owned()
73+
);
74+
trx.clear(&self.node_subspace.bytes());
75+
//trx.clear_subspace_range(&self.node_subspace.to_owned());
7276
Ok(())
7377
}
7478

79+
pub(crate) async fn delete_content_from_content_subspace(
80+
&self,
81+
trx: &Transaction,
82+
) -> Result<(), DirectoryError> {
83+
match self.content_subspace.to_owned() {
84+
None => Ok(()),
85+
Some(subspace) => {
86+
println!(
87+
"deleting content_subspace {:?}",
88+
&self.content_subspace.to_owned()
89+
);
90+
trx.clear_subspace_range(&subspace);
91+
Ok(())
92+
}
93+
}
94+
}
95+
7596
/// retrieve the layer used for this node
7697
pub(crate) async fn retrieve_layer(&mut self, trx: &Transaction) -> Result<(), FdbError> {
7798
if self.layer == None {

foundationdb/tests/directory.rs

Lines changed: 139 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use foundationdb::*;
1212
mod common;
1313

1414
#[test]
15-
fn test_directory() {
15+
fn test_create_or_open_directory() {
1616
let _guard = unsafe { foundationdb::boot() };
1717
let db = futures::executor::block_on(common::database()).expect("cannot open fdb");
1818

@@ -49,12 +49,63 @@ fn test_directory() {
4949
.expect("failed to run");
5050

5151
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();
52103

53104
futures::executor::block_on(test_create_then_move_to(
54105
&db,
55106
&directory,
56107
vec![String::from("d"), String::from("e")],
57-
vec![String::from("a"), String::from("g")],
108+
vec![String::from("a")],
58109
))
59110
.expect("failed to run");
60111

@@ -130,6 +181,68 @@ fn test_directory() {
130181
}
131182
}
132183

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+
133246
async fn test_create_then_move_to(
134247
db: &Database,
135248
directory: &DirectoryLayer,
@@ -231,6 +344,30 @@ async fn test_create_or_open_async(
231344
Ok(())
232345
}
233346

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+
234371
/// testing that we throwing Err(DirectoryError::IncompatibleLayer)
235372
async fn test_bad_layer(db: &Database) -> Result<(), DirectoryError> {
236373
let directory = DirectoryLayer {

0 commit comments

Comments
 (0)