Custom operations

Satori provides an interface to create your own custom operations and expand beyond Satori’s built-in domain. To build your own custom operation you must clone this repository first
git clone https://github.com/satori-db/custom-operation && cd custom-operation
Inside of custom-operation you will find a library cargo project. In this project you will find the following files
src
|---lib.rs
|---types.rs
In types.rs you will find Satori’s custom data types to work with, you don’t need to touch this file. Inside lib.rs is where your lib’s code live.
use std::sync::Mutex;

use lazy_static::lazy_static;
use serde_json::{json, Value};
use tungstenite::{connect, Utf8Bytes};
use types::types::{get_closest_owner, notify_fn, notify_ownership, redirection, specific_redirect, Hyper, Object, Owners, Peers, References, Satori, Stats};

pub mod types;

#[no_mangle]
pub extern "C" fn list() -> Vec<String> {
    vec![("SAMPLE".to_string())]
}

#[no_mangle]
pub extern "C" fn startup(
    satori: &Satori,
    peers: &Peers,
    references: &References,
    stats: &Stats,
) -> String {
    String::from("Mylib")
}

pub extern "C" fn SAMPLE(
    satori: &Satori,
    references: &References,
    stats: &Stats,
    rq: &Value,
    notify: notify_fn,
) -> String {
    String::from("OK")
}
Explaining this code:
  • satori is a HashMap<String, Object> , this is where all the objects are
  • peers is the list of all peers your node is connected to
  • references is a HashMap<String, String> where all the references are
  • stats is a HashMap<String, (f32, f32)> that tracks the size of object and the time they are accessed with the purpouse of memory balancing. When an object is accessed or modified we recommend to change the first value to the actual size of the object and increment the second value by one.
  • notify is a function that notifies to all peers in the network that the current object has been updated, it takes a &mut object as parameter
  • rq is the request as json
In the function called list you must return a vec with the name of all the operations you will declare in this library, the name of the operations and the names on the list must be equal startup executes code on Satori’s starting up process, it must return an string that will be printed Then you declare all your custom operations, these need to return a String as response and must have the following structure
pub extern "C" fn SAMPLE(
    satori: &Satori,
    references: &References,
    stats: &Stats,
    rq: &Value,
    notify: notify_fn,
) -> String {
    String::from("OK")
}
When you are ready run
cargo build
and paste the compiled library into /dll folder in your Satori’s root folder (the folder is called dll in both windows and linux but in linux you must paste .so files to it)