Created
September 5, 2024 20:04
-
-
Save ochaton/5662dc07167bba6270e01cc20d6ddecc to your computer and use it in GitHub Desktop.
Rust stored Proc registration without Lua
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::collections::HashMap; | |
use serde::{Deserialize, Serialize}; | |
use tarantool::{self, session, tuple::Encode}; | |
use mlua::Lua; | |
#[tarantool::proc] | |
fn add(left: i64, right: i64) -> i64 { | |
left + right | |
} | |
#[derive(Serialize, Deserialize)] | |
struct FuncTuple { | |
pub id: u32, | |
pub owner: u32, | |
pub name: String, | |
pub setuid: u32, | |
pub language: String, | |
pub body: String, | |
pub routine_type: String, | |
pub param_list: Vec<String>, | |
pub returns: String, | |
pub aggregate: String, | |
pub sql_data_access: String, | |
pub is_deterministic: bool, | |
pub is_sandboxed: bool, | |
pub is_null_call: bool, | |
pub exports: Vec<String>, | |
pub opts: HashMap<String, String>, | |
pub comment: String, | |
pub created: String, | |
pub last_altered: String, | |
pub trigger: Vec<String>, | |
} | |
impl Encode for FuncTuple {} | |
fn register_func(name: &str) { | |
let _func = tarantool::space::Space::find_cached("_func").unwrap(); | |
let _vfunc = tarantool::space::Space::find_cached("_vfunc").unwrap(); | |
if _vfunc.index("name").unwrap().get(&("libmodules.add",)).unwrap().is_none() { | |
let max_id = match _vfunc.primary_key().max(&()).unwrap() { | |
Some(t) => t.decode::<FuncTuple>().unwrap().id, | |
None => 0 | |
}; | |
_func.insert(&FuncTuple{ | |
id: max_id+1, | |
owner: session::euid().unwrap(), | |
name: name.to_string(), | |
setuid: 0, | |
language: "C".to_string(), | |
body: "".to_string(), | |
routine_type: "function".to_string(), | |
param_list: vec![], | |
returns: "any".to_string(), | |
aggregate: "none".to_string(), | |
sql_data_access: "none".to_string(), | |
is_deterministic: false, | |
is_sandboxed: false, | |
is_null_call: true, | |
exports: vec!["LUA".to_string()], | |
opts: HashMap::new(), | |
comment: "".to_string(), | |
created: "".to_string(), | |
last_altered: "".to_string(), | |
trigger: vec![], | |
}).unwrap(); | |
} | |
} | |
#[mlua::lua_module] | |
fn libmodules(_lua: &Lua) -> mlua::Result<bool> { | |
register_func("libmodules.add"); | |
Ok(true) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Should by compiled as .dylib (or .so) and placed into (.rocks/lib/tarantool/).
Sample of config.yaml: