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
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
// Copyright 2015-2020 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.

//! List bin operations. Create list operations used by the client's `operate()` method.
//!
//! List operations support negative indexing. If the index is negative, the resolved index starts
//! backwards from the end of the list.
//!
//! Index/Count examples:
//!
//! * Index 0: First item in list.
//! * Index 4: Fifth item in list.
//! * Index -1: Last item in list.
//! * Index -3: Third to last item in list.
//! * Index 1, Count 2: Second and third item in list.
//! * Index -3, Count 3: Last three items in list.
//! * Index -5, Count 4: Range between fifth to last item to second to last item inclusive.
//!
//! If an index is out of bounds, a parameter error will be returned. If a range is partially out of
//! bounds, the valid part of the range will be returned.

use crate::msgpack::encoder::pack_cdt_op;
use crate::operations::cdt::{CdtArgument, CdtOperation};
use crate::operations::cdt_context::{CdtContext, DEFAULT_CTX};
use crate::operations::{Operation, OperationBin, OperationData, OperationType};
use crate::Value;

#[derive(Debug, Clone, Copy)]
#[doc(hidden)]
pub enum CdtListOpType {
    SetType = 0,
    Append = 1,
    AppendItems = 2,
    Insert = 3,
    InsertItems = 4,
    Pop = 5,
    PopRange = 6,
    Remove = 7,
    RemoveRange = 8,
    Set = 9,
    Trim = 10,
    Clear = 11,
    Increment = 12,
    Sort = 13,
    Size = 16,
    Get = 17,
    GetRange = 18,
    GetByIndex = 19,
    GetByRank = 21,
    GetByValue = 22,
    GetByValueList = 23,
    GetByIndexRange = 24,
    GetByValueInterval = 25,
    GetByRankRange = 26,
    GetByValueRelRankRange = 27,
    RemoveByIndex = 32,
    RemoveByRank = 34,
    RemoveByValue = 35,
    RemoveByValueList = 36,
    RemoveByIndexRange = 37,
    RemoveByValueInterval = 38,
    RemoveByRankRange = 39,
    RemoveByValueRelRankRange = 40,
}

/// List storage order.
#[derive(Debug, Clone, Copy)]
pub enum ListOrderType {
    /// List is not ordered. This is the default.
    Unordered = 0,
    /// List is ordered.
    Ordered = 1,
}

/// `CdtListReturnType` determines the returned values in CDT List operations.
#[derive(Debug, Clone, Copy)]
pub enum ListReturnType {
    /// Do not return a result.
    None = 0,
    /// Return index offset order.
    /// 0 = first key
    /// N = Nth key
    /// -1 = last key
    Index = 1,
    /// Return reverse index offset 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 value for single key read and value list for range read.
    Values = 7,
    /// Invert meaning of list command and return values.
    /// With the INVERTED flag enabled, the items outside of the specified index range will be returned.
    /// The meaning of the list command can also be inverted.
    /// With the INVERTED flag enabled, the items outside of the specified index range will be removed and returned.
    Inverted = 0x10000,
}

/// `CdtListSortFlags` determines sort flags for CDT lists
#[derive(Debug, Clone, Copy)]
pub enum ListSortFlags {
    /// Default is the default sort flag for CDT lists, and sort in Ascending order.
    Default = 0,
    /// Descending will sort the contents of the list in descending order.
    Descending = 1,
    /// DropDuplicates will drop duplicate values in the results of the CDT list operation.
    DropDuplicates = 2,
}

/// `CdtListWriteFlags` determines write flags for CDT lists
#[derive(Debug, Clone, Copy)]
pub enum ListWriteFlags {
    /// Default is the default behavior. It means:  Allow duplicate values and insertions at any index.
    Default = 0,
    /// AddUnique means: Only add unique values.
    AddUnique = 1,
    /// InsertBounded means: Enforce list boundaries when inserting.  Do not allow values to be inserted
    /// at index outside current list boundaries.
    InsertBounded = 2,
    /// NoFail means: do not raise error if a list item fails due to write flag constraints.
    NoFail = 4,
    /// Partial means: allow other valid list items to be committed if a list item fails due to
    /// write flag constraints.
    Partial = 8,
}

/// `ListPolicy` directives when creating a list and writing list items.
#[derive(Debug, Clone, Copy)]
pub struct ListPolicy {
    /// CdtListOrderType
    pub attributes: ListOrderType,
    /// CdtListWriteFlags
    pub flags: ListWriteFlags,
}

impl ListPolicy {
    /// Create unique key list with specified order when list does not exist.
    /// Use specified write mode when writing list items.
    pub const fn new(order: ListOrderType, write_flags: ListWriteFlags) -> Self {
        ListPolicy {
            attributes: order,
            flags: write_flags,
        }
    }
}

