From 1743922dfd784e6cec328ac9e577ff8594f24f4f Mon Sep 17 00:00:00 2001 From: Ziyang Hu Date: Thu, 10 Nov 2022 14:02:09 +0800 Subject: [PATCH] towards storage API --- Cargo.toml | 2 +- src/lib.rs | 1 + src/runtime/relation.rs | 9 ----- src/storage/mod.rs | 32 +++++++++++++++++ src/storage/rocks.rs | 78 +++++++++++++++++++++++++++++++++++++++++ src/storage/sled.rs | 4 +++ src/storage/tikv.rs | 4 +++ 7 files changed, 120 insertions(+), 10 deletions(-) create mode 100644 src/storage/mod.rs create mode 100644 src/storage/rocks.rs create mode 100644 src/storage/sled.rs create mode 100644 src/storage/tikv.rs diff --git a/Cargo.toml b/Cargo.toml index 67e7ef0b..9ba179da 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "cozo" -version = "0.1.6" +version = "0.1.7" edition = "2021" description = "A general-purpose, transactional, relational database that uses Datalog and focuses on graph data and algorithms" authors = ["Ziyang Hu"] diff --git a/src/lib.rs b/src/lib.rs index a4c36f19..72e5bcda 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -26,3 +26,4 @@ pub(crate) mod parse; pub(crate) mod query; pub(crate) mod runtime; pub(crate) mod utils; +pub(crate) mod storage; diff --git a/src/runtime/relation.rs b/src/runtime/relation.rs index 27d6e44c..ebda8f05 100644 --- a/src/runtime/relation.rs +++ b/src/runtime/relation.rs @@ -280,9 +280,6 @@ impl RelationIterator { None => None, Some((k_slice, v_slice)) => { if self.upper_bound.as_slice() <= k_slice { - // - // } - // if compare_tuple_keys(&self.upper_bound, k_slice) != Greater { None } else { let mut tup = Tuple::decode_from_key(k_slice); @@ -290,12 +287,6 @@ impl RelationIterator { let vals: Vec = rmp_serde::from_slice(&v_slice[ENCODED_KEY_MIN_LEN..]).unwrap(); tup.0.extend(vals); } - // if !v_slice.is_empty() { - // let v_tup = EncodedTuple(v_slice); - // if v_tup.arity() > 0 { - // tup.0.extend(v_tup.decode().0); - // } - // } Some(tup) } } diff --git a/src/storage/mod.rs b/src/storage/mod.rs new file mode 100644 index 00000000..2ee9d26a --- /dev/null +++ b/src/storage/mod.rs @@ -0,0 +1,32 @@ +/* + * Copyright 2022, The Cozo Project Authors. Licensed under MPL-2.0. + */ + +use miette::Result; + +pub(crate) mod rocks; +pub(crate) mod sled; +pub(crate) mod tikv; + +pub(crate) trait Storage { + type Tx: StoreTx; + + fn tx(&self) -> Result; + fn del_range(&self, lower: &[u8], upper: &[u8]) -> Result<()>; +} + +pub(crate) trait StoreTx { + type ReadSlice: AsRef<[u8]>; + type IterSlice: AsRef<[u8]>; + + type KeyIter: Iterator>; + type KeyValueIter: Iterator>; + + fn get(&self, key: &[u8], for_update: bool) -> Result>; + fn put(&mut self, key: &[u8], val: &[u8]) -> Result<()>; + fn del(&mut self, key: &[u8]) -> Result<()>; + fn exists(&self, key: &[u8], for_update: bool) -> Result; + fn commit(&mut self) -> Result<()>; + fn range_scan(&self, lower: &[u8], upper: &[u8]) -> Self::KeyValueIter; + fn range_key_scan(&self, lower: &[u8], upper: &[u8]) -> Self::KeyIter; +} diff --git a/src/storage/rocks.rs b/src/storage/rocks.rs new file mode 100644 index 00000000..21af33f8 --- /dev/null +++ b/src/storage/rocks.rs @@ -0,0 +1,78 @@ +/* + * Copyright 2022, The Cozo Project Authors. Licensed under MPL-2.0. + */ + +use miette::Result; + +use crate::storage::{Storage, StoreTx}; + +struct RocksDbStorage; + +impl Storage for RocksDbStorage { + type Tx = RocksDbTx; + + fn tx(&self) -> miette::Result { + todo!() + } + + fn del_range(&self, lower: &[u8], upper: &[u8]) -> miette::Result<()> { + todo!() + } +} + +struct RocksDbTx; + +impl StoreTx for RocksDbTx { + type ReadSlice = Vec; + type IterSlice = Vec; + type KeyIter = RocksDbKeyIter; + type KeyValueIter = RocksDbIter; + + fn get(&self, key: &[u8], for_update: bool) -> miette::Result> { + todo!() + } + + fn put(&mut self, key: &[u8], val: &[u8]) -> miette::Result<()> { + todo!() + } + + fn del(&mut self, key: &[u8]) -> miette::Result<()> { + todo!() + } + + fn exists(&self, key: &[u8], for_update: bool) -> miette::Result { + todo!() + } + + fn commit(&mut self) -> miette::Result<()> { + todo!() + } + + fn range_scan(&self, lower: &[u8], upper: &[u8]) -> Self::KeyValueIter { + todo!() + } + + fn range_key_scan(&self, lower: &[u8], upper: &[u8]) -> Self::KeyIter { + todo!() + } +} + +struct RocksDbKeyIter; + +impl Iterator for RocksDbKeyIter { + type Item = Result>; + + fn next(&mut self) -> Option { + todo!() + } +} + +struct RocksDbIter; + +impl Iterator for RocksDbIter { + type Item = Result<(Vec, Vec)>; + + fn next(&mut self) -> Option { + todo!() + } +} \ No newline at end of file diff --git a/src/storage/sled.rs b/src/storage/sled.rs new file mode 100644 index 00000000..54b1970e --- /dev/null +++ b/src/storage/sled.rs @@ -0,0 +1,4 @@ +/* + * Copyright 2022, The Cozo Project Authors. Licensed under MPL-2.0. + */ + diff --git a/src/storage/tikv.rs b/src/storage/tikv.rs new file mode 100644 index 00000000..54b1970e --- /dev/null +++ b/src/storage/tikv.rs @@ -0,0 +1,4 @@ +/* + * Copyright 2022, The Cozo Project Authors. Licensed under MPL-2.0. + */ +