diff --git a/src/main.rs b/src/main.rs index 477f98e9f269f51ae71d0cb219cd9f38ebad46f6..2b7e3abb3f503d45ab54f7e751b71c62f57b06f7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -815,9 +815,12 @@ fn postjoin(args: &args::Run, storage: Storage) { } loop { - let peer = storage.peers.get(&raft_id) + let peer = storage + .peers + .get(&raft_id) .expect("peer must be persisted at the time of postjoin"); - let cluster_id = storage.raft + let cluster_id = storage + .raft .cluster_id() .unwrap() .expect("cluster_id must be persisted at the time of postjoin"); diff --git a/src/traft/mod.rs b/src/traft/mod.rs index d492e69e1acdefa5bcbe65cccf9466d3c5eb8efa..1ef16d1a7aebb08f22a1b075fb1d99ce046935fb 100644 --- a/src/traft/mod.rs +++ b/src/traft/mod.rs @@ -33,7 +33,7 @@ use protobuf::Message as _; pub use network::ConnectionPool; pub use raft_storage::RaftSpaceAccess; use storage::ClusterSpace; -pub use storage::{Storage as StorageOld, StorageNew as Storage}; +pub use storage::Storage; pub use topology::Topology; pub type RaftId = u64; @@ -318,13 +318,13 @@ pub enum OpDML { } impl OpResult for OpDML { - type Result = Result<Option<Tuple>, ::raft::StorageError>; + type Result = tarantool::Result<Option<Tuple>>; fn result(&self) -> Self::Result { match self { - Self::Insert { space, tuple } => StorageOld::insert(*space, tuple).map(Some), - Self::Replace { space, tuple } => StorageOld::replace(*space, tuple).map(Some), - Self::Update { space, key, ops } => StorageOld::update(*space, key, ops), - Self::Delete { space, key } => StorageOld::delete(*space, key), + Self::Insert { space, tuple } => space.insert(tuple).map(Some), + Self::Replace { space, tuple } => space.replace(tuple).map(Some), + Self::Update { space, key, ops } => space.update(key, ops), + Self::Delete { space, key } => space.delete(key), } } } diff --git a/src/traft/node.rs b/src/traft/node.rs index c91f9afedd1d8d1963acf1a609fa633e72cf3bad..91eb9927c06bc78e8f424a375482047177baec5d 100644 --- a/src/traft/node.rs +++ b/src/traft/node.rs @@ -566,7 +566,10 @@ impl NodeImpl { expelled: &mut bool, ) { assert_eq!(entry.entry_type, raft::EntryType::EntryNormal); - let result = entry.op().unwrap_or(&traft::Op::Nop).on_commit(&self.storage.peers); + let result = entry + .op() + .unwrap_or(&traft::Op::Nop) + .on_commit(&self.storage.peers); if let Some(lc) = entry.lc() { if let Some(notify) = self.notifications.remove(lc) { diff --git a/src/traft/storage.rs b/src/traft/storage.rs index 58ba0d11bfc1daaa85352126c5599b43d83cf49d..79122da749af4dc510afb72ebdd9206ccfc0109e 100644 --- a/src/traft/storage.rs +++ b/src/traft/storage.rs @@ -1,4 +1,3 @@ -use ::raft::StorageError; use ::raft::INVALID_ID; use ::tarantool::index::{Index, IteratorType}; use ::tarantool::space::{FieldType, Space}; @@ -33,6 +32,43 @@ define_str_enum! { #[error("unknown cluster space {0}")] pub struct UnknownClusterSpace(pub String); +impl ClusterSpace { + #[inline] + fn get(&self) -> tarantool::Result<Space> { + Space::find(self.as_str()).ok_or_else(|| { + tarantool::set_error!( + tarantool::error::TarantoolErrorCode::NoSuchSpace, + "no such space \"{self}\"" + ); + tarantool::error::TarantoolError::last().into() + }) + } + + #[inline] + pub fn insert(&self, tuple: &impl ToTupleBuffer) -> tarantool::Result<Tuple> { + self.get()?.insert(tuple) + } + + #[inline] + pub fn replace(&self, tuple: &impl ToTupleBuffer) -> tarantool::Result<Tuple> { + self.get()?.replace(tuple) + } + + #[inline] + pub fn update( + &self, + key: &impl ToTupleBuffer, + ops: &[impl ToTupleBuffer], + ) -> tarantool::Result<Option<Tuple>> { + self.get()?.update(key, ops) + } + + #[inline] + pub fn delete(&self, key: &impl ToTupleBuffer) -> tarantool::Result<Option<Tuple>> { + self.get()?.delete(key) + } +} + //////////////////////////////////////////////////////////////////////////////// // StateKey //////////////////////////////////////////////////////////////////////////////// @@ -50,85 +86,18 @@ define_str_enum! { #[error("unknown state key {0}")] pub struct UnknownStateKey(pub String); -//////////////////////////////////////////////////////////////////////////////// -// Error -//////////////////////////////////////////////////////////////////////////////// - -// TODO: remove this type, use traft::error::Error instead -#[allow(clippy::enum_variant_names)] -#[derive(Debug, Error)] -enum Error { - #[error("no such space \"{0}\"")] - NoSuchSpace(String), - - #[allow(dead_code)] - #[error("no such index \"{1}\" in space \"{0}\"")] - NoSuchIndex(String, String), -} - -fn box_err(e: impl std::error::Error + Sync + Send + 'static) -> StorageError { - StorageError::Other(Box::new(e)) -} - -//////////////////////////////////////////////////////////////////////////////// -// Storage -//////////////////////////////////////////////////////////////////////////////// - -pub struct Storage; - -impl Storage { - fn space(name: impl AsRef<str> + Into<String>) -> Result<Space, StorageError> { - Space::find(name.as_ref()) - .ok_or_else(|| Error::NoSuchSpace(name.into())) - .map_err(box_err) - } - - pub fn insert(space: ClusterSpace, tuple: &impl ToTupleBuffer) -> Result<Tuple, StorageError> { - Storage::space(space.as_str())? - .insert(tuple) - .map_err(box_err) - } - - pub fn replace(space: ClusterSpace, tuple: &impl ToTupleBuffer) -> Result<Tuple, StorageError> { - Storage::space(space.as_str())? - .replace(tuple) - .map_err(box_err) - } - - pub fn update( - space: ClusterSpace, - key: &impl ToTupleBuffer, - ops: &[impl ToTupleBuffer], - ) -> Result<Option<Tuple>, StorageError> { - Storage::space(space.as_str())? - .update(key, ops) - .map_err(box_err) - } - - #[rustfmt::skip] - pub fn delete( - space: ClusterSpace, - key: &impl ToTupleBuffer, - ) -> Result<Option<Tuple>, StorageError> { - Storage::space(space.as_str())? - .delete(key) - .map_err(box_err) - } -} - //////////////////////////////////////////////////////////////////////////////// // Storage //////////////////////////////////////////////////////////////////////////////// -// TODO: get rid of old `Storage`, and rename this to `Storage` #[derive(Clone, Debug)] -pub struct StorageNew { +pub struct Storage { pub state: State, pub peers: Peers, pub raft: RaftSpaceAccess, } -impl StorageNew { +impl Storage { pub fn new() -> tarantool::Result<Self> { Ok(Self { state: State::new()?,