impl Default for ListPolicy {
    /// Returns the default policy for CDT list operations.
    fn default() -> Self {
        ListPolicy::new(ListOrderType::Unordered, ListWriteFlags::Default)
    }
}

#[doc(hidden)]
pub const fn list_order_flag(order: ListOrderType, pad: bool) -> u8 {
    if let ListOrderType::Ordered = order {
        return 0xc0;
    }
    if pad {
        return 0x80;
    }
    0x40
}

/// Creates list create operation.
/// Server creates list at given context level. The context is allowed to be beyond list
/// boundaries only if pad is set to true.  In that case, nil list entries will be inserted to
/// satisfy the context position.
pub fn create(bin: &str, list_order: ListOrderType, pad: bool) -> Operation {
    let cdt_op = CdtOperation {
        op: CdtListOpType::SetType as u8,
        encoder: Box::new(pack_cdt_op),
        args: vec![
            CdtArgument::Byte(list_order_flag(list_order, pad)),
            CdtArgument::Byte(list_order as u8),
        ],
    };
    Operation {
        op: OperationType::CdtWrite,
        ctx: DEFAULT_CTX,
        bin: OperationBin::Name(bin),
        data: OperationData::CdtListOp(cdt_op),
    }
}

/// Creates a set list order operation.
/// Server sets list order.  Server returns null.
pub fn set_order<'a>(
    bin: &'a str,
    list_order: ListOrderType,
    ctx: &'a [CdtContext],
) -> Operation<'a> {
    let cdt_op = CdtOperation {
        op: CdtListOpType::SetType as u8,
        encoder: Box::new(pack_cdt_op),
        args: vec![CdtArgument::Byte(list_order as u8)],
    };
    Operation {
        op: OperationType::CdtWrite,
        ctx,
        bin: OperationBin::Name(bin),
        data: OperationData::CdtListOp(cdt_op),
    }
}
/// Create list append operation. Server appends value to the end of list bin. Server returns
/// list size.
pub fn append<'a>(policy: &ListPolicy, bin: &'a str, value: &'a Value) -> Operation<'a> {
    let cdt_op = CdtOperation {
        op: CdtListOpType::Append as u8,
        encoder: Box::new(pack_cdt_op),
        args: vec![
            CdtArgument::Value(value),
            CdtArgument::Byte(policy.attributes as u8),
            CdtArgument::Byte(policy.flags as u8),
        ],
    };
    Operation {
        op: OperationType::CdtWrite,
        ctx: DEFAULT_CTX,
        bin: OperationBin::Name(bin),
        data: OperationData::CdtListOp(cdt_op),
    }
}

/// Create list append items operation. Server appends each input list item to the end of list
/// bin. Server returns list size.
pub fn append_items<'a>(policy: &ListPolicy, bin: &'a str, values: &'a [Value]) -> Operation<'a> {
    assert!(!values.is_empty());

    let cdt_op = CdtOperation {
        op: CdtListOpType::AppendItems as u8,
        encoder: Box::new(pack_cdt_op),
        args: vec![
            CdtArgument::List(values),
            CdtArgument::Byte(policy.attributes as u8),
            CdtArgument::Byte(policy.flags as u8),
        ],
    };
    Operation {
        op: OperationType::CdtWrite,
        ctx: DEFAULT_CTX,
        bin: OperationBin::Name(bin),
        data: OperationData::CdtListOp(cdt_op),
    }
}

/// Create list insert operation. Server inserts value to the specified index of the list bin.
/// Server returns list size.
pub fn insert<'a>(
    policy: &ListPolicy,
    bin: &'a str,
    index: i64,
    value: &'a Value,
) -> Operation<'a> {
    let cdt_op = CdtOperation {
        op: CdtListOpType::Insert as u8,
        encoder: Box::new(pack_cdt_op),
        args: vec![
            CdtArgument::Int(index),
            CdtArgument::Value(value),
            CdtArgument::Byte(policy.flags as u8),
        ],
    };
    Operation {
        op: OperationType::CdtWrite,
        ctx: DEFAULT_CTX,
        bin: OperationBin::Name(bin),
        data: OperationData::CdtListOp(cdt_op),
    }
}

/// Create list insert items operation. Server inserts each input list item starting at the
/// specified index of the list bin. Server returns list size.
pub fn insert_items<'a>(
    policy: &ListPolicy,
    bin: &'a str,
    index: i64,
    values: &'a [Value],
) -> Operation<'a> {
    assert!(!values.is_empty());

    let cdt_op = CdtOperation {
        op: CdtListOpType::InsertItems as u8,
        encoder: Box::new(pack_cdt_op),
        args: vec![
            CdtArgument::Int(index),
            CdtArgument::List(values),
            CdtArgument::Byte(policy.flags as u8),
        ],
    };
    Operation {
        op: OperationType::CdtWrite,
        ctx: DEFAULT_CTX,
        bin: OperationBin::Name(bin),
        data: OperationData::CdtListOp(cdt_op),
    }
}

