-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathcommand_info_manager.rs
299 lines (268 loc) · 11 KB
/
command_info_manager.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
use crate::{
commands::{BeginSearch, CommandInfo, FindKeys, ServerCommands},
network::Version,
resp::{cmd, Command, CommandArgs},
Error, Result, StandaloneConnection,
};
use smallvec::SmallVec;
use std::collections::HashMap;
pub(crate) struct CommandInfoManager {
command_info_map: HashMap<String, CommandInfo>,
legacy: bool,
}
impl CommandInfoManager {
pub async fn initialize(connection: &mut StandaloneConnection) -> Result<CommandInfoManager> {
let mut command_info_result = connection.command().await?;
let sub_commands = command_info_result
.iter()
.filter_map(|c| {
if c.sub_commands.is_empty() {
None
} else {
Some(c.sub_commands.clone())
}
})
.flatten()
.collect::<Vec<_>>();
command_info_result.extend(sub_commands);
let version: Version = connection.get_version().try_into()?;
Ok(CommandInfoManager {
command_info_map: command_info_result
.into_iter()
.map(|mut c| {
c.name = c.name.to_uppercase();
(c.name.to_string(), c)
})
.collect(),
legacy: version.major < 7,
})
}
pub fn get_command_info_by_name(&self, command_name: &str) -> Option<&CommandInfo> {
self.command_info_map.get(command_name)
}
pub fn get_command_info(&self, command: &Command) -> Option<&CommandInfo> {
let command_info = self.command_info_map.get(command.name);
if let Some(command_info) = command_info {
if command_info.arity == -2 && !command_info.sub_commands.is_empty() {
if let Some(first_arg) = command.args.into_iter().next() {
if let Ok(first_arg) = std::str::from_utf8(first_arg) {
let command_name = format!("{}|{}", command.name, first_arg);
return self.command_info_map.get(&command_name);
}
}
}
}
command_info
}
/// see <https://redis.io/docs/reference/key-specs/>
pub async fn extract_keys(
&self,
command: &Command,
connection: &mut StandaloneConnection,
) -> Result<SmallVec<[String; 10]>> {
let command_info = if let Some(command_info) = self.command_info_map.get(command.name) {
command_info
} else {
return Err(Error::Client(format!("Unknown command {}", command.name)));
};
if self.legacy {
if command_info.first_key == 0 || command_info.last_key == 0 {
return Ok(SmallVec::new());
} else if command_info.flags.iter().any(|f| f == "movablekeys") {
let args = Self::prepare_command_getkeys_args(command);
let keys: SmallVec<[String; 10]> = connection.command_getkeys(args).await?;
return Ok(keys);
} else {
let mut slice: &[Vec<u8>] = &command.args[command_info.first_key - 1..];
let stop_index = if command_info.last_key >= 0 {
command_info.last_key as usize
} else {
slice.len() - (-command_info.last_key as usize) + 1
};
slice = &slice[..stop_index];
let keys = slice
.iter()
.step_by(command_info.step)
.filter_map(|bs| {
if bs.is_empty() {
None
} else {
String::from_utf8(bs.clone()).ok()
}
})
.collect();
return Ok(keys);
}
}
let mut keys = SmallVec::<[String; 10]>::new();
for key_spec in &command_info.key_specifications {
let mut slice: &[Vec<u8>] = &command.args;
// begin_search
match &key_spec.begin_search {
BeginSearch::Index(i) => slice = &slice[*i - 1..],
BeginSearch::Keyword {
keyword,
start_from,
} => {
let start_index = if *start_from >= 0 {
slice
.iter()
.skip(*start_from as usize - 1)
.position(|arg| arg.as_slice() == keyword.as_bytes())
.map(|i| i + *start_from as usize)
} else {
slice
.iter()
.rev()
.skip((-*start_from - 1) as usize)
.position(|arg| arg.as_slice() == keyword.as_bytes())
.map(|i| slice.len() - (i + -start_from as usize - 1))
};
if let Some(start_index) = start_index {
slice = &slice[start_index..];
} else {
return Err(Error::Client(format!(
"Cannot find keyword {} in {:?}",
*keyword, command
)));
}
}
BeginSearch::Unknown => {
let args = Self::prepare_command_getkeys_args(command);
let keys: SmallVec<[String; 10]> = connection.command_getkeys(args).await?;
return Ok(keys);
}
}
// find_keys
let key_step = match &key_spec.find_keys {
FindKeys::Range {
last_key,
key_step,
limit,
} => {
let stop_index = if *last_key >= 0 {
*last_key as usize
} else if *last_key == -1 && *limit >= 2 {
slice.len() / limit - 1
} else {
slice.len() - (-*last_key as usize)
};
slice = &slice[..stop_index + 1];
*key_step
}
FindKeys::KeyNum {
key_num_idx,
first_key,
key_step,
} => {
let num_keys = slice[*key_num_idx].as_slice();
let num_keys: usize = atoi::atoi(num_keys).ok_or_else(|| {
Error::Client(format!(
"Cannot parse integer from {}",
String::from_utf8_lossy(num_keys)
))
})?;
slice = &slice[*first_key..num_keys + 1];
*key_step
}
FindKeys::Unknown {} => {
let args = Self::prepare_command_getkeys_args(command);
let keys: SmallVec<[String; 10]> = connection.command_getkeys(args).await?;
return Ok(keys);
}
};
keys.extend(slice.iter().step_by(key_step).filter_map(|bs| {
if bs.is_empty() {
None
} else {
String::from_utf8(bs.clone()).ok()
}
}));
}
Ok(keys)
}
/// Function used for commands which RequestPolicy is MultiShard
/// Its purpose consists in building a command for a specific shard,
/// based on the original command provided by the user.
/// Redis 7 official commands offer multi shard commands only in the context of
/// BeginSearch::Index & FindKeys::Range with a single key specification.
/// We will only support this configuration for the first implementation
pub fn prepare_command_for_shard<'a>(
&self,
command: &Command,
shard_keys: impl Iterator<Item = &'a String>,
) -> Result<Command> {
let command_info = if let Some(command_info) = self.command_info_map.get(command.name) {
command_info
} else {
return Err(Error::Client(format!("Unknown command {}", command.name)));
};
if let Some(key_spec) = command_info.key_specifications.first() {
let slice: &[Vec<u8>] = &command.args;
let mut shard_command = cmd(command.name);
// begin_search
let keys_start_index = match &key_spec.begin_search {
BeginSearch::Index(i) => *i - 1,
BeginSearch::Keyword {
keyword: _,
start_from: _,
} => todo!("Command not yet supported, ask for it !"),
BeginSearch::Unknown => todo!("Command not yet supported, ask for it !"),
};
// find_keys
let (keys_end_index, key_step) = match &key_spec.find_keys {
FindKeys::Range {
last_key,
key_step,
limit,
} => {
let keys_end_index = if *last_key >= 0 {
*last_key as usize
} else if *last_key == -1 && *limit >= 2 {
slice.len() / limit - 1
} else {
slice.len() - (-*last_key as usize)
} + keys_start_index;
(keys_end_index, key_step)
}
FindKeys::KeyNum {
key_num_idx: _,
first_key: _,
key_step: _,
} => todo!("Command not yet supported, ask for it !"),
FindKeys::Unknown {} => todo!("Command not yet supported, ask for it !"),
};
if keys_start_index > 0 {
for arg in &slice[..keys_start_index + 1] {
shard_command = shard_command.arg(arg.clone());
}
}
for shard_key in shard_keys {
let key_index = if let Some(key_index) = slice
.iter()
.position(|arg| arg.as_slice() == shard_key.as_bytes())
{
key_index
} else {
return Err(Error::Client(format!("Cannot find key {}", *shard_key)));
};
for key in &slice[key_index..key_index + key_step] {
shard_command = shard_command.arg(key.clone());
}
}
if keys_end_index < command.args.len() - 1 {
for arg in &slice[keys_end_index..] {
shard_command = shard_command.arg(arg.clone());
}
}
return Ok(shard_command);
}
unreachable!();
}
fn prepare_command_getkeys_args(command: &Command) -> CommandArgs {
let mut args = CommandArgs::default();
args.arg(command.name);
args.arg(&command.args);
args
}
}