Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //! Defines the interfaces that the KV store exposes.
- use futures::{
- future::{self, Ready},
- prelude::*,
- };
- use tarpc::{
- client, context,
- server::{self, Handler},
- };
- //pub mod client;
- //mod protocol;
- //pub mod server;
- pub type TxnId = u64;
- /// Represents the different types of errors that can occur on a `put`
- #[derive(Copy, Clone, Debug, Serialize, Deserialize)]
- pub enum PutError {
- /// The transaction is already committed
- AlreadyDone,
- /// The transaction does not exist
- NoSuchTxn,
- }
- /// Represents the different types of errors that can occur on a `commit`
- #[derive(Copy, Clone, Debug, Serialize, Deserialize)]
- pub enum CommitError {
- /// The transaction aborted
- Abort,
- /// The transaction does not exist
- NoSuchTxn,
- }
- #[tarpc::service]
- pub trait KVStore {
- /// Ping (for testing connectivity)
- async fn null();
- /// Start a transaction.
- ///
- /// This gets a new TxnId for the Txn.
- async fn start_txn() -> TxnId;
- /// Add the insertion operation to the given transaction. The operation inserts the `(key,
- /// value)` pair into the KV store.
- ///
- /// If the key is already there, overwrite the value.
- ///
- /// If the user has already called `commit` on the txn or the txn does not exist, `Err` is
- /// returned. Otherwise, `Ok` is returned.
- async fn put(key: String, value: String, txn: TxnId) -> Result<(), PutError>;
- /// Commit the given transaction if possible. If the transaction commits, this returns
- /// `Ok(())`; otherwise, `Err(())`.
- ///
- /// If the txn aborts, `Err` is returned. The user should obtain a new TxnId and start
- /// over.
- ///
- /// Transactions in this system satisfy the Zippy Property, which implies that they are
- /// idempotent. Formally, a function `f` is _zippy_ if for all functions `g`, `f(g(f(x))) =
- /// g(f(x))`. Informally, this means that if `commit` returns a decision `C` for transaction
- /// `t`, it will never return anything other than `C` in subsequent calls to `commit` for `t`,
- /// even if the subsequent calls are interleaved with arbitrary other transactions.
- async fn commit(txn: TxnId) -> Result<(), CommitError>;
- /// Get the current value of `key` in the KV store or `None` if it is not in the
- /// store.
- ///
- /// Returns `Err` if the value is locked by a transaction.
- async fn get(key: String) -> Result<Option<String>, ()>;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement