1
+ using NRedisStack . Core . Bloom . DataTypes ;
1
2
using NRedisStack . Core . Literals ;
2
3
using StackExchange . Redis ;
3
4
namespace NRedisStack . Core
@@ -11,24 +12,62 @@ public BloomCommands(IDatabase db)
11
12
_db = db ;
12
13
}
13
14
14
- public RedisResult Add ( RedisKey key , string item )
15
+ /// <summary>
16
+ /// Adds an item to a Bloom 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/bf.add"/></remarks>
22
+ public bool Add ( RedisKey key , RedisValue item )
15
23
{
16
- return _db . Execute ( BF . ADD , key , item ) ;
24
+ return _db . Execute ( BF . ADD , key , item ) . ToString ( ) == "1" ;
17
25
}
18
26
19
- public bool Exists ( RedisKey key , string item )
27
+ /// <summary>
28
+ /// Checks whether an item exist in the Bloom Filter or not.
29
+ /// </summary>
30
+ /// <param name="key">The name of the filter.</param>
31
+ /// <param name="item">The item to check for.</param>
32
+ /// <returns><see langword="true"/> means the item may exist in the filter,
33
+ /// and <see langword="false"/> means the item may exist in the filter.</returns>
34
+ /// <remarks><seealso href="https://redis.io/commands/bf.exists"/></remarks>
35
+ public bool Exists ( RedisKey key , RedisValue item )
20
36
{
21
37
return _db . Execute ( BF . EXISTS , key , item ) . ToString ( ) == "1" ;
22
38
}
23
39
24
- public RedisResult Info ( RedisKey key )
40
+ /// <summary>
41
+ /// Return information about a bloom filter.
42
+ /// </summary>
43
+ /// <param name="key">Name of the key to return information about.</param>
44
+ /// <returns>Array with information of the filter.</returns>
45
+ /// <remarks><seealso href="https://redis.io/commands/bf.info"/></remarks>
46
+ public BloomInformation ? Info ( RedisKey key )
25
47
{
26
- return _db . Execute ( BF . INFO , key ) ;
48
+ var info = _db . Execute ( BF . INFO , key ) ;
49
+ return ResponseParser . ToBloomInfo ( info ) ;
27
50
}
28
51
29
- public RedisResult Insert ( RedisKey key , RedisValue [ ] items , int ? capacity = null ,
52
+ /// <summary>
53
+ /// Adds one or more items to a Bloom Filter. A filter will be created if it does not exist.
54
+ /// </summary>
55
+ /// <param name="key">The name of the filter.</param>
56
+ /// <param name="items">One or more items to add.</param>
57
+ /// <param name="capacity">(Optional) Specifies the desired capacity for the filter to be created.</param>
58
+ /// <param name="error">(Optional) Specifies the error ratio of the newly created filter if it does not yet exist.</param>
59
+ /// <param name="expansion">(Optional) When capacity is reached, an additional sub-filter is
60
+ /// created in size of the last sub-filter multiplied by expansion.</param>
61
+ /// <param name="nocreate">(Optional) <see langword="true"/> to indicates that the
62
+ /// filter should not be created if it does not already exist.</param>
63
+ /// <param name="nonscaling">(Optional) <see langword="true"/> toprevent the filter
64
+ /// from creating additional sub-filters if initial capacity is reached.</param>
65
+ /// <returns>An array of booleans. Each element is either true or false depending on whether the
66
+ /// corresponding input element was newly added to the filter or may have previously existed.</returns>
67
+ /// <remarks><seealso href="https://redis.io/commands/bf.insert"/></remarks>
68
+ public bool [ ] Insert ( RedisKey key , RedisValue [ ] items , int ? capacity = null ,
30
69
double ? error = null , int ? expansion = null ,
31
- bool nocreate = false , bool nonscaling = false ) //NOT DONE
70
+ bool nocreate = false , bool nonscaling = false )
32
71
{
33
72
if ( items == null )
34
73
throw new ArgumentNullException ( nameof ( items ) ) ;
@@ -54,29 +93,125 @@ public RedisResult Insert(RedisKey key, RedisValue[] items, int? capacity = null
54
93
}
55
94
56
95
if ( nocreate )
96
+ {
57
97
args . Add ( BloomArgs . NOCREATE ) ;
58
98
99
+ }
100
+
59
101
if ( nonscaling )
102
+ {
60
103
args . Add ( BloomArgs . NONSCALING ) ;
104
+ }
61
105
62
106
args . Add ( BloomArgs . ITEMS ) ;
63
107
foreach ( var item in items )
64
108
{
65
109
args . Add ( item ) ;
66
110
}
67
111
68
- return _db . Execute ( BF . INSERT , args ) ;
112
+ return ResponseParser . ToBooleanArray ( _db . Execute ( BF . INSERT , args ) ) ;
69
113
}
70
114
71
- public RedisResult ScanDump ( RedisKey key , int iterator )
115
+ /// <summary>
116
+ /// Restores a filter previosly saved using SCANDUMP.
117
+ /// </summary>
118
+ /// <param name="key">Name of the key to restore.</param>
119
+ /// <param name="iterator">Iterator value associated with data (returned by SCANDUMP).</param>
120
+ /// <param name="data">Current data chunk (returned by SCANDUMP).</param>
121
+ /// <returns>Array with information of the filter.</returns>
122
+ /// <remarks><seealso href="https://redis.io/commands/bf.loadchunk"/></remarks>
123
+ public bool LoadChunk ( RedisKey key , long iterator , Byte [ ] data )
72
124
{
73
- return _db . Execute ( BF . SCANDUMP , key , iterator ) ;
125
+ return ResponseParser . ParseOKtoBoolean ( _db . Execute ( BF . LOADCHUNK , key , iterator , data ) ) ;
74
126
}
75
127
76
- public RedisResult LoadChunk ( RedisKey key , int iterator , RedisValue data )
128
+ /// <summary>
129
+ /// Adds one or more items to the Bloom Filter. A filter will be created if it does not exist yet.
130
+ /// </summary>
131
+ /// <param name="key">The name of the filter.</param>
132
+ /// <param name="items">One or more items to add.</param>
133
+ /// <returns>An array of booleans. Each element is either true or false depending on whether the
134
+ /// corresponding input element was newly added to the filter or may have previously existed.</returns>
135
+ /// <remarks><seealso href="https://redis.io/commands/bf.madd"/></remarks>
136
+ public bool [ ] MAdd ( RedisKey key , RedisValue [ ] items )
77
137
{
78
- return _db . Execute ( BF . LOADCHUNK , key , iterator , data ) ;
138
+ if ( items == null )
139
+ throw new ArgumentNullException ( nameof ( items ) ) ;
140
+
141
+ List < object > args = new List < object > { key } ;
142
+
143
+ foreach ( var item in items )
144
+ {
145
+ args . Add ( item ) ;
146
+ }
147
+
148
+ return ResponseParser . ToBooleanArray ( _db . Execute ( BF . MADD , args ) ) ;
79
149
}
80
150
151
+ /// <summary>
152
+ /// Checks whether one or more items may exist in the filter or not.
153
+ /// </summary>
154
+ /// <param name="key">The name of the filter.</param>
155
+ /// <param name="items">One or more items to check.</param>
156
+ /// <returns>An array of booleans, for each item <see langword="true"/> means the item may exist in the filter,
157
+ /// and <see langword="false"/> means the item may exist in the filter.</returns>
158
+ /// <remarks><seealso href="https://redis.io/commands/bf.mexists"/></remarks>
159
+ public bool [ ] MExists ( RedisKey key , RedisValue [ ] items )
160
+ {
161
+ if ( items == null )
162
+ throw new ArgumentNullException ( nameof ( items ) ) ;
163
+
164
+ List < object > args = new List < object > { key } ;
165
+
166
+ foreach ( var item in items )
167
+ {
168
+ args . Add ( item ) ;
169
+ }
170
+
171
+ return ResponseParser . ToBooleanArray ( _db . Execute ( BF . MEXISTS , args ) ) ;
172
+
173
+ }
174
+
175
+ /// <summary>
176
+ /// Creates a new Bloom Filter.
177
+ /// </summary>
178
+ /// <param name="key">The key under which the filter is found.</param>
179
+ /// <param name="errorRate">The desired probability for false positives (value between 0 to 1).</param>
180
+ /// <param name="capacity">The number of entries intended to be added to the filter.</param>
181
+ /// <param name="expansion">(Optional) When capacity is reached, an additional sub-filter is
182
+ /// created in size of the last sub-filter multiplied by expansion.</param>
183
+ /// <param name="nonscaling">(Optional) <see langword="true"/> toprevent the filter
184
+ /// from creating additional sub-filters if initial capacity is reached.</param>
185
+ /// <returns><see langword="true"/> if executed correctly, <see langword="false"/> otherwise.</returns>
186
+ /// <remarks><seealso href="https://redis.io/commands/bf.reserve"/></remarks>
187
+ public bool Reserve ( RedisKey key , double errorRate , long capacity ,
188
+ int ? expansion = null , bool nonscaling = false )
189
+ {
190
+ List < object > args = new List < object > { key , errorRate , capacity } ;
191
+
192
+ if ( expansion != null )
193
+ {
194
+ args . Add ( expansion ) ;
195
+ }
196
+
197
+ if ( nonscaling )
198
+ {
199
+ args . Add ( BloomArgs . NONSCALING ) ;
200
+ }
201
+
202
+ return ResponseParser . ParseOKtoBoolean ( _db . Execute ( BF . RESERVE , args ) ) ;
203
+ }
204
+
205
+ /// <summary>
206
+ /// Restores a filter previosly saved using SCANDUMP.
207
+ /// </summary>
208
+ /// <param name="key">Name of the filter.</param>
209
+ /// <param name="iterator">Iterator value; either 0 or the iterator from a previous invocation of this command.</param>
210
+ /// <returns>Tuple of iterator and data.</returns>
211
+ /// <remarks><seealso href="https://redis.io/commands/bf.scandump"/></remarks>
212
+ public Tuple < long , Byte [ ] > ? ScanDump ( RedisKey key , long iterator )
213
+ {
214
+ return ResponseParser . ToScanDumpTuple ( _db . Execute ( BF . SCANDUMP , key , iterator ) ) ;
215
+ }
81
216
}
82
217
}
0 commit comments