/// Create list pop operation. Server returns the item at the specified index and removes the
/// item from the list bin.
pub fn pop(bin: &str, index: i64) -> Operation {
    let cdt_op = CdtOperation {
        op: CdtListOpType::Pop as u8,
        encoder: Box::new(pack_cdt_op),
        args: vec![CdtArgument::Int(index)],
    };
    Operation {
        op: OperationType::CdtWrite,
        ctx: DEFAULT_CTX,
        bin: OperationBin::Name(bin),
        data: OperationData::CdtListOp(cdt_op),
    }
}

/// Create list pop range operation. Server returns `count` items starting at the specified
/// index and removes the items from the list bin.
pub fn pop_range(bin: &str, index: i64, count: i64) -> Operation {
    let cdt_op = CdtOperation {
        op: CdtListOpType::PopRange as u8,
        encoder: Box::new(pack_cdt_op),
        args: vec![CdtArgument::Int(index), CdtArgument::Int(count)],
    };
    Operation {
        op: OperationType::CdtWrite,
        ctx: DEFAULT_CTX,
        bin: OperationBin::Name(bin),
        data: OperationData::CdtListOp(cdt_op),
    }
}

/// Create list pop range operation. Server returns the items starting at the specified index
/// to the end of the list and removes those items from the list bin.
pub fn pop_range_from(bin: &str, index: i64) -> Operation {
    let cdt_op = CdtOperation {
        op: CdtListOpType::PopRange as u8,
        encoder: Box::new(pack_cdt_op),
        args: vec![CdtArgument::Int(index)],
    };
    Operation {
        op: OperationType::CdtWrite,
        ctx: DEFAULT_CTX,
        bin: OperationBin::Name(bin),
        data: OperationData::CdtListOp(cdt_op),
    }
}

/// Create list remove operation. Server removes the item at the specified index from the list
/// bin. Server returns the number of items removed.
pub fn remove(bin: &str, index: i64) -> Operation {
    let cdt_op = CdtOperation {
        op: CdtListOpType::Remove as u8,
        encoder: Box::new(pack_cdt_op),
        args: vec![CdtArgument::Int(index)],
    };
    Operation {
        op: OperationType::CdtWrite,
        ctx: DEFAULT_CTX,
        bin: OperationBin::Name(bin),
        data: OperationData::CdtListOp(cdt_op),
    }
}

/// Create list remove range operation. Server removes `count` items starting at the specified
/// index from the list bin. Server returns the number of items removed.
pub fn remove_range(bin: &str, index: i64, count: i64) -> Operation {
    let cdt_op = CdtOperation {
        op: CdtListOpType::RemoveRange as u8,
        encoder: Box::new(pack_cdt_op),
        args: vec![CdtArgument::Int(index), CdtArgument::Int(count)],
    };
    Operation {
        op: OperationType::CdtWrite,
        ctx: DEFAULT_CTX,
        bin: OperationBin::Name(bin),
        data: OperationData::CdtListOp(cdt_op),
    }
}

/// Create list remove range operation. Server removes the items starting at the specified
/// index to the end of the list. Server returns the number of items removed.
pub fn remove_range_from(bin: &str, index: i64) -> Operation {
    let cdt_op = CdtOperation {
        op: CdtListOpType::RemoveRange as u8,
        encoder: Box::new(pack_cdt_op),
        args: vec![CdtArgument::Int(index)],
    };
    Operation {
        op: OperationType::CdtWrite,
        ctx: DEFAULT_CTX,
        bin: OperationBin::Name(bin),
        data: OperationData::CdtListOp(cdt_op),
    }
}

/// Create list remove value operation. Server removes all items that are equal to the
/// specified value. Server returns the number of items removed.
pub fn remove_by_value<'a>(
    bin: &'a str,
    value: &'a Value,
    return_type: ListReturnType,
) -> Operation<'a> {
    let cdt_op = CdtOperation {
        op: CdtListOpType::RemoveByValue as u8,
        encoder: Box::new(pack_cdt_op),
        args: vec![
            CdtArgument::Byte(return_type as u8),
            CdtArgument::Value(value),
        ],
    };
    Operation {
        op: OperationType::CdtWrite,
        ctx: DEFAULT_CTX,
        bin: OperationBin::Name(bin),
        data: OperationData::CdtListOp(cdt_op),
    }
}

