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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
/// Object related structs and functions for Riak objects.
///
/// For more information: https://docs.basho.com/riak/kv/latest/developing/usage/creating-objects/

use errors::RiakError;
use protobuf::Message;
use rpb::utils::{fetch_object_req_to_rpb_get_req, store_object_req_to_rpb_put_req, RpbGenerator};

/// The data used to perform an store object request.
///
/// # Examples
///
/// ```
/// ```
#[derive(Clone, Debug)]
pub struct StoreObjectReq {
    // Required
    bucket: String,
    content: ObjectContent,

    // Optional
    key: Option<String>,
    vclock: Option<Vec<u8>>,
    w: Option<u32>,
    dw: Option<u32>,
    return_body: Option<bool>,
    pw: Option<u32>,
    if_not_modified: Option<bool>,
    if_none_match: Option<bool>,
    return_head: Option<bool>,
    timeout: Option<u32>,
    asis: Option<bool>,
    sloppy_quorum: Option<bool>,
    n_val: Option<u32>,
    bucket_type: Option<String>,
}

impl StoreObjectReq {
    pub fn new(bucket: &str, content: &ObjectContent) -> StoreObjectReq {
        StoreObjectReq {
            bucket: bucket.to_string(),
            content: content.clone(),
            key: None,
            vclock: None,
            w: None,
            dw: None,
            return_body: None,
            pw: None,
            if_not_modified: None,
            if_none_match: None,
            return_head: None,
            timeout: None,
            asis: None,
            sloppy_quorum: None,
            n_val: None,
            bucket_type: None,
        }
    }

    pub fn get_bucket(&self) -> String {
        self.bucket.clone()
    }

    pub fn set_bucket(&mut self, bucket: &str) {
        self.bucket = bucket.to_string();
    }

    pub fn get_content(&self) -> ObjectContent {
        self.content.clone()
    }

    pub fn set_content(&mut self, content: ObjectContent) {
        self.content = content;
    }

    pub fn get_key(&self) -> Option<String> {
        self.key.clone()
    }

    pub fn set_key(&mut self, key: &str) {
        self.key = Some(key.to_string());
    }

    pub fn get_vclock(&self) -> Option<Vec<u8>> {
        self.vclock.clone()
    }

    pub fn set_vclock(&mut self, vclock: &[u8]) {
        self.vclock = Some(vclock.to_vec());
    }

    pub fn get_w(&self) -> Option<u32> {
        self.w
    }

    pub fn set_w(&mut self, w: u32) {
        self.w = Some(w);
    }

    pub fn get_dw(&self) -> Option<u32> {
        self.dw
    }

    pub fn set_dw(&mut self, dw: u32) {
        self.dw = Some(dw);
    }

    pub fn get_return_body(&self) -> Option<bool> {
        self.return_body
    }

    pub fn set_return_body(&mut self, return_body: bool) {
        self.return_body = Some(return_body);
    }

    pub fn get_pw(&self) -> Option<u32> {
        self.pw
    }

    pub fn set_pw(&mut self, pw: u32) {
        self.pw = Some(pw);
    }

    pub fn get_if_not_modified(&self) -> Option<bool> {
        self.if_not_modified
    }

    pub fn set_if_not_modified(&mut self, if_not_modified: bool) {
        self.if_not_modified = Some(if_not_modified);
    }

    pub fn get_if_none_match(&self) -> Option<bool> {
        self.if_none_match
    }

    pub fn set_if_none_match(&mut self, if_none_match: bool) {
        self.if_none_match = Some(if_none_match);
    }

    pub fn get_return_head(&self) -> Option<bool> {
        self.return_head
    }

    pub fn set_return_head(&mut self, return_head: bool) {
        self.return_head = Some(return_head);
    }

    pub fn get_timeout(&self) -> Option<u32> {
        self.timeout
    }

    pub fn set_timeout(&mut self, timeout: u32) {
        self.timeout = Some(timeout);
    }

    pub fn get_asis(&self) -> Option<bool> {
        self.asis
    }

    pub fn set_asis(&mut self, asis: bool) {
        self.asis = Some(asis);
    }

    pub fn get_sloppy_quorum(&self) -> Option<bool> {
        self.sloppy_quorum
    }

    pub fn set_sloppy_quorum(&mut self, sloppy_quorum: bool) {
        self.sloppy_quorum = Some(sloppy_quorum);
    }

    pub fn get_n_val(&self) -> Option<u32> {
        self.n_val
    }

    pub fn set_n_val(&mut self, n_val: u32) {
        self.n_val = Some(n_val);
    }

    pub fn get_bucket_type(&self) -> Option<String> {
        self.bucket_type.clone()
    }

    pub fn set_bucket_type(&mut self, bucket_type: &str) {
        self.bucket_type = Some(bucket_type.to_string());
    }
}

impl RpbGenerator for StoreObjectReq {
    fn generate_rpb(&self) -> Result<Vec<u8>, RiakError> {
        let req = store_object_req_to_rpb_put_req(&self);
        match req.write_to_bytes() {
            Ok(b) => Ok(b),
            Err(err) => Err(RiakError::ProtobufError(err)),
        }
    }
}

/// The data used to perform a fetch object request
///
/// # Examples
///
/// ```
/// ```
#[derive(Clone, Debug)]
pub struct FetchObjectReq {
    // Required
    bucket: String,
    key: String,

