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.SortingParams;
13 
14 import hunt.redis.Protocol;
15 
16 import hunt.collection.ArrayList;
17 import hunt.collection.Collection;
18 import hunt.collection.Collections;
19 import hunt.collection.List;
20 
21 import hunt.redis.util.SafeEncoder;
22 
23 alias Keyword = Protocol.Keyword;
24 
25 import std.conv;
26 
27 /**
28  * Builder Class for {@link Redis#sort(string, SortingParams) SORT} Parameters.
29  */
30 class SortingParams {
31     private List!(const(ubyte)[]) params;
32 
33     this() {
34         params = new ArrayList!(const(ubyte)[])();
35     }
36 
37     /**
38      * Sort by weight in keys.
39      * <p>
40      * Takes a pattern that is used in order to generate the key names of the weights used for
41      * sorting. Weight key names are obtained substituting the first occurrence of * with the actual
42      * value of the elements on the list.
43      * <p>
44      * The pattern for a normal key/value pair is "field*" and for a value in a hash
45      * "field*-&gt;fieldname".
46      * @param pattern
47      * @return the SortingParams Object
48      */
49     SortingParams by(const(ubyte)[] pattern) {
50         params.add(cast(const(ubyte)[])Keyword.BY.to!string());
51         params.add(pattern);
52         return this;
53     }
54 
55     /**
56      * No sorting.
57      * <p>
58      * This is useful if you want to retrieve a external key (using {@link #get(string...) GET}) but
59      * you don't want the sorting overhead.
60      * @return the SortingParams Object
61      */
62     SortingParams nosort() {
63         params.add(cast(const(ubyte)[])Keyword.BY.to!string());
64         params.add(cast(const(ubyte)[])Keyword.NOSORT.to!string());
65         return this;
66     }
67 
68     Collection!(const(ubyte)[]) getParams() {
69         // return Collections.unmodifiableCollection(params);
70         return params;
71     }
72 
73     /**
74      * Get the Sorting in Descending Order.
75      * @return the sortingParams Object
76      */
77     SortingParams desc() {
78         params.add(cast(const(ubyte)[])Keyword.DESC.to!string());
79         return this;
80     }
81 
82     /**
83      * Get the Sorting in Ascending Order. This is the default order.
84      * @return the SortingParams Object
85      */
86     SortingParams asc() {
87         params.add(cast(const(ubyte)[])Keyword.ASC.to!string());
88         return this;
89     }
90 
91     /**
92      * Limit the Numbers of returned Elements.
93      * @param start is zero based
94      * @param count
95      * @return the SortingParams Object
96      */
97     SortingParams limit(int start, int count) {
98         params.add(cast(const(ubyte)[])Keyword.LIMIT.to!string());
99         params.add(Protocol.toByteArray(start));
100         params.add(Protocol.toByteArray(count));
101         return this;
102     }
103 
104     /**
105      * Sort lexicographicaly. Note that Redis is utf-8 aware assuming you set the right value for the
106      * LC_COLLATE environment variable.
107      * @return the SortingParams Object
108      */
109     SortingParams alpha() {
110         params.add(cast(const(ubyte)[])Keyword.ALPHA.to!string());
111         return this;
112     }
113 
114     /**
115      * Retrieving external keys from the result of the search.
116      * <p>
117      * Takes a pattern that is used in order to generate the key names of the result of sorting. The
118      * key names are obtained substituting the first occurrence of * with the actual value of the
119      * elements on the list.
120      * <p>
121      * The pattern for a normal key/value pair is "field*" and for a value in a hash
122      * "field*-&gt;fieldname".
123      * <p>
124      * To get the list itself use the char # as pattern.
125      * @param patterns
126      * @return the SortingParams Object
127      */
128     SortingParams get(string[] patterns...) {
129         foreach(string pattern ; patterns) {
130             params.add(cast(const(ubyte)[])Keyword.GET.to!string());
131             params.add(SafeEncoder.encode(pattern));
132         }
133         return this;
134     }
135 
136 }