/// Create list remove by value list operation. Server removes all items that are equal to
/// one of the specified values. Server returns the number of items removed
pub fn remove_by_value_list<'a>(
    bin: &'a str,
    values: &'a [Value],
    return_type: ListReturnType,
) -> Operation<'a> {
    let cdt_op = CdtOperation {
        op: CdtListOpType::RemoveByValueList as u8,
        encoder: Box::new(pack_cdt_op),
        args: vec![
            CdtArgument::Byte(return_type as u8),
            CdtArgument::List(values),
        ],
    };
    Operation {
        op: OperationType::CdtWrite,
        ctx: DEFAULT_CTX,
        bin: OperationBin::Name(bin),
        data: OperationData::CdtListOp(cdt_op),
    }
}

/// Creates a list remove operation.
/// Server removes list items identified by value range (valueBegin inclusive, valueEnd exclusive).
/// If valueBegin is nil, the range is less than valueEnd.
/// If valueEnd is nil, the range is greater than equal to valueBegin.
/// Server returns removed data specified by returnType
pub fn remove_by_value_range<'a>(
    bin: &'a str,
    return_type: ListReturnType,
    begin: &'a Value,
    end: &'a Value,
) -> Operation<'a> {
    let cdt_op = CdtOperation {
        op: CdtListOpType::RemoveByValueInterval as u8,
        encoder: Box::new(pack_cdt_op),
        args: vec![
            CdtArgument::Byte(return_type as u8),
            CdtArgument::Value(begin),
            CdtArgument::Value(end),
        ],
    };
    Operation {
        op: OperationType::CdtWrite,
        ctx: DEFAULT_CTX,
        bin: OperationBin::Name(bin),
        data: OperationData::CdtListOp(cdt_op),
    }
}

/// Creates a list remove by value relative to rank range operation.
/// Server removes list items nearest to value and greater by relative rank.
/// Server returns removed data specified by returnType.
///
/// Examples for ordered list \[0, 4, 5, 9, 11, 15\]:
/// ```text
/// (value,rank) = [removed items]
/// (5,0) = [5,9,11,15]
/// (5,1) = [9,11,15]
/// (5,-1) = [4,5,9,11,15]
/// (3,0) = [4,5,9,11,15]
/// (3,3) = [11,15]
/// (3,-3) = [0,4,5,9,11,15]
/// ```
pub fn remove_by_value_relative_rank_range<'a>(
    bin: &'a str,
    return_type: ListReturnType,
    value: &'a Value,
    rank: i64,
) -> Operation<'a> {
    let cdt_op = CdtOperation {
        op: CdtListOpType::RemoveByValueRelRankRange as u8,
        encoder: Box::new(pack_cdt_op),
        args: vec![
            CdtArgument::Byte(return_type as u8),
            CdtArgument::Value(value),
            CdtArgument::Int(rank),
        ],
    };
    Operation {
        op: OperationType::CdtWrite,
        ctx: DEFAULT_CTX,
        bin: OperationBin::Name(bin),
        data: OperationData::CdtListOp(cdt_op),
    }
}

/// Creates a list remove by value relative to rank range operation.
/// Server removes list items nearest to value and greater by relative rank with a count limit.
/// Server returns removed data specified by returnType.
///
/// Examples for ordered list \[0, 4, 5, 9, 11, 15\]:
/// ```text
/// (value,rank,count) = [removed items]
/// (5,0,2) = [5,9]
/// (5,1,1) = [9]
/// (5,-1,2) = [4,5]
/// (3,0,1) = [4]
/// (3,3,7) = [11,15]
/// (3,-3,2) = []
/// ```
pub fn remove_by_value_relative_rank_range_count<'a>(
    bin: &'a str,
    return_type: ListReturnType,
    value: &'a Value,
    rank: i64,
    count: i64,
) -> Operation<'a> {
    let cdt_op = CdtOperation {
        op: CdtListOpType::RemoveByValueRelRankRange as u8,
        encoder: Box::new(pack_cdt_op),
        args: vec![
            CdtArgument::Byte(return_type as u8),
            CdtArgument::Value(value),
            CdtArgument::Int(rank),
            CdtArgument::Int(count),
        ],
    };
    Operation {
        op: OperationType::CdtWrite,
        ctx: DEFAULT_CTX,
        bin: OperationBin::Name(bin),
        data: OperationData::CdtListOp(cdt_op),
    }
}

