Skip to content

Commit 315b2c8

Browse files
committed
feat(foundationdb,bindingtester): initial comit for Directory trait
1 parent ff42b12 commit 315b2c8

File tree

7 files changed

+732
-548
lines changed

7 files changed

+732
-548
lines changed

foundationdb-bindingtester/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,4 @@ futures = "0.3.0"
3737
log = "0.4.8"
3838
num-bigint = "0.3.0"
3939
structopt = "0.3.3"
40+
async-trait = "0.1.48"

foundationdb-bindingtester/src/main.rs

Lines changed: 107 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,18 @@ static GOT_COMMITTED_VERSION: Element =
2727
static ERROR_NONE: Element = Element::Bytes(Bytes(Cow::Borrowed(b"ERROR: NONE")));
2828
static ERROR_MULTIPLE: Element = Element::Bytes(Bytes(Cow::Borrowed(b"ERROR: MULTIPLE")));
2929
static OK: Element = Element::Bytes(Bytes(Cow::Borrowed(b"OK")));
30+
static ERROR_DIRECTORY: Element = Element::Bytes(Bytes(Cow::Borrowed(b"DIRECTORY_ERROR")));
3031

3132
#[cfg(feature = "fdb-6_2")]
3233
static GOT_APPROXIMATE_SIZE: Element =
3334
Element::Bytes(Bytes(Cow::Borrowed(b"GOT_APPROXIMATE_SIZE")));
3435

3536
use crate::fdb::options::{MutationType, StreamingMode};
3637

37-
use foundationdb::directory::DirectoryLayer;
38+
use foundationdb::directory::directory_layer::DirectoryLayer;
39+
use foundationdb::directory::directory_subspace::DirectorySubspace;
40+
use foundationdb::directory::error::DirectoryError;
41+
use foundationdb::directory::Directory;
3842
use tuple::VersionstampOffset;
3943

