Skip to content

Commit 5241604

Browse files
committed
feat(foundationdb): add validation for a Directory layer
1 parent 8259f77 commit 5241604

File tree

2 files changed

+57
-28
lines changed

2 files changed

+57
-28
lines changed

foundationdb/src/directory/mod.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -117,13 +117,13 @@ impl DirectoryLayer {
117117
txn: &Transaction,
118118
paths: Vec<String>,
119119
) -> Result<Subspace, DirectoryError> {
120-
self.create_or_open_internal(txn, paths, vec![], vec![], true, true)
120+
self.create_or_open_internal(txn, paths, vec![], true, true)
121121
.await
122122
}
123123

124124
/// Creates a directory located at path (creating parent directories if necessary).
125125
pub async fn create(&self, txn: &Transaction, paths: Vec<String>) -> Option<DirectoryError> {
126-
self.create_or_open_internal(txn, paths, vec![], vec![], true, false)
126+
self.create_or_open_internal(txn, paths, vec![], true, false)
127127
.await
128128
.err()
129129
}
@@ -134,7 +134,7 @@ impl DirectoryLayer {
134134
txn: &Transaction,
135135
paths: Vec<String>,
136136
) -> Result<Subspace, DirectoryError> {
137-
self.create_or_open_internal(txn, paths, vec![], vec![], false, true)
137+
self.create_or_open_internal(txn, paths, vec![], false, true)
138138
.await
139139
}
140140

@@ -143,7 +143,6 @@ impl DirectoryLayer {
143143
trx: &Transaction,
144144
paths: Vec<String>,
145145
prefix: Vec<u8>,
146-
layer: Vec<u8>,
147146
allow_create: bool,
148147
allow_open: bool,
149148
) -> Result<Subspace, DirectoryError> {
@@ -178,8 +177,8 @@ impl DirectoryLayer {
178177
return Err(DirectoryError::DirAlreadyExists);
179178
}
180179

181-
if layer.len() > 0 {
182-
node.check_layer(layer)?;
180+
if self.layer.len() > 0 {
181+
node.check_layer(self.layer.to_owned())?;
183182
}
184183

185184
return Ok(node.content_subspace.clone().unwrap());

foundationdb/tests/directory.rs

Lines changed: 52 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,42 @@
55
// http://opensource.org/licenses/MIT>, at your option. This file may not be
66
// copied, modified, or distributed except according to those terms.
77

8+
use foundationdb::directory::error::DirectoryError;
89
use foundationdb::directory::DirectoryLayer;
910
use foundationdb::*;
1011

1112
mod common;
1213

14+
#[test]
15+
fn test_directory() {
16+
let _guard = unsafe { foundationdb::boot() };
17+
let db = futures::executor::block_on(common::database()).expect("cannot open fdb");
18+
19+
eprintln!("clearing all keys");
20+
let trx = db.create_trx().expect("cannot create txn");
21+
trx.clear_range(b"\x00", b"\xff");
22+
futures::executor::block_on(trx.commit()).expect("could not clear keys");
23+
24+
eprintln!("creating directories");
25+
let directory = DirectoryLayer::default();
26+
27+
futures::executor::block_on(test_create_or_open_async(
28+
&db,
29+
&directory,
30+
vec![String::from("a")],
31+
))
32+
.expect("failed to run");
33+
34+
futures::executor::block_on(test_create_then_open_async(
35+
&db,
36+
&directory,
37+
vec![String::from("b"), String::from("a")],
38+
))
39+
.expect("failed to run");
40+
41+
futures::executor::block_on(test_bad_layer(&db)).expect("failed to run");
42+
}
43+
1344
async fn test_create_then_open_async(
1445
db: &Database,
1546
directory: &DirectoryLayer,
@@ -42,30 +73,29 @@ async fn test_create_or_open_async(
4273
Ok(())
4374
}
4475

45-
#[test]
46-
fn test_directory() {
47-
let _guard = unsafe { foundationdb::boot() };
48-
let db = futures::executor::block_on(common::database()).expect("cannot open fdb");
76+
async fn test_bad_layer(db: &Database) -> Result<(), DirectoryError> {
77+
let directory = DirectoryLayer {
78+
layer: vec![0u8],
79+
..Default::default()
80+
};
81+
let trx = db.create_trx()?;
4982

50-
eprintln!("clearing all keys");
51-
let trx = db.create_trx().expect("cannot create txn");
52-
trx.clear_range(b"\x00", b"\xff");
53-
futures::executor::block_on(trx.commit()).expect("could not clear keys");
83+
directory
84+
.create_or_open(&trx, vec![String::from("bad_layer")])
85+
.await?;
5486

55-
eprintln!("creating directories");
56-
let directory = DirectoryLayer::default();
87+
let directory = DirectoryLayer {
88+
layer: vec![1u8],
89+
..Default::default()
90+
};
5791

58-
futures::executor::block_on(test_create_or_open_async(
59-
&db,
60-
&directory,
61-
vec![String::from("a")],
62-
))
63-
.expect("failed to run");
92+
let result = directory
93+
.create_or_open(&trx, vec![String::from("bad_layer")])
94+
.await;
95+
match result {
96+
Err(DirectoryError::IncompatibleLayer) => {}
97+
_ => panic!("should have been an IncompatibleLayer error"),
98+
}
6499

65-
futures::executor::block_on(test_create_then_open_async(
66-
&db,
67-
&directory,
68-
vec![String::from("b"), String::from("a")],
69-
))
70-
.expect("failed to run");
100+
Ok(())
71101
}

0 commit comments

Comments
 (0)