simplify fixed rule implementation

main
Ziyang Hu 2 years ago
parent 525cfeff59
commit 00825ac39e

@ -186,31 +186,20 @@ impl CozoDbPy {
) -> PyResult<()> { ) -> PyResult<()> {
if let Some(db) = &self.db { if let Some(db) = &self.db {
let cb: Py<PyAny> = callback.into(); let cb: Py<PyAny> = callback.into();
let (rule_impl, receiver) = SimpleFixedRule::rule_with_channel(arity); let rule_impl = SimpleFixedRule::new(arity, move |inputs, options| -> Result<_> {
match db.register_fixed_rule(name, rule_impl) { Python::with_gil(|py| -> Result<NamedRows> {
Ok(_) => { let py_inputs = PyList::new(
thread::spawn(move || { py,
for (inputs, options, sender) in receiver { inputs.into_iter().map(|nr| rows_to_py_rows(nr.rows, py)),
let res = Python::with_gil(|py| -> Result<NamedRows> { );
let py_inputs = PyList::new( let py_opts = options_to_py(options, py).into_diagnostic()?;
py, let args = PyTuple::new(py, vec![PyObject::from(py_inputs), py_opts]);
inputs.into_iter().map(|nr| rows_to_py_rows(nr.rows, py)), let res = cb.as_ref(py).call1(args).into_diagnostic()?;
); py_to_named_rows(res).into_diagnostic()
let py_opts = options_to_py(options, py).into_diagnostic()?; })
let args = });
PyTuple::new(py, vec![PyObject::from(py_inputs), py_opts]); db.register_fixed_rule(name, rule_impl)
let res = cb.as_ref(py).call1(args).into_diagnostic()?; .map_err(|err| PyException::new_err(err.to_string()))
py_to_named_rows(res).into_diagnostic()
});
if sender.send(res).is_err() {
break;
}
}
});
Ok(())
}
Err(err) => Err(PyException::new_err(err.to_string())),
}
} else { } else {
Err(PyException::new_err(DB_CLOSED_MSG)) Err(PyException::new_err(DB_CLOSED_MSG))
} }

Loading…
Cancel
Save