memcached/proto/
mod.rs

1// Copyright (c) 2015 Y. T. Chung <zonyitoo@gmail.com>
2// Licensed under the Apache License, Version 2.0
3// <LICENSE-APACHE or
4// http://www.apache.org/licenses/LICENSE-2.0> or the MIT
5// license <LICENSE-MIT or http://opensource.org/licenses/MIT>,
6// at your option. All files in the project carrying such
7// notice may not be copied, modified, or distributed except
8// according to those terms.
9
10//! Memcached protocol
11
12use std::collections::{BTreeMap, HashMap};
13use std::convert::From;
14use std::error;
15use std::fmt::{self, Display};
16use std::io;
17
18use semver::Version;
19
20pub use self::binary::BinaryProto;
21
22pub mod binary;
23mod binarydef;
24
25/// Protocol type
26#[derive(Copy, Clone)]
27pub enum ProtoType {
28    Binary,
29}
30
31#[derive(Debug)]
32pub enum Error {
33    BinaryProtoError(binary::Error),
34    IoError(io::Error),
35    OtherError {
36        desc: &'static str,
37        detail: Option<String>,
38    },
39}
40
41pub type MemCachedResult<T> = Result<T, Error>;
42
43impl error::Error for Error {}
44
45impl Display for Error {
46    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
47        match *self {
48            Error::BinaryProtoError(ref err) => err.fmt(f),
49            Error::IoError(ref err) => err.fmt(f),
50            Error::OtherError { desc, ref detail } => {
51                write!(f, "{}", desc)?;
52                match *detail {
53                    Some(ref s) => write!(f, " ({})", s),
54                    None => Ok(()),
55                }
56            }
57        }
58    }
59}
60
61impl From<io::Error> for Error {
62    fn from(err: io::Error) -> Error {
63        Error::IoError(err)
64    }
65}
66
67impl From<binary::Error> for Error {
68    fn from(err: binary::Error) -> Error {
69        Error::BinaryProtoError(err)
70    }
71}
72
73pub trait Proto:
74    Operation + MultiOperation + ServerOperation + NoReplyOperation + CasOperation + AuthOperation
75{
76    // fn clone(&self) -> Box<Proto + Send>;
77}
78
79impl<T> Proto for T where
80    T: Operation
81        + MultiOperation
82        + ServerOperation
83        + NoReplyOperation
84        + CasOperation
85        + AuthOperation
86{
87}
88
89pub trait Operation {
90    fn set(&mut self, key: &[u8], value: &[u8], flags: u32, expiration: u32)
91        -> MemCachedResult<()>;
92    fn add(&mut self, key: &[u8], value: &[u8], flags: u32, expiration: u32)
93        -> MemCachedResult<()>;
94    fn delete(&mut self, key: &[u8]) -> MemCachedResult<()>;
95    fn replace(
96        &mut self,
97        key: &[u8],
98        value: &[u8],
99        flags: u32,
100        expiration: u32,
101    ) -> MemCachedResult<()>;
102    fn get(&mut self, key: &[u8]) -> MemCachedResult<(Vec<u8>, u32)>;
103    fn getk(&mut self, key: &[u8]) -> MemCachedResult<(Vec<u8>, Vec<u8>, u32)>;
104    fn increment(
105        &mut self,
106        key: &[u8],
107        amount: u64,
108        initial: u64,
109        expiration: u32,
110    ) -> MemCachedResult<u64>;
111    fn decrement(
112        &mut self,
113        key: &[u8],
114        amount: u64,
115        initial: u64,
116        expiration: u32,
117    ) -> MemCachedResult<u64>;
118    fn append(&mut self, key: &[u8], value: &[u8]) -> MemCachedResult<()>;
119    fn prepend(&mut self, key: &[u8], value: &[u8]) -> MemCachedResult<()>;
120    fn touch(&mut self, key: &[u8], expiration: u32) -> MemCachedResult<()>;
121}
122
123pub trait CasOperation {
124    fn set_cas(
125        &mut self,
126        key: &[u8],
127        value: &[u8],
128        flags: u32,
129        expiration: u32,
130        cas: u64,
131    ) -> MemCachedResult<u64>;
132    fn add_cas(
133        &mut self,
134        key: &[u8],
135        value: &[u8],
136        flags: u32,
137        expiration: u32,
138    ) -> MemCachedResult<u64>;
139    fn replace_cas(
140        &mut self,
141        key: &[u8],
142        value: &[u8],
143        flags: u32,
144        expiration: u32,
145        cas: u64,
146    ) -> MemCachedResult<u64>;
147    fn get_cas(&mut self, key: &[u8]) -> MemCachedResult<(Vec<u8>, u32, u64)>;
148    fn getk_cas(&mut self, key: &[u8]) -> MemCachedResult<(Vec<u8>, Vec<u8>, u32, u64)>;
149    fn increment_cas(
150        &mut self,
151        key: &[u8],
152        amount: u64,
153        initial: u64,
154        expiration: u32,
155        cas: u64,
156    ) -> MemCachedResult<(u64, u64)>;
157    fn decrement_cas(
158        &mut self,
159        key: &[u8],
160        amount: u64,
161        initial: u64,
162        expiration: u32,
163        cas: u64,
164    ) -> MemCachedResult<(u64, u64)>;
165    fn append_cas(&mut self, key: &[u8], value: &[u8], cas: u64) -> MemCachedResult<u64>;
166    fn prepend_cas(&mut self, key: &[u8], value: &[u8], cas: u64) -> MemCachedResult<u64>;
167    fn touch_cas(&mut self, key: &[u8], expiration: u32, cas: u64) -> MemCachedResult<u64>;
168}
169
170pub trait ServerOperation {
171    fn quit(&mut self) -> MemCachedResult<()>;
172    fn flush(&mut self, expiration: u32) -> MemCachedResult<()>;
173    fn noop(&mut self) -> MemCachedResult<()>;
174    fn version(&mut self) -> MemCachedResult<Version>;
175    fn stat(&mut self) -> MemCachedResult<BTreeMap<String, String>>;
176}
177
178pub trait MultiOperation {
179    fn set_multi(&mut self, kv: BTreeMap<&[u8], (&[u8], u32, u32)>) -> MemCachedResult<()>;
180    fn delete_multi(&mut self, keys: &[&[u8]]) -> MemCachedResult<()>;
181    fn increment_multi<'a>(
182        &mut self,
183        kv: HashMap<&'a [u8], (u64, u64, u32)>,
184    ) -> MemCachedResult<HashMap<&'a [u8], u64>>;
185    fn get_multi(&mut self, keys: &[&[u8]]) -> MemCachedResult<HashMap<Vec<u8>, (Vec<u8>, u32)>>;
186}
187
188pub trait NoReplyOperation {
189    fn set_noreply(
190        &mut self,
191        key: &[u8],
192        value: &[u8],
193        flags: u32,
194        expiration: u32,
195    ) -> MemCachedResult<()>;
196    fn add_noreply(
197        &mut self,
198        key: &[u8],
199        value: &[u8],
200        flags: u32,
201        expiration: u32,
202    ) -> MemCachedResult<()>;
203    fn delete_noreply(&mut self, key: &[u8]) -> MemCachedResult<()>;
204    fn replace_noreply(
205        &mut self,
206        key: &[u8],
207        value: &[u8],
208        flags: u32,
209        expiration: u32,
210    ) -> MemCachedResult<()>;
211    fn increment_noreply(
212        &mut self,
213        key: &[u8],
214        amount: u64,
215        initial: u64,
216        expiration: u32,
217    ) -> MemCachedResult<()>;
218    fn decrement_noreply(
219        &mut self,
220        key: &[u8],
221        amount: u64,
222        initial: u64,
223        expiration: u32,
224    ) -> MemCachedResult<()>;
225    fn append_noreply(&mut self, key: &[u8], value: &[u8]) -> MemCachedResult<()>;
226    fn prepend_noreply(&mut self, key: &[u8], value: &[u8]) -> MemCachedResult<()>;
227}
228
229#[derive(Debug)]
230pub enum AuthResponse {
231    Continue(Vec<u8>),
232    Succeeded,
233    Failed,
234}
235
236pub trait AuthOperation {
237    fn list_mechanisms(&mut self) -> MemCachedResult<Vec<String>>;
238    fn auth_start(&mut self, mech: &str, init: &[u8]) -> MemCachedResult<AuthResponse>;
239    fn auth_continue(&mut self, mech: &str, data: &[u8]) -> MemCachedResult<AuthResponse>;
240}