/// Creates a list remove operation.
/// Server removes list item identified by index and returns removed data specified by returnType.
pub fn remove_by_index(bin: &str, index: i64, return_type: ListReturnType) -> Operation {
    let cdt_op = CdtOperation {
        op: CdtListOpType::RemoveByIndex as u8,
        encoder: Box::new(pack_cdt_op),
        args: vec![
            CdtArgument::Byte(return_type as u8),
            CdtArgument::Int(index),
        ],
    };
    Operation {
        op: OperationType::CdtWrite,
        ctx: DEFAULT_CTX,
        bin: OperationBin::Name(bin),
        data: OperationData::CdtListOp(cdt_op),
    }
}

/// Creates a list remove operation.
/// Server removes list items starting at specified index to the end of list and returns removed
/// data specified by returnType.
pub fn remove_by_index_range(bin: &str, index: i64, return_type: ListReturnType) -> Operation {
    let cdt_op = CdtOperation {
        op: CdtListOpType::RemoveByIndexRange as u8,
        encoder: Box::new(pack_cdt_op),
        args: vec![
            CdtArgument::Byte(return_type as u8),
            CdtArgument::Int(index),
        ],
    };
    Operation {
        op: OperationType::CdtWrite,
        ctx: DEFAULT_CTX,
        bin: OperationBin::Name(bin),
        data: OperationData::CdtListOp(cdt_op),
    }
}

/// Creates a list remove operation.
/// Server removes "count" list items starting at specified index and returns removed data specified by returnType.
pub fn remove_by_index_range_count(
    bin: &str,
    index: i64,
    count: i64,
    return_type: ListReturnType,
) -> Operation {
    let cdt_op = CdtOperation {
        op: CdtListOpType::RemoveByIndexRange as u8,
        encoder: Box::new(pack_cdt_op),
        args: vec![
            CdtArgument::Byte(return_type as u8),
            CdtArgument::Int(index),
            CdtArgument::Int(count),
        ],
    };
    Operation {
        op: OperationType::CdtWrite,
        ctx: DEFAULT_CTX,
        bin: OperationBin::Name(bin),
        data: OperationData::CdtListOp(cdt_op),
    }
}

/// Creates a list remove operation.
/// Server removes list item identified by rank and returns removed data specified by returnType.
pub fn remove_by_rank(bin: &str, rank: i64, return_type: ListReturnType) -> Operation {
    let cdt_op = CdtOperation {
        op: CdtListOpType::RemoveByRank as u8,
        encoder: Box::new(pack_cdt_op),
        args: vec![CdtArgument::Byte(return_type as u8), CdtArgument::Int(rank)],
    };
    Operation {
        op: OperationType::CdtWrite,
        ctx: DEFAULT_CTX,
        bin: OperationBin::Name(bin),
        data: OperationData::CdtListOp(cdt_op),
    }
}

/// Creates a list remove operation.
/// Server removes list items starting at specified rank to the last ranked item and returns removed
/// data specified by returnType.
pub fn remove_by_rank_range(bin: &str, rank: i64, return_type: ListReturnType) -> Operation {
    let cdt_op = CdtOperation {
        op: CdtListOpType::RemoveByRankRange as u8,
        encoder: Box::new(pack_cdt_op),
        args: vec![CdtArgument::Byte(return_type as u8), CdtArgument::Int(rank)],
    };
    Operation {
        op: OperationType::CdtWrite,
        ctx: DEFAULT_CTX,
        bin: OperationBin::Name(bin),
        data: OperationData::CdtListOp(cdt_op),
    }
}

/// Creates a list remove operation.
/// Server removes "count" list items starting at specified rank and returns removed data specified by returnType.
pub fn remove_by_rank_range_count(
    bin: &str,
    rank: i64,
    count: i64,
    return_type: ListReturnType,
) -> Operation {
    let cdt_op = CdtOperation {
        op: CdtListOpType::RemoveByRankRange as u8,
        encoder: Box::new(pack_cdt_op),
        args: vec![
            CdtArgument::Byte(return_type as u8),
            CdtArgument::Int(rank),
            CdtArgument::Int(count),
        ],
    };
    Operation {
        op: OperationType::CdtWrite,
        ctx: DEFAULT_CTX,
        bin: OperationBin::Name(bin),
        data: OperationData::CdtListOp(cdt_op),
    }
}

/// Create list set operation. Server sets the item value at the specified index in the list
/// bin. Server does not return a result by default.
pub fn set<'a>(bin: &'a str, index: i64, value: &'a Value) -> Operation<'a> {
    assert!(!value.is_nil());

    let cdt_op = CdtOperation {
        op: CdtListOpType::Set as u8,
        encoder: Box::new(pack_cdt_op),
        args: vec![CdtArgument::Int(index), CdtArgument::Value(value)],
    };
    Operation {
        op: OperationType::CdtWrite,
        ctx: DEFAULT_CTX,
        bin: OperationBin::Name(bin),
        data: OperationData::CdtListOp(cdt_op),
    }
}

