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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
/// 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 private_traits::DeleteObjectReqPrivate;
use protobuf::Message;
use rpb::riak_kv::RpbDelReq;
use rpb::utils::{fetch_object_req_to_rpb_get_req, store_object_req_to_rpb_put_req, RpbGenerator};

/// `DeleteObjectReq` represents a request to delete an object from Riak
#[derive(Clone, Debug)]
pub struct DeleteObjectReq(RpbDelReq);

impl DeleteObjectReq {
    /// constructs a new `DeleteObjectReq`
    pub fn new(bucket: &str, key: &str) -> DeleteObjectReq {
        let mut req = DeleteObjectReq(RpbDelReq::new());
        req.0.set_bucket(bucket.to_string().into_bytes());
        req.0.set_key(key.to_string().into_bytes());
        req
    }

    /// get the value of the "bucket_type" property
    pub fn get_bucket_type(&self) -> Option<String> {
        if self.0.has_field_type() {
            let bucket_type = self.0.get_field_type().clone();
            let bucket_type_string = String::from_utf8_lossy(bucket_type).into_owned();
            Some(bucket_type_string)
        } else {
            None
        }
    }

    /// set the value of the "bucket_type" property
    pub fn set_bucket_type(&mut self, bucket_type: &str) {
        self.0.set_field_type(bucket_type.to_string().into_bytes());
    }

    /// get the value of the "bucket" property
    pub fn get_bucket(&self) -> String {
        let bucket = self.0.get_bucket().clone();
        String::from_utf8_lossy(bucket).into_owned()
    }

    /// set the value of the "bucket" property
    pub fn set_bucket(&mut self, bucket: &str) {
        self.0.set_bucket(bucket.to_string().into_bytes());
    }

    /// get the value of the "key" property
    pub fn get_key(&self) -> String {
        let key = self.0.get_key().clone();
        String::from_utf8_lossy(key).into_owned()
    }

    /// set the value of the "key" property
    pub fn set_key(&mut self, key: &str) {
        self.0.set_key(key.to_string().into_bytes());
    }

    /// get the value of the "dw" property
    pub fn get_dw(&self) -> Option<u32> {
        if self.0.has_dw() {
            Some(self.0.get_dw())
        } else {
            None
        }
    }

    /// set the value of the "dw" property
    pub fn set_dw(&mut self, dw: u32) {
        self.0.set_dw(dw);
    }

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

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

    /// get the value of the "pw" property
    pub fn get_pw(&self) -> Option<u32> {
        if self.0.has_pw() {
            Some(self.0.get_pw())
        } else {
            None
        }
    }

    /// set the value of the "pw" property
    pub fn set_pw(&mut self, pw: u32) {
        self.0.set_pw(pw);
    }

    /// get the value of the "rw" property
    pub fn get_rw(&self) -> Option<u32> {
        if self.0.has_rw() {
            Some(self.0.get_rw())
        } else {
            None
        }
    }

    /// set the value of the "rw" property
    pub fn set_rw(&mut self, rw: u32) {
        self.0.set_rw(rw);
    }

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

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

    /// get the value of the "w" property
    pub fn get_w(&self) -> Option<u32> {
        if self.0.has_w() {
            Some(self.0.get_w())
        } else {
            None
        }
    }

    /// set the value of the "w" property
    pub fn set_w(&mut self, w: u32) {
        self.0.set_w(w);
    }

    /// get the value of 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 value of the "n_val" property
    pub fn set_n_val(&mut self, n_val: u32) {
        self.0.set_n_val(n_val);
    }

    /// get the value of 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 value of the "sloppy_quorum" property
    pub fn set_sloppy_quorum(&mut self, sloppy_quorum: bool) {
        self.0.set_sloppy_quorum(sloppy_quorum);
    }

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

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

    /// get the value of the "vclock" property
    pub fn get_vclock(&self) -> Option<Vec<u8>> {
        if self.0.has_vclock() {
            let vclock = self.0.get_vclock().to_vec();
            Some(vclock)
        } else {
            None
        }
    }
}

impl DeleteObjectReqPrivate for DeleteObjectReq {
    fn write_to_bytes(&self) -> Result<Vec<u8>, RiakError> {
        match self.0.write_to_bytes() {
            Ok(bytes) => Ok(bytes),
            Err(error) => Err(RiakError::ProtobufError(error)),
        }
    }
}

/// The data used to perform an store object request.
///
/// # Examples
///
/// ```
/// ```
#[derive(Clone, Debug)]
pub struct StoreObjectReq {
    // Required
    bucket: String,
    content: ObjectContent,
    // optional
    asis: Option<bool>,
    bucket_type: Option<String>,
    if_none_match: Option<bool>,
    if_not_modified: Option<bool>,
    key: Option<String>,
    n_val: Option<u32>,
    return_body: Option<bool>,
    return_head: Option<bool>,
    sloppy_quorum: Option<bool>,
    timeout: Option<u32>,
    vclock: Option<Vec<u8>>,
    // optional w
    dw: Option<u32>,
    pw: Option<u32>,
    w: Option<u32>,
}

impl StoreObjectReq {
    pub fn new(bucket: &str, content: &ObjectContent) -> StoreObjectReq {
        StoreObjectReq {
            // required
            bucket: bucket.to_string(),
            content: content.clone(),
            // optional
            asis: None,
            bucket_type: None,
            if_none_match: None,
            if_not_modified: None,
            key: None,
            n_val: None,
            return_body: None,
            return_head: None,
            sloppy_quorum: None,
            timeout: None,
            vclock: None,
            // optional w
            dw: None,
            pw: None,
            w: 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 write_to_bytes(&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 write_to_bytes(&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);
    }
}