4044
fn mutation_from_str(s: &str) -> MutationType {
@@ -101,6 +105,7 @@ impl std::fmt::Debug for Instr {
101105
#[derive(Debug)]
102106
enum DirectoryStackItem {
103107
Directory(DirectoryLayer),
108+
DirectorySubspace(DirectorySubspace),
104109
Subspace(Subspace),
105110
Null,
106111
}
@@ -701,6 +706,27 @@ impl StackMachine {
701706
}
702707
}
703708

709+
fn push_directory_err(&mut self, code: InstrCode, number: usize, err: DirectoryError) {
710+
debug!("DIRECTORY_ERROR during {:?}: {:?}", code, err);
711+
let packed = pack(&(ERROR_DIRECTORY));
712+
self.push(number, Element::Bytes(packed.into()));
713+
714+
if let InstrCode::DirectoryCreateSubspace
715+
| InstrCode::DirectoryCreateLayer
716+
| InstrCode::DirectoryCreate
717+
| InstrCode::DirectoryOpen
718+
| InstrCode::DirectoryMove
719+
| InstrCode::DirectoryMoveTo
720+
| InstrCode::DirectoryOpenSubspace = code
721+
{
722+
debug!(
723+
"pushed NULL in the directory_stack at index {} because of an error",
724+
self.directory_stack.len()
725+
);
726+
self.directory_stack.push(DirectoryStackItem::Null);
727+
}
728+
}
729+
704730
fn push_err(&mut self, number: usize, err: FdbError) {
705731
trace!("ERROR {:?}", err);
706732
let packed = pack(&Element::Tuple(vec![
@@ -1614,6 +1640,11 @@ impl StackMachine {
16141640
let raw_prefix = self.pop_bytes().await;
16151641
let subspace =
16161642
Subspace::from_bytes(&raw_prefix).subspace(tuple_prefix.get(0).unwrap());
1643+
debug!(
1644+
"pushing {:?} at index {}",
1645+
&subspace,
1646+
self.directory_stack.len()
1647+
);
16171648
self.directory_stack
16181649
.push(DirectoryStackItem::Subspace(subspace));
16191650
debug!("directory_stack: {:?}", self.directory_stack);
@@ -1663,14 +1694,13 @@ impl StackMachine {
16631694

16641695
debug!("pushed a directory at index {}", self.directory_stack.len());
16651696

1666-
self.directory_stack.push(DirectoryStackItem::Directory(
1667-
directory::DirectoryLayer {
1697+
self.directory_stack
1698+
.push(DirectoryStackItem::Directory(DirectoryLayer {
16681699
node_subspace,
16691700
content_subspace,
16701701
allow_manual_prefix,
16711702
..Default::default()
1672-
},
1673-
));
1703+
}));
16741704
}
16751705
}
16761706

@@ -1708,9 +1738,7 @@ impl StackMachine {
17081738
}
17091739
};
17101740

1711-
dbg!(&prefix);
1712-
1713-
let subspace = match directory
1741+
let directory_subspace = match directory
17141742
.create(
17151743
txn,
17161744
(*path.get(0).unwrap().to_owned()).to_vec(),
@@ -1725,8 +1753,13 @@ impl StackMachine {
17251753
}
17261754
};
17271755

1756+
debug!(
1757+
"pushing created {:?} at index {}",
1758+
&directory_subspace,
1759+
self.directory_stack.len()
1760+
);
17281761
self.directory_stack
1729-
.push(DirectoryStackItem::Subspace(subspace));
1762+
.push(DirectoryStackItem::DirectorySubspace(directory_subspace));
17301763
}
17311764

17321765
DirectoryOpen => {
@@ -1752,18 +1785,24 @@ impl StackMachine {
17521785
}
17531786
};
17541787

1755-
let subspace = match directory
1788+
let directory_subspace = match directory
17561789
.open(txn, (*path.get(0).unwrap().to_owned()).to_vec(), layer)
17571790
.await
17581791
{
17591792
Ok(s) => s,
17601793
Err(e) => {
1761-
panic!("could not call directory.create: {:?}", e);
1794+
self.push_directory_err(instr.code, number, e);
1795+
return Err(());
17621796
}
17631797
};
17641798

1799+
debug!(
1800+
"pushing newly opened {:?} at index {}",
1801+
&directory_subspace,
1802+
self.directory_stack.len()
1803+
);
17651804
self.directory_stack
1766-
.push(DirectoryStackItem::Subspace(subspace));
1805+
.push(DirectoryStackItem::DirectorySubspace(directory_subspace));
17671806
}
17681807

17691808
// Use the current directory for this operation.
@@ -1794,7 +1833,7 @@ impl StackMachine {
17941833
}
17951834
};
17961835

1797-
let subspace = match directory
1836+
let directory_subspace = match directory
17981837
.create_or_open(
17991838
txn,
18001839
(*path.get(0).unwrap().to_owned()).to_vec(),
@@ -1809,8 +1848,13 @@ impl StackMachine {
18091848
}
18101849
};
18111850

1851+
debug!(
1852+
"pushing created_or_opened {:?} at index {}",
1853+
&directory_subspace,
1854+
self.directory_stack.len()
1855+
);
18121856
self.directory_stack
1813-
.push(DirectoryStackItem::Subspace(subspace));
1857+
.push(DirectoryStackItem::DirectorySubspace(directory_subspace));
18141858
}
18151859