/// Create list trim operation. Server removes `count` items in the list bin that do not fall
/// into the range specified by `index` and `count`. If the range is out of bounds, then all
/// items will be removed. Server returns list size after trim.
pub fn trim(bin: &str, index: i64, count: i64) -> Operation {
    let cdt_op = CdtOperation {
        op: CdtListOpType::Trim as u8,
        encoder: Box::new(pack_cdt_op),
        args: vec![CdtArgument::Int(index), CdtArgument::Int(count)],
    };
    Operation {
        op: OperationType::CdtWrite,
        ctx: DEFAULT_CTX,
        bin: OperationBin::Name(bin),
        data: OperationData::CdtListOp(cdt_op),
    }
}

/// Create list clear operation. Server removes all items in the list bin. Server does not
/// return a result by default.
pub fn clear(bin: &str) -> Operation {
    let cdt_op = CdtOperation {
        op: CdtListOpType::Clear as u8,
        encoder: Box::new(pack_cdt_op),
        args: vec![],
    };
    Operation {
        op: OperationType::CdtWrite,
        ctx: DEFAULT_CTX,
        bin: OperationBin::Name(bin),
        data: OperationData::CdtListOp(cdt_op),
    }
}

/// Create list increment operation. Server increments the item value at the specified index by the
/// given amount and returns the final result.
pub fn increment<'a>(policy: &ListPolicy, bin: &'a str, index: i64, value: i64) -> Operation<'a> {
    let cdt_op = CdtOperation {
        op: CdtListOpType::Increment as u8,
        encoder: Box::new(pack_cdt_op),
        args: vec![
            CdtArgument::Int(index),
            CdtArgument::Int(value),
            CdtArgument::Byte(policy.flags as u8),
        ],
    };
    Operation {
        op: OperationType::CdtWrite,
        ctx: DEFAULT_CTX,
        bin: OperationBin::Name(bin),
        data: OperationData::CdtListOp(cdt_op),
    }
}

/// Create list size operation. Server returns size of the list.
pub fn size(bin: &str) -> Operation {
    let cdt_op = CdtOperation {
        op: CdtListOpType::Size as u8,
        encoder: Box::new(pack_cdt_op),
        args: vec![],
    };
    Operation {
        op: OperationType::CdtRead,
        ctx: DEFAULT_CTX,
        bin: OperationBin::Name(bin),
        data: OperationData::CdtListOp(cdt_op),
    }
}

/// Create list get operation. Server returns the item at the specified index in the list bin.
pub fn get(bin: &str, index: i64) -> Operation {
    let cdt_op = CdtOperation {
        op: CdtListOpType::Get as u8,
        encoder: Box::new(pack_cdt_op),
        args: vec![CdtArgument::Int(index)],
    };
    Operation {
        op: OperationType::CdtRead,
        ctx: DEFAULT_CTX,
        bin: OperationBin::Name(bin),
        data: OperationData::CdtListOp(cdt_op),
    }
}

/// Create list get range operation. Server returns `count` items starting at the specified
/// index in the list bin.
pub fn get_range(bin: &str, index: i64, count: i64) -> Operation {
    let cdt_op = CdtOperation {
        op: CdtListOpType::GetRange as u8,
        encoder: Box::new(pack_cdt_op),
        args: vec![CdtArgument::Int(index), CdtArgument::Int(count)],
    };
    Operation {
        op: OperationType::CdtRead,
        ctx: DEFAULT_CTX,
        bin: OperationBin::Name(bin),
        data: OperationData::CdtListOp(cdt_op),
    }
}

/// Create list get range operation. Server returns items starting at the index to the end of
/// the list.
pub fn get_range_from(bin: &str, index: i64) -> Operation {
    let cdt_op = CdtOperation {
        op: CdtListOpType::GetRange as u8,
        encoder: Box::new(pack_cdt_op),
        args: vec![CdtArgument::Int(index)],
    };
    Operation {
        op: OperationType::CdtRead,
        ctx: DEFAULT_CTX,
        bin: OperationBin::Name(bin),
        data: OperationData::CdtListOp(cdt_op),
    }
}

/// Creates a list get by value operation.
/// Server selects list items identified by value and returns selected data specified by returnType.
pub fn get_by_value<'a>(
    bin: &'a str,
    value: &'a Value,
    return_type: ListReturnType,
) -> Operation<'a> {
    let cdt_op = CdtOperation {
        op: CdtListOpType::GetByValue as u8,
        encoder: Box::new(pack_cdt_op),
        args: vec![
            CdtArgument::Byte(return_type as u8),
            CdtArgument::Value(value),
        ],
    };

    Operation {
        op: OperationType::CdtRead,
        ctx: DEFAULT_CTX,
        bin: OperationBin::Name(bin),
        data: OperationData::CdtListOp(cdt_op),
    }
}

