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
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
// Copyright 2015-2017 Aerospike, Inc.
//
// Portions may be licensed to Aerospike, Inc. under one or more contributor
// license agreements.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
// use this file except in compliance with the License. You may obtain a copy of
// the License at http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations under
// the License.

//! Unique key map bin operations. Create map operations used by the client's `operate()` method.
//!
//! All maps maintain an index and a rank. The index is the item offset from the start of the map,
//! for both unordered and ordered maps. The rank is the sorted index of the value component.
//! Map supports negative indexing for indexjkj and rank.
//!
//! The default unique key map is unordered.
//!
//! Index/Count examples:
//!
//! * Index 0: First item in map.
//! * Index 4: Fifth item in map.
//! * Index -1: Last item in map.
//! * Index -3: Third to last item in map.
//! * Index 1, Count 2: Second and third items in map.
//! * Index -3, Count 3: Last three items in map.
//! * Index -5, Count 4: Range between fifth to last item to second to last item inclusive.
//!
//! Rank examples:
//!
//! * Rank 0: Item with lowest value rank in map.
//! * Rank 4: Fifth lowest ranked item in map.
//! * Rank -1: Item with highest ranked value in map.
//! * Rank -3: Item with third highest ranked value in map.
//! * Rank 1 Count 2: Second and third lowest ranked items in map.
//! * Rank -3 Count 3: Top three ranked items in map.

use std::collections::HashMap;

use Value;
use operations::{Operation, OperationType, OperationBin, OperationData};
use operations::cdt::{CdtOperation, CdtOpType, CdtArgument};

/// Map storage order.
#[derive(Debug, Clone, Copy)]
pub enum MapOrder {
    /// Map is not ordered. This is the default.
    Unordered = 0,

    /// Order map by key.
    KeyOrdered = 1,

    /// Order map by key, then value.
    KeyValueOrdered = 3,
}


/// Map return type. Type of data to return when selecting or removing items from the map.
#[derive(Debug, Clone)]
pub enum MapReturnType {
    /// Do not return a result.
    None = 0,

    /// Return key index order.
    ///
    /// * 0 = first key
    /// * N = Nth key
    /// * -1 = last key
    Index = 1,

    /// Return reverse key order.
    ///
    /// * 0 = last key
    /// * -1 = first key
    ReverseIndex = 2,

    /// Return value order.
    ///
    /// * 0 = smallest value
    /// * N = Nth smallest value
    /// * -1 = largest value
    Rank = 3,

    /// Return reserve value order.
    ///
    /// * 0 = largest value
    /// * N = Nth largest value
    /// * -1 = smallest value
    ReverseRank = 4,

    /// Return count of items selected.
    Count = 5,

    /// Return key for single key read and key list for range read.
    Key = 6,

    /// Return value for single key read and value list for range read.
    Value = 7,

    /// Return key/value items. The possible return types are:
    ///
    /// * `Value::HashMap`: Returned for unordered maps
    /// * `Value::OrderedMap`: Returned for range results where range order needs to be preserved.
    KeyValue = 8,
}

/// Unique key map write type.
#[derive(Debug, Clone)]
pub enum MapWriteMode {
    /// If the key already exists, the item will be overwritten.
    /// If the key does not exist, a new item will be created.
    Update,

    /// If the key already exists, the item will be overwritten.
    /// If the key does not exist, the write will fail.
    UpdateOnly,

    /// If the key already exists, the write will fail.
    /// If the key does not exist, a new item will be created.
    CreateOnly,
}

/// `MapPolicy` directives when creating a map and writing map items.
#[derive(Debug)]
pub struct MapPolicy {
    order: MapOrder,
    write_mode: MapWriteMode,
}

impl MapPolicy {
    /// Create a new map policy given the ordering for the map and the write mode.
    pub fn new(order: MapOrder, write_mode: MapWriteMode) -> Self {
        MapPolicy {
            order: order,
            write_mode: write_mode,
        }
    }
}

impl Default for MapPolicy {
    fn default() -> Self {
        MapPolicy::new(MapOrder::Unordered, MapWriteMode::Update)
    }
}

