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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
use errors::RiakErr;
use protobuf::Message;
use rpb::riak_dt::{DtFetchResp, DtFetchResp_DataType, DtFetchReq, DtUpdateReq, DtValue, MapEntry,
                   MapField, MapField_MapFieldType};
use utils::{DataTypeMapEntryPrivate, DataTypeFetchRespPrivate, DataTypeMapFieldPrivate,
            DataTypeValuePrivate, ProtobufBytes};

/// represents a request to store a Data Type object
#[derive(Clone, Debug)]
pub struct DataTypeUpdateReq(DtUpdateReq);

impl DataTypeUpdateReq {
    /// constructs a new `DataTypeUpdateReq`
    pub fn new<T: Into<Vec<u8>>>(bucket: T, bucket_type: T) -> DataTypeUpdateReq {
        let mut req = DtUpdateReq::new();
        req.set_bucket(bucket.into());
        req.set_field_type(bucket_type.into());
        DataTypeUpdateReq(req)
    }
}

/// represents a request to fetch an object via Riak Data Types
#[derive(Clone, Debug)]
pub struct DataTypeFetchReq(DtFetchReq);

impl DataTypeFetchReq {
    /// constructs a new `DataTypeFetchReq`
    pub fn new<T: Into<Vec<u8>>>(bucket_type: T, bucket: T, key: T) -> DataTypeFetchReq {
        let mut req = DtFetchReq::new();
        req.set_bucket(bucket.into());
        req.set_field_type(bucket_type.into());
        req.set_key(key.into());
        DataTypeFetchReq(req)
    }

    /// get the "bucket_type" property
    pub fn get_bucket_type(&self) -> &[u8] {
        self.0.get_field_type()
    }

    /// set the "bucket_type" property
    pub fn set_bucket_type<T: Into<Vec<u8>>>(&mut self, bucket_type: T) {
        self.0.set_field_type(bucket_type.into());
    }

    /// get the "bucket" property
    pub fn get_bucket(&self) -> &[u8] {
        self.0.get_bucket()
    }

    /// set the "bucket" property
    pub fn set_bucket<T: Into<Vec<u8>>>(&mut self, bucket: T) {
        self.0.set_bucket(bucket.into());
    }

    /// get the "key" property
    pub fn get_key(&self) -> &[u8] {
        self.0.get_key()
    }

    /// set the "key" property
    pub fn set_key<T: Into<Vec<u8>>>(&mut self, key: T) {
        self.0.set_key(key.into());
    }

    /// get the "r" property
    pub fn get_r(&self) -> Option<u32> {
        if self.0.has_r() {
            Some(self.0.get_r())
        } else {
            None
        }
    }

    /// set the "r" property
    pub fn set_r(&mut self, r: u32) {
        self.0.set_r(r);
    }

    /// get the "pr" property
    pub fn get_pr(&self) -> Option<u32> {
        if self.0.has_pr() {
            Some(self.0.get_pr())
        } else {
            None
        }
    }

    /// set the "pr" property
    pub fn set_pr(&mut self, pr: u32) {
        self.0.set_pr(pr);
    }

    /// get the "basic_quorum" property
    pub fn get_basic_quorum(&self) -> Option<bool> {
        if self.0.has_basic_quorum() {
            Some(self.0.get_basic_quorum())
        } else {
            None
        }
    }

    /// set the "basic_quorum" property
    pub fn set_basic_quorum(&mut self, basic_quorum: bool) {
        self.0.set_basic_quorum(basic_quorum);
    }

    /// get the "notfound_ok" property
    pub fn get_notfound_ok(&self) -> Option<bool> {
        if self.0.has_notfound_ok() {
            Some(self.0.get_notfound_ok())
        } else {
            None
        }
    }

    /// set the "notfound_ok" property
    pub fn set_notfound_ok(&mut self, notfound_ok: bool) {
        self.0.set_notfound_ok(notfound_ok);
    }

    /// get the "timeout" property
    pub fn get_timeout(&self) -> Option<u32> {
        if self.0.has_timeout() {
            Some(self.0.get_timeout())
        } else {
            None
        }
    }

    /// set the "timeout" property
    pub fn set_timeout(&mut self, timeout: u32) {
        self.0.set_timeout(timeout);
    }

    /// get the "sloppy_quorum" property
    pub fn get_sloppy_quorum(&self) -> Option<bool> {
        if self.0.has_sloppy_quorum() {
            Some(self.0.get_sloppy_quorum())
        } else {
            None
        }
    }

    /// set the "sloppy_quorum" property
    pub fn set_sloppy_quorum(&mut self, sloppy_quorum: bool) {
        self.0.set_sloppy_quorum(sloppy_quorum);
    }

    /// get the "n_val" property
    pub fn get_n_val(&self) -> Option<u32> {
        if self.0.has_n_val() {
            Some(self.0.get_n_val())
        } else {
            None
        }
    }

    /// set the "n_val" property
    pub fn set_n_val(&mut self, n_val: u32) {
        self.0.set_n_val(n_val);
    }

    /// get the "include_context" property
    pub fn get_include_context(&self) -> Option<bool> {
        if self.0.has_include_context() {
            Some(self.0.get_include_context())
        } else {
            None
        }
    }

    /// set the "include_context" property
    pub fn set_include_context(&mut self, include_context: bool) {
        self.0.set_include_context(include_context);
    }
}

impl ProtobufBytes for DataTypeFetchReq {
    fn write_to_bytes(self) -> Result<Vec<u8>, RiakErr> {
        match self.0.write_to_bytes() {
            Ok(bytes) => Ok(bytes),
            Err(e) => Err(RiakErr::ProtobufError(e)),
        }
    }
}

