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
/// Bucket related structs for dealing with Riak buckets.
///
/// For more information: https://docs.basho.com/riak/kv/latest/learn/concepts/buckets/

use errors::RiakError;
use protobuf::Message;
use rpb::riak::RpbSetBucketReq;
use rpb::utils::{bucket_props_to_rpb_bucket_props, RpbGeneratorID};

/// `BucketProps` represents the properties that can be PUT or SET on a Riak bucket.
///
/// # Examples
///
/// ```
/// ```
#[derive(Clone, Debug, Default)]
pub struct BucketProps {
    allow_mult: Option<bool>,
    backend: Option<String>,
    basic_quorum: Option<bool>,
    big_vclock: Option<u32>,
    chash_keyfun: Option<ModFun>,
    consistent: Option<bool>,
    datatype: Option<String>,
    dw: Option<u32>,
    has_postcommit: Option<bool>,
    has_precommit: Option<bool>,
    hll_precision: Option<u32>,
    last_write_wins: Option<bool>,
    linkfun: Option<ModFun>,
    notfound_ok: Option<bool>,
    n_val: Option<u32>,
    old_vclock: Option<u32>,
    postcommit: Option<Vec<CommitHook>>,
    precommit: Option<Vec<CommitHook>>,
    pr: Option<u32>,
    pw: Option<u32>,
    // pub repl: Option<RpbBucketProps_RpbReplMode>, TODO
    r: Option<u32>,
    rw: Option<u32>,
    search_index: Option<String>,
    search: Option<bool>,
    small_vclock: Option<u32>,
    write_once: Option<bool>,
    w: Option<u32>,
    young_vclock: Option<u32>,
}

impl BucketProps {
    pub fn new() -> BucketProps {
        Default::default()
    }

    pub fn get_allow_mult(&self) -> Option<bool> {
        self.allow_mult
    }

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

    pub fn get_backend(&self) -> Option<String> {
        self.backend.clone()
    }

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

    pub fn get_basic_quorum(&self) -> Option<bool> {
        self.basic_quorum
    }

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

    pub fn get_big_vclock(&self) -> Option<u32> {
        self.big_vclock
    }

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

    pub fn get_chash_keyfun(&self) -> Option<ModFun> {
        self.chash_keyfun.clone()
    }

    pub fn set_chash_keyfun(&mut self, modfun: &ModFun) {
        self.chash_keyfun = Some(modfun.clone());
    }

    pub fn get_consistent(&self) -> Option<bool> {
        self.consistent
    }

    pub fn set_consistent(&mut self, consistent: bool) {
        self.consistent = Some(consistent)
    }

    pub fn get_datatype(&self) -> Option<String> {
        self.datatype.clone()
    }

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

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

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

    pub fn get_has_postcommit(&self) -> Option<bool> {
        self.has_postcommit
    }

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

    pub fn get_has_precommit(&self) -> Option<bool> {
        self.has_precommit
    }

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

    pub fn get_hll_precision(&self) -> Option<u32> {
        self.hll_precision
    }

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

    pub fn get_last_write_wins(&self) -> Option<bool> {
        self.last_write_wins
    }

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

    pub fn get_linkfun(&self) -> Option<ModFun> {
        self.linkfun.clone()
    }

    pub fn set_linkfun(&mut self, modfun: &ModFun) {
        self.linkfun = Some(modfun.clone());
    }

    pub fn get_notfound_ok(&self) -> Option<bool> {
        self.notfound_ok
    }

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

    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_old_vclock(&self) -> Option<u32> {
        self.old_vclock
    }

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

    pub fn get_postcommit(&self) -> Option<Vec<CommitHook>> {
        self.postcommit.clone()
    }

    pub fn set_postcommit(&mut self, commithook: &Vec<CommitHook>) {
        self.postcommit = Some(commithook.clone());
    }

    pub fn get_precommit(&self) -> Option<Vec<CommitHook>> {
        self.precommit.clone()
    }

    pub fn set_precommit(&mut self, commithook: &Vec<CommitHook>) {
        self.precommit = Some(commithook.clone());
    }

    pub fn get_pr(&self) -> Option<u32> {
        self.pr
    }

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

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

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

    pub fn get_r(&self) -> Option<u32> {
        self.r
    }

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

    pub fn get_rw(&self) -> Option<u32> {
        self.rw
    }

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

    pub fn get_search_index(&self) -> Option<String> {
        self.search_index.clone()
    }

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

    pub fn get_search(&self) -> Option<bool> {
        self.search
    }

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

    pub fn get_small_vclock(&self) -> Option<u32> {
        self.small_vclock
    }

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

    pub fn get_write_once(&self) -> Option<bool> {
        self.write_once
    }

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

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

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

    pub fn get_young_vclock(&self) -> Option<u32> {
        self.young_vclock
    }

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

impl RpbGeneratorID for BucketProps {
    fn generate_rpb(&self, bucket: &str) -> Result<Vec<u8>, RiakError> {
        let mut req = RpbSetBucketReq::new();
        req.set_bucket(bucket.to_string().into_bytes());

        let props = bucket_props_to_rpb_bucket_props(self);
        req.set_props(props);

        match req.write_to_bytes() {
            Ok(b) => Ok(b),
            Err(err) => Err(RiakError::ProtobufError(err)),
        }
    }
}

/// `ModFun` represents an Erlang module and function and is used for `BucketProps`.
///
/// This is specifically useful for "linkfun", "chash_keyfun", and as the internal to a
/// `CommitHook`. `ModFun` will be stored as configuration to execute the given Erlang function
/// under special conditions on the server.
///
/// For more information see: https://docs.basho.com/riak/kv/latest/learn/concepts/buckets/
///
/// # Examples
///
/// ```
/// ```
#[derive(Clone, Debug, Default)]
pub struct ModFun {
    pub module: String,
    pub function: String,
}

impl ModFun {
    pub fn new(module: &str, function: &str) -> ModFun {
        ModFun {
            module: module.to_string(),
            function: function.to_string(),
        }
    }
}

/// `CommitHook` represents a commit hook that will be set on a Riak bucket via `BucketProps`.
///
/// This is used specifically for "precommit" and "postcommit", properties on a bucket that
/// represent functions that should run before and after an object is stored in Riak.
///
/// For more information see: https://docs.basho.com/riak/kv/2.1.4/developing/usage/commit-hooks/
///
/// # Examples
///
/// ```
/// ```
#[derive(Clone, Debug, Default)]
pub struct CommitHook {
    pub name: String,
    pub modfun: ModFun,
}

impl CommitHook {
    pub fn new(name: &str, modfun: &ModFun) -> CommitHook {
        CommitHook {
            name: name.to_string(),
            modfun: modfun.clone(),
        }
    }
}