Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.39 KB | None | 0 0
  1. //! Defines the interfaces that the KV store exposes.
  2.  
  3. use futures::{
  4. future::{self, Ready},
  5. prelude::*,
  6. };
  7.  
  8. use tarpc::{
  9. client, context,
  10. server::{self, Handler},
  11. };
  12.  
  13. //pub mod client;
  14. //mod protocol;
  15. //pub mod server;
  16.  
  17. pub type TxnId = u64;
  18.  
  19. /// Represents the different types of errors that can occur on a `put`
  20. #[derive(Copy, Clone, Debug, Serialize, Deserialize)]
  21. pub enum PutError {
  22. /// The transaction is already committed
  23. AlreadyDone,
  24.  
  25. /// The transaction does not exist
  26. NoSuchTxn,
  27. }
  28.  
  29. /// Represents the different types of errors that can occur on a `commit`
  30. #[derive(Copy, Clone, Debug, Serialize, Deserialize)]
  31. pub enum CommitError {
  32. /// The transaction aborted
  33. Abort,
  34.  
  35. /// The transaction does not exist
  36. NoSuchTxn,
  37. }
  38.  
  39. #[tarpc::service]
  40. pub trait KVStore {
  41. /// Ping (for testing connectivity)
  42. async fn null();
  43.  
  44. /// Start a transaction.
  45. ///
  46. /// This gets a new TxnId for the Txn.
  47. async fn start_txn() -> TxnId;
  48.  
  49. /// Add the insertion operation to the given transaction. The operation inserts the `(key,
  50. /// value)` pair into the KV store.
  51. ///
  52. /// If the key is already there, overwrite the value.
  53. ///
  54. /// If the user has already called `commit` on the txn or the txn does not exist, `Err` is
  55. /// returned. Otherwise, `Ok` is returned.
  56. async fn put(key: String, value: String, txn: TxnId) -> Result<(), PutError>;
  57.  
  58. /// Commit the given transaction if possible. If the transaction commits, this returns
  59. /// `Ok(())`; otherwise, `Err(())`.
  60. ///
  61. /// If the txn aborts, `Err` is returned. The user should obtain a new TxnId and start
  62. /// over.
  63. ///
  64. /// Transactions in this system satisfy the Zippy Property, which implies that they are
  65. /// idempotent. Formally, a function `f` is _zippy_ if for all functions `g`, `f(g(f(x))) =
  66. /// g(f(x))`. Informally, this means that if `commit` returns a decision `C` for transaction
  67. /// `t`, it will never return anything other than `C` in subsequent calls to `commit` for `t`,
  68. /// even if the subsequent calls are interleaved with arbitrary other transactions.
  69. async fn commit(txn: TxnId) -> Result<(), CommitError>;
  70.  
  71. /// Get the current value of `key` in the KV store or `None` if it is not in the
  72. /// store.
  73. ///
  74. /// Returns `Err` if the value is locked by a transaction.
  75. async fn get(key: String) -> Result<Option<String>, ()>;
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement