Skip to content

Commit ce614fe

Browse files
committed
fix(foundationdb): generate the right subspace with a nested path on directory
1 parent 03b78dd commit ce614fe

File tree

3 files changed

+71
-9
lines changed

3 files changed

+71
-9
lines changed

foundationdb/src/directory/mod.rs

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,9 @@ impl DirectoryLayer {
167167
/// parent directory of newPath does not exist.
168168
pub async fn move_to(
169169
&self,
170-
trx: &Transaction,
171-
old_path: Vec<String>,
172-
new_path: Vec<String>,
170+
_trx: &Transaction,
171+
_old_path: Vec<String>,
172+
_new_path: Vec<String>,
173173
) -> Result<bool, DirectoryError> {
174174
unimplemented!("move is not supported yet")
175175
}
@@ -178,8 +178,8 @@ impl DirectoryLayer {
178178
/// exists, and false otherwise.
179179
pub async fn remove(
180180
&self,
181-
trx: &Transaction,
182-
path: Vec<String>,
181+
_trx: &Transaction,
182+
_path: Vec<String>,
183183
) -> Result<bool, DirectoryError> {
184184
unimplemented!("move is not supported yet")
185185
}
@@ -250,13 +250,21 @@ impl DirectoryLayer {
250250
return Err(DirectoryError::DirNotExists);
251251
}
252252

253-
let mut subspace = self.content_subspace.clone();
253+
let mut parent_subspace = self.content_subspace.clone();
254254

255255
for mut node in nodes {
256-
let allocator = self.allocator.allocate(trx).await?;
257-
subspace = node.create_subspace(&trx, allocator, &subspace).await?;
256+
match node.content_subspace {
257+
None => {
258+
// creating subspace
259+
let allocator = self.allocator.allocate(trx).await?;
260+
parent_subspace = node
261+
.create_subspace(&trx, allocator, &parent_subspace)
262+
.await?;
263+
}
264+
Some(subspace) => parent_subspace = subspace.clone(),
265+
}
258266
}
259-
Ok(subspace)
267+
Ok(parent_subspace)
260268
}
261269

262270
/// checks the version of the directory within FDB
@@ -330,12 +338,16 @@ impl DirectoryLayer {
330338

331339
let mut subspace = self.node_subspace.to_owned();
332340

341+
let mut node_path = vec![];
342+
333343
for path_name in paths {
344+
node_path.push(path_name.to_owned());
334345
let mut next_node_key = vec![DEFAULT_SUB_DIRS];
335346
pack_into(&path_name, &mut next_node_key);
336347
subspace = subspace.subspace(&next_node_key);
337348

338349
let mut node = Node {
350+
paths: node_path.clone(),
339351
layer: None,
340352
node_subspace: subspace.to_owned(),
341353
content_subspace: None,

foundationdb/src/directory/node.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,12 @@ use crate::{FdbError, RangeOption, Transaction};
1212

1313
/// Node are used to represent the paths generated by a Directory.
1414
/// They are stored in the `Directory.`
15+
#[derive(Debug)]
1516
pub(crate) struct Node {
1617
pub(crate) layer: Option<Vec<u8>>,
1718

19+
pub(crate) paths: Vec<String>,
20+
1821
pub(crate) node_subspace: Subspace,
1922
pub(crate) content_subspace: Option<Subspace>,
2023
}
@@ -34,6 +37,7 @@ impl Node {
3437
}
3538

3639
/// This will use the generated id and:
40+
///
3741
/// * persist the node in the directory subspace dedicated to nodes
3842
/// * create the content_subspace and returns it
3943
pub(crate) async fn create_subspace(

foundationdb/tests/directory.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,13 @@ fn test_directory() {
4141
futures::executor::block_on(test_list(&db, &directory, vec![String::from("a")], 10))
4242
.expect("failed to run");
4343

44+
futures::executor::block_on(test_children_content_subspace(
45+
&db,
46+
&directory,
47+
vec![String::from("c")],
48+
))
49+
.expect("failed to run");
50+
4451
futures::executor::block_on(test_bad_layer(&db)).expect("failed to run");
4552
}
4653

@@ -76,6 +83,7 @@ async fn test_create_or_open_async(
7683
Ok(())
7784
}
7885

86+
/// testing that we throwing Err(DirectoryError::IncompatibleLayer)
7987
async fn test_bad_layer(db: &Database) -> Result<(), DirectoryError> {
8088
let directory = DirectoryLayer {
8189
layer: vec![0u8],
@@ -103,6 +111,7 @@ async fn test_bad_layer(db: &Database) -> Result<(), DirectoryError> {
103111
Ok(())
104112
}
105113

114+
/// testing list functionality. Will open paths and create n sub-folders.
106115
async fn test_list(
107116
db: &Database,
108117
directory: &DirectoryLayer,
@@ -145,3 +154,40 @@ async fn test_list(
145154

146155
Ok(())
147156
}
157+
158+
/// checks that the content_subspace of the children is inside the parent
159+
async fn test_children_content_subspace(
160+
db: &Database,
161+
directory: &DirectoryLayer,
162+
paths: Vec<String>,
163+
) -> Result<(), DirectoryError> {
164+
let trx = db.create_trx()?;
165+
166+
eprintln!("parent = {:?}", paths.to_owned());
167+
168+
let root_subspace = directory.create_or_open(&trx, paths.to_owned()).await?;
169+
170+
let mut children_path = paths.clone();
171+
children_path.push(String::from("nested"));
172+
eprintln!("children = {:?}", children_path.to_owned());
173+
174+
let children_subspace = directory
175+
.create_or_open(&trx, children_path.to_owned())
176+
.await?;
177+
178+
assert!(
179+
children_subspace.bytes().starts_with(root_subspace.bytes()),
180+
"children subspace '{:?} does not start with parent subspace '{:?}'",
181+
children_subspace.bytes(),
182+
root_subspace.bytes()
183+
);
184+
185+
trx.commit().await.expect("could not commit");
186+
let trx = db.create_trx()?;
187+
188+
let open_children_subspace = directory.open(&trx, children_path.to_owned()).await?;
189+
190+
assert_eq!(children_subspace.bytes(), open_children_subspace.bytes());
191+
192+
Ok(())
193+
}

0 commit comments

Comments
 (0)