/
low_level_rs.rs
204 lines (176 loc) · 5.63 KB
/
low_level_rs.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
// This file is part of https://github.com/SpringQL/SpringQL which is licensed under MIT OR Apache-2.0. See file LICENSE-MIT or LICENSE-APACHE for full license details.
//! Low-level API functions to execute / register SpringQL from Rust.
//!
//! C API and high-level Rust API are provided separately.
mod engine_mutex;
mod spring_config;
pub use spring_config::*;
use std::{sync::Once, thread, time::Duration};
use crate::{
error::{Result, SpringError},
pipeline::name::QueueName,
sql_processor::SqlProcessor,
stream_engine::{command::Command, SinkRow, SpringValue, SqlValue},
};
use self::engine_mutex::EngineMutex;
fn setup_logger() {
static INIT: Once = Once::new();
INIT.call_once(|| {
let _ = env_logger::builder()
.is_test(false) // To enable color. Logs are not captured by test framework.
.try_init();
log_panics::init();
});
log::info!("setup_logger(): done");
}
/// Connection object.
///
/// 1 stream pipeline has only 1 connection.
/// In other words, the lifecycle of SpringConnection and internal stream pipeline are the same.
#[derive(Debug)]
pub struct SpringPipeline {
engine: EngineMutex,
sql_processor: SqlProcessor,
}
/// Row object from an in memory queue.
#[derive(Debug)]
pub struct SpringRow(pub(in crate::api) SinkRow);
impl From<SinkRow> for SpringRow {
fn from(sink_row: SinkRow) -> Self {
Self(sink_row)
}
}
/// Creates and open an in-process stream pipeline.
pub fn spring_open(config: &SpringConfig) -> Result<SpringPipeline> {
setup_logger();
let engine = EngineMutex::new(config);
let sql_processor = SqlProcessor::default();
Ok(SpringPipeline {
engine,
sql_processor,
})
}
/// Execute commands (DDL).
///
/// # Failure
///
/// - [SpringError::Sql](crate::error::SpringError::Sql) when:
/// - Invalid SQL syntax.
/// - Refers to undefined objects (streams, pumps, etc)
/// - Other semantic errors.
/// - [SpringError::InvalidOption](crate::error::SpringError::Sql) when:
/// - `OPTIONS` in `CREATE` statement includes invalid key or value.
pub fn spring_command(pipeline: &SpringPipeline, sql: &str) -> Result<()> {
let mut engine = pipeline.engine.get()?;
let command = pipeline
.sql_processor
.compile(sql, engine.current_pipeline())?;
match command {
Command::AlterPipeline(c) => engine.alter_pipeline(c),
}
}
/// Pop a row from an in memory queue. This is a blocking function.
///
/// **Do not call this function from threads.**
/// If you need to pop from multiple in-memory queues using threads, use `spring_pop_non_blocking()`.
/// See: <https://github.com/SpringQL/SpringQL/issues/125>
///
/// # Failure
///
/// - [SpringError::Unavailable](crate::error::SpringError::Unavailable) when:
/// - queue named `queue` does not exist.
pub fn spring_pop(pipeline: &SpringPipeline, queue: &str) -> Result<SpringRow> {
const SLEEP_MSECS: u64 = 10;
let mut engine = pipeline.engine.get()?;
loop {
if let Some(sink_row) =
engine.pop_in_memory_queue_non_blocking(QueueName::new(queue.to_string()))?
{
return Ok(SpringRow::from(sink_row));
} else {
thread::sleep(Duration::from_millis(SLEEP_MSECS));
}
}
}
/// Pop a row from an in memory queue. This is a non-blocking function.
///
/// # Returns
///
/// - `Ok(Some)` when at least a row is in the queue.
/// - `None` when no row is in the queue.
///
/// # Failure
///
/// - [SpringError::Unavailable](crate::error::SpringError::Unavailable) when:
/// - queue named `queue` does not exist.
pub fn spring_pop_non_blocking(
pipeline: &SpringPipeline,
queue: &str,
) -> Result<Option<SpringRow>> {
let mut engine = pipeline.engine.get()?;
let sink_row = engine.pop_in_memory_queue_non_blocking(QueueName::new(queue.to_string()))?;
Ok(sink_row.map(SpringRow::from))
}
/// Get an integer column.
///
/// # Failure
///
/// - [SpringError::Unavailable](crate::error::SpringError::Unavailable) when:
/// - `i_col` already fetched.
/// - `i_col` out of range.
/// - [SpringError::Null](crate::error::SpringError::Null) when:
/// - Column value is NULL
pub fn spring_column_i32(row: &SpringRow, i_col: usize) -> Result<i32> {
spring_column_not_null(row, i_col)
}
/// Get a text column.
///
/// # Failure
///
/// Same as [spring_column_i32()](spring_column_i32)
pub fn spring_column_text(row: &SpringRow, i_col: usize) -> Result<String> {
spring_column_not_null(row, i_col)
}
/// Get a boolean column.
///
/// # Failure
///
/// Same as [spring_column_i32()](spring_column_i32)
pub fn spring_column_bool(row: &SpringRow, i_col: usize) -> Result<bool> {
spring_column_not_null(row, i_col)
}
/// Get a float column.
///
/// # Failure
///
/// Same as [spring_column_i32()](spring_column_i32)
pub fn spring_column_f32(row: &SpringRow, i_col: usize) -> Result<f32> {
spring_column_not_null(row, i_col)
}
/// Get a 2-byte integer column.
///
/// # Failure
///
/// Same as [spring_column_i32()](spring_column_i32)
pub fn spring_column_i16(row: &SpringRow, i_col: usize) -> Result<i16> {
spring_column_not_null(row, i_col)
}
/// Get a 8-byte integer column.
///
/// # Failure
///
/// Same as [spring_column_i32()](spring_column_i32)
pub fn spring_column_i64(row: &SpringRow, i_col: usize) -> Result<i64> {
spring_column_not_null(row, i_col)
}
fn spring_column_not_null<T: SpringValue>(row: &SpringRow, i_col: usize) -> Result<T> {
let v = row.0.get_by_index(i_col)?;
if let SqlValue::NotNull(v) = v {
v.unpack()
} else {
Err(SpringError::Null {
stream_name: row.0.stream_name().clone(),
i_col,
})
}
}