-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtypes.rs
More file actions
211 lines (190 loc) · 5.79 KB
/
Copy pathtypes.rs
File metadata and controls
211 lines (190 loc) · 5.79 KB
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
pub type Result<T> = std::result::Result<T, RdbError>;
#[derive(Debug, thiserror::Error)]
pub enum RdbError {
#[error("parser error (code {code}): {message}")]
Parser { code: u32, message: String },
#[error("{0}")]
Handler(Box<dyn std::error::Error + Send + Sync>),
}
impl RdbError {
pub fn handler(e: impl std::error::Error + Send + Sync + 'static) -> Self {
Self::Handler(Box::new(e))
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive] // RDB gains data types across versions (e.g. Array in v14); keep this open.
pub enum DataType {
String,
List,
Set,
Zset,
Hash,
Module,
Stream,
Function,
/// Sparse array (RDB v14+, `RDB_TYPE_ARRAY`).
Array,
}
impl DataType {
pub(crate) fn from_raw(value: i32) -> Result<Self> {
#![allow(non_upper_case_globals)]
use librdb_sys::{
RdbDataType_RDB_DATA_TYPE_ARRAY, RdbDataType_RDB_DATA_TYPE_FUNCTION,
RdbDataType_RDB_DATA_TYPE_HASH, RdbDataType_RDB_DATA_TYPE_LIST,
RdbDataType_RDB_DATA_TYPE_MODULE, RdbDataType_RDB_DATA_TYPE_SET,
RdbDataType_RDB_DATA_TYPE_STREAM, RdbDataType_RDB_DATA_TYPE_STRING,
RdbDataType_RDB_DATA_TYPE_ZSET,
};
#[allow(clippy::cast_sign_loss)]
match value as u32 {
RdbDataType_RDB_DATA_TYPE_STRING => Ok(Self::String),
RdbDataType_RDB_DATA_TYPE_LIST => Ok(Self::List),
RdbDataType_RDB_DATA_TYPE_SET => Ok(Self::Set),
RdbDataType_RDB_DATA_TYPE_ZSET => Ok(Self::Zset),
RdbDataType_RDB_DATA_TYPE_HASH => Ok(Self::Hash),
RdbDataType_RDB_DATA_TYPE_MODULE => Ok(Self::Module),
RdbDataType_RDB_DATA_TYPE_STREAM => Ok(Self::Stream),
RdbDataType_RDB_DATA_TYPE_FUNCTION => Ok(Self::Function),
RdbDataType_RDB_DATA_TYPE_ARRAY => Ok(Self::Array),
_ => Err(RdbError::Parser {
code: 0,
message: format!("unknown data type: {value}"),
}),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct KeyInfo {
/// Unix timestamp in milliseconds, or `None` if no expiry is set.
pub expire_time_ms: Option<i64>,
/// LRU idle seconds, or `None` if not available.
pub lru_idle: Option<i64>,
/// LFU frequency counter, or `None` if not available.
pub lfu_freq: Option<i32>,
pub data_type: DataType,
}
impl KeyInfo {
pub(crate) fn from_raw(raw: &librdb_sys::RdbKeyInfo) -> Result<Self> {
Ok(Self {
expire_time_ms: if raw.expiretime == -1 {
None
} else {
Some(raw.expiretime)
},
lru_idle: if raw.lruIdle == -1 {
None
} else {
Some(raw.lruIdle)
},
lfu_freq: if raw.lfuFreq == -1 {
None
} else {
Some(raw.lfuFreq)
},
data_type: DataType::from_raw(raw.dataType)?,
})
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct SlotInfo {
pub slot_id: u64,
pub slot_size: u64,
pub expires_slot_size: u64,
}
impl SlotInfo {
pub(crate) const fn from_raw(raw: &librdb_sys::RdbSlotInfo) -> Self {
Self {
slot_id: raw.slot_id,
slot_size: raw.slot_size,
expires_slot_size: raw.expires_slot_size,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct StreamId {
pub ms: u64,
pub seq: u64,
}
impl StreamId {
pub(crate) const fn from_raw(raw: &librdb_sys::RdbStreamID) -> Self {
Self {
ms: raw.ms,
seq: raw.seq,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct StreamMeta {
pub length: u64,
pub entries_added: u64,
pub first_id: StreamId,
pub last_id: StreamId,
pub max_del_entry_id: StreamId,
}
impl StreamMeta {
pub(crate) const fn from_raw(raw: &librdb_sys::RdbStreamMeta) -> Self {
Self {
length: raw.length,
entries_added: raw.entriesAdded,
first_id: StreamId::from_raw(&raw.firstID),
last_id: StreamId::from_raw(&raw.lastID),
max_del_entry_id: StreamId::from_raw(&raw.maxDelEntryID),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct StreamGroupMeta {
pub last_id: StreamId,
pub entries_read: i64,
}
impl StreamGroupMeta {
pub(crate) const fn from_raw(raw: &librdb_sys::RdbStreamGroupMeta) -> Self {
Self {
last_id: StreamId::from_raw(&raw.lastId),
entries_read: raw.entriesRead,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct StreamConsumerMeta {
pub active_time: i64,
pub seen_time: i64,
}
impl StreamConsumerMeta {
pub(crate) const fn from_raw(raw: &librdb_sys::RdbStreamConsumerMeta) -> Self {
Self {
active_time: raw.activeTime,
seen_time: raw.seenTime,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct StreamPendingEntry {
pub id: StreamId,
pub delivery_time: u64,
pub delivery_count: u64,
}
impl StreamPendingEntry {
pub(crate) const fn from_raw(raw: &librdb_sys::RdbStreamPendingEntry) -> Self {
Self {
id: StreamId::from_raw(&raw.id),
delivery_time: raw.deliveryTime,
delivery_count: raw.deliveryCount,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct StreamIdmpMeta {
pub duration: u64,
pub max_entries: u64,
pub num_producers: u64,
}
impl StreamIdmpMeta {
pub(crate) const fn from_raw(raw: &librdb_sys::RdbStreamIdmpMeta) -> Self {
Self {
duration: raw.duration,
max_entries: raw.maxEntries,
num_producers: raw.numProducers,
}
}
}