fn map_write_op(policy: &MapPolicy, multi: bool) -> CdtOpType {
    match policy.write_mode {
        MapWriteMode::Update => {
            if multi {
                CdtOpType::MapPutItems
            } else {
                CdtOpType::MapPut
            }
        }
        MapWriteMode::UpdateOnly => {
            if multi {
                CdtOpType::MapReplaceItems
            } else {
                CdtOpType::MapReplace
            }
        }
        MapWriteMode::CreateOnly => {
            if multi {
                CdtOpType::MapAddItems
            } else {
                CdtOpType::MapAdd
            }
        }
    }
}

fn map_order_arg(policy: &MapPolicy) -> Option<CdtArgument> {
    match policy.write_mode {
        MapWriteMode::UpdateOnly => None,
        _ => Some(CdtArgument::Byte(policy.order as u8)),
    }
}

/// Create set map policy operation. Server set the map policy attributes. Server does not
/// return a result.
///
/// The required map policy attributes can be changed after the map has been created.
pub fn set_order(bin: &str, map_order: MapOrder) -> Operation {
    let cdt_op = CdtOperation {
        op: CdtOpType::MapSetType,
        args: vec![CdtArgument::Byte(map_order as u8)],
    };
    Operation {
        op: OperationType::CdtWrite,
        bin: OperationBin::Name(bin),
        data: OperationData::CdtMapOp(cdt_op),
    }
}

/// Create map put operation. Server writes the key/value item to the map bin and returns the
/// map size.
///
/// The required map policy dictates the type of map to create when it does not exist. The map
/// policy also specifies the mode used when writing items to the map.
pub fn put_item<'a>(policy: &'a MapPolicy,
                    bin: &'a str,
                    key: &'a Value,
                    val: &'a Value)
                    -> Operation<'a> {
    let mut args = vec![CdtArgument::Value(key)];
    if !val.is_nil() {
        args.push(CdtArgument::Value(val));
    }
    if let Some(arg) = map_order_arg(policy) {
        args.push(arg);
    }
    let cdt_op = CdtOperation {
        op: map_write_op(policy, false),
        args: args,
    };
    Operation {
        op: OperationType::CdtWrite,
        bin: OperationBin::Name(bin),
        data: OperationData::CdtMapOp(cdt_op),
    }
}

/// Create map put items operation. Server writes each map item to the map bin and returns the
/// map size.
///
/// The required map policy dictates the type of map to create when it does not exist. The map
/// policy also specifies the mode used when writing items to the map.
pub fn put_items<'a>(policy: &'a MapPolicy,
                     bin: &'a str,
                     items: &'a HashMap<Value, Value>)
                     -> Operation<'a> {
    let mut args = vec![CdtArgument::Map(items)];
    if let Some(arg) = map_order_arg(policy) {
        args.push(arg);
    }
    let cdt_op = CdtOperation {
        op: map_write_op(policy, true),
        args: args,
    };
    Operation {
        op: OperationType::CdtWrite,
        bin: OperationBin::Name(bin),
        data: OperationData::CdtMapOp(cdt_op),
    }
}

/// Create map increment operation. Server increments values by `incr` for all items identified
/// by the key and returns the final result. Valid only for numbers.
///
/// The required map policy dictates the type of map to create when it does not exist. The map
/// policy also specifies the mode used when writing items to the map.
pub fn increment_value<'a>(policy: &'a MapPolicy,
                           bin: &'a str,
                           key: &'a Value,
                           incr: &'a Value)
                           -> Operation<'a> {
    let mut args = vec![CdtArgument::Value(key)];
    if !incr.is_nil() {
        args.push(CdtArgument::Value(incr));
    }
    if let Some(arg) = map_order_arg(policy) {
        args.push(arg);
    }
    let cdt_op = CdtOperation {
        op: CdtOpType::MapIncrement,
        args: args,
    };
    Operation {
        op: OperationType::CdtWrite,
        bin: OperationBin::Name(bin),
        data: OperationData::CdtMapOp(cdt_op),
    }
}

