-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathcuckoo_commands.rs
245 lines (196 loc) · 6.6 KB
/
cuckoo_commands.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
use crate::{
commands::{
CfInsertOptions, CfReserveOptions, CfScanDumpResult, CuckooCommands, FlushingMode,
ServerCommands, StringCommands,
},
tests::get_redis_stack_test_client,
Error, RedisError, RedisErrorKind, Result,
};
use serial_test::serial;
use std::collections::VecDeque;
#[cfg_attr(feature = "tokio-runtime", tokio::test)]
#[cfg_attr(feature = "async-std-runtime", async_std::test)]
#[serial]
async fn cf_add() -> Result<()> {
let client = get_redis_stack_test_client().await?;
client.flushall(FlushingMode::Sync).await?;
client.cf_add("key", "item1").await?;
// Cuckoo filters can contain the same item multiple times
client.cf_add("key", "item1").await?;
client.cf_add("key", "item2").await?;
client.set("key", "item").await?;
let result = client.cf_add("key", "item").await;
assert!(matches!(
result,
Err(Error::Redis(RedisError {
kind: RedisErrorKind::WrongType,
description: _
}))
));
Ok(())
}
#[cfg_attr(feature = "tokio-runtime", tokio::test)]
#[cfg_attr(feature = "async-std-runtime", async_std::test)]
#[serial]
async fn cf_addnx() -> Result<()> {
let client = get_redis_stack_test_client().await?;
client.flushall(FlushingMode::Sync).await?;
let result = client.cf_addnx("key", "item").await?;
assert!(result);
let result = client.cf_addnx("key", "item").await?;
assert!(!result);
Ok(())
}
#[cfg_attr(feature = "tokio-runtime", tokio::test)]
#[cfg_attr(feature = "async-std-runtime", async_std::test)]
#[serial]
async fn cf_count() -> Result<()> {
let client = get_redis_stack_test_client().await?;
client.flushall(FlushingMode::Sync).await?;
client.cf_add("key", "item1").await?;
client.cf_add("key", "item1").await?;
client.cf_add("key", "item1").await?;
let count = client.cf_count("key", "item1").await?;
assert_eq!(3, count);
Ok(())
}
#[cfg_attr(feature = "tokio-runtime", tokio::test)]
#[cfg_attr(feature = "async-std-runtime", async_std::test)]
#[serial]
async fn cf_del() -> Result<()> {
let client = get_redis_stack_test_client().await?;
client.flushall(FlushingMode::Sync).await?;
client.cf_add("key", "item1").await?;
client.cf_add("key", "item1").await?;
client.cf_add("key", "item1").await?;
let deleted = client.cf_del("key", "item1").await?;
assert!(deleted);
let deleted = client.cf_del("key", "item1").await?;
assert!(deleted);
let deleted = client.cf_del("key", "item1").await?;
assert!(deleted);
let deleted = client.cf_del("key", "item1").await?;
assert!(!deleted);
Ok(())
}
#[cfg_attr(feature = "tokio-runtime", tokio::test)]
#[cfg_attr(feature = "async-std-runtime", async_std::test)]
#[serial]
async fn cf_exists() -> Result<()> {
let client = get_redis_stack_test_client().await?;
client.flushall(FlushingMode::Sync).await?;
let exists = client.cf_exists("key", "item1").await?;
assert!(!exists);
client.cf_add("key", "item1").await?;
let exists = client.cf_exists("key", "item1").await?;
assert!(exists);
Ok(())
}
#[cfg_attr(feature = "tokio-runtime", tokio::test)]
#[cfg_attr(feature = "async-std-runtime", async_std::test)]
#[serial]
async fn cf_info() -> Result<()> {
let client = get_redis_stack_test_client().await?;
client.flushall(FlushingMode::Sync).await?;
client.cf_add("key", "item1").await?;
client.cf_add("key", "item2").await?;
client.cf_add("key", "item3").await?;
client.cf_add("key", "item4").await?;
client.cf_del("key", "item1").await?;
let info = client.cf_info("key").await?;
log::debug!("info: {info:?}");
assert_eq!(3, info.num_items_inserted);
assert_eq!(1, info.num_items_deleted);
Ok(())
}
#[cfg_attr(feature = "tokio-runtime", tokio::test)]
#[cfg_attr(feature = "async-std-runtime", async_std::test)]
#[serial]
async fn cf_insert() -> Result<()> {
let client = get_redis_stack_test_client().await?;
client.flushall(FlushingMode::Sync).await?;
client
.cf_insert(
"key",
CfInsertOptions::default().capacity(2048),
["item1", "item2", "item3"],
)
.await?;
let result = client
.cf_insert(
"key2",
CfInsertOptions::default().nocreate(),
["item1", "item2", "item3"],
)
.await;
assert!(result.is_err());
Ok(())
}
#[cfg_attr(feature = "tokio-runtime", tokio::test)]
#[cfg_attr(feature = "async-std-runtime", async_std::test)]
#[serial]
async fn cf_insertnx() -> Result<()> {
let client = get_redis_stack_test_client().await?;
client.flushall(FlushingMode::Sync).await?;
let results: Vec<i64> = client
.cf_insertnx(
"key",
CfInsertOptions::default(),
["item1", "item2", "item3"],
)
.await?;
assert_eq!(vec![1, 1, 1], results);
let results: Vec<i64> = client
.cf_insertnx(
"key",
CfInsertOptions::default(),
["item3", "item4", "item5"],
)
.await?;
assert_eq!(vec![0, 1, 1], results);
Ok(())
}
#[cfg_attr(feature = "tokio-runtime", tokio::test)]
#[cfg_attr(feature = "async-std-runtime", async_std::test)]
#[serial]
async fn cf_reserve_loadchunk_scandump() -> Result<()> {
let client = get_redis_stack_test_client().await?;
client.flushall(FlushingMode::Sync).await?;
client
.cf_reserve("cf", 10, CfReserveOptions::default())
.await?;
client.cf_add("cf", "item1").await?;
let mut iterator: i64 = 0;
let mut chunks: VecDeque<CfScanDumpResult> = VecDeque::new();
loop {
let result = client.cf_scandump("cf", iterator).await?;
if result.iterator == 0 {
break;
} else {
iterator = result.iterator;
chunks.push_back(result);
}
}
client.flushall(FlushingMode::Sync).await?;
while let Some(dump) = chunks.pop_front() {
client.cf_loadchunk("cf", dump.iterator, dump.data).await?;
}
let result = client.cf_exists("cf", "item1").await?;
assert!(result);
Ok(())
}
#[cfg_attr(feature = "tokio-runtime", tokio::test)]
#[cfg_attr(feature = "async-std-runtime", async_std::test)]
#[serial]
async fn cf_mexists() -> Result<()> {
let client = get_redis_stack_test_client().await?;
client.flushall(FlushingMode::Sync).await?;
client
.cf_insert("filter", CfInsertOptions::default(), ["item1", "item2"])
.await?;
let results: [bool; 3] = client
.cf_mexists("filter", ["item1", "item2", "item3"])
.await?;
assert_eq!([true, true, false], results);
Ok(())
}