Skip to content

Commit 6c301cd

Browse files
committed
feat(foundationdb): add more tests to directory.Move
1 parent 76b5691 commit 6c301cd

File tree

3 files changed

+132
-6
lines changed

3 files changed

+132
-6
lines changed

foundationdb/src/directory/error.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ pub enum DirectoryError {
2626
ParentDirDoesNotExists,
2727
/// the layer is incompatible.
2828
IncompatibleLayer,
29+
/// the destination directory cannot be a subdirectory of the source directory.
30+
BadDestinationDirectory,
2931
Message(String),
3032
/// Bad directory version.
3133
Version(String),

foundationdb/src/directory/mod.rs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,27 +173,43 @@ impl DirectoryLayer {
173173
) -> Result<bool, DirectoryError> {
174174
self.check_version(trx, false).await?;
175175

176+
if old_path.is_empty() || new_path.is_empty() {
177+
return Err(DirectoryError::NoPathProvided);
178+
}
179+
180+
if new_path.starts_with(old_path.as_slice()) {
181+
return Err(DirectoryError::BadDestinationDirectory);
182+
}
183+
176184
let mut old_nodes = self.find_nodes(&trx, old_path.to_owned()).await?;
177185
let last_node_from_old_path = match old_nodes.last_mut() {
178186
None => return Err(DirectoryError::Message(String::from("empty path"))),
179187
Some(node) => node,
180188
};
189+
181190
let content_subspace = last_node_from_old_path
182191
.content_subspace
183192
.as_ref()
184193
.ok_or(DirectoryError::DirNotExists)?;
185194

186195
let mut new_nodes = self.find_nodes(&trx, new_path.to_owned()).await?;
196+
187197
// assert that parent of the new node exists
188-
if new_nodes.get(new_nodes.len() - 2).is_none() {
189-
return Err(DirectoryError::ParentDirDoesNotExists);
198+
if new_nodes.len() >= 2 {
199+
match new_nodes.get(new_nodes.len() - 2) {
200+
None => {}
201+
Some(parent_node) => match parent_node.content_subspace {
202+
None => return Err(DirectoryError::ParentDirDoesNotExists),
203+
Some(_) => {}
204+
},
205+
}
190206
}
191207

192208
let last_node_from_new_path = match new_nodes.last_mut() {
193209
None => return Err(DirectoryError::Message(String::from("empty path"))),
194210
Some(node) => {
195211
if node.content_subspace.is_some() {
196-
return Err(DirectoryError::DirNotExists);
212+
return Err(DirectoryError::DirAlreadyExists);
197213
}
198214
node
199215
}

foundationdb/tests/directory.rs

Lines changed: 111 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ fn test_directory() {
1818

1919
eprintln!("clearing all keys");
2020
let trx = db.create_trx().expect("cannot create txn");
21-
trx.clear_range(b"\x00", b"\xff");
21+
trx.clear_range(b"", b"\xff");
2222
futures::executor::block_on(trx.commit()).expect("could not clear keys");
2323

2424
eprintln!("creating directories");
@@ -50,21 +50,97 @@ fn test_directory() {
5050

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

53-
futures::executor::block_on(test_move_to(
53+
futures::executor::block_on(test_create_then_move_to(
5454
&db,
5555
&directory,
5656
vec![String::from("d"), String::from("e")],
5757
vec![String::from("a"), String::from("g")],
5858
))
5959
.expect("failed to run");
60+
61+
// trying to move on empty path
62+
match futures::executor::block_on(test_move_to(
63+
&db,
64+
&directory,
65+
vec![String::from("dsa")],
66+
vec![],
67+
)) {
68+
Err(DirectoryError::NoPathProvided) => {}
69+
_ => panic!("should have failed"),
70+
}
71+
72+
// trying to move on empty path
73+
match futures::executor::block_on(test_move_to(
74+
&db,
75+
&directory,
76+
vec![],
77+
vec![String::from("dsa")],
78+
)) {
79+
Err(DirectoryError::NoPathProvided) => {}
80+
Err(err) => panic!("should have NoPathProvided, got {:?}", err),
81+
Ok(()) => panic!("should not be fine"),
82+
}
83+
84+
// source path does not exists
85+
match futures::executor::block_on(test_move_to(
86+
&db,
87+
&directory,
88+
vec![String::from("e")],
89+
vec![String::from("f")],
90+
)) {
91+
Err(DirectoryError::DirNotExists) => {}
92+
Err(err) => panic!("should have NoPathProvided, got {:?}", err),
93+
Ok(()) => panic!("should not be fine"),
94+
}
95+
96+
// destination's parent does not exists
97+
match futures::executor::block_on(test_create_then_move_to(
98+
&db,
99+
&directory,
100+
vec![String::from("a"), String::from("g")],
101+
vec![String::from("i-do-not-exists-yet"), String::from("z")],
102+
)) {
103+
Err(DirectoryError::ParentDirDoesNotExists) => {}
104+
Err(err) => panic!("should have ParentDirDoesNotExists, got {:?}", err),
105+
Ok(()) => panic!("should not be fine"),
106+
}
107+
108+
// destination not empty
109+
match futures::executor::block_on(test_create_then_move_to(
110+
&db,
111+
&directory,
112+
vec![String::from("a"), String::from("g")],
113+
vec![String::from("a"), String::from("g")],
114+
)) {
115+
Err(DirectoryError::BadDestinationDirectory) => {}
116+
Err(err) => panic!("should have BadDestinationDirectory, got {:?}", err),
117+
Ok(()) => panic!("should not be fine"),
118+
}
119+
120+
// cannot move to a children of the old-path
121+
match futures::executor::block_on(test_create_then_move_to(
122+
&db,
123+
&directory,
124+
vec![String::from("a"), String::from("g")],
125+
vec![String::from("a"), String::from("g"), String::from("s")],
126+
)) {
127+
Err(DirectoryError::BadDestinationDirectory) => {}
128+
Err(err) => panic!("should have BadDestinationDirectory, got {:?}", err),
129+
Ok(()) => panic!("should not be fine"),
130+
}
60131
}
61132

62-
async fn test_move_to(
133+
async fn test_create_then_move_to(
63134
db: &Database,
64135
directory: &DirectoryLayer,
65136
old_paths: Vec<String>,
66137
new_paths: Vec<String>,
67138
) -> Result<(), DirectoryError> {
139+
eprintln!(
140+
"moving {:?} to {:?}",
141+
old_paths.to_owned(),
142+
new_paths.to_owned()
143+
);
68144
let trx = db.create_trx()?;
69145
let create_output = directory.create_or_open(&trx, old_paths.to_owned()).await?;
70146

@@ -91,6 +167,38 @@ async fn test_move_to(
91167
Ok(())
92168
}
93169

170+
async fn test_move_to(
171+
db: &Database,
172+
directory: &DirectoryLayer,
173+
old_paths: Vec<String>,
174+
new_paths: Vec<String>,
175+
) -> Result<(), DirectoryError> {
176+
eprintln!(
177+
"moving {:?} to {:?}",
178+
old_paths.to_owned(),
179+
new_paths.to_owned()
180+
);
181+
let trx = db.create_trx()?;
182+
183+
let move_output = directory
184+
.move_to(&trx, old_paths.to_owned(), new_paths.to_owned())
185+
.await?;
186+
assert!(move_output);
187+
188+
trx.commit().await.expect("could not commit");
189+
let trx = db.create_trx()?;
190+
191+
directory.open(&trx, new_paths).await?;
192+
193+
trx.commit().await.expect("could not commit");
194+
let trx = db.create_trx()?;
195+
196+
let open_old_path = directory.open(&trx, old_paths).await;
197+
assert!(open_old_path.is_err());
198+
199+
Ok(())
200+
}
201+
94202
async fn test_create_then_open_async(
95203
db: &Database,
96204
directory: &DirectoryLayer,

0 commit comments

Comments
 (0)