niamtokik / cozo

An interface to cozodb
https://github.com/niamtokik/cozo
BSD 2-Clause "Simplified" License
7 stars 1 forks source link

Create a new NIF to support Rust library #10

Open niamtokik opened 1 year ago

niamtokik commented 1 year ago

The current implementation is using libcozo_c, but this library has lot of limitation. Instead of passing a cozoscript query, we should pass a cozo query data-structure, based on Erlang syntax (or from something close to Erlang syntax).

 When invoking cozoscript:parse/1, this code will

  ____________________       ___________________       ___
 |                    |     |                   |     /   |
 | cozoscript:parse/1 |---->| cozo_rust:parse/1 |--->| {ok, bytecode}
 |____________________|     |___________________|    |____|

 The same bytecode can be reinjected into another module
 lie cozo_bytecode:eval/1 to be evaluated and get back the result.

   ___      ______________________      __________________
  /   |    |                      |    |                  |
 |  B |--->| cozo_bytecode:eval/1 |--->| cozo_rust:eval/1 |
 |____|    |______________________|    |__________________|
                                                ||
                                               _||_
                                               \  /
                                                \/
                                               ___
                                              /   |
                                             | {ok, Result}
                                             |____|

 In fact, by using this strategy, we don't modify the current
 structure of cozo application.

                           ______________________
                          |                      |
                     +--->| cozo_nif:run_query/4 |-->Result
  ______________     |    |______________________|
 |              |    |
 | cozo:query/3 |---(+)
 |______________|  / |     ___________________     __________________
                  /  |    |                   |   |                  |
                 /   +--->| cozo_rust:parse/1 |-->| cozo_rust:eval/1 |-->Result
 [based on params]        |___________________|   |__________________|

Cozo Rust Interfaces

A cozo core example can be seen in cozo bin source (REPL and server).

Cozoscript to Cozo bytecode

  1. send a cozoscript to cozo parser
  2. instead of returning a json get the cozo bytecode (or an error)

Erlang Terms to Cozo bytecode

  1. identify and understand cozo bytecode generated from cozoscript and based on source code
  2. select adequate Erlang Terms to be use as equivalent to cozo bytecode
  3. create an erlang terms to cozo bytecode compiler

Cozo bytecode to Erlang Terms

  1. based on previous translation, convert cozo bytecode to Erlang terms

Erlang interfaces

  1. Create a new interface using Erlang Terms instead of cozoscript

Resources

niamtokik commented 11 months ago

At this time, Rustler is not compatible with Erlang for many reasons but rebar3_rust is based on ecozodb project. I need to fork official cozodb project in my own space to inject some code and export data-structures and functions, modification will be done here for the moment: https://github.com/niamtokik/cozodb/tree/test/public-export-before-creating-custom-module