#plugin #graph #database

tugraph-plugin-util

A helper crate for writing plugins for TuGraph

2 releases

0.1.2 Jun 21, 2023
0.1.0 Jun 19, 2023

#184 in Database implementations

Apache-2.0

6MB
123K SLoC

C++ 87K SLoC // 0.1% comments Python 14K SLoC // 0.3% comments C 9K SLoC // 0.2% comments Rust 7.5K SLoC // 0.1% comments Visual Studio Project 4K SLoC Java 858 SLoC // 0.3% comments AsciiDoc 424 SLoC // 0.0% comments Shell 389 SLoC // 0.3% comments Visual Studio Solution 265 SLoC Cython 230 SLoC // 0.1% comments Bitbake 153 SLoC Batch 143 SLoC Apache Velocity 121 SLoC Swift 30 SLoC Forge Config 14 SLoC INI 4 SLoC

tugraph-plugin-util is a helper crate which make any rust function with following signature

fn (_: &mut Graph, _: &str) -> Result<String>

to be a plugin entry point.

Example

use tugraph::{db::Graph, Result, txn::TxnRead};
use tugraph_plugin_util::tugraph_plugin;

#[tugraph_plugin]
fn user_process_func(graph: &mut Graph, request: &str) -> Result<String> {
    // user process code
    Ok("Process Result".to_string())
}

It exports a attribute proc-macro #[tugraph_plugin] which can decorate a rust function and make the function expanded as:

use tugraph_plugin_util::lgraph_api_graph_db_t;
use tugraph_plugin_util::CxxString;
use tugraph_plugin_util::Graph as TuGraph;

#[no_mangle]
pub unsafe extern "C" fn Process(
    graph_db: *mut lgraph_api_graph_db_t,
    request: *const CxxString,
    response: *mut CxxString) -> bool {
    let mut graph = TuGraph::from_ptr(graph_db);
    let request = if request.is_null() {
        ""
    } else {
        (*request).to_str().unwrap()
    };
    // user defined process function are nested here
    fn user_process_func(graph: &mut Graph, request: &str) -> Result<String> {
        // ...
    }
    let result = user_process_func(&mut graph, request);
    ::std::mem::forget(graph);
    
    match result {
        Ok(val) => {
            if !response.is_null() {
                let mut reponse = ::std::pin::Pin::new_unchecked(&mut *response);
                reponse.as_mut().clear();
                reponse.as_mut().push_str(val.as_str())
            }
            true
        }
        Err(e) => {
            eprintln!("run rust plugin failed: {:?}", e);
            false
        }
    }
}

Dependencies

~2.4–5MB
~95K SLoC