forked from RedisJSON/RedisJSON
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackward.rs
74 lines (71 loc) · 2.29 KB
/
backward.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
use redis_module::raw;
use serde_json::map::Map;
use serde_json::Number;
use serde_json::Value;
use std::vec::Vec;
#[derive(Debug, PartialEq)]
enum NodeType {
Null, // used in masks and consistent type checking
String,
Number,
Integer,
Boolean,
Dict,
Array,
KeyVal,
// N_DATETIME = 0x100
// N_BINARY = 0x200
}
impl From<u64> for NodeType {
fn from(n: u64) -> Self {
match n {
0x1u64 => NodeType::Null,
0x2u64 => NodeType::String,
0x4u64 => NodeType::Number,
0x8u64 => NodeType::Integer,
0x10u64 => NodeType::Boolean,
0x20u64 => NodeType::Dict,
0x40u64 => NodeType::Array,
0x80u64 => NodeType::KeyVal,
_ => panic!("Can't load old RedisJSON RDB1"),
}
}
}
pub fn json_rdb_load(rdb: *mut raw::RedisModuleIO) -> Value {
let node_type = raw::load_unsigned(rdb).into();
match node_type {
NodeType::Null => Value::Null,
NodeType::Boolean => {
let buffer = raw::load_string_buffer(rdb);
Value::Bool(buffer.as_ref()[0] == b'1')
}
NodeType::Integer => Value::Number(raw::load_signed(rdb).into()),
NodeType::Number => Value::Number(Number::from_f64(raw::load_double(rdb)).unwrap()),
NodeType::String => {
let buffer = raw::load_string_buffer(rdb);
Value::String(buffer.to_string().unwrap())
}
NodeType::Dict => {
let len = raw::load_unsigned(rdb);
let mut m = Map::with_capacity(len as usize);
for _ in 0..len {
let t: NodeType = raw::load_unsigned(rdb).into();
if t != NodeType::KeyVal {
panic!("Can't load old RedisJSON RDB");
}
let buffer = raw::load_string_buffer(rdb);
m.insert(buffer.to_string().unwrap(), json_rdb_load(rdb));
}
Value::Object(m)
}
NodeType::Array => {
let len = raw::load_unsigned(rdb);
let mut v = Vec::with_capacity(len as usize);
for _ in 0..len {
v.push(json_rdb_load(rdb))
}
Value::Array(v)
}
NodeType::KeyVal => panic!("Can't load old RedisJSON RDB"),
}
}