1 /*
2  * Hunt - A redis client library for D programming language.
3  *
4  * Copyright (C) 2018-2019 HuntLabs
5  *
6  * Website: https://www.huntlabs.net/
7  *
8  * Licensed under the Apache-2.0 License.
9  *
10  */
11  
12 module hunt.redis.params.ZIncrByParams;
13 
14 import hunt.redis.params.Params;
15 import hunt.redis.util.SafeEncoder;
16 
17 import hunt.collection.ArrayList;
18 
19 /**
20  * Parameters for ZINCRBY commands <br/>
21  * <br/>
22  * In fact, Redis doesn't have parameters for ZINCRBY. Instead Redis has INCR parameter for ZADD.<br/>
23  * When users call ZADD with INCR option, its restriction (only one member) and return type is same
24  * to ZINCRBY. <br/>
25  * Document page for ZADD also describes INCR option to act like ZINCRBY. <br/>
26  * http://redis.io/commands/zadd <br/>
27  * <br/>
28  * So we decided to wrap "ZADD with INCR option" to ZINCRBY. <br/>
29  * https://github.com/xetorthio/redis/issues/1067 <br/>
30  * <br/>
31  * Works with Redis 3.0.2 and onwards.
32  */
33 class ZIncrByParams : Params {
34 
35     private enum string XX = "xx";
36     private enum string NX = "nx";
37     private enum string INCR = "incr";
38 
39     this() {
40     }
41 
42     static ZIncrByParams zIncrByParams() {
43         return new ZIncrByParams();
44     }
45 
46     /**
47    * Only set the key if it does not already exist.
48    * @return ZIncrByParams
49    */
50     ZIncrByParams nx() {
51         addParam(NX);
52         return this;
53     }
54 
55     /**
56    * Only set the key if it already exist.
57    * @return ZIncrByParams
58    */
59     ZIncrByParams xx() {
60         addParam(XX);
61         return this;
62     }
63 
64     const(ubyte)[][] getByteParams(const(ubyte)[] key, const(ubyte)[][] args...) {
65         ArrayList!(const(ubyte)[]) byteParams = new ArrayList!(const(ubyte)[])();
66         byteParams.add(key);
67 
68         if (contains(NX)) {
69             byteParams.add(SafeEncoder.encode(NX));
70         }
71         if (contains(XX)) {
72             byteParams.add(SafeEncoder.encode(XX));
73         }
74 
75         byteParams.add(SafeEncoder.encode(INCR));
76 
77         foreach (const(ubyte)[] arg; args) {
78             byteParams.add(arg);
79         }
80 
81         return byteParams.toArray();
82     }
83 
84 }