    // Optional
    r: Option<u32>,
    pr: Option<u32>,
    basic_quorum: Option<bool>,
    notfound_ok: Option<bool>,
    if_modified: Option<bool>,
    head: Option<bool>,
    deletedvclock: Option<bool>,
    timeout: Option<u32>,
    sloppy_quorum: Option<bool>,
    n_val: Option<u32>,
    bucket_type: Option<String>,
}

impl FetchObjectReq {
    pub fn new(bucket: &str, key: &str) -> FetchObjectReq {
        FetchObjectReq {
            bucket: bucket.to_string(),
            key: key.to_string(),
            r: None,
            pr: None,
            basic_quorum: None,
            notfound_ok: None,
            if_modified: None,
            head: None,
            deletedvclock: None,
            timeout: None,
            sloppy_quorum: None,
            n_val: None,
            bucket_type: None,
        }
    }

    pub fn get_bucket(&self) -> String {
        self.bucket.clone()
    }

    pub fn set_bucket(&mut self, bucket: &str) {
        self.bucket = bucket.to_string();
    }

    pub fn get_key(&self) -> String {
        self.key.clone()
    }

    pub fn set_key(&mut self, key: &str) {
        self.key = key.to_string();
    }
}

impl RpbGenerator for FetchObjectReq {
    fn generate_rpb(&self) -> Result<Vec<u8>, RiakError> {
        let req = fetch_object_req_to_rpb_get_req(self);
        match req.write_to_bytes() {
            Ok(b) => Ok(b),
            Err(err) => Err(RiakError::ProtobufError(err)),
        }
    }
}

/// Represents the response received from Riak, including all siblings of the object,
/// the vector clock, and "unchanged" if "if_modified" was set to true in the request.
#[derive(Clone, Debug)]
pub struct FetchObjectResp {
    content: Vec<ObjectContent>,
    vclock: Vec<u8>,
    unchanged: Option<bool>,
}

impl FetchObjectResp {
    pub fn new(content: &Vec<ObjectContent>, vclock: &[u8]) -> FetchObjectResp {
        FetchObjectResp {
            content: content.clone(),
            vclock: vclock.to_vec(),
            unchanged: None,
        }
    }

    pub fn get_content(&self) -> Vec<ObjectContent> {
        self.content.clone()
    }

    pub fn set_content(&mut self, content: Vec<ObjectContent>) {
        self.content = content;
    }

    pub fn get_vclock(&self) -> Vec<u8> {
        self.vclock.clone()
    }

    pub fn set_vclock(&mut self, vclock: Vec<u8>) {
        self.vclock = vclock;
    }

    pub fn get_unchanged(&self) -> Option<bool> {
        self.unchanged
    }

    pub fn set_unchanged(&mut self, unchanged: bool) {
        self.unchanged = Some(unchanged);
    }
}

/// Represents the actual value and metadata for the value being stored in Riak.
///
/// # Examples
///
/// ```
/// ```
#[derive(Clone, Debug)]
pub struct ObjectContent {
    // Required
    value: Vec<u8>,

    // Optional
    content_type: Option<String>,
    charset: Option<String>,
    content_encoding: Option<String>,
    vtag: Option<Vec<u8>>,
    // links: Vec<RpbLink>,
    last_mod: Option<u32>,
    last_mod_usecs: Option<u32>,
    // repeated RpbPair usermeta = 9;
    // repeated RpbPair indexes = 10;
    deleted: Option<bool>,
}

impl ObjectContent {
    pub fn new(value: &[u8]) -> ObjectContent {
        ObjectContent {
            value: value.to_vec(),
            content_type: None,
            charset: None,
            content_encoding: None,
            vtag: None,
            last_mod: None,
            last_mod_usecs: None,
            deleted: None,
        }
    }

    pub fn get_value(&self) -> Vec<u8> {
        self.value.clone()
    }

    pub fn set_value(&mut self, value: Vec<u8>) {
        self.value = value;
    }

    pub fn get_content_type(&self) -> Option<String> {
        self.content_type.clone()
    }

    pub fn set_content_type(&mut self, content_type: &str) {
        self.content_type = Some(content_type.to_string());
    }

    pub fn get_charset(&self) -> Option<String> {
        self.charset.clone()
    }

    pub fn set_charset(&mut self, charset: &str) {
        self.charset = Some(charset.to_string());
    }

    pub fn get_content_encoding(&self) -> Option<String> {
        self.content_encoding.clone()
    }

    pub fn set_content_encoding(&mut self, content_encoding: &str) {
        self.content_encoding = Some(content_encoding.to_string());
    }

    pub fn get_vtag(&self) -> Option<Vec<u8>> {
        self.vtag.clone()
    }

    pub fn set_vtag(&mut self, vtag: &Vec<u8>) {
        self.vtag = Some(vtag.clone());
    }

    pub fn get_last_mod(&self) -> Option<u32> {
        self.last_mod
    }

    pub fn set_last_mod(&mut self, last_mod: u32) {
        self.last_mod = Some(last_mod);
    }

    pub fn get_last_mod_usecs(&self) -> Option<u32> {
        self.last_mod_usecs
    }

    pub fn set_last_mod_usecs(&mut self, last_mod_usecs: u32) {
        self.last_mod_usecs = Some(last_mod_usecs);
    }

    pub fn get_deleted(&self) -> Option<bool> {
        self.deleted
    }

    pub fn set_deleted(&mut self, deleted: bool) {
        self.deleted = Some(deleted);
    }
}