/// Creates list get by value list operation.
/// Server selects list items identified by values and returns selected data specified by returnType.
pub fn get_by_value_list<'a>(
    bin: &'a str,
    values: &'a [Value],
    return_type: ListReturnType,
) -> Operation<'a> {
    let cdt_op = CdtOperation {
        op: CdtListOpType::GetByValueList as u8,
        encoder: Box::new(pack_cdt_op),
        args: vec![
            CdtArgument::Byte(return_type as u8),
            CdtArgument::List(values),
        ],
    };
    Operation {
        op: OperationType::CdtRead,
        ctx: DEFAULT_CTX,
        bin: OperationBin::Name(bin),
        data: OperationData::CdtListOp(cdt_op),
    }
}

/// Creates a list get by value range operation.
/// Server selects list items identified by value range (valueBegin inclusive, valueEnd exclusive)
/// If valueBegin is null, the range is less than valueEnd.
/// If valueEnd is null, the range is greater than equal to valueBegin.
/// Server returns selected data specified by returnType.
pub fn get_by_value_range<'a>(
    bin: &'a str,
    begin: &'a Value,
    end: &'a Value,
    return_type: ListReturnType,
) -> Operation<'a> {
    let cdt_op = CdtOperation {
        op: CdtListOpType::GetByValueInterval as u8,
        encoder: Box::new(pack_cdt_op),
        args: vec![
            CdtArgument::Byte(return_type as u8),
            CdtArgument::Value(begin),
            CdtArgument::Value(end),
        ],
    };
    Operation {
        op: OperationType::CdtRead,
        ctx: DEFAULT_CTX,
        bin: OperationBin::Name(bin),
        data: OperationData::CdtListOp(cdt_op),
    }
}

/// Creates list get by index operation.
/// Server selects list item identified by index and returns selected data specified by returnType
pub fn get_by_index(bin: &str, index: i64, return_type: ListReturnType) -> Operation {
    let cdt_op = CdtOperation {
        op: CdtListOpType::GetByIndex as u8,
        encoder: Box::new(pack_cdt_op),
        args: vec![
            CdtArgument::Byte(return_type as u8),
            CdtArgument::Int(index),
        ],
    };

    Operation {
        op: OperationType::CdtRead,
        ctx: DEFAULT_CTX,
        bin: OperationBin::Name(bin),
        data: OperationData::CdtListOp(cdt_op),
    }
}

/// Creates list get by index range operation.
/// Server selects list items starting at specified index to the end of list and returns selected
/// data specified by returnType.
pub fn get_by_index_range(bin: &str, index: i64, return_type: ListReturnType) -> Operation {
    let cdt_op = CdtOperation {
        op: CdtListOpType::GetByIndexRange as u8,
        encoder: Box::new(pack_cdt_op),
        args: vec![
            CdtArgument::Byte(return_type as u8),
            CdtArgument::Int(index),
        ],
    };

    Operation {
        op: OperationType::CdtRead,
        ctx: DEFAULT_CTX,
        bin: OperationBin::Name(bin),
        data: OperationData::CdtListOp(cdt_op),
    }
}

/// Creates list get by index range operation.
/// Server selects "count" list items starting at specified index and returns selected data specified
/// by returnType.
pub fn get_by_index_range_count(
    bin: &str,
    index: i64,
    count: i64,
    return_type: ListReturnType,
) -> Operation {
    let cdt_op = CdtOperation {
        op: CdtListOpType::GetByIndexRange as u8,
        encoder: Box::new(pack_cdt_op),
        args: vec![
            CdtArgument::Byte(return_type as u8),
            CdtArgument::Int(index),
            CdtArgument::Int(count),
        ],
    };

    Operation {
        op: OperationType::CdtRead,
        ctx: DEFAULT_CTX,
        bin: OperationBin::Name(bin),
        data: OperationData::CdtListOp(cdt_op),
    }
}

/// Creates a list get by rank operation.
/// Server selects list item identified by rank and returns selected data specified by returnType.
pub fn get_by_rank(bin: &str, rank: i64, return_type: ListReturnType) -> Operation {
    let cdt_op = CdtOperation {
        op: CdtListOpType::GetByRank as u8,
        encoder: Box::new(pack_cdt_op),
        args: vec![CdtArgument::Byte(return_type as u8), CdtArgument::Int(rank)],
    };
    Operation {
        op: OperationType::CdtRead,
        ctx: DEFAULT_CTX,
        bin: OperationBin::Name(bin),
        data: OperationData::CdtListOp(cdt_op),
    }
}

