diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 00000000..0a395358 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,16 @@ +[package] +name = "tikv-client" +version = "0.0.0" +keywords = ["TiKV", "KV", "distributed-systems"] +license = "Apache-2.0" +authors = ["The TiKV Project Developers"] +repository = "https://github.com/tikv/client-rust" +description = "The rust language implementation of TiKV client." + +[lib] +name = "tikv_client" + +[dependencies] +futures = "0.1" +serde = "1.0" +serde_derive = "1.0" diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 00000000..8567b9f7 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,88 @@ +extern crate futures; +extern crate serde; +#[macro_use] +extern crate serde_derive; + +use std::io::Error; +use std::path::PathBuf; + +use futures::Future; + +pub mod raw; +pub mod transaction; + +#[derive(Default, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)] +pub struct Key(Vec); +#[derive(Default, Clone, Eq, PartialEq, Hash, Debug)] +pub struct Value(Vec); +#[derive(Default, Clone, Eq, PartialEq, Debug)] +pub struct KvPair(Key, Value); +#[derive(Default, Clone, Eq, PartialEq, Debug)] +pub struct KeyRange(Key, Key); + +pub type KvFuture = Box + Send>; + +impl Into for Vec { + fn into(self) -> Key { + Key(self) + } +} + +impl Into for Vec { + fn into(self) -> Value { + Value(self) + } +} + +impl Into for (Key, Value) { + fn into(self) -> KvPair { + KvPair(self.0, self.1) + } +} + +impl Into for (Key, Key) { + fn into(self) -> KeyRange { + KeyRange(self.0, self.1) + } +} + +#[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq)] +#[serde(default)] +#[serde(rename_all = "kebab-case")] +pub struct Config { + pub pd_endpoints: Vec, + pub ca_path: Option, + pub cert_path: Option, + pub key_path: Option, +} + +impl Config { + pub fn new(pd_endpoints: E) -> Self + where + E: IntoIterator, + { + Config { + pd_endpoints: pd_endpoints.into_iter().collect(), + ca_path: None, + cert_path: None, + key_path: None, + } + } + + pub fn with_security( + pd_endpoints: E, + ca_path: PathBuf, + cert_path: PathBuf, + key_path: PathBuf, + ) -> Self + where + E: IntoIterator, + { + Config { + pd_endpoints: pd_endpoints.into_iter().collect(), + ca_path: Some(ca_path), + cert_path: Some(cert_path), + key_path: Some(key_path), + } + } +} diff --git a/src/raw.rs b/src/raw.rs new file mode 100644 index 00000000..2178c561 --- /dev/null +++ b/src/raw.rs @@ -0,0 +1,62 @@ +use {Config, Key, KeyRange, KvFuture, KvPair, Value}; + +pub trait Client { + fn new(_config: &Config) -> KvFuture { + unimplemented!() + } + + fn get(&self, key: K, cf: C) -> KvFuture + where + K: Into, + C: Into>; + + fn batch_get(&self, keys: I, cf: C) -> KvFuture> + where + I: IntoIterator, + K: Into, + C: Into>; + + fn put(&self, pair: P, cf: C) -> KvFuture<()> + where + P: Into, + C: Into>; + + fn batch_put(&self, pairs: I, cf: C) -> KvFuture<()> + where + I: IntoIterator, + P: Into, + C: Into>; + + fn delete(&self, key: K, cf: C) -> KvFuture<()> + where + K: Into, + C: Into>; + + fn batch_delete(&self, keys: I, cf: C) -> KvFuture<()> + where + I: IntoIterator, + K: Into, + C: Into>; + + fn scan(&self, range: R, limit: u32, key_only: bool, cf: C) -> KvFuture> + where + R: Into, + C: Into>; + + fn batch_scan( + &self, + ranges: I, + each_limit: u32, + key_only: bool, + cf: C, + ) -> KvFuture> + where + I: IntoIterator, + R: Into, + C: Into>; + + fn delete_range(&self, range: R, cf: C) -> KvFuture<()> + where + R: Into, + C: Into>; +} diff --git a/src/transaction.rs b/src/transaction.rs new file mode 100644 index 00000000..3a37afc2 --- /dev/null +++ b/src/transaction.rs @@ -0,0 +1,229 @@ +use std::io::Error; + +use futures::{Poll, Stream}; + +use {Config, Key, KvFuture, KvPair, Value}; + +#[derive(Copy, Clone)] +pub struct Timestamp(u64); + +impl Into for u64 { + fn into(self) -> Timestamp { + Timestamp(self) + } +} + +impl Timestamp { + pub fn timestamp(self) -> u64 { + self.0 + } + + pub fn physical(self) -> i64 { + (self.0 >> 16) as i64 + } + + pub fn logical(self) -> i64 { + (self.0 & 0xFFFF as u64) as i64 + } +} + +pub struct Scanner; + +impl Stream for Scanner { + type Item = KvPair; + type Error = Error; + + fn poll(&mut self) -> Poll, Self::Error> { + unimplemented!() + } +} + +pub trait Retriever { + fn get(&self, key: K) -> KvFuture + where + K: Into; + + fn batch_get(&self, keys: I) -> KvFuture> + where + I: IntoIterator, + K: Into; + + fn seek(&self, key: K) -> KvFuture + where + K: Into; + + fn seek_reverse(&self, key: K) -> KvFuture + where + K: Into; +} + +pub trait Mutator { + fn set

