|
| 1 | +using NRedisStack.Core.TopK.DataTypes; |
| 2 | +using NRedisStack.Core.Literals; |
| 3 | +using StackExchange.Redis; |
| 4 | +namespace NRedisStack.Core |
| 5 | +{ |
| 6 | + |
| 7 | + public class TopKCommands //TODO: Finish this |
| 8 | + { |
| 9 | + IDatabase _db; |
| 10 | + public TopKCommands(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">Item to be added.</param> |
| 20 | + /// <returns>Array of simple-string-reply - if an element was dropped from the TopK list, null otherwise</returns> |
| 21 | + /// <remarks><seealso href="https://redis.io/commands/topk.add"/></remarks> |
| 22 | + public RedisResult[]? Add(RedisKey key, RedisValue item) |
| 23 | + { |
| 24 | + return ResponseParser.ToArray(_db.Execute(TOPK.ADD, key, item)); |
| 25 | + } |
| 26 | + |
| 27 | + /// <summary> |
| 28 | + /// Increases the count of item by increment. |
| 29 | + /// </summary> |
| 30 | + /// <param name="key">The name of the sketch.</param> |
| 31 | + /// <param name="items">Items to be added</param> |
| 32 | + /// <returns>Array of simple-string-reply - if an element was dropped from the TopK list, null otherwise</returns> |
| 33 | + /// <remarks><seealso href="https://redis.io/commands/topk.add"/></remarks> |
| 34 | + public RedisResult[]? Add(RedisKey key, params RedisValue[] items) |
| 35 | + { |
| 36 | + var args = Auxiliary.MergeArgs(key, items); |
| 37 | + |
| 38 | + return (RedisResult[]?)_db.Execute(TOPK.ADD, args); |
| 39 | + } |
| 40 | + |
| 41 | + /// <summary> |
| 42 | + /// Returns count for an item. |
| 43 | + /// </summary> |
| 44 | + /// <param name="key">Name of sketch where item is counted</param> |
| 45 | + /// <param name="item">Item to be counted.</param> |
| 46 | + /// <returns>count for responding item.</returns> |
| 47 | + /// <remarks><seealso href="https://redis.io/commands/cf.count"/></remarks> |
| 48 | + public long Count(RedisKey key, RedisValue item) |
| 49 | + { |
| 50 | + return ResponseParser.ToLong(_db.Execute(TOPK.COUNT, key, item)); |
| 51 | + } |
| 52 | + |
| 53 | + /// <summary> |
| 54 | + /// Returns count for an items. |
| 55 | + /// </summary> |
| 56 | + /// <param name="key">Name of sketch where item is counted</param> |
| 57 | + /// <param name="item">Items to be counted.</param> |
| 58 | + /// <returns>count for responding item.</returns> |
| 59 | + /// <remarks><seealso href="https://redis.io/commands/cf.count"/></remarks> |
| 60 | + public long[]? Count(RedisKey key, params RedisValue[] items) |
| 61 | + { |
| 62 | + var args = Auxiliary.MergeArgs(key, items); |
| 63 | + return ResponseParser.ToLongArray(_db.Execute(TOPK.COUNT, args)); |
| 64 | + } |
| 65 | + |
| 66 | + |
| 67 | + /// <summary> |
| 68 | + /// Increase the score of an item in the data structure by increment. |
| 69 | + /// </summary> |
| 70 | + /// <param name="key">Name of sketch where item is added.</param> |
| 71 | + /// <param name="itemIncrements">Tuple of The items which counter is to be increased |
| 72 | + /// and the Amount by which the item score is to be increased.</param> |
| 73 | + /// <returns>Score of each item after increment.</returns> |
| 74 | + /// <remarks><seealso href="https://redis.io/commands/topk.incrby"/></remarks> |
| 75 | + public RedisResult[]? IncrBy(RedisKey key, params Tuple<RedisValue, long>[] itemIncrements) |
| 76 | + { |
| 77 | + if (itemIncrements.Length < 1) |
| 78 | + throw new ArgumentException(nameof(itemIncrements)); |
| 79 | + |
| 80 | + List<object> args = new List<object> { key }; |
| 81 | + foreach (var pair in itemIncrements) |
| 82 | + { |
| 83 | + args.Add(pair.Item1); |
| 84 | + args.Add(pair.Item2); |
| 85 | + } |
| 86 | + return ResponseParser.ToArray(_db.Execute(TOPK.INCRBY, args)); |
| 87 | + } |
| 88 | + |
| 89 | + // //TODO: information about what? |
| 90 | + /// <summary> |
| 91 | + /// Return TopK information. |
| 92 | + /// </summary> |
| 93 | + /// <param name="key">Name of the key to return information about.</param> |
| 94 | + /// <returns>TopK Information.</returns> |
| 95 | + /// <remarks><seealso href="https://redis.io/commands/topk.info"/></remarks> |
| 96 | + public TopKInformation? Info(RedisKey key) |
| 97 | + { |
| 98 | + var info = _db.Execute(TOPK.INFO, key); |
| 99 | + return ResponseParser.ToTopKInfo(info); |
| 100 | + } |
| 101 | + |
| 102 | + /// <summary> |
| 103 | + /// Return full list of items in Top K list. |
| 104 | + /// </summary> |
| 105 | + /// <param name="key">The name of the sketch.</param> |
| 106 | + /// <param name="withcount">return Count of each element is returned.</param> |
| 107 | + /// <returns>Full list of items in Top K list</returns> |
| 108 | + /// <remarks><seealso href="https://redis.io/commands/topk.list"/></remarks> |
| 109 | + public RedisResult[]? List(RedisKey key, bool withcount = false) |
| 110 | + { |
| 111 | + var result = (withcount) ? _db.Execute(TOPK.LIST, key, "WITHCOUNT") |
| 112 | + : _db.Execute(TOPK.LIST, key); |
| 113 | + return ResponseParser.ToArray(result); |
| 114 | + } |
| 115 | + |
| 116 | + /// <summary> |
| 117 | + /// Returns the count for one or more items in a sketch. |
| 118 | + /// </summary> |
| 119 | + /// <param name="key">The name of the sketch</param> |
| 120 | + /// <param name="item">Item to be queried.</param> |
| 121 | + /// <returns><see langword="true"/> if item is in Top-K, <see langword="false"/> otherwise/></returns> |
| 122 | + /// <remarks><seealso href="https://redis.io/commands/topk.query"/></remarks> |
| 123 | + public bool? Query(RedisKey key, RedisValue item) |
| 124 | + { |
| 125 | + return _db.Execute(TOPK.QUERY, key, item).ToString() == "1"; |
| 126 | + } |
| 127 | + |
| 128 | + /// <summary> |
| 129 | + /// Returns the count for one or more items in a sketch. |
| 130 | + /// </summary> |
| 131 | + /// <param name="key">The name of the sketch</param> |
| 132 | + /// <param name="items">Items to be queried.</param> |
| 133 | + /// <returns>Bolean Array where <see langword="true"/> if item is in Top-K, <see langword="false"/> otherwise/></returns> |
| 134 | + /// <remarks><seealso href="https://redis.io/commands/topk.query"/></remarks> |
| 135 | + public bool[]? Query(RedisKey key, params RedisValue[] items) |
| 136 | + { |
| 137 | + if (items.Length < 1) |
| 138 | + throw new ArgumentNullException(nameof(items)); |
| 139 | + |
| 140 | + var args = Auxiliary.MergeArgs(key, items); |
| 141 | + |
| 142 | + return ResponseParser.ToBooleanArray(_db.Execute(TOPK.QUERY, args)); |
| 143 | + } |
| 144 | + |
| 145 | + /// <summary> |
| 146 | + /// Initializes a TopK with specified parameters. |
| 147 | + /// </summary> |
| 148 | + /// <param name="key">Key under which the sketch is to be found.</param> |
| 149 | + /// <param name="topk">Number of top occurring items to keep.</param> |
| 150 | + /// <param name="width">Number of counters kept in each array. (Default 8)</param> |
| 151 | + /// <param name="depth">Number of arrays. (Default 7)</param> |
| 152 | + /// <param name="decay">The probability of reducing a counter in an occupied bucket. (Default 0.9)</param> |
| 153 | + /// <returns><see langword="true"/> if executed correctly, error otherwise/></returns> |
| 154 | + /// <remarks><seealso href="https://redis.io/commands/topk.reserve"/></remarks> |
| 155 | + public bool? Reserve(RedisKey key, long topk, long width = 7, long depth = 8, double decay = 0.9) |
| 156 | + { |
| 157 | + return ResponseParser.ParseOKtoBoolean(_db.Execute(TOPK.RESERVE, key, topk, width, depth, decay)); |
| 158 | + } |
| 159 | + } |
| 160 | +} |
0 commit comments