1+ using NRedisStack . Core . CuckooFilter . DataTypes ;
2+ using NRedisStack . Core . Literals ;
3+ using StackExchange . Redis ;
4+ namespace NRedisStack . Core
5+ {
6+
7+ public class CuckooCommands
8+ {
9+ IDatabase _db ;
10+ public CuckooCommands ( IDatabase db )
11+ {
12+ _db = db ;
13+ }
14+
15+ /// <summary>
16+ /// Adds an item to a Cuckoo Filter.
17+ /// </summary>
18+ /// <param name="key">The key under which the filter is found.</param>
19+ /// <param name="item">The item to add.</param>
20+ /// <returns><see langword="true"/> if the item did not exist in the filter, <see langword="false"/> otherwise.</returns>
21+ /// <remarks><seealso href="https://redis.io/commands/cf.add"/></remarks>
22+ public bool Add ( RedisKey key , RedisValue item )
23+ {
24+ return _db . Execute ( CF . ADD , key , item ) . ToString ( ) == "1" ;
25+ }
26+
27+ /// <summary>
28+ /// Adds an item to a Cuckoo Filter if the item did not exist previously.
29+ /// </summary>
30+ /// <param name="key">The key under which the filter is found.</param>
31+ /// <param name="item">The item to add.</param>
32+ /// <returns><see langword="true"/> if the item did not exist in the filter, <see langword="false"/> otherwise.</returns>
33+ /// <remarks><seealso href="https://redis.io/commands/cf.addnx"/></remarks>
34+ public bool AddNX ( RedisKey key , RedisValue item )
35+ {
36+ return _db . Execute ( CF . ADDNX , key , item ) . ToString ( ) == "1" ;
37+ }
38+
39+ /// <summary>
40+ /// Returns the number of times an item may be in the filter.
41+ /// </summary>
42+ /// <param name="key">The name of the filter</param>
43+ /// <param name="item">The item to count.</param>
44+ /// <returns>the count of possible matching copies of the item in the filter.</returns>
45+ /// <remarks><seealso href="https://redis.io/commands/cf.count"/></remarks>
46+ public long Count ( RedisKey key , RedisValue item )
47+ {
48+ return ResponseParser . ToLong ( _db . Execute ( CF . COUNT , key , item ) ) ;
49+ }
50+
51+ /// <summary>
52+ /// Deletes an item from the Cuckoo Filter.
53+ /// </summary>
54+ /// <param name="key">The name of the filter</param>
55+ /// <param name="item">The item to delete from the filter.</param>
56+ /// <returns>see langword="true"/> if the item has been deleted from the filter, <see langword="false"/> otherwise.</returns>
57+ /// <remarks><seealso href="https://redis.io/commands/cf.del"/></remarks>
58+ public bool Del ( RedisKey key , RedisValue item )
59+ {
60+ return _db . Execute ( CF . DEL , key , item ) . ToString ( ) == "1" ;
61+ }
62+
63+ /// <summary>
64+ /// Checks whether an item exist in the Cuckoo Filter or not.
65+ /// </summary>
66+ /// <param name="key">The name of the filter.</param>
67+ /// <param name="item">The item to check for.</param>
68+ /// <returns><see langword="true"/> means the item may exist in the filter,
69+ /// and <see langword="false"/> means it does not exist in the filter.</returns>
70+ /// <remarks><seealso href="https://redis.io/commands/cf.exists"/></remarks>
71+ public bool Exists ( RedisKey key , RedisValue item )
72+ {
73+ return _db . Execute ( CF . EXISTS , key , item ) . ToString ( ) == "1" ;
74+ }
75+
76+ /// <summary>
77+ /// Return information about a Cuckoo filter.
78+ /// </summary>
79+ /// <param name="key">Name of the key to return information about.</param>
80+ /// <returns>Information of the filter.</returns>
81+ /// <remarks><seealso href="https://redis.io/commands/cf.info"/></remarks>
82+ public CuckooInformation ? Info ( RedisKey key )
83+ {
84+ var info = _db . Execute ( CF . INFO , key ) ;
85+ return ResponseParser . ToCuckooInfo ( info ) ;
86+ }
87+
88+ /// <summary>
89+ /// Adds one or more items to a Cuckoo Filter. A filter will be created if it does not exist.
90+ /// </summary>
91+ /// <param name="key">The name of the filter.</param>
92+ /// <param name="items">One or more items to add.</param>
93+ /// <param name="capacity">(Optional) Specifies the desired capacity for the filter to be created.</param>
94+ /// <param name="nocreate">(Optional) <see langword="true"/> to indicates that the
95+ /// <returns>An array of booleans.</returns>
96+ /// <remarks><seealso href="https://redis.io/commands/cf.insert"/></remarks>
97+ public bool [ ] Insert ( RedisKey key , RedisValue [ ] items , int ? capacity = null , bool nocreate = false )
98+ {
99+ if ( items == null )
100+ throw new ArgumentNullException ( nameof ( items ) ) ;
101+
102+ List < object > args = new List < object > { key } ;
103+
104+ if ( capacity != null )
105+ {
106+ args . Add ( CuckooArgs . CAPACITY ) ;
107+ args . Add ( capacity ) ;
108+ }
109+
110+ if ( nocreate )
111+ {
112+ args . Add ( CuckooArgs . NOCREATE ) ;
113+ }
114+
115+ args . Add ( CuckooArgs . ITEMS ) ;
116+ foreach ( var item in items )
117+ {
118+ args . Add ( item ) ;
119+ }
120+
121+ return ResponseParser . ToBooleanArray ( _db . Execute ( CF . INSERT , args ) ) ;
122+ }
123+
124+ /// <summary>
125+ /// Adds one or more items to a Cuckoo Filter if the items did not exist previously.
126+ /// A filter will be created if it does not exist.
127+ /// </summary>
128+ /// <param name="key">The name of the filter.</param>
129+ /// <param name="items">One or more items to add.</param>
130+ /// <param name="capacity">(Optional) Specifies the desired capacity for the filter to be created.</param>
131+ /// <param name="nocreate">(Optional) <see langword="true"/> to indicates that the
132+ /// <returns>An array of booleans.where <see langword="true"/> means the item has been added to the filter,
133+ /// and <see langword="false"/> mean, the item already existed</returns>
134+ /// <remarks><seealso href="https://redis.io/commands/cf.insertnx"/></remarks>
135+ public bool [ ] InsertNX ( RedisKey key , RedisValue [ ] items , int ? capacity = null , bool nocreate = false )
136+ {
137+ if ( items == null )
138+ throw new ArgumentNullException ( nameof ( items ) ) ;
139+
140+ List < object > args = new List < object > { key } ;
141+
142+ if ( capacity != null )
143+ {
144+ args . Add ( CuckooArgs . CAPACITY ) ;
145+ args . Add ( capacity ) ;
146+ }
147+
148+ if ( nocreate )
149+ {
150+ args . Add ( CuckooArgs . NOCREATE ) ;
151+ }
152+
153+ args . Add ( CuckooArgs . ITEMS ) ;
154+ foreach ( var item in items )
155+ {
156+ args . Add ( item ) ;
157+ }
158+
159+ return ResponseParser . ToBooleanArray ( _db . Execute ( CF . INSERTNX , args ) ) ;
160+ }
161+
162+ /// <summary>
163+ /// Restores a filter previosly saved using SCANDUMP.
164+ /// </summary>
165+ /// <param name="key">Name of the key to restore.</param>
166+ /// <param name="iterator">Iterator value associated with data (returned by SCANDUMP).</param>
167+ /// <param name="data">Current data chunk (returned by SCANDUMP).</param>
168+ /// <returns>Array with information of the filter.</returns>
169+ /// <remarks><seealso href="https://redis.io/commands/cf.loadchunk"/></remarks>
170+ public bool LoadChunk ( RedisKey key , long iterator , Byte [ ] data )
171+ {
172+ return ResponseParser . ParseOKtoBoolean ( _db . Execute ( CF . LOADCHUNK , key , iterator , data ) ) ;
173+ }
174+
175+ /// <summary>
176+ /// Checks whether one or more items may exist in the a Cuckoo Filter.
177+ /// </summary>
178+ /// <param name="key">The name of the filter.</param>
179+ /// <param name="items">One or more items to check.</param>
180+ /// <returns>An array of booleans, for each item <see langword="true"/> means the item may exist in the filter,
181+ /// and <see langword="false"/> means the item may exist in the filter.</returns>
182+ /// <remarks><seealso href="https://redis.io/commands/cf.mexists"/></remarks>
183+ public bool [ ] MExists ( RedisKey key , RedisValue [ ] items )
184+ {
185+ if ( items == null )
186+ throw new ArgumentNullException ( nameof ( items ) ) ;
187+
188+ List < object > args = new List < object > { key } ;
189+
190+ foreach ( var item in items )
191+ {
192+ args . Add ( item ) ;
193+ }
194+
195+ return ResponseParser . ToBooleanArray ( _db . Execute ( CF . MEXISTS , args ) ) ;
196+ }
197+
198+ /// <summary>
199+ /// Creates a new Cuckoo Filter.
200+ /// </summary>
201+ /// <param name="key">The key under which the filter is found.</param>
202+ /// <param name="capacity">The number of entries intended to be added to the filter.</param>
203+ /// <param name="bucketSize">Number of items in each bucket.</param>
204+ /// <param name="maxIterations">Number of attempts to swap items between buckets before
205+ /// declaring filter as full and creating an additional filter.</param>
206+ /// <param name="expansion">(Optional) When capacity is reached, an additional sub-filter is
207+ /// created in size of the last sub-filter multiplied by expansion.</param>
208+ /// <returns><see langword="true"/> if executed correctly, <see langword="false"/> otherwise.</returns>
209+ /// <remarks><seealso href="https://redis.io/commands/cf.reserve"/></remarks>
210+ public bool Reserve ( RedisKey key , long capacity ,
211+ long ? bucketSize = null , int ? maxIterations = null , int ? expansion = null )
212+ {
213+ List < object > args = new List < object > { key , capacity } ;
214+
215+ if ( bucketSize != null )
216+ {
217+ args . Add ( CuckooArgs . BUCKETSIZE ) ;
218+ args . Add ( bucketSize ) ;
219+ }
220+
221+ if ( maxIterations != null )
222+ {
223+ args . Add ( CuckooArgs . MAXITERATIONS ) ;
224+ args . Add ( maxIterations ) ;
225+ }
226+
227+ if ( expansion != null )
228+ {
229+ args . Add ( CuckooArgs . EXPANSION ) ;
230+ args . Add ( expansion ) ;
231+ }
232+
233+ return ResponseParser . ParseOKtoBoolean ( _db . Execute ( CF . RESERVE , args ) ) ;
234+ }
235+
236+ /// <summary>
237+ /// Begins an incremental save of the Cuckoo Filter.
238+ /// </summary>
239+ /// <param name="key">Name of the filter.</param>
240+ /// <param name="iterator">Iterator value; either 0 or the iterator from a previous invocation of this command.</param>
241+ /// <returns>Tuple of iterator and data.</returns>
242+ /// <remarks><seealso href="https://redis.io/commands/cf.scandump"/></remarks>
243+ public Tuple < long , Byte [ ] > ? ScanDump ( RedisKey key , long iterator )
244+ {
245+ return ResponseParser . ToScanDumpTuple ( _db . Execute ( CF . SCANDUMP , key , iterator ) ) ;
246+ }
247+ }
248+ }
0 commit comments