Skip to content

Commit 61b3403

Browse files
authored
[query-engine] Add expression and valuepath to recordset engine (#570)
## Changes * Add expression and valuepath to recordset engine
1 parent a987e15 commit 61b3403

File tree

2 files changed

+795
-0
lines changed

2 files changed

+795
-0
lines changed
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
use core::fmt;
2+
use std::fmt::Write;
3+
use std::sync::atomic::{AtomicUsize, Ordering};
4+
5+
use sha2::{Digest, Sha256};
6+
7+
use crate::execution_context::ExecutionContext;
8+
9+
pub(crate) fn get_next_id() -> usize {
10+
static COUNTER: AtomicUsize = AtomicUsize::new(1);
11+
12+
return COUNTER.fetch_add(1, Ordering::Relaxed);
13+
}
14+
15+
pub(crate) struct Hasher {
16+
hasher: Sha256,
17+
}
18+
19+
impl Hasher {
20+
pub fn new() -> Hasher {
21+
Self {
22+
hasher: Sha256::new(),
23+
}
24+
}
25+
26+
pub fn add_bytes(&mut self, data: &[u8]) {
27+
self.hasher.update(data);
28+
}
29+
}
30+
31+
impl Into<ExpressionHash> for Hasher {
32+
fn into(self) -> ExpressionHash {
33+
let hash = self.hasher.finalize();
34+
35+
let bytes = &hash[..];
36+
37+
return ExpressionHash {
38+
bytes: bytes.into(),
39+
hex: hex::encode(bytes).into(),
40+
};
41+
}
42+
}
43+
44+
impl Into<Box<str>> for Hasher {
45+
fn into(self) -> Box<str> {
46+
let hash = self.hasher.finalize();
47+
48+
let bytes = &hash[..];
49+
50+
return hex::encode(bytes).into();
51+
}
52+
}
53+
54+
#[derive(Debug)]
55+
pub(crate) struct ExpressionHash {
56+
bytes: Box<[u8]>,
57+
hex: Box<str>,
58+
}
59+
60+
impl ExpressionHash {
61+
pub fn new<F>(build: F) -> ExpressionHash
62+
where
63+
F: FnOnce(&mut Hasher),
64+
{
65+
let mut hasher = Hasher::new();
66+
67+
build(&mut hasher);
68+
69+
hasher.into()
70+
}
71+
72+
pub fn get_bytes(&self) -> &[u8] {
73+
&self.bytes
74+
}
75+
76+
pub fn get_hex(&self) -> &str {
77+
&self.hex
78+
}
79+
}
80+
81+
pub(crate) trait Expression: fmt::Debug {
82+
fn get_id(&self) -> usize;
83+
84+
fn get_hash(&self) -> &ExpressionHash;
85+
86+
fn write_debug(
87+
&self,
88+
execution_context: &dyn ExecutionContext,
89+
heading: &'static str,
90+
level: i32,
91+
output: &mut String,
92+
);
93+
}
94+
95+
#[derive(Debug)]
96+
pub(crate) enum ExpressionMessage {
97+
Info(ExpressionMessageData),
98+
Warn(ExpressionMessageData),
99+
Err(ExpressionMessageData),
100+
}
101+
102+
#[derive(Debug)]
103+
pub(crate) struct ExpressionMessageData {
104+
scope: Option<String>,
105+
message: String,
106+
}
107+
108+
impl ExpressionMessage {
109+
pub fn add_scope(&mut self, scope: &str) {
110+
match self {
111+
ExpressionMessage::Info(expression_message_data) => {
112+
expression_message_data.scope = Some(scope.to_string())
113+
}
114+
ExpressionMessage::Warn(expression_message_data) => {
115+
expression_message_data.scope = Some(scope.to_string())
116+
}
117+
ExpressionMessage::Err(expression_message_data) => {
118+
expression_message_data.scope = Some(scope.to_string())
119+
}
120+
}
121+
}
122+
123+
pub fn info(message: String) -> ExpressionMessage {
124+
ExpressionMessage::Info(ExpressionMessageData {
125+
scope: None,
126+
message,
127+
})
128+
}
129+
130+
pub fn warn(message: String) -> ExpressionMessage {
131+
ExpressionMessage::Warn(ExpressionMessageData {
132+
scope: None,
133+
message,
134+
})
135+
}
136+
137+
pub fn err(message: String) -> ExpressionMessage {
138+
ExpressionMessage::Err(ExpressionMessageData {
139+
scope: None,
140+
message,
141+
})
142+
}
143+
144+
pub fn write_debug_comment(&self, output: &mut String) {
145+
let result = match self {
146+
ExpressionMessage::Info(m) => ("Info", &m.scope, &m.message),
147+
ExpressionMessage::Warn(m) => ("Warn", &m.scope, &m.message),
148+
ExpressionMessage::Err(m) => ("Error", &m.scope, &m.message),
149+
};
150+
151+
if !result.1.is_none() {
152+
write!(
153+
output,
154+
"// [{}] {}: {}\n",
155+
result.1.as_ref().unwrap(),
156+
result.0,
157+
result.2
158+
);
159+
} else {
160+
write!(output, "// {}: {}\n", result.0, result.2);
161+
}
162+
}
163+
}

0 commit comments

Comments
 (0)