|
| 1 | +using NRedisStack.Core.CountMinSketch.DataTypes; |
| 2 | +using NRedisStack.Core.Literals; |
| 3 | +using StackExchange.Redis; |
| 4 | +namespace NRedisStack.Core |
| 5 | +{ |
| 6 | + |
| 7 | + public class CmsCommands |
| 8 | + { |
| 9 | + IDatabase _db; |
| 10 | + public CmsCommands(IDatabase db) |
| 11 | + { |
| 12 | + _db = db; |
| 13 | + } |
| 14 | + |
| 15 | + /// <summary> |
| 16 | + /// Increases the count of item by increment. |
| 17 | + /// </summary> |
| 18 | + /// <param name="key">The name of the sketch.</param> |
| 19 | + /// <param name="item">The item which counter is to be increased.</param> |
| 20 | + /// <param name="increment">Amount by which the item counter is to be increased.</param> |
| 21 | + /// <returns>Count of each item after increment.</returns> |
| 22 | + /// <remarks><seealso href="https://redis.io/commands/cms.incrby"/></remarks> |
| 23 | + public long IncrBy(RedisKey key, RedisValue item, long increment) |
| 24 | + { |
| 25 | + return ResponseParser.ToLong(_db.Execute(CMS.INCRBY, key, item, increment)); |
| 26 | + } |
| 27 | + |
| 28 | + /// <summary> |
| 29 | + /// Increases the count of item by increment. |
| 30 | + /// </summary> |
| 31 | + /// <param name="key">The name of the sketch.</param> |
| 32 | + /// <param name="itemIncrements">Tuple of The items which counter is to be increased |
| 33 | + /// and the Amount by which the item counter is to be increased.</param> |
| 34 | + /// <returns>Count of each item after increment.</returns> |
| 35 | + /// <remarks><seealso href="https://redis.io/commands/cms.incrby"/></remarks> |
| 36 | + public long[]? IncrBy(RedisKey key, Tuple<RedisValue, long>[] itemIncrements) |
| 37 | + { |
| 38 | + if (itemIncrements.Length < 1) |
| 39 | + throw new ArgumentException(nameof(itemIncrements)); |
| 40 | + |
| 41 | + List<object> args = new List<object> { key }; |
| 42 | + foreach (var pair in itemIncrements) |
| 43 | + { |
| 44 | + args.Add(pair.Item1); |
| 45 | + args.Add(pair.Item2); |
| 46 | + } |
| 47 | + return ResponseParser.ToLongArray(_db.Execute(CMS.INCRBY, args)); |
| 48 | + } |
| 49 | + |
| 50 | + /// <summary> |
| 51 | + /// Return information about a sketch. |
| 52 | + /// </summary> |
| 53 | + /// <param name="key">Name of the key to return information about.</param> |
| 54 | + /// <returns>Information of the sketch.</returns> |
| 55 | + /// <remarks><seealso href="https://redis.io/commands/cms.info"/></remarks> |
| 56 | + public CmsInformation? Info(RedisKey key) |
| 57 | + { |
| 58 | + var info = _db.Execute(CMS.INFO, key); |
| 59 | + return ResponseParser.ToCmsInfo(info); |
| 60 | + } |
| 61 | + |
| 62 | + //TODO: functions that returns OK cannot return false, they return OK or ERROR. fix this (the Redis functions that can return also false - will return 1 for true ans 0 for false) |
| 63 | + /// <summary> |
| 64 | + /// Initializes a Count-Min Sketch to dimensions specified by user. |
| 65 | + /// </summary> |
| 66 | + /// <param name="key">TThe name of the sketch.</param> |
| 67 | + /// <param name="width">Number of counters in each array. Reduces the error size.</param> |
| 68 | + /// <param name="depth">Number of counter-arrays. Reduces the probability for an error |
| 69 | + /// of a certain size (percentage of total count).</param> |
| 70 | + /// <returns><see langword="true"/> if if executed correctly, Error otherwise.</returns> |
| 71 | + /// <remarks><seealso href="https://redis.io/commands/cms.initbydim"/></remarks> |
| 72 | + public bool InitByDim(RedisKey key, long width, long depth) |
| 73 | + { |
| 74 | + return ResponseParser.ParseOKtoBoolean(_db.Execute(CMS.INITBYDIM, key, width, depth)); |
| 75 | + } |
| 76 | + |
| 77 | + /// <summary> |
| 78 | + /// Initializes a Count-Min Sketch to accommodate requested tolerances. |
| 79 | + /// </summary> |
| 80 | + /// <param name="key">The name of the sketch.</param> |
| 81 | + /// <param name="error">Estimate size of error.</param> |
| 82 | + /// <param name="probability">The desired probability for inflated count.</param> |
| 83 | + /// <returns><see langword="true"/> if if executed correctly, Error otherwise.</returns> |
| 84 | + /// <remarks><seealso href="https://redis.io/commands/cms.initbyprob"/></remarks> |
| 85 | + public bool InitByProb(RedisKey key, double error, double probability) |
| 86 | + { |
| 87 | + return ResponseParser.ParseOKtoBoolean(_db.Execute(CMS.INITBYPROB, key, error, probability)); |
| 88 | + } |
| 89 | + |
| 90 | + /// <summary> |
| 91 | + /// Merges several sketches into one sketch. |
| 92 | + /// </summary> |
| 93 | + /// <param name="destination">The name of destination sketch. Must be initialized</param> |
| 94 | + /// <param name="numKeys">Number of sketches to be merged.</param> |
| 95 | + /// <param name="source">Names of source sketches to be merged.</param> |
| 96 | + /// <param name="weight">Multiple of each sketch. Default = 1.</param> |
| 97 | + /// <returns><see langword="true"/> if if executed correctly, Error otherwise.</returns> |
| 98 | + /// <remarks><seealso href="https://redis.io/commands/cms.merge"/></remarks> |
| 99 | + public bool Merge(RedisValue destination, long numKeys, RedisValue[] source, long[]? weight = null) |
| 100 | + { |
| 101 | + if (source.Length < 1) |
| 102 | + throw new ArgumentNullException(nameof(source)); |
| 103 | + |
| 104 | + List<object> args = new List<object> { destination, numKeys }; |
| 105 | + |
| 106 | + foreach (var s in source) args.Add(s); |
| 107 | + |
| 108 | + if (weight != null && weight.Length >= 1) |
| 109 | + { |
| 110 | + args.Add(CmsArgs.WEIGHTS); |
| 111 | + foreach (var w in weight) args.Add(w); |
| 112 | + } |
| 113 | + |
| 114 | + return ResponseParser.ParseOKtoBoolean(_db.Execute(CMS.MERGE, args)); |
| 115 | + } |
| 116 | + |
| 117 | + /// <summary> |
| 118 | + /// Returns the count for one or more items in a sketch. |
| 119 | + /// </summary> |
| 120 | + /// <param name="key">The name of the sketch</param> |
| 121 | + /// <param name="items">One or more items for which to return the count.</param> |
| 122 | + /// <returns>Array with a min-count of each of the items in the sketch</returns> |
| 123 | + /// <remarks><seealso href="https://redis.io/commands/cms.merge"/></remarks> |
| 124 | + public long[]? Query(RedisKey key, RedisValue[] items) //TODO: Create second version of this function using params for items input |
| 125 | + { |
| 126 | + if (items.Length < 1) |
| 127 | + throw new ArgumentNullException(nameof(items)); |
| 128 | + |
| 129 | + List<object> args = new List<object> { key }; |
| 130 | + foreach (var item in items) args.Add(item); |
| 131 | + |
| 132 | + return ResponseParser.ToLongArray(_db.Execute(CMS.QUERY, args)); |
| 133 | + } |
| 134 | + } |
| 135 | +} |
0 commit comments