-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathformatter.rs
More file actions
201 lines (176 loc) · 7.08 KB
/
Copy pathformatter.rs
File metadata and controls
201 lines (176 loc) · 7.08 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
use once_cell::sync::Lazy;
use prost::Message as PMessage;
use pyo3::{prelude::*, types::PyBytes, PyObject, Python};
use raftify::raft::{
eraftpb::{ConfChange, ConfChangeV2},
formatter::{format_confchange, format_confchangev2, Bytes, CustomFormatter},
};
use std::sync::Mutex;
pub struct PythonFormatter;
// TODO: Refactor below codes to reduce code redundancy.
static ENTRY_CONTEXT_DESERIALIZE_CB: Lazy<Mutex<Option<PyObject>>> = Lazy::new(|| Mutex::new(None));
static ENTRY_DATA_DESERIALIZE_CB: Lazy<Mutex<Option<PyObject>>> = Lazy::new(|| Mutex::new(None));
static CONFCHANGEV2_CONTEXT_DESERIALIZE_CB: Lazy<Mutex<Option<PyObject>>> =
Lazy::new(|| Mutex::new(None));
static CONFCHANGE_CONTEXT_DESERIALIZE_CB: Lazy<Mutex<Option<PyObject>>> =
Lazy::new(|| Mutex::new(None));
static MESSAGE_CONTEXT_DESERIALIZER_CB: Lazy<Mutex<Option<PyObject>>> =
Lazy::new(|| Mutex::new(None));
static SNAPSHOT_DATA_DESERIALIZER_CB: Lazy<Mutex<Option<PyObject>>> =
Lazy::new(|| Mutex::new(None));
pub static ENTRY_LOG_ENTRY_DESERIALIZE_CB: Lazy<Mutex<Option<PyObject>>> =
Lazy::new(|| Mutex::new(None));
pub static ENTRY_FSM_DESERIALIZE_CB: Lazy<Mutex<Option<PyObject>>> = Lazy::new(|| Mutex::new(None));
#[pyfunction]
pub fn set_custom_formatters(
entry_context: Option<PyObject>,
entry_data: Option<PyObject>,
confchangev2_context: Option<PyObject>,
confchange_context: Option<PyObject>,
message_context: Option<PyObject>,
snapshot_data: Option<PyObject>,
log_entry: Option<PyObject>,
fsm: Option<PyObject>,
) {
if let Some(cb) = entry_context {
*ENTRY_CONTEXT_DESERIALIZE_CB.lock().unwrap() = Some(cb);
}
if let Some(cb) = entry_data {
*ENTRY_DATA_DESERIALIZE_CB.lock().unwrap() = Some(cb);
}
if let Some(cb) = confchangev2_context {
*CONFCHANGEV2_CONTEXT_DESERIALIZE_CB.lock().unwrap() = Some(cb);
}
if let Some(cb) = confchange_context {
*CONFCHANGE_CONTEXT_DESERIALIZE_CB.lock().unwrap() = Some(cb);
}
if let Some(cb) = message_context {
*MESSAGE_CONTEXT_DESERIALIZER_CB.lock().unwrap() = Some(cb);
}
if let Some(cb) = snapshot_data {
*SNAPSHOT_DATA_DESERIALIZER_CB.lock().unwrap() = Some(cb);
}
if let Some(cb) = log_entry {
*ENTRY_LOG_ENTRY_DESERIALIZE_CB.lock().unwrap() = Some(cb);
}
if let Some(cb) = fsm {
*ENTRY_FSM_DESERIALIZE_CB.lock().unwrap() = Some(cb);
}
}
impl CustomFormatter for PythonFormatter {
fn format_entry_context(&self, v: &Bytes) -> String {
fn deserialize(py: Python, data: &[u8]) -> String {
let callback_lock = ENTRY_CONTEXT_DESERIALIZE_CB.lock().unwrap();
if let Some(callback) = &*callback_lock {
callback
.call(py, (PyBytes::new(py, data),), None)
.unwrap()
.as_ref(py)
.to_string()
} else {
format!("{:?}", data)
}
}
Python::with_gil(|py| match v {
Bytes::Prost(v) => format!("{:?}", deserialize(py, &v[..])),
Bytes::Protobuf(v) => format!("{:?}", deserialize(py, &v[..])),
})
}
fn format_entry_data(&self, v: &Bytes) -> String {
fn deserialize(py: Python, data: &[u8]) -> String {
let callback_lock = ENTRY_DATA_DESERIALIZE_CB.lock().unwrap();
if let Some(callback) = &*callback_lock {
if data.len() != 0 {
let res = callback.call(py, (PyBytes::new(py, data),), None).unwrap();
let res = res.as_ref(py);
if !res.is_none() {
return res.to_string();
}
if let Ok(cc) = ConfChange::decode(data) {
return format_confchange(&cc);
}
if let Ok(cc) = ConfChangeV2::decode(data) {
return format_confchangev2(&cc);
}
}
}
format!("{:?}", data)
}
Python::with_gil(|py| match v {
Bytes::Prost(v) => format!("{:?}", deserialize(py, &v[..])),
Bytes::Protobuf(v) => format!("{:?}", deserialize(py, &v[..])),
})
}
fn format_confchangev2_context(&self, v: &Bytes) -> String {
fn deserialize(py: Python, data: &[u8]) -> String {
let callback_lock = CONFCHANGEV2_CONTEXT_DESERIALIZE_CB.lock().unwrap();
if let Some(callback) = &*callback_lock {
callback
.call(py, (PyBytes::new(py, data),), None)
.unwrap()
.as_ref(py)
.to_string()
} else {
format!("{:?}", data)
}
}
Python::with_gil(|py| match v {
Bytes::Prost(v) => format!("{:?}", deserialize(py, &v[..])),
Bytes::Protobuf(v) => format!("{:?}", deserialize(py, &v[..])),
})
}
fn format_confchange_context(&self, v: &Bytes) -> String {
fn deserialize(py: Python, data: &[u8]) -> String {
let callback_lock = CONFCHANGE_CONTEXT_DESERIALIZE_CB.lock().unwrap();
if let Some(callback) = &*callback_lock {
callback
.call(py, (PyBytes::new(py, data),), None)
.unwrap()
.as_ref(py)
.to_string()
} else {
format!("{:?}", data)
}
}
Python::with_gil(|py| match v {
Bytes::Prost(v) => format!("{:?}", deserialize(py, &v[..])),
Bytes::Protobuf(v) => format!("{:?}", deserialize(py, &v[..])),
})
}
fn format_message_context(&self, v: &Bytes) -> String {
fn deserialize(py: Python, data: &[u8]) -> String {
let callback_lock = MESSAGE_CONTEXT_DESERIALIZER_CB.lock().unwrap();
if let Some(callback) = &*callback_lock {
callback
.call(py, (PyBytes::new(py, data),), None)
.unwrap()
.as_ref(py)
.to_string()
} else {
format!("{:?}", data)
}
}
Python::with_gil(|py| match v {
Bytes::Prost(v) => format!("{:?}", deserialize(py, &v[..])),
Bytes::Protobuf(v) => format!("{:?}", deserialize(py, &v[..])),
})
}
fn format_snapshot_data(&self, v: &Bytes) -> String {
fn deserialize(py: Python, data: &[u8]) -> String {
let callback_lock = SNAPSHOT_DATA_DESERIALIZER_CB.lock().unwrap();
if let Some(callback) = &*callback_lock {
callback
.call(py, (PyBytes::new(py, data),), None)
.unwrap()
.as_ref(py)
.to_string()
} else {
format!("{:?}", data)
}
}
Python::with_gil(|py| match v {
Bytes::Prost(v) => format!("{:?}", deserialize(py, &v[..])),
Bytes::Protobuf(v) => format!("{:?}", deserialize(py, &v[..])),
})
}
}