/// Creates a list get by rank range operation.
/// Server selects list items starting at specified rank to the last ranked item and returns selected
/// data specified by returnType.
pub fn get_by_rank_range(bin: &str, rank: i64, return_type: ListReturnType) -> Operation {
    let cdt_op = CdtOperation {
        op: CdtListOpType::GetByRankRange as u8,
        encoder: Box::new(pack_cdt_op),
        args: vec![CdtArgument::Byte(return_type as u8), CdtArgument::Int(rank)],
    };
    Operation {
        op: OperationType::CdtRead,
        ctx: DEFAULT_CTX,
        bin: OperationBin::Name(bin),
        data: OperationData::CdtListOp(cdt_op),
    }
}

/// Creates a list get by rank range operation.
/// Server selects "count" list items starting at specified rank and returns selected data specified by returnType.
pub fn get_by_rank_range_count(
    bin: &str,
    rank: i64,
    count: i64,
    return_type: ListReturnType,
) -> Operation {
    let cdt_op = CdtOperation {
        op: CdtListOpType::GetByRankRange as u8,
        encoder: Box::new(pack_cdt_op),
        args: vec![
            CdtArgument::Byte(return_type as u8),
            CdtArgument::Int(rank),
            CdtArgument::Int(count),
        ],
    };
    Operation {
        op: OperationType::CdtRead,
        ctx: DEFAULT_CTX,
        bin: OperationBin::Name(bin),
        data: OperationData::CdtListOp(cdt_op),
    }
}

/// Creates a list get by value relative to rank range operation.
/// Server selects list items nearest to value and greater by relative rank.
/// Server returns selected data specified by returnType.
///
/// Examples for ordered list \[0, 4, 5, 9, 11, 15\]:
/// ```text
/// (value,rank) = [selected items]
/// (5,0) = [5,9,11,15]
/// (5,1) = [9,11,15]
/// (5,-1) = [4,5,9,11,15]
/// (3,0) = [4,5,9,11,15]
/// (3,3) = [11,15]
/// (3,-3) = [0,4,5,9,11,15]
/// ```
pub fn get_by_value_relative_rank_range<'a>(
    bin: &'a str,
    value: &'a Value,
    rank: i64,
    return_type: ListReturnType,
) -> Operation<'a> {
    let cdt_op = CdtOperation {
        op: CdtListOpType::GetByValueRelRankRange as u8,
        encoder: Box::new(pack_cdt_op),
        args: vec![
            CdtArgument::Byte(return_type as u8),
            CdtArgument::Value(value),
            CdtArgument::Int(rank),
        ],
    };
    Operation {
        op: OperationType::CdtRead,
        ctx: DEFAULT_CTX,
        bin: OperationBin::Name(bin),
        data: OperationData::CdtListOp(cdt_op),
    }
}

/// Creates a list get by value relative to rank range operation.
/// Server selects list items nearest to value and greater by relative rank with a count limit.
/// Server returns selected data specified by returnType.
///
/// Examples for ordered list \[0, 4, 5, 9, 11, 15\]:
/// ```text
/// (value,rank,count) = [selected items]
/// (5,0,2) = [5,9]
/// (5,1,1) = [9]
/// (5,-1,2) = [4,5]
/// (3,0,1) = [4]
/// (3,3,7) = [11,15]
/// (3,-3,2) = []
/// ```
pub fn get_by_value_relative_rank_range_count<'a>(
    bin: &'a str,
    value: &'a Value,
    rank: i64,
    count: i64,
    return_type: ListReturnType,
) -> Operation<'a> {
    let cdt_op = CdtOperation {
        op: CdtListOpType::GetByValueRelRankRange as u8,
        encoder: Box::new(pack_cdt_op),
        args: vec![
            CdtArgument::Byte(return_type as u8),
            CdtArgument::Value(value),
            CdtArgument::Int(rank),
            CdtArgument::Int(count),
        ],
    };
    Operation {
        op: OperationType::CdtRead,
        ctx: DEFAULT_CTX,
        bin: OperationBin::Name(bin),
        data: OperationData::CdtListOp(cdt_op),
    }
}

/// Creates list sort operation.
/// Server sorts list according to sortFlags.
/// Server does not return a result by default.
pub fn sort(bin: &str, sort_flags: ListSortFlags) -> Operation {
    let cdt_op = CdtOperation {
        op: CdtListOpType::Sort as u8,
        encoder: Box::new(pack_cdt_op),
        args: vec![CdtArgument::Byte(sort_flags as u8)],
    };
    Operation {
        op: OperationType::CdtWrite,
        ctx: DEFAULT_CTX,
        bin: OperationBin::Name(bin),
        data: OperationData::CdtListOp(cdt_op),
    }
}