diff --git a/src/lib.rs b/src/lib.rs index d245689b2d074658c8c892f5286f554a423ebbf6..1012f6d31fd902e520c1ee6fb256a71a8d3150bd 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -7,6 +7,7 @@ use ::tarantool::fiber::r#async::timeout; use ::tarantool::fiber::r#async::timeout::IntoTimeout as _; use ::tarantool::tlua; use ::tarantool::transaction::start_transaction; +use ::tarantool::tuple::Decode; use std::convert::TryFrom; use std::time::{Duration, Instant}; use storage::Clusterwide; @@ -124,6 +125,26 @@ fn picolib_setup(args: &args::Run) { traft::node::global()?.propose_and_wait(Op::Nop, Duration::from_secs(1)) }), ); + luamod.set( + "raft_propose", + tlua::function1(|lua: tlua::LuaState| -> traft::Result<()> { + use tlua::{AnyLuaString, AsLua, LuaError, LuaTable}; + let lua = unsafe { tlua::Lua::from_static(lua) }; + let t: LuaTable<_> = AsLua::read(&lua).map_err(|(_, e)| LuaError::from(e))?; + let mp: AnyLuaString = lua + .eval_with("return require 'msgpack'.encode(...)", &t) + .map_err(LuaError::from)?; + let op: Op = Decode::decode(mp.as_bytes())?; + traft::node::global()?.propose_and_wait(op, Duration::from_secs(1)) + }), + ); + luamod.set( + "raft_propose_mp", + tlua::function1(|op: tlua::AnyLuaString| -> traft::Result<()> { + let op: Op = Decode::decode(op.as_bytes())?; + traft::node::global()?.propose_and_wait(op, Duration::from_secs(1)) + }), + ); luamod.set( "raft_timeout_now", tlua::function0(|| -> traft::Result<()> { @@ -154,6 +175,25 @@ fn picolib_setup(args: &args::Run) { ); // TODO: remove this if cfg!(debug_assertions) { + use ::tarantool::index::IteratorType; + use ::tarantool::space::Space; + use ::tarantool::tuple::Tuple; + luamod.set( + "prop", + tlua::Function::new(|property: Option<String>| -> traft::Result<Vec<Tuple>> { + if let Some(property) = property { + let _ = property; + todo!(); + } + let space = Space::find("_picodata_property").expect("always defined"); + let iter = space.select(IteratorType::All, &())?; + let mut res = vec![]; + for t in iter { + res.push(t); + } + Ok(res) + }), + ); luamod.set( "emit", tlua::Function::new(|event: String| -> traft::Result<()> {