Skip to content

Commit

Permalink
fix(foundationdb): generate the right subspace with a nested path on …
Browse files Browse the repository at this point in the history
…directory
  • Loading branch information
PierreZ committed Jan 3, 2021
1 parent 03b78dd commit 17129a6
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 11 deletions.
30 changes: 21 additions & 9 deletions foundationdb/src/directory/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,9 @@ impl DirectoryLayer {
/// parent directory of newPath does not exist.
pub async fn move_to(
&self,
trx: &Transaction,
old_path: Vec<String>,
new_path: Vec<String>,
_trx: &Transaction,
_old_path: Vec<String>,
_new_path: Vec<String>,
) -> Result<bool, DirectoryError> {
unimplemented!("move is not supported yet")
}
Expand All @@ -178,8 +178,8 @@ impl DirectoryLayer {
/// exists, and false otherwise.
pub async fn remove(
&self,
trx: &Transaction,
path: Vec<String>,
_trx: &Transaction,
_path: Vec<String>,
) -> Result<bool, DirectoryError> {
unimplemented!("move is not supported yet")
}
Expand Down Expand Up @@ -250,13 +250,21 @@ impl DirectoryLayer {
return Err(DirectoryError::DirNotExists);
}

let mut subspace = self.content_subspace.clone();
let mut parent_subspace = self.content_subspace.clone();

for mut node in nodes {
let allocator = self.allocator.allocate(trx).await?;
subspace = node.create_subspace(&trx, allocator, &subspace).await?;
match node.content_subspace {
None => {
// creating subspace
let allocator = self.allocator.allocate(trx).await?;
parent_subspace = node
.create_subspace(&trx, allocator, &parent_subspace)
.await?;
}
Some(subspace) => parent_subspace = subspace.clone(),
}
}
Ok(subspace)
Ok(parent_subspace)
}

/// checks the version of the directory within FDB
Expand Down Expand Up @@ -330,12 +338,16 @@ impl DirectoryLayer {

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

let mut node_path = vec![];

for path_name in paths {
node_path.push(path_name.to_owned());
let mut next_node_key = vec![DEFAULT_SUB_DIRS];
pack_into(&path_name, &mut next_node_key);
subspace = subspace.subspace(&next_node_key);

let mut node = Node {
paths: node_path.clone(),
layer: None,
node_subspace: subspace.to_owned(),
content_subspace: None,
Expand Down
8 changes: 6 additions & 2 deletions foundationdb/src/directory/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@ use crate::{FdbError, RangeOption, Transaction};

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

pub(crate) paths: Vec<String>,

pub(crate) node_subspace: Subspace,
pub(crate) content_subspace: Option<Subspace>,
}
Expand All @@ -34,8 +37,9 @@ impl Node {
}

/// This will use the generated id and:
/// * persist the node in the directory subspace dedicated to nodes
/// * create the content_subspace and returns it
///
/// * persist the node in the directory subspace dedicated to nodes
/// * create the content_subspace and returns it
pub(crate) async fn create_subspace(
&mut self,
trx: &Transaction,
Expand Down
46 changes: 46 additions & 0 deletions foundationdb/tests/directory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ fn test_directory() {
futures::executor::block_on(test_list(&db, &directory, vec![String::from("a")], 10))
.expect("failed to run");

futures::executor::block_on(test_children_content_subspace(
&db,
&directory,
vec![String::from("c")],
))
.expect("failed to run");

futures::executor::block_on(test_bad_layer(&db)).expect("failed to run");
}

Expand Down Expand Up @@ -76,6 +83,7 @@ async fn test_create_or_open_async(
Ok(())
}

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

/// testing list functionality. Will open paths and create n sub-folders.
async fn test_list(
db: &Database,
directory: &DirectoryLayer,
Expand Down Expand Up @@ -145,3 +154,40 @@ async fn test_list(

Ok(())
}

/// checks that the content_subspace of the children is inside the parent
async fn test_children_content_subspace(
db: &Database,
directory: &DirectoryLayer,
paths: Vec<String>,
) -> Result<(), DirectoryError> {
let trx = db.create_trx()?;

eprintln!("parent = {:?}", paths.to_owned());

let root_subspace = directory.create_or_open(&trx, paths.to_owned()).await?;

let mut children_path = paths.clone();
children_path.push(String::from("nested"));
eprintln!("children = {:?}", children_path.to_owned());

let children_subspace = directory
.create_or_open(&trx, children_path.to_owned())
.await?;

assert!(
children_subspace.bytes().starts_with(root_subspace.bytes()),
"children subspace '{:?} does not start with parent subspace '{:?}'",
children_subspace.bytes(),
root_subspace.bytes()
);

trx.commit().await.expect("could not commit");
let trx = db.create_trx()?;

let open_children_subspace = directory.open(&trx, children_path.to_owned()).await?;

assert_eq!(children_subspace.bytes(), open_children_subspace.bytes());

Ok(())
}

0 comments on commit 17129a6

Please sign in to comment.