|
1 | 1 | # SQLite
|
2 | 2 |
|
3 |
| -Sqlite fork ported to WebAssembly and adapted for the Fluence network. Could be launched and played on the Fluence [dashboard](http://dash.fluence.network/deploy/sqlite). |
4 | 3 |
|
5 |
| -Based on SQlite version 3.40.1. |
| 4 | +## Overview |
| 5 | +SQLite fork adapted to work with Fluence the protocol. The current version is based on SQLite version 3.40.1. The artifact is SQLite WASM module: |
| 6 | + |
| 7 | + |
| 8 | +- that is compiled following [Marine runtime ABI conventions](https://fluence.dev/docs/marine-book/marine-rust-sdk/module-abi) |
| 9 | +- to be used with [Marine runtime](https://github.com/fluencelabs/marine) |
| 10 | + |
| 11 | + |
| 12 | +## How to build |
| 13 | + |
| 14 | + |
| 15 | + |
| 16 | +Wasm module can be built with either docker-compose |
6 | 17 |
|
7 |
| -# How to build |
8 | 18 |
|
9 |
| -This app could be built either with docker |
10 | 19 |
|
11 | 20 | ```bash
|
| 21 | + |
12 | 22 | docker-compose up
|
| 23 | + |
13 | 24 | ```
|
14 | 25 |
|
15 |
| -or by Makefile with [wasi-sdk](https://github.com/WebAssembly/wasi-sdk) installed |
| 26 | + |
| 27 | + |
| 28 | +or using this [Makefile](./Makefile) with GNU make. There are prerequisites to be installed following this path, namely [wasi-sdk](https://github.com/WebAssembly/wasi-sdk) and marine crate. Take a look at the Dockerfile for the details. |
| 29 | + |
| 30 | + |
16 | 31 | ```bash
|
| 32 | + |
17 | 33 | make
|
| 34 | + |
18 | 35 | ```
|
19 | 36 |
|
20 |
| -# How to use |
21 |
| - |
22 |
| -At now, this fork exports five API functions: |
23 |
| -```cpp |
24 |
| - |
25 |
| -/** |
26 |
| - * Executes given SQL request and returns result in as a pointer to the following structure: | result size (4 bytes, le)| result (size bytes) |. |
27 |
| - * |
28 |
| - * @param sql a pointer to the supplied sql request |
29 |
| - * @param length a size of the supplied sql request |
30 |
| - * @return a pointer to the struct contains result_size and result |
31 |
| - */ |
32 |
| -char *invoke(const char *sql, size_t length); |
33 |
| - |
34 |
| -/** |
35 |
| - * Allocates a memory region of a given size. Could be used by Wasm execution environments for byte array passing. |
36 |
| - * |
37 |
| - * @param size a size of allocated memory region |
38 |
| - * @return a pointer to the allocated memory region |
39 |
| - */ |
40 |
| -void *allocate(size_t size); |
41 |
| - |
42 |
| -/** |
43 |
| - * Frees a memory region. Could be used by Wasm execution environments for freeing previous memory allocated by `allocate` function. |
44 |
| - * |
45 |
| - * @param ptr a pointer to the previously allocated memory region |
46 |
| - * @param size a size of the previously allocated memory region |
47 |
| - */ |
48 |
| -void deallocate(void *ptr, size_t size); |
49 |
| - |
50 |
| -/** |
51 |
| - * Stores one byte by a given address in the module memory. |
52 |
| - * |
53 |
| - * @param ptr a address where byte should be stored |
54 |
| - * @param value a byte to be stored |
55 |
| - */ |
56 |
| -void store(void *ptr, char value); |
57 |
| - |
58 |
| -/** |
59 |
| - * Returns one byte by a given address in the module memory. |
60 |
| - * |
61 |
| - * @param ptr a address at which the needed byte is located |
62 |
| - * @return the byte at the given address |
63 |
| - */ |
64 |
| -char load(void *ptr); |
| 37 | + |
| 38 | + |
| 39 | +## How to use |
| 40 | + |
| 41 | + |
| 42 | + |
| 43 | +The SQLite Wasm module exports a set of SQLite C API functions. The easiest way to try this module is to run it with Marine REPL. |
| 44 | +You can find MREPL output for a simple "CREATE-INSERT-SELECT" scenario below. Mind the second argument to sqlite3_open_v2() that is an OR-ed SQLite flag values. The value "6" in this context means `SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE`. |
| 45 | + |
| 46 | + |
| 47 | +```bash |
| 48 | +$cargo install mrepl |
| 49 | +... |
| 50 | +$mrepl -q Config.toml |
| 51 | + |
| 52 | +1> call sqlite3 sqlite3_open_v2 [":memory:",6,""] |
| 53 | +result: { |
| 54 | + "db_handle": 198600, |
| 55 | + "ret_code": 0 |
| 56 | +} |
| 57 | + elapsed time: 357.556µs |
| 58 | + |
| 59 | +2> call sqlite3 sqlite3_exec [198600, "CREATE TABLE tab1(i bigint);", 0, 0] |
| 60 | +result: { |
| 61 | + "err_msg": "", |
| 62 | + "ret_code": 0 |
| 63 | +} |
| 64 | + elapsed time: 1.736661ms |
| 65 | + |
| 66 | +3> call sqlite3 sqlite3_exec [198600, "INSERT INTO tab1 VALUES (42);", 0, 0] |
| 67 | +result: { |
| 68 | + "err_msg": "", |
| 69 | + "ret_code": 0 |
| 70 | +} |
| 71 | + elapsed time: 330.098µs |
| 72 | + |
| 73 | +4> call sqlite3 sqlite3_prepare_v2 [198600, "SELECT * FROM tab1;"] |
| 74 | +result: { |
| 75 | + "ret_code": 0, |
| 76 | + "stmt_handle": 244584, |
| 77 | + "tail": 268147 |
| 78 | +} |
| 79 | + elapsed time: 280.668µs |
| 80 | + |
| 81 | +5> call sqlite3 sqlite3_step [244584] |
| 82 | +result: 100 |
| 83 | + elapsed time: 124.122µs |
| 84 | + |
| 85 | +6> call sqlite3 sqlite3_column_int64 [244584,0] |
| 86 | +result: 42 |
| 87 | + elapsed time: 72.483µs |
| 88 | + |
65 | 89 | ```
|
66 | 90 |
|
67 |
| -Given char string `sql` as the request, the general scheme to use it is following: |
68 |
| - 1. `void *ptr = allocate(strlen(sql))` that returns a pointer to the memory region enough for the string |
69 |
| - 2. `void *res = invoke(ptr, strlen(sql))` to execute the request |
70 |
| - 3. read a result from the `res` by reading 4 bytes as little-endian `result_size` and the read `result_size` bytes as the final result. |
71 |
| - 4. `deallocate(res, strlen(sql))` to clean memory. |
72 | 91 |
|
73 |
| -Depends on your Wasm execution environment, `load`/`store` could be used for reading and writing a module memory. |
| 92 | +You can also try the SQLite Wasm module using Rust together with [this Sqlite connector](https://github.com/fluencelabs/sqlite-wasm-connector). [Here](https://github.com/fluencelabs/examples/tree/main/marine-examples/sqlite) you can find a simple SQL REPL utility example built into a Wasm module. |
| 93 | + |
| 94 | +## Support |
74 | 95 |
|
75 |
| -## More insights |
| 96 | +Please, [file an issue](https://github.com/fluencelabs/sqlite/issues) if you find a bug. You can also contact us at [Discord](https://discord.com/invite/5qSnPZKh7u) or [Telegram](https://t.me/fluence_project). We will do our best to resolve the issue ASAP. |
76 | 97 |
|
77 |
| -At Fluence, we use WebAssembly (Wasm) to run applications in a trustless network of independent nodes. This trustless environment mandates that every piece of the code executed by any network node must be verified by another node to check whether the execution was performed correctly. In order to be verifiable, every computation must be made deterministic, which means that to run WebAssembly code in a trustless network, we need to make its execution deterministic. |
78 | 98 |
|
79 |
| -There are three primary sources of nondeterminism in WebAssembly: external functions invocation, NaN payloads, and VM resource exhaustion. The first one is the most problematic; currently, we deal with it by simply prohibiting any interactions with the host environment from Wasm programs. This means that submitted WebAssembly code should not contain any external imports. |
| 99 | +## Contributing |
80 | 100 |
|
81 |
| -The main purpose of this fork is to compile to Wasm without any imports. SQlite is famous for it embeddability and allows to configure itself by variety of macros. E.g. multithreading that produces a lot of imports of host functions could be completely disabled by setting SQLITE_THREADSAFE to 0. Also this fork has been patched to use only memory database without any interaction with a hard disk. So, now it is Wasm in-memory database. |
| 101 | +Any interested person is welcome to contribute to the project. Please, make sure you read and follow some basic [rules](https://github.com/fluencelabs/rust-peer/blob/master/CONTRIBUTING.md). |
82 | 102 |
|
83 |
| -More information about ways of porting C/C++ code to Wasm could be found in our [article](https://medium.com/fluence-network/porting-redis-to-webassembly-with-clang-wasi-af99b264ca8) about porting of Redis. |
84 | 103 |
|
85 |
| -## Future plans |
| 104 | +## License |
86 | 105 |
|
87 |
| -We are working on supporting of subset of WASI syscalls and interface types support for more convinient parameter passing scheme. |
| 106 | +All software code is copyright (c) Fluence Labs, Inc. under the [Apache-2.0](https://github.com/fluencelabs/rust-peer/blob/master/LICENSE) license. |
0 commit comments