1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#[doc(hidden)]
pub mod cdt;
pub mod scalar;
pub mod lists;
pub mod maps;
pub use self::maps::{MapOrder, MapReturnType, MapWriteMode, MapPolicy};
pub use self::scalar::*;
use self::cdt::CdtOperation;
use errors::*;
use Value;
use commands::ParticleType;
use commands::buffer::Buffer;
#[derive(Debug, Clone, Copy)]
#[doc(hidden)]
pub enum OperationType {
Read = 1,
Write = 2,
CdtRead = 3,
CdtWrite = 4,
Incr = 5,
Append = 9,
Prepend = 10,
Touch = 11,
}
#[derive(Debug)]
#[doc(hidden)]
pub enum OperationData<'a> {
None,
Value(&'a Value),
CdtListOp(CdtOperation<'a>),
CdtMapOp(CdtOperation<'a>),
}
#[derive(Debug)]
#[doc(hidden)]
pub enum OperationBin<'a> {
None,
All,
Name(&'a str),
}
#[derive(Debug)]
pub struct Operation<'a> {
#[doc(hidden)]
pub op: OperationType,
#[doc(hidden)]
pub bin: OperationBin<'a>,
#[doc(hidden)]
pub data: OperationData<'a>,
}
impl<'a> Operation<'a> {
#[doc(hidden)]
pub fn estimate_size(&self) -> Result<usize> {
let mut size: usize = 0;
size += match self.bin {
OperationBin::Name(bin) => bin.len(),
OperationBin::None | OperationBin::All => 0,
};
size += match self.data {
OperationData::None => 0,
OperationData::Value(value) => try!(value.estimate_size()),
OperationData::CdtListOp(ref cdt_op) |
OperationData::CdtMapOp(ref cdt_op) => try!(cdt_op.estimate_size()),
};
Ok(size)
}
#[doc(hidden)]
pub fn write_to(&self, buffer: &mut Buffer) -> Result<usize> {
let mut size: usize = 0;
let op_size = try!(self.estimate_size());
size += try!(buffer.write_u32(op_size as u32 + 4));
size += try!(buffer.write_u8(self.op as u8));
match self.data {
OperationData::None => {
size += try!(self.write_op_header_to(buffer, ParticleType::NULL as u8));
}
OperationData::Value(value) => {
size += try!(self.write_op_header_to(buffer, value.particle_type() as u8));
size += try!(value.write_to(buffer));
}
OperationData::CdtListOp(ref cdt_op) |
OperationData::CdtMapOp(ref cdt_op) => {
size += try!(self.write_op_header_to(buffer, cdt_op.particle_type() as u8));
size += try!(cdt_op.write_to(buffer));
}
};
Ok(size)
}
#[doc(hidden)]
fn write_op_header_to(&self, buffer: &mut Buffer, particle_type: u8) -> Result<usize> {
let mut size = try!(buffer.write_u8(particle_type as u8));
size += try!(buffer.write_u8(0));
match self.bin {
OperationBin::Name(bin) => {
size += try!(buffer.write_u8(bin.len() as u8));
size += try!(buffer.write_str(bin));
}
OperationBin::None | OperationBin::All => {
size += try!(buffer.write_u8(0));
}
}
Ok(size)
}
}