@@ -269,9 +269,119 @@ func (client *Client) CmsMerge(key string, srcs []string, weights []string) (str
269269func (client * Client ) CmsInfo (key string ) (map [string ]int64 , error ) {
270270 conn := client .Pool .Get ()
271271 defer conn .Close ()
272- reply , err := conn .Do ("CMS.INFO" , key )
272+ return ParseInfoReply (redis .Values (conn .Do ("CMS.INFO" , key )))
273+ }
273274
274- values , err := redis .Values (reply , err )
275+ // Create an empty cuckoo filter with an initial capacity of {capacity} items.
276+ func (client * Client ) CfReserve (key string , capacity int64 , bucketSize int64 , maxIterations int64 , expansion int64 ) (string , error ) {
277+ conn := client .Pool .Get ()
278+ defer conn .Close ()
279+ args := redis.Args {key }.Add (capacity )
280+ if bucketSize > 0 {
281+ args = args .Add ("BUCKETSIZE" , bucketSize )
282+ }
283+ if maxIterations > 0 {
284+ args = args .Add ("MAXITERATIONS" , maxIterations )
285+ }
286+ if expansion > 0 {
287+ args = args .Add ("EXPANSION" , expansion )
288+ }
289+ return redis .String (conn .Do ("CF.RESERVE" , args ... ))
290+ }
291+
292+ // Adds an item to the cuckoo filter, creating the filter if it does not exist.
293+ func (client * Client ) CfAdd (key string , item string ) (bool , error ) {
294+ conn := client .Pool .Get ()
295+ defer conn .Close ()
296+ return redis .Bool (conn .Do ("CF.ADD" , key , item ))
297+ }
298+
299+ // Adds an item to a cuckoo filter if the item did not exist previously.
300+ func (client * Client ) CfAddNx (key string , item string ) (bool , error ) {
301+ conn := client .Pool .Get ()
302+ defer conn .Close ()
303+ return redis .Bool (conn .Do ("CF.ADDNX" , key , item ))
304+ }
305+
306+ // Adds one or more items to a cuckoo filter, allowing the filter to be created with a custom capacity if it does not yet exist.
307+ func (client * Client ) CfInsert (key string , cap int64 , noCreate bool , items []string ) ([]int64 , error ) {
308+ conn := client .Pool .Get ()
309+ defer conn .Close ()
310+ args := GetInsertArgs (key , cap , noCreate , items )
311+ return redis .Int64s (conn .Do ("CF.INSERT" , args ... ))
312+ }
313+
314+ // Adds one or more items to a cuckoo filter, allowing the filter to be created with a custom capacity if it does not yet exist.
315+ func (client * Client ) CfInsertNx (key string , cap int64 , noCreate bool , items []string ) ([]int64 , error ) {
316+ conn := client .Pool .Get ()
317+ defer conn .Close ()
318+ args := GetInsertArgs (key , cap , noCreate , items )
319+ return redis .Int64s (conn .Do ("CF.INSERTNX" , args ... ))
320+ }
321+
322+ func GetInsertArgs (key string , cap int64 , noCreate bool , items []string ) redis.Args {
323+ args := redis.Args {key }
324+ if cap > 0 {
325+ args = args .Add ("CAPACITY" , cap )
326+ }
327+ if noCreate {
328+ args = args .Add ("NOCREATE" )
329+ }
330+ args = args .Add ("ITEMS" ).AddFlat (items )
331+ return args
332+ }
333+
334+ // Check if an item exists in a Cuckoo Filter
335+ func (client * Client ) CfExists (key string , item string ) (bool , error ) {
336+ conn := client .Pool .Get ()
337+ defer conn .Close ()
338+ return redis .Bool (conn .Do ("CF.EXISTS" , key , item ))
339+ }
340+
341+ // Deletes an item once from the filter.
342+ func (client * Client ) CfDel (key string , item string ) (bool , error ) {
343+ conn := client .Pool .Get ()
344+ defer conn .Close ()
345+ return redis .Bool (conn .Do ("CF.DEL" , key , item ))
346+ }
347+
348+ // Returns the number of times an item may be in the filter.
349+ func (client * Client ) CfCount (key string , item string ) (int64 , error ) {
350+ conn := client .Pool .Get ()
351+ defer conn .Close ()
352+ return redis .Int64 (conn .Do ("CF.COUNT" , key , item ))
353+ }
354+
355+ // Begins an incremental save of the cuckoo filter.
356+ func (client * Client ) CfScanDump (key string , iter int64 ) (int64 , []byte , error ) {
357+ conn := client .Pool .Get ()
358+ defer conn .Close ()
359+ reply , err := redis .Values (conn .Do ("CF.SCANDUMP" , key , iter ))
360+ if err != nil || len (reply ) != 2 {
361+ return 0 , nil , err
362+ }
363+ iter = reply [0 ].(int64 )
364+ if reply [1 ] == nil {
365+ return iter , nil , err
366+ }
367+ return iter , reply [1 ].([]byte ), err
368+ }
369+
370+ // Restores a filter previously saved using SCANDUMP
371+ func (client * Client ) CfLoadChunk (key string , iter int64 , data []byte ) (string , error ) {
372+ conn := client .Pool .Get ()
373+ defer conn .Close ()
374+ return redis .String (conn .Do ("CF.LOADCHUNK" , key , iter , data ))
375+ }
376+
377+ // Return information about key
378+ func (client * Client ) CfInfo (key string ) (map [string ]int64 , error ) {
379+ conn := client .Pool .Get ()
380+ defer conn .Close ()
381+ return ParseInfoReply (redis .Values (conn .Do ("CF.INFO" , key )))
382+ }
383+
384+ func ParseInfoReply (values []interface {}, err error ) (map [string ]int64 , error ) {
275385 if err != nil {
276386 return nil , err
277387 }
0 commit comments