(&mut self, pair: P) -> KvFuture<()> + where + P: Into; + + fn delete(&mut self, key: K) -> KvFuture<()> + where + K: Into; +} + +pub struct Transaction; + +impl Transaction { + pub fn commit(&mut self) -> KvFuture<()> { + unimplemented!() + } + + pub fn rollback(&mut self) -> KvFuture<()> { + unimplemented!() + } + + pub fn lock_keys(&mut self, keys: I) -> KvFuture<()> + where + I: IntoIterator, + K: Into, + { + drop(keys); + unimplemented!() + } + + pub fn is_readonly(&self) -> bool { + unimplemented!() + } + + pub fn start_ts(&self) -> Timestamp { + unimplemented!() + } + + pub fn snapshot(&self) -> KvFuture { + unimplemented!() + } +} + +impl Retriever for Transaction { + fn get(&self, key: K) -> KvFuture + where + K: Into, + { + drop(key); + unimplemented!() + } + + fn batch_get(&self, keys: I) -> KvFuture> + where + I: IntoIterator, + K: Into, + { + drop(keys); + unimplemented!() + } + + fn seek(&self, key: K) -> KvFuture + where + K: Into, + { + drop(key); + unimplemented!() + } + + fn seek_reverse(&self, key: K) -> KvFuture + where + K: Into, + { + drop(key); + unimplemented!() + } +} + +impl Mutator for Transaction { + fn set

(&mut self, pair: P) -> KvFuture<()> + where + P: Into, + { + drop(pair); + unimplemented!() + } + + fn delete(&mut self, key: K) -> KvFuture<()> + where + K: Into, + { + drop(key); + unimplemented!() + } +} + +pub struct Snapshot; + +impl Retriever for Snapshot { + fn get(&self, key: K) -> KvFuture + where + K: Into, + { + drop(key); + unimplemented!() + } + + fn batch_get(&self, keys: I) -> KvFuture> + where + I: IntoIterator, + K: Into, + { + drop(keys); + unimplemented!() + } + + fn seek(&self, key: K) -> KvFuture + where + K: Into, + { + drop(key); + unimplemented!() + } + + fn seek_reverse(&self, key: K) -> KvFuture + where + K: Into, + { + drop(key); + unimplemented!() + } +} + +pub struct Oracle; + +impl Oracle { + pub fn timestamp(&self) -> KvFuture { + unimplemented!() + } + + pub fn is_expired(&self, _lock_timestamp: Timestamp, _ttl: Timestamp) -> KvFuture { + unimplemented!() + } +} + +pub trait Client { + fn new(_config: &Config) -> KvFuture { + unimplemented!() + } + + fn begin(&self) -> KvFuture { + unimplemented!() + } + + fn begin_with_timestamp(&self, _timestamp: Timestamp) -> KvFuture { + unimplemented!() + } + + fn snapshot(&self) -> KvFuture { + unimplemented!() + } + + fn current_timestamp(&self) -> Timestamp { + unimplemented!() + } + + fn oracle(&self) -> Oracle { + unimplemented!() + } +}