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.ScanParams;
13 
14 import hunt.redis.Protocol;
15 import hunt.redis.util.SafeEncoder;
16 
17 import hunt.collection;
18 import hunt.Integer;
19 
20 import std.conv;
21 
22 class ScanParams {
23 
24     private Map!(Protocol.Keyword, ByteBuffer) params;
25 
26     enum string SCAN_POINTER_START = "0";
27     enum const(ubyte)[] SCAN_POINTER_START_BINARY = SafeEncoder.encode(SCAN_POINTER_START);
28 
29     this() {
30         params = new HashMap!(Protocol.Keyword, ByteBuffer); // new EnumMap!(Keyword, ByteBuffer)(Keyword.class);
31     }
32 
33     ScanParams match(const(ubyte)[] pattern) {
34         params.put(Protocol.Keyword.MATCH, BufferUtils.toBuffer(cast(byte[])pattern));
35         return this;
36     }
37 
38     /**
39    * @see <a href="https://redis.io/commands/scan#the-match-option">MATCH option in Redis documentation</a>
40    * 
41    * @param pattern
42    * @return 
43    */
44     ScanParams match(string pattern) {
45         params.put(Protocol.Keyword.MATCH, BufferUtils.toBuffer(cast(byte[])SafeEncoder.encode(pattern)));
46         return this;
47     }
48 
49     /**
50    * @see <a href="https://redis.io/commands/scan#the-count-option">COUNT option in Redis documentation</a>
51    * 
52    * @param count
53    * @return 
54    */
55     ScanParams count(int count) {
56         params.put(Protocol.Keyword.COUNT, BufferUtils.toBuffer(cast(byte[])Protocol.toByteArray(count)));
57         return this;
58     }
59 
60     Collection!(const(ubyte)[]) getParams() {
61         List!(const(ubyte)[]) paramsList = new ArrayList!(const(ubyte)[])(params.size());
62         foreach (Protocol.Keyword key, ByteBuffer value; params) {
63             paramsList.add(cast(const(ubyte)[])key.to!string());
64             paramsList.add(cast(const(ubyte)[])value.array());
65         }
66         // return Collections.unmodifiableCollection(paramsList);
67         return paramsList;
68     }
69 
70     const(ubyte)[] binaryMatch() {
71         if (params.containsKey(Protocol.Keyword.MATCH)) {
72             return cast(const(ubyte)[])(params.get(Protocol.Keyword.MATCH).array());
73         } else {
74             return null;
75         }
76     }
77 
78     string match() {
79         if (params.containsKey(Protocol.Keyword.MATCH)) {
80             return cast(string)(params.get(Protocol.Keyword.MATCH).array());
81         } else {
82             return null;
83         }
84     }
85 
86     Integer count() {
87         if (params.containsKey(Protocol.Keyword.COUNT)) {
88             return new Integer(params.get(Protocol.Keyword.COUNT).getInt());
89         } else {
90             return null;
91         }
92     }
93 }