18161860
// Pop the top item off the stack as [index]. Set the current directory list
@@ -1927,7 +1971,6 @@ impl StackMachine {
19271971
// tuple off the stack as [path]. Call list, passing it path if one was popped.
19281972
// Pack the resulting list of directories using the tuple layer and push the
19291973
// packed string onto the stack.
1930-
// TODO(PZ): fix seed 3110103296
19311974
DirectoryList => {
19321975
debug!("List stack: {:?}", self.stack);
19331976
let count = self.pop_usize().await;
@@ -1946,20 +1989,55 @@ impl StackMachine {
19461989

19471990
let paths = paths.get(0).expect("could not retrieve a path");
19481991

1949-
directory
1950-
.list(txn, paths.to_vec())
1951-
.await
1952-
.expect("could not list directory")
1953-
.iter()
1954-
.for_each(|s| self.push(number, Element::String(Cow::Owned(s.clone()))));
1992+
let children = match directory.list(txn, paths.to_vec()).await {
1993+
Ok(v) => v,
1994+
Err(e) => {
1995+
self.push_directory_err(instr.code, number, e);
1996+
return Err(());
1997+
}
1998+
};
1999+
2000+
let mut elements: Vec<Element> = vec![];
2001+
for child in children {
2002+
let element = Element::String(Cow::from(child));
2003+
elements.push(element);
2004+
}
2005+
let tuple = Element::Tuple(elements);
2006+
self.push(number, Element::Bytes(pack(&tuple).into()));
19552007
}
19562008

19572009
// Use the current directory for this operation.
19582010
//
19592011
// Pop 1 item off the stack as [count] (either 0 or 1). If count is 1, pop 1
19602012
// tuple off the stack as [path]. Call exists, passing it path if one
19612013
// was popped. Push 1 onto the stack if the path exists and 0 if it does not.
1962-
DirectoryExists => unimplemented!(),
2014+
DirectoryExists => {
2015+
debug!("Exists stack: {:?}", self.stack);
2016+
let count = self.pop_usize().await;
2017+
2018+
let paths = self.pop_tuple(count).await;
2019+
2020+
let directory = self
2021+
.get_current_directory()
2022+
.expect("could not find a directory");
2023+
2024+
let txn = match trx {
2025+
TransactionState::Transaction(ref t) => t,
2026+
_ => {
2027+
panic!("could not find an active transaction");
2028+
}
2029+
};
2030+
2031+
let paths = paths.get(0).expect("could not retrieve a path");
2032+
let exists = match directory.exists(txn, paths.to_owned()).await {
2033+
Ok(exist) => exist,
2034+
Err(e) => {
2035+
self.push_directory_err(instr.code, number, e);
2036+
return Err(());
2037+
}
2038+
};
2039+
self.push(number, Element::Int(i64::from(exists)));
2040+
}
19632041

19642042
// Use the current directory for this operation.
19652043
//
@@ -2064,16 +2142,15 @@ impl StackMachine {
20642142
handle.join().expect("joined thread to not panic");
20652143
}
20662144
}
2067-
fn get_current_directory(&self) -> Option<&DirectoryLayer> {
2145+
fn get_current_directory(&self) -> Option<Box<dyn Directory>> {
2146+
debug!("current directory is at index {}", self.directory_index);
20682147
match self.directory_stack.get(self.directory_index) {
20692148
None => None,
2070-
Some(directory_or_subspace) => {
2071-
dbg!(directory_or_subspace);
2072-
match directory_or_subspace {
2073-
DirectoryStackItem::Directory(d) => Some(d),
2074-
_ => None,
2075-
}
2076-
}
2149+
Some(directory_or_subspace) => match directory_or_subspace {
2150+
DirectoryStackItem::Directory(d) => Some(Box::new(d.clone())),
2151+
DirectoryStackItem::DirectorySubspace(d) => Some(Box::new((*d).clone())),
2152+
_ => None,
2153+
},
20772154
}
20782155
}
20792156
}

foundationdb/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ static_assertions = "1.1.0"
5050
uuid = { version = "0.8.1", optional = true }
5151
num-bigint = { version = "0.3.0", optional = true }
5252
byteorder = "1.3.2"
53+
async-trait = "0.1.48"
5354

5455
[dev-dependencies]
5556
lazy_static = "1.4.0"

0 commit comments

Comments
 (0)