/// Create map decrement operation. Server decrements values by `decr` for all items identified
/// by the key and returns the final result. Valid only for numbers.
///
/// The required map policy dictates the type of map to create when it does not exist. The map
/// policy also specifies the mode used when writing items to the map.
pub fn decrement_value<'a>(policy: &'a MapPolicy,
                           bin: &'a str,
                           key: &'a Value,
                           decr: &'a Value)
                           -> Operation<'a> {
    let mut args = vec![CdtArgument::Value(key)];
    if !decr.is_nil() {
        args.push(CdtArgument::Value(decr));
    }
    if let Some(arg) = map_order_arg(policy) {
        args.push(arg);
    }
    let cdt_op = CdtOperation {
        op: CdtOpType::MapDecrement,
        args: args,
    };
    Operation {
        op: OperationType::CdtWrite,
        bin: OperationBin::Name(bin),
        data: OperationData::CdtMapOp(cdt_op),
    }
}

/// Create map clear operation. Server removes all items in the map. Server does not return a
/// result.
pub fn clear(bin: &str) -> Operation {
    let cdt_op = CdtOperation {
        op: CdtOpType::MapClear,
        args: vec![],
    };
    Operation {
        op: OperationType::CdtWrite,
        bin: OperationBin::Name(bin),
        data: OperationData::CdtMapOp(cdt_op),
    }
}

/// Create map remove operation. Server removes the map item identified by the key and returns
/// the removed data specified by `return_type`.
pub fn remove_by_key<'a>(bin: &'a str,
                         key: &'a Value,
                         return_type: MapReturnType)
                         -> Operation<'a> {
    let cdt_op = CdtOperation {
        op: CdtOpType::MapRemoveByKey,
        args: vec![CdtArgument::Byte(return_type as u8), CdtArgument::Value(key)],
    };
    Operation {
        op: OperationType::CdtWrite,
        bin: OperationBin::Name(bin),
        data: OperationData::CdtMapOp(cdt_op),
    }
}

/// Create map remove operation. Server removes map items identified by keys and returns
/// removed data specified by `return_type`.
pub fn remove_by_key_list<'a>(bin: &'a str,
                              keys: &'a [Value],
                              return_type: MapReturnType)
                              -> Operation<'a> {
    let cdt_op = CdtOperation {
        op: CdtOpType::MapRemoveByKeyList,
        args: vec![CdtArgument::Byte(return_type as u8), CdtArgument::List(keys)],
    };
    Operation {
        op: OperationType::CdtWrite,
        bin: OperationBin::Name(bin),
        data: OperationData::CdtMapOp(cdt_op),
    }
}

/// Create map remove operation. Server removes map items identified by the key range
/// (`begin` inclusive, `end` exclusive). If `begin` is `Value::Nil`, the range is less than
/// `end`. If `end` is `Value::Nil`, the range is greater than equal to `begin`. Server returns
/// removed data specified by `return_type`.
pub fn remove_by_key_range<'a>(bin: &'a str,
                               begin: &'a Value,
                               end: &'a Value,
                               return_type: MapReturnType)
                               -> Operation<'a> {
    let mut args = vec![CdtArgument::Byte(return_type as u8), CdtArgument::Value(begin)];
    if !end.is_nil() {
        args.push(CdtArgument::Value(end));
    }
    let cdt_op = CdtOperation {
        op: CdtOpType::MapRemoveByKeyInterval,
        args: args,
    };
    Operation {
        op: OperationType::CdtWrite,
        bin: OperationBin::Name(bin),
        data: OperationData::CdtMapOp(cdt_op),
    }
}

/// Create map remove operation. Server removes the map items identified by value and returns
/// the removed data specified by `return_type`.
pub fn remove_by_value<'a>(bin: &'a str,
                           value: &'a Value,
                           return_type: MapReturnType)
                           -> Operation<'a> {
    let cdt_op = CdtOperation {
        op: CdtOpType::MapRemoveByValue,
        args: vec![CdtArgument::Byte(return_type as u8), CdtArgument::Value(value)],
    };
    Operation {
        op: OperationType::CdtWrite,
        bin: OperationBin::Name(bin),
        data: OperationData::CdtMapOp(cdt_op),
    }
}

