The goal

Use an existing library and replace the calls out to filesystem to be routed to our filesystem.

How KV works

KV works by replacing all instances of redb std::fs::File with a custom File implementation. That File implementation calls out to our filesystem using Message passing.

The key tricky thing here was: how do we have a library know how to talk to our system?

The solution in this case was to add an argument to File creation: a function pointer to send_and_await_response(). Then, whenever our File needed to read, write, or otherwise interact with the filesystem, it can do so using that function.

One tricky aspect here was getting around Rust's type system. Rust wants you to have types defined in file A, and import that same file into places B and C that use those types. The problem is that our types come from the compilation of the wit file. The solution was to wrap send_and_await_response() with a wrapper that has no custom types. So for example, Address, which is composed of a node (String) and a ProcessId (enum of either Id(u64) or Name(String)) is translated by the wrapper to and from

(String, Result<u64, String>)

What new problems come along with SQLite?

SQLite is slightly different because:

  1. It has a VFS extension system,
  2. It is C code.

We still have the same overall problem of getting a means of talking to our system into our VFS extension.

My current plan is to try to do a similar "send_and_await_response() wrapper" trick as above, but to also export it so that the VFS C code can access it, e.g.

//  rust
use std::os::raw::c_int;

#[no_mangle]
pub extern "C" fn my_rust_function() -> c_int {
    // ... your logic here ...
    0 // return an int, for example
}
//  C

// ...
extern int my_rust_function();
// ...

(see https://chat.openai.com/share/4ce0fd14-ba65-4fcf-a287-528aaefabf8d).

If all goes according to plan, the VFS should be able to talk to our system, and we should be able to write our component using rusqlite.

Original gist on GitHub