/// represents the valid data types
#[derive(Clone, Debug)]
pub enum DataType {
    COUNTER,
    SET,
    MAP,
    HLL,
}

/// represents the response from a `DTFetchReq`
#[derive(Clone, Debug)]
pub struct DataTypeFetchResp {
    pub context: Option<Vec<u8>>,
    pub data_type: DataType,
    pub value: Option<DataTypeValue>,
}

impl DataTypeFetchRespPrivate for DataTypeFetchResp {
    fn new_from_rpb(mut dt_fetch_resp: DtFetchResp) -> DataTypeFetchResp {
        // get the context
        let mut context: Option<Vec<u8>> = None;
        if dt_fetch_resp.has_context() {
            context = Some(dt_fetch_resp.take_context());
        }

        // get the data_type
        let data_type = match dt_fetch_resp.get_field_type() {
            DtFetchResp_DataType::COUNTER => DataType::COUNTER,
            DtFetchResp_DataType::SET => DataType::SET,
            DtFetchResp_DataType::MAP => DataType::MAP,
            DtFetchResp_DataType::HLL => DataType::HLL,
        };

        // get the value
        let mut value: Option<DataTypeValue> = None;
        if dt_fetch_resp.has_value() {
            value = Some(DataTypeValue::new_from_rpb(dt_fetch_resp.take_value()));
        }

        DataTypeFetchResp {
            context: context,
            data_type: data_type,
            value: value,
        }
    }
}

/// represents a map entry
#[derive(Clone, Debug)]
pub struct DataTypeMapEntry {
    pub field: DataTypeMapField,
    pub counter_value: Option<i64>,
    pub set_value: Vec<Vec<u8>>,
    pub register_value: Option<Vec<u8>>,
    pub flag_value: Option<bool>,
    pub map_value: Vec<DataTypeMapEntry>,
}

impl DataTypeMapEntryPrivate for DataTypeMapEntry {
    fn new_from_rpb(mut rpb_map_entry: MapEntry) -> DataTypeMapEntry {
        // get the field
        let field = DataTypeMapField::new_from_rpb(rpb_map_entry.take_field());

        // get the counter_value
        let mut counter_value: Option<i64> = None;
        if rpb_map_entry.has_counter_value() {
            counter_value = Some(rpb_map_entry.get_counter_value());
        }

        // get the set_value
        let mut set_value: Vec<Vec<u8>> = Vec::new();
        for next_set_value in rpb_map_entry.take_set_value().into_iter() {
            set_value.push(next_set_value);
        }

        // get the register_value
        let mut register_value: Option<Vec<u8>> = None;
        if rpb_map_entry.has_register_value() {
            register_value = Some(rpb_map_entry.take_register_value());
        }

        // get the flag_value
        let mut flag_value: Option<bool> = None;
        if rpb_map_entry.has_flag_value() {
            flag_value = Some(rpb_map_entry.get_flag_value());
        }

        // get the map_value
        let mut map_value = Vec::new();
        for next_rpb_map_entry in rpb_map_entry.take_map_value().into_iter() {
            map_value.push(DataTypeMapEntry::new_from_rpb(next_rpb_map_entry));
        }

        DataTypeMapEntry {
            field: field,
            counter_value: counter_value,
            set_value: set_value,
            register_value: register_value,
            flag_value: flag_value,
            map_value: map_value,
        }
    }
}

/// represents the map field
#[derive(Clone, Debug)]
pub struct DataTypeMapField {
    pub name: Vec<u8>,
    pub field_type: DataTypeMapFieldType,
}

impl DataTypeMapFieldPrivate for DataTypeMapField {
    fn new_from_rpb(mut rpb_map_field: MapField) -> DataTypeMapField {
        let field_type = match rpb_map_field.get_field_type() {
            MapField_MapFieldType::COUNTER => DataTypeMapFieldType::COUNTER,
            MapField_MapFieldType::SET => DataTypeMapFieldType::SET,
            MapField_MapFieldType::REGISTER => DataTypeMapFieldType::REGISTER,
            MapField_MapFieldType::FLAG => DataTypeMapFieldType::FLAG,
            MapField_MapFieldType::MAP => DataTypeMapFieldType::MAP,
        };

        DataTypeMapField {
            name: rpb_map_field.take_name(),
            field_type: field_type,
        }
    }
}

/// represents the types valid for a map field
#[derive(Clone, Debug)]
pub enum DataTypeMapFieldType {
    COUNTER,
    SET,
    REGISTER,
    FLAG,
    MAP,
}

/// represents the value of a data-type
#[derive(Clone, Debug)]
pub struct DataTypeValue {
    pub counter_value: Option<i64>,
    pub set_value: Vec<Vec<u8>>,
    pub map_value: Vec<DataTypeMapEntry>,
}

impl DataTypeValuePrivate for DataTypeValue {
    fn new_from_rpb(mut dt_value: DtValue) -> DataTypeValue {
        // get the map_value if present
        let mut map_value: Vec<DataTypeMapEntry> = Vec::new();
        for rpb_map_entry in dt_value.take_map_value().into_iter() {
            let map_entry = DataTypeMapEntry::new_from_rpb(rpb_map_entry);
            map_value.push(map_entry);
        }

        // get the counter_value if present
        let mut counter_value: Option<i64> = None;
        if dt_value.has_counter_value() {
            counter_value = Some(dt_value.get_counter_value());
        }

        // get the set_value
        let set_value = dt_value.take_set_value();

        DataTypeValue {
            counter_value: counter_value,
            set_value: set_value.to_vec(),
            map_value: map_value,
        }
    }
}