/// Create map remove operation. Server removes the map items identified by values and returns
/// the removed data specified by `return_type`.
pub fn remove_by_value_list<'a>(bin: &'a str,
                                values: &'a [Value],
                                return_type: MapReturnType)
                                -> Operation<'a> {
    let cdt_op = CdtOperation {
        op: CdtOpType::MapRemoveByValueList,
        args: vec![CdtArgument::Byte(return_type as u8), CdtArgument::List(values)],
    };
    Operation {
        op: OperationType::CdtWrite,
        bin: OperationBin::Name(bin),
        data: OperationData::CdtMapOp(cdt_op),
    }
}

/// Create map remove operation. Server removes map items identified by value range (`begin`
/// inclusive, `end` exclusive). If `begin` is `Value::Nil`, the range is less than `end`. If
/// `end` is `Value::Nil`, the range is greater than equal to `begin`. Server returns the
/// removed data specified by `return_type`.
pub fn remove_by_value_range<'a>(bin: &'a str,
                                 begin: &'a Value,
                                 end: &'a Value,
                                 return_type: MapReturnType)
                                 -> Operation<'a> {
    let mut args = vec![CdtArgument::Byte(return_type as u8), CdtArgument::Value(begin)];
    if !end.is_nil() {
        args.push(CdtArgument::Value(end));
    }
    let cdt_op = CdtOperation {
        op: CdtOpType::MapRemoveByValueInterval,
        args: args,
    };
    Operation {
        op: OperationType::CdtWrite,
        bin: OperationBin::Name(bin),
        data: OperationData::CdtMapOp(cdt_op),
    }
}

/// Create map remove operation. Server removes the map item identified by the index and return
/// the removed data specified by `return_type`.
pub fn remove_by_index(bin: &str, index: i64, return_type: MapReturnType) -> Operation {
    let cdt_op = CdtOperation {
        op: CdtOpType::MapRemoveByIndex,
        args: vec![CdtArgument::Byte(return_type as u8), CdtArgument::Int(index)],
    };
    Operation {
        op: OperationType::CdtWrite,
        bin: OperationBin::Name(bin),
        data: OperationData::CdtMapOp(cdt_op),
    }
}

/// Create map remove operation. Server removes `count` map items starting at the specified
/// index and returns the removed data specified by `return_type`.
pub fn remove_by_index_range(bin: &str,
                             index: i64,
                             count: i64,
                             return_type: MapReturnType)
                             -> Operation {
    let cdt_op = CdtOperation {
        op: CdtOpType::MapRemoveByIndexRange,
        args: vec![CdtArgument::Byte(return_type as u8),
                   CdtArgument::Int(index),
                   CdtArgument::Int(count)],
    };
    Operation {
        op: OperationType::CdtWrite,
        bin: OperationBin::Name(bin),
        data: OperationData::CdtMapOp(cdt_op),
    }
}

/// Create map remove operation. Server removes the map items starting at the specified index
/// to the end of the map and returns the removed data specified by `return_type`.
pub fn remove_by_index_range_from(bin: &str, index: i64, return_type: MapReturnType) -> Operation {
    let cdt_op = CdtOperation {
        op: CdtOpType::MapRemoveByIndexRange,
        args: vec![CdtArgument::Byte(return_type as u8), CdtArgument::Int(index)],
    };
    Operation {
        op: OperationType::CdtWrite,
        bin: OperationBin::Name(bin),
        data: OperationData::CdtMapOp(cdt_op),
    }
}

/// Create map remove operation. Server removes the map item identified by rank and returns the
/// removed data specified by `return_type`.
pub fn remove_by_rank(bin: &str, rank: i64, return_type: MapReturnType) -> Operation {
    let cdt_op = CdtOperation {
        op: CdtOpType::MapRemoveByRank,
        args: vec![CdtArgument::Byte(return_type as u8), CdtArgument::Int(rank)],
    };
    Operation {
        op: OperationType::CdtWrite,
        bin: OperationBin::Name(bin),
        data: OperationData::CdtMapOp(cdt_op),
    }
}

/// Create map remove operation. Server removes `count` map items starting at the specified
/// rank and returns the removed data specified by `return_type`.
pub fn remove_by_rank_range(bin: &str,
                            rank: i64,
                            count: i64,
                            return_type: MapReturnType)
                            -> Operation {
    let cdt_op = CdtOperation {
        op: CdtOpType::MapRemoveByRankRange,
        args: vec![CdtArgument::Byte(return_type as u8),
                   CdtArgument::Int(rank),
                   CdtArgument::Int(count)],
    };
    Operation {
        op: OperationType::CdtWrite,
        bin: OperationBin::Name(bin),
        data: OperationData::CdtMapOp(cdt_op),
    }
}

/// Create map remove operation. Server removes the map items starting at the specified rank to
/// the last ranked item and returns the removed data specified by `return_type`.
pub fn remove_by_rank_range_from(bin: &str, rank: i64, return_type: MapReturnType) -> Operation {
    let cdt_op = CdtOperation {
        op: CdtOpType::MapRemoveByRankRange,
        args: vec![CdtArgument::Byte(return_type as u8), CdtArgument::Int(rank)],
    };
    Operation {
        op: OperationType::CdtWrite,
        bin: OperationBin::Name(bin),
        data: OperationData::CdtMapOp(cdt_op),
    }
}

/// Create map size operation. Server returns the size of the map.
pub fn size(bin: &str) -> Operation {
    let cdt_op = CdtOperation {
        op: CdtOpType::MapSize,
        args: vec![],
    };
    Operation {
        op: OperationType::CdtRead,
        bin: OperationBin::Name(bin),
        data: OperationData::CdtMapOp(cdt_op),
    }
}

/// Create map get by key operation. Server selects the map item idenfieid by the key and
/// returns the selected data specified by `return_type`.
pub fn get_by_key<'a>(bin: &'a str, key: &'a Value, return_type: MapReturnType) -> Operation<'a> {
    let cdt_op = CdtOperation {
        op: CdtOpType::MapGetByKey,
        args: vec![CdtArgument::Byte(return_type as u8), CdtArgument::Value(key)],
    };
    Operation {
        op: OperationType::CdtRead,
        bin: OperationBin::Name(bin),
        data: OperationData::CdtMapOp(cdt_op),
    }
}

/// Create map get by key range operation. Server selects the map items identified by the key
/// range (`begin` inclusive, `end` exclusive). If `begin` is `Value::Nil`, the range is less
/// than `end`. If `end` is `Value::Nil` the range is greater than equal to `begin`. Server
/// returns the selected data specified by `return_type`.
pub fn get_by_key_range<'a>(bin: &'a str,
                            begin: &'a Value,
                            end: &'a Value,
                            return_type: MapReturnType)
                            -> Operation<'a> {
    let mut args = vec![CdtArgument::Byte(return_type as u8), CdtArgument::Value(begin)];
    if !end.is_nil() {
        args.push(CdtArgument::Value(end));
    }
    let cdt_op = CdtOperation {
        op: CdtOpType::MapGetByKeyInterval,
        args: args,
    };
    Operation {
        op: OperationType::CdtRead,
        bin: OperationBin::Name(bin),
        data: OperationData::CdtMapOp(cdt_op),
    }
}

/// Create map get by value operation. Server selects the map items identified by value and
/// returns the selected data specified by `return_type`.
pub fn get_by_value<'a>(bin: &'a str,
                        value: &'a Value,
                        return_type: MapReturnType)
                        -> Operation<'a> {
    let cdt_op = CdtOperation {
        op: CdtOpType::MapGetByValue,
        args: vec![CdtArgument::Byte(return_type as u8), CdtArgument::Value(value)],
    };
    Operation {
        op: OperationType::CdtRead,
        bin: OperationBin::Name(bin),
        data: OperationData::CdtMapOp(cdt_op),
    }
}

/// Create map get by value range operation. Server selects the map items identified by the
/// value range (`begin` inclusive, `end` exclusive). If `begin` is `Value::Nil`, the range is
/// less than `end`. If `end` is `Value::Nil`, the range is greater than equal to `begin`.
/// Server returns the selected data specified by `return_type`.
pub fn get_by_value_range<'a>(bin: &'a str,
                              begin: &'a Value,
                              end: &'a Value,
                              return_type: MapReturnType)
                              -> Operation<'a> {
    let mut args = vec![CdtArgument::Byte(return_type as u8), CdtArgument::Value(begin)];
    if !end.is_nil() {
        args.push(CdtArgument::Value(end));
    }
    let cdt_op = CdtOperation {
        op: CdtOpType::MapGetByValueInterval,
        args: args,
    };
    Operation {
        op: OperationType::CdtRead,
        bin: OperationBin::Name(bin),
        data: OperationData::CdtMapOp(cdt_op),
    }
}

/// Create map get by index operation. Server selects the map item identified by index and
/// returns the selected data specified by `return_type`.
pub fn get_by_index(bin: &str, index: i64, return_type: MapReturnType) -> Operation {
    let cdt_op = CdtOperation {
        op: CdtOpType::MapGetByIndex,
        args: vec![CdtArgument::Byte(return_type as u8), CdtArgument::Int(index)],
    };
    Operation {
        op: OperationType::CdtRead,
        bin: OperationBin::Name(bin),
        data: OperationData::CdtMapOp(cdt_op),
    }
}

/// Create map get by index range operation. Server selects `count` map items starting at the
/// specified index and returns the selected data specified by `return_type`.
pub fn get_by_index_range(bin: &str,
                          index: i64,
                          count: i64,
                          return_type: MapReturnType)
                          -> Operation {
    let cdt_op = CdtOperation {
        op: CdtOpType::MapGetByIndexRange,
        args: vec![CdtArgument::Byte(return_type as u8),
                   CdtArgument::Int(index),
                   CdtArgument::Int(count)],
    };
    Operation {
        op: OperationType::CdtRead,
        bin: OperationBin::Name(bin),
        data: OperationData::CdtMapOp(cdt_op),
    }
}

/// Create map get by index range operation. Server selects the map items starting at the
/// specified index to the end of the map and returns the selected data specified by
/// `return_type`.
pub fn get_by_index_range_from(bin: &str, index: i64, return_type: MapReturnType) -> Operation {
    let cdt_op = CdtOperation {
        op: CdtOpType::MapGetByIndexRange,
        args: vec![CdtArgument::Byte(return_type as u8), CdtArgument::Int(index)],
    };
    Operation {
        op: OperationType::CdtRead,
        bin: OperationBin::Name(bin),
        data: OperationData::CdtMapOp(cdt_op),
    }
}

/// Create map get by rank operation. Server selects the mamp item identified by rank and
/// returns the selected data specified by `return_type`.
pub fn get_by_rank(bin: &str, rank: i64, return_type: MapReturnType) -> Operation {
    let cdt_op = CdtOperation {
        op: CdtOpType::MapGetByRank,
        args: vec![CdtArgument::Byte(return_type as u8), CdtArgument::Int(rank)],
    };
    Operation {
        op: OperationType::CdtRead,
        bin: OperationBin::Name(bin),
        data: OperationData::CdtMapOp(cdt_op),
    }
}

/// Create map get ranke range operation. Server selects `count` map items at the specified
/// rank and returns the selected data specified by `return_type`.
pub fn get_by_rank_range(bin: &str,
                         rank: i64,
                         count: i64,
                         return_type: MapReturnType)
                         -> Operation {
    let cdt_op = CdtOperation {
        op: CdtOpType::MapGetByRankRange,
        args: vec![CdtArgument::Byte(return_type as u8),
                   CdtArgument::Int(rank),
                   CdtArgument::Int(count)],
    };
    Operation {
        op: OperationType::CdtRead,
        bin: OperationBin::Name(bin),
        data: OperationData::CdtMapOp(cdt_op),
    }
}

/// Create map get by rank range operation. Server selects the map items starting at the
/// specified rank to the last ranked item and returns the selected data specified by
/// `return_type`.
pub fn get_by_rank_range_from(bin: &str, rank: i64, return_type: MapReturnType) -> Operation {
    let cdt_op = CdtOperation {
        op: CdtOpType::MapGetByRankRange,
        args: vec![CdtArgument::Byte(return_type as u8), CdtArgument::Int(rank)],
    };
    Operation {
        op: OperationType::CdtRead,
        bin: OperationBin::Name(bin),
        data: OperationData::CdtMapOp(cdt_op),
    }
}