Mobile and native apps
Keep the database in a file on the device: no server, no network, the same engine and file format as the server and the browser.
An iOS, macOS, Android or Flutter app opens a fennecdb file, writes to it
and asks it questions in FennecQL or through the query builder, with
near, match and lookup as anywhere
else. A live query hands the UI its rows again after every write to what it
reads. A Linux program -- a GTK app, a C or C++ program, any language with
a C FFI -- links the same engine from the release's library
(C on Linux).
| Swift | Kotlin and Java | Dart and Flutter | |
|---|---|---|---|
| install | SwiftPM: github.com/fennec-db/fennecdb, product FennecDB | com.fennecdb:fennecdb-android (the AAR), com.fennecdb:fennecdb (the JVM) | fennecdb_flutter, or fennecdb in Dart alone |
| platforms | iOS 15, macOS 11 | Android 5 (API 21): arm64-v8a, armeabi-v7a, x86_64; the JVM | iOS 15, macOS 11, Android 5 (API 21) |
| calls | async, off the caller's thread | suspend on Dispatchers.IO; ...Blocking for Java | Futures, on an isolate of the database's |
| live queries | LiveQuery (ObservableObject), LiveRows (@Observable), an AsyncThrowingStream | a Flow: Compose's collectAsState | a Stream: StreamBuilder |
| rows | Row, or any Decodable | Row, a Map with typed getters | Map<String, Object?> |
How the bindings share one library
Each binding calls the same native library, crates/fennec-ffi:
the engine built for the device, behind a C ABI of a few functions --
open, query, the change notice, sync, flush, checkpoint, close. A statement
goes in as text with its parameters as JSON, and its answer comes back as
the JSON the browser module answers with, from the same code
(fennec-abi), so a page and an app handed the same text get the
same bytes. A vector goes in as its f32 bytes beside the
JSON, never as digits, and so does a list of numbers each of which an
f32 holds — a [Float],
FloatArray or Float32List, integers up to
224, doubles such as 0.5 — marked exact, which a
list of numbers ([int], [float],
[timestamp]) takes as it came: a put of an [int]
of 8 beside a 128-dim vector is one call, 2.0 µs. Any other list given a
json field or a list of numbers, or compared with one, the
library names before running anything and the binding sends it again
inside the JSON, so [16777217] and [0.1] are
kept as the numbers written, not as f32s; each binding
remembers a text's places and sends them so from its second call on, one
call rather than two (2.4 µs a put of a [float] of 8 tenths,
against 3.6 asked again). The same file can be a replica a server keeps in
step (Sync with a server), and an app can ask a server
with no file at all (Without a file).
A todo list
The file in the app's own directory, a collection made once, a list on screen that is a live query, and writes from the buttons. The list redraws itself: nothing calls it after a write.
import FennecDB
import SwiftUI
struct Todo: Codable, Sendable, FennecValue {
var id: Int?
var title: String
var done: Bool
}
@main struct TodosApp: App {
@State var db: Fennec?
var body: some Scene {
WindowGroup {
if let db { TodoList(db: db) } else { ProgressView().task { db = try? await open() } }
}
}
func open() async throws -> Fennec {
let dir = try FileManager.default.url(for: .applicationSupportDirectory, in: .userDomainMask,
appropriateFor: nil, create: true)
let db = try await Fennec.open(dir.appendingPathComponent("todos.fennec"))
try await db.execute("create collection if not exists todos (title text, done bool @hash)")
return db
}
}
struct TodoList: View {
let db: Fennec
@StateObject var open: LiveQuery<Todo>
init(db: Fennec) {
self.db = db
_open = StateObject(wrappedValue: LiveQuery(db, try! db.from("todos").where("done", false), as: Todo.self))
}
var body: some View {
List(open.rows, id: \.id) { todo in
Button(todo.title) {
Task { try await db.from("todos").where("id", todo.id!).update(["done": true] as Value) }
}
}
.toolbar {
Button("Add") { Task { try await db.from("todos").insert(Todo(title: "milk", done: false)) } }
}
}
}On iOS 17 and macOS 14, LiveRows is the same as an
@Observable, held in @State.
import com.fennecdb.*
class TodosModel(app: Application) : AndroidViewModel(app) {
val db = Fennec.open(File(app.filesDir, "todos.fennec").path) // in a background start-up, or openAsync
init {
db.executeBlocking("create collection if not exists todos (title text, done bool @hash)")
}
val open: Flow<List<Row>> = db.live(db.from("todos").where("done", false))
fun add(title: String) = viewModelScope.launch { db.from("todos").insert(mapOf("title" to title, "done" to false)) }
fun finish(id: Long) = viewModelScope.launch { db.from("todos").where("id", id).update(mapOf("done" to true)) }
override fun onCleared() = db.close()
}
@Composable
fun Todos(model: TodosModel) {
val open by model.open.collectAsState(emptyList())
LazyColumn {
items(open) { todo -> Text(todo.string("title") ?: "", Modifier.clickable { model.finish(todo.long("id")!!) }) }
}
Button(onClick = { model.add("milk") }) { Text("Add") }
}A live query is a conflated Flow: a collector that falls
behind sees the latest rows.
import 'package:fennecdb_flutter/fennecdb_flutter.dart';
import 'package:flutter/material.dart';
import 'package:path_provider/path_provider.dart';
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
final dir = await getApplicationSupportDirectory();
final db = await Fennec.open('${dir.path}/todos.fennec');
await db.execute('create collection if not exists todos (title text, done bool @hash)');
runApp(MaterialApp(home: Todos(db)));
}
class Todos extends StatelessWidget {
final Fennec db;
const Todos(this.db, {super.key});
@override
Widget build(BuildContext context) => Scaffold(
body: StreamBuilder(
stream: db.live(db.from('todos').where('done', false)),
builder: (context, snap) => ListView(children: [
for (final todo in snap.data ?? const <Map<String, Object?>>[])
ListTile(
title: Text(todo['title'] as String),
onTap: () => db.from('todos').where('id', todo['id']).update({'done': true}),
),
]),
),
floatingActionButton: FloatingActionButton(
onPressed: () => db.from('todos').insert({'title': 'milk', 'done': false}),
child: const Icon(Icons.add),
),
);
}The plugin's example/ is this app, built for Android on
every change, and for macOS and the iOS simulator from the plugin as
pub.dev gets it: packed as a release publishes it, with each platform's
library in it once, and the macOS app's own test opening a file through
the library it embeds.
A fuller app, Notes -- search with match and
fuse, filters, a schema file, an add form -- is in the
repository for each: SwiftUI
(and a CLI), Jetpack
Compose (and a JVM CLI) and Flutter,
each built on every change and its headless part run.
The builder
The same query builder as JavaScript, Python, Go and .NET:
from, select, where with every
operator, near, match, fuse,
rerank, highlight, snippet,
facet, lookup, order,
limit, and the writes, each value a parameter and each name
checked. All seven make the same text of the same chain, to the byte, held
to one golden file; each language's spelling is on
Your language. A builder query also
knows the collections it reads, which is what a live query runs again for.
To see the text and parameters a chain builds, for logging or a test,
toFennecQL() returns them and runs nothing.
let near = try await db.from("notes")
.select("title")
.where("lang", "en")
.near("embed", embedding, ef: 64) // a [Float]
.limit(5)
.rows(as: Note.self)The schema in code
An app that makes its database declares it once, as
create collection and create index statements --
a schema.fennecql in the app's resources -- and hands it to
schema after each open. The engine compares the file with it:
what only adds (a collection, a field, an index) is made, the migrations
not yet recorded run first, in order, and are kept in
_migrations, all one block; a field the text lacks, a type or
an index changed, is refused with what resolves it, and nothing is written.
It is the check the browser's Fennec.open({ schema }) and the
server's /_schema run (Schema in
code).
let plan = try await db.schema(schemaText, migrations: [
"alter collection notes rename field title to name",
])val plan = db.schema(schemaText, migrations = listOf(
"alter collection notes rename field title to name",
))final plan = await db.schema(schemaText, migrations: [
'alter collection notes rename field title to name',
]);fennec types --lang swift|kotlin|dart schema.fennecql writes
a row of each collection as the language reads it: a Codable
struct, a data class with from(row), a class with
fromJson — and, for each named
statement the file holds, a …Args type for its arguments and
a …Row for its answer, as far as the answer is typed (plain fields
of one collection and a count).
Named statements
A named statement — a block of
FennecQL defined once in a database and called by name with typed arguments,
create statement transfer(...) — is called with the SDK's
call (Dart's callStatement, since a method named
call would make the object callable): the arguments are an
object by name, sent as the one parameter of call transfer $1,
never as text, and only the name — ASCII letters, digits and
_ — is checked into it. On the server's connection it is a
request like any other, and takes idempotencyKey, wait
and read, which marks a statement that only reads so a
connection reading near has a read replica answer it; a statement that
writes is refused there (403), never landed. claims is the
call's as, the server's token alone may say it.
let db = Fennec.connect(url: "https://api.example.com", token: jwt)
let moved = try await db.call("transfer", ["from": 1, "to": 2, "amount": 300] as Value,
idempotencyKey: UUID().uuidString)
let mine = try await db.call("history", claims: ["sub": "alice"] as Value, read: true).rowsval db = Fennec.connect("https://api.example.com", token = jwt)
val moved = db.call("transfer", mapOf("from" to 1, "to" to 2, "amount" to 300),
idempotencyKey = UUID.randomUUID().toString())
val mine = db.call("history", claims = mapOf("sub" to "alice"), read = true).rowsfinal db = Fennec.connect('https://api.example.com', token: jwt);
final moved = await db.callStatement('transfer',
args: {'from': 1, 'to': 2, 'amount': 300}, idempotencyKey: uuid.v4());
final mine = (await db.callStatement('history', claims: {'sub': 'alice'}, read: true)).rows;The database in the app's file runs a statement it holds itself —
db.call("history", claims: …), with the library, its
claims the signed-in user's, held to the types the statement
declares as a token's are; it takes no key and no wait. A database
opened as a replica of a server's shapes (below) holds its
shapes' rows and none of the server's statements, so a call would run against
a part of the rows: it is refused, in the SDK's error, and the app calls the
server through its connection — what the call wrote comes back through the
shapes' streams like any server write.
db.statements() lists them — Kotlin's
statementsBlocking() too — each a NamedStatement:
its signature, whether it writes, its comment, why the schema broke it, what a
binding is bound to, and a scheduled
statement's clock, its schedule, next slot,
last run and held slot (a Date in
Swift, milliseconds since the epoch in Kotlin, a UTC DateTime in
Dart), and a webhook consumer's hook
and whether the server holds its secret (armed). The server
lists them to its own token alone, and its token's call sends a
webhook consumer a test event; the app's own database lists what its file
holds, runs no schedule and takes no event (every SDK's).
Live queries
A live query hands over its rows at once, and again after a write to a collection it reads. The writes of a burst -- several at once, a loop, a text of several statements -- run it once, a frame after the last of them lands. A write to another collection runs nothing, and a text that fails is put back whole and runs nothing either.
How a live query re-runs
The engine keeps a ring of the writes it landed, and
fennec_changes(since) names the collections written since a
cursor -- or answers "everything" when the cursor fell behind the ring or a
collection written since was dropped. A write asks for a look; the looks
asked for within a frame (16 ms) are one, taken once no write through the
database is under way, and each query that reads a collection named runs
again from scratch -- well under a millisecond over a local file, where
keeping track of which rows changed would cost more than the query. Knowing
costs a write nothing: the ring is asked once after a burst, and only by a
database holding a live query. It is how the browser's
Fennec.live works, with a frame in place of a microtask: an
app's writes run on other threads and come back one at a time.
A builder query reads its own collection and each lookup's; a
FennecQL text names its collections, or runs after every write.
Sync with a server
The todo list kept in step with a server, every device seeing every change: open the file as a replica of the rows you want. That is the one line that changes -- the list is still a live query of the file, and a tap still writes to it.
let db = try await Fennec.sync(url: "https://api.example.com", token: jwt,
shapes: [Shape("todos", key: "key")],
path: dir.appendingPathComponent("todos.fennec").path)val db = Fennec.sync(url = "https://api.example.com", token = jwt,
shapes = listOf(Shape("todos", key = "key")),
path = File(app.filesDir, "todos.fennec").path)final db = await Fennec.openSynced(url: 'https://api.example.com', token: jwt,
shapes: [Shape('todos', key: 'key')],
path: '${dir.path}/todos.fennec');The collection is made on the server, with a key the app's inserts are
matched by -- create collection todos (key text @unique, title text,
done bool @hash) -- and comes to the replica from there, so the
create collection line goes. Dart's is
openSynced: sync() is the fsync, and Dart has no
static and instance member of one name.
- Reads are the file's. A query, a live query and the builder read the replica: no network, and the app works offline.
- A shape is a set. The rows of one collection a filter on the
server picks,
Shape("todos", where: ["owner": me]), in the JS sync layer's form; one shape a collection. Order and limits are a query's, not a shape's. - A write shows at once. It is applied to the replica as it is made, and sent. An insert gets a temporary id and a key, and the server's copy takes its place when it arrives.
- A refused write is put back. When the server refuses it -- a
409for a unique value held, a403from a token's rules, a507from a tenant's quota of bytes or rows (quotanames it) -- the replica is put back as it was and the app is told:db.replica.refusals. A row the server sent meanwhile stays as the server sent it. - Offline, writes wait. A write made while the server cannot be reached waits in the file, and goes once it can, in order, once each. An app closed meanwhile sends it when it opens again.
- It catches up from where it stopped, after a dropped connection, a server restart or a week in a drawer -- or is sent the whole shape again when the server's ring of changes no longer reaches back that far.
| Swift | Kotlin | Dart | |
|---|---|---|---|
| open | Fennec.sync(url:token:shapes:path:) | Fennec.sync(url, token, shapes, path) | Fennec.openSynced(url:, token:, shapes:, path:) |
| status | replica.status, statuses() (an AsyncStream) | replica.status (a StateFlow) | replica.status, statuses (a Stream) |
| first seed, writes answered | ready(), pushed() | ||
| refused writes | refusals() | refusals (a SharedFlow) | refusals |
| a new token | setToken(jwt), or a tokenProvider asked on a 401, handed a TokenRefusal | ||
| a token wanted | status.token?.stage: .delayed, .asked, .failed | status.value.token?.stage: DELAYED, ASKED, FAILED | status.token?.stage: TokenStage.delayed, .asked, .failed |
| the network | setOnline(bool), resume() | ||
| the server itself | replica.remote (below) | ||
The status says online, offline or
catchingUp (connecting, seeding, or sending the writes left
from before), how many writes the server has not answered, and the last
error.
A 401 stops the sending until a token is handed over, and
asks tokenProvider for one, handed a TokenRefusal:
errorCode, the code the server named --
token_expired where the token's exp passed, none
otherwise (Expiry) -- expired,
that code's, hadToken, whether the refused request carried a
token, and attempt, how many times in a row it is asked since
the server last took the token held. The provider asks the issuer for a
fresh token at every 401, whatever the code -- forcing a
refresh at expired -- and the replica goes on where it
stopped, its waiting writes sent under the keys they kept. A
401 alone is never a sign-out: the server keeps no list of
revoked tokens, so only the issuer knows that a session ended, and the app
signs out where it refuses to issue a token. Where the token handed over
is refused too (attempt past 1: a key the server has not
fetched yet, an nbf its clock has not reached, a
misconfiguration -- none of which a fresh token cures), the sync asks
again after 1 s doubling to 60 s, a wait the token just refused, handed
over again, does not end; an expiry of the token the server took last, or
of the first one handed over since, starts the count at 1 again, any other
-- a token minted expired, as every one is where the server's clock runs
ahead of the issuer's -- counts. A provider that throws -- the issuer out
of reach -- or hands over an empty token leaves the sync waiting, the
status's token reading failed and its error the token
provider failed: …, and is asked again at resume() or
setOnline(true); while it is at work nothing asks it again.
Nothing bounds its time, since it may be waiting on a sign-in, so a
provider must return: it times out its own requests to the issuer, and
throws. One that never returns holds the sync until the app hands a token
over itself with setToken, which gets it going whatever the
stage.
While a token is wanted the status's token says where the
ask stands -- its stage: delayed while the wait before the
next ask runs, asked once the provider is asked and no token has come,
failed where it threw or handed over none -- beside its
refusal, the TokenRefusal. Nothing is sent
meanwhile, and the status's error is the token's, its
errorCode the code the 401 named, whatever else
fails while it waits: streams end where the network goes, which is where
providers fail. An app that hands its tokens over with
setToken rather than a provider does so at asked.
A replica belongs to one account: its file's path names the account,
and it is never opened for another, or the next account signing in on the
device sends the writes left in it under its own token and is shown its
rows. Signing out, an app closes the replica and keeps the file while its
status says pending writes, for that account's next sign-in to
send; deleting it loses them.
let db = try await Fennec.sync(url: url, token: jwt, shapes: shapes,
path: dir.appendingPathComponent("\(account.id).fennec").path,
tokenProvider: { refusal in
// A fresh token at every 401; throws where the issuer refuses.
try await auth.refresh(force: refusal.expired)
})val db = Fennec.sync(url = url, token = jwt, shapes = shapes,
path = File(context.filesDir, "${account.id}.fennec").path,
tokenProvider = { refusal ->
// A fresh token at every 401; throws where the issuer refuses.
auth.refresh(force = refusal.expired)
})final db = await Fennec.openSynced(url: url, token: jwt, shapes: shapes,
path: '${dir.path}/${account.id}.fennec',
// A fresh token at every 401; throws where the issuer refuses.
tokenProvider: (refusal) => auth.refresh(force: refusal.expired));Before 1.0 a provider took no argument. Swift's no longer compiles
({ _ in try await refresh() } for it); a Kotlin lambda asking
nothing still takes, but a reference to a function of none,
::refresh, no longer does ({ refresh() } for it);
Dart's provider takes the TokenRefusal,
(_) => refresh() where it reads none of it.
How the sync core and the bindings divide the work
What to ask, what an answer means and what is written are one state
machine in Rust (fennec_abi::sync), with no network of its own:
a binding feeds it what happened -- a request's answer, a stream's bytes, a
timer, the network, a new token -- and does what it hands back: make this
request, open this stream, wait this long. So the logic exists once, with
one suite of tests driving it through scripted server event sequences, and
each binding's loop is a page over its platform's HTTP client. A write to a
shape's collection through run, the builder or
execute goes to it on its own: applied to the replica, what
puts it back kept, and the write queued with an idempotency key -- all one
block in the file. Writes go one at a time, in order, each under its key, so
a write whose answer was lost is sent again and answered as the first time,
not made again. A server's answer of 0 (no answer),
408, 429 or 5xx is tried again with
backoff, 250 ms doubling to 15 s, or after the wait a tenant's quota (to
the millisecond, retry_after_ms), a move or a failover
(retry_after) names where that is longer; a
401 asks for a token, the action naming the code the server
did (token_expired), whether the refused request carried a
token and the asks in a row, after 1 s doubling to 60 s where the token
handed over was refused too -- a 401 to a request sent with a
token since replaced goes again with the new one, unasked, and one ask is
out at a time, the binding answering it with a token or with why none came
({"token_failed":..}), which the status's token says; a 507 naming a tenant's
quota, and any other 4xx, is a refusal, and the writes behind
it go on
(why). The status's
error names the quota too: a stream refused by the quota of streams leaves
the replica offline, as the network does. A change applies in one block with the
shape's cursor, so a crash leaves the two agreeing. A change saying the
server's schema changed, or rows with a field the replica has not got, has
the collection made again from GET /collections -- its rows kept
in the fields that are left -- and the whole shape sent again.
The file holds four collections of the sync's own beside the
replica's: _sync_shapes (each shape's cursor),
_sync_queue (the writes not yet answered),
_sync_temps (rows an insert made under a temporary id, until
the server's copy) and _sync_seed (the parts of a seed that
came in parts, until the last). A collection with no shape is the app's own, written
and read as in a file of its own; a synced collection's schema is the
server's, and create index or drop over it is
refused. A change of one row costs the replica 18.4 µs against 7.1 for the
same put straight into the engine (the cursor is written in the same
block), rows in changes of 100 cost 3.8 µs either way, and a seed of 10 000
rows 72 ms (make sync-bench).
A seed past 1 MiB of JSON comes in parts
(a seed in parts). The replica holds
them in _sync_seed in its file and lands the seed in one block
once the last part came, so every read answers the rows as they were until
then, and ready() waits for that last part. A stream that drops
mid-seed leaves the replica as it was: the parts are deleted at the drop and
when the replica opens again, and the next stream seeds from its start.
The browser's sync (Sync) is
written apart -- this core in the browser module was 21 KB brotli more --
and the two are held to one script: integrations/sync-scenarios.json,
112 scenarios of shapes, writes, answers and server events, each step's
replica, queue and requests stated once and run against both. What is left
between them is the platforms':
- A write returns once it is applied and kept:
awaiton a button would hang offline. A refusal comes onrefusals, andpushed()waits for the answers. In the browser the write's promise settles with the server's answer, which a page need not await;onRefusedandpushed()are there too. - A text of several writes is one write, sent as one
/batch; the browser's builder writes one statement at a time, andbatch()makes several one write the same way. - There is one replica a file, held by one process, so no leader is
elected among tabs; the file is the persistence, where a page stores its
replica in IndexedDB (
persist) or anopenFile. - Every collation is in the library; the browser module fetches a script's chunk the first time a row needs it.
TLS on devices
iOS (App Transport Security) and Android refuse plain http://
by default, and so should you: serve fennec-server over HTTPS.
It can hold the certificate itself, --tls-cert and
--tls-key (HTTPS), and a device
trusts a public CA's certificate, Let's Encrypt's for one, as it is. A proxy
is still an option. Caddy does it in three lines, certificate included:
api.example.com {
reverse_proxy 127.0.0.1:8080
}The server behind it still needs its token: reached by a name, a server with none refuses every request (Behind a proxy).
Each binding makes its requests with the platform's client --
URLSession, HttpURLConnection,
dart:io's HttpClient -- so the system's trust
store, proxies and pinning apply. An Android app declares the
INTERNET permission and a sandboxed macOS app the
com.apple.security.network.client entitlement, as for any
request. During development, an emulator reaches
a server on the machine at plain http:// once allowed: iOS's
NSAllowsLocalNetworking, Android's
network_security_config with cleartextTrafficPermitted
for 10.0.2.2.
Why a proxy can cut the change stream
The change stream is Server-Sent Events: a response that does not end.
Caddy passes text/event-stream through as it comes; behind
nginx, the server's X-Accel-Buffering: no does the same. A
proxy that closes an idle connection sooner than the server's keepalive
(a comment every 20 s) cuts the stream, which then opens again from its
cursor -- harmless, a round trip each time. fennecdb's own HTTP client, the
server's, dials https:// as well as http://
(Dialing HTTPS) and is not used on a
device.
Battery and the background
A replica holds one connection a shape open while the app runs, quiet
but for the keepalive. iOS suspends an app soon after it leaves the
screen, and Android stops one's network in Doze: the streams are cut, and
nothing is lost -- a write waits in the file, a change waits on the server.
As the app comes back, call resume() (on
scenePhase == .active, onStart,
AppLifecycleState.resumed): the streams open at once from their
cursors, the waiting writes go first, and the live queries run again as the
changes land.
What setOnline(false) and backoff do
setOnline(false) as the app leaves the screen closes the
streams at once rather than when the system cuts them, and spares the radio
the keepalive. Backoff after a failure doubles from 250 ms to 15 s, with
jitter, so a server back up is not met by every device at once;
resume() and setOnline(true) forget it. Nothing
here runs in the background: a write made just before suspension is sent on
the next resume, or by an app's own background task (iOS's
BGAppRefreshTask, Android's WorkManager) that opens the replica
and waits for pushed(). A replica away longer than the server's
ring of changes reaches back (--changes, 4 096 writes by
default) is sent its shapes whole on return.
Without a file: the server alone
An app that keeps nothing on the device asks the server directly:
Fennec.connect makes every query a request, through the same
builder, as connect does in JavaScript.
let db = Fennec.connect(url: "https://api.example.com", token: jwt)
let open = try await db.from("todos").where("done", false).rows()Kotlin's is Fennec.connect(url, token = jwt), Dart's
Fennec.connect(url, token: jwt); a replica's
replica.remote is the same, for what the replica does not
hold. Live queries are the file's alone.
A request goes again only where that is safe: one the
server says ran nothing -- a 503, or a 429 naming its wait -- after that
wait and never sooner, unless it says statements of a batch landed
(completed), and one whose connection was refused; a read the
builder made, one sent near or a write under an idempotency key after a
dropped connection too. A text sent home without a key,
asRead() or not, goes once, as a write without a key does, and
nothing goes again after the connection's own timeout or once the caller
gives it up
(Retrying a write). The
timeout bounds the wait for an answer's head and each wait
between the pieces of its body -- 30 s unless given, longer for a
compact -- and retry the tries: three, their
waits within 10 s, unless given, and none at all as nil
(null).
let db = Fennec.connect(url: "https://api.example.com", token: jwt, timeout: 60,
retry: FennecRemote.Retry(attempts: 5, maxWait: 30))Kotlin's are milliseconds, Fennec.connect(url, token = jwt, timeout
= 60_000, retry = FennecRemote.Retry(attempts = 5, maxWait = 30_000)),
Dart's Durations, Fennec.connect(url, token: jwt,
timeout: const Duration(minutes: 1), retry: const RetryPolicy(attempts: 5,
maxWait: Duration(seconds: 30))). Kotlin sends a request that must go
once with its length fixed: the JVM's HttpURLConnection sends a
buffered POST again on its own after a reset, a keyless write made twice.
Several writes that must land together are one batch
(POST /batch): the builder's toInsert,
toUpdate and toDelete under one write lock, all
of them or none. One that stops it is an error naming its place
(at, from 0) and the HTTP status -- 412, code
unmet, for a write whose require was not met -- and
its reason where the require gave one
(reason: beside require:, Kotlin's with
=; A reason for the
refusal), and the name of the statement bound to the write
that refused it in binding, from the server's token's request and
from the app's own database alike. A
retry after a timeout cannot tell whether the write ran, so a write that
must not run twice goes under an idempotency key: the second answer is the
first one kept (replayed), and the key sent with another
request is refused (422).
let accounts = try db.from("accounts")
let done = try await db.batch([
try accounts.where("id", from).where("balance", ">=", 10).toUpdate(["balance": .inc(-10)] as Value, require: 1, reason: "insufficient funds"),
try accounts.where("id", to).toUpdate(["balance": .inc(10)] as Value, require: 1, reason: "no account to credit"),
], idempotencyKey: transferID)
// done.results, done.seq, done.replayed
try await db.withIdempotencyKey(orderID).from("orders").insert(order)Kotlin's is db.batch(listOf(...), idempotencyKey = id) and
runList(text, params, idempotencyKey = id), Dart's
db.batch([...], idempotencyKey: id); all three have
withIdempotencyKey(key), a copy whose every write -- the
builder's too -- carries the key, and seq, the furthest change
a write left the server at, through the connection or any copy of it.
withWait -- Swift's in seconds,
Kotlin's in milliseconds, Dart's a Duration -- is a copy whose
sets and dels the server holds while they write
nothing, a worker's claim waiting there for a job
(Waiting for a job); cancelling the task or
the coroutine gives it up, as Dart's withAbort(future) -- a copy
whose requests are given up once the future completes -- does.
An app far from its tenant's home reads near itself where the tenant has
a read replica in the app's
region: connected through that region's router with reads nearest, the
builder's reads, and an asRead() copy's requests, are answered
by the replica there, each after the furthest position an answer from home
named to this connection -- its change and its history, shared by every
copy -- so the app never reads older than it wrote; writes go home.
let db = Fennec.connect(url: "https://ap.example.com/t/acme", token: jwt, reads: .nearest)
try await db.from("orders").insert(["number": "SG-1"] as Value) // home, in eu
let mine = try await db.from("orders").rows() // ap's replica, after that write
let n = try await db.asRead().query("get orders count") // a text that only readsKotlin's is Fennec.connect(url, token = jwt, reads =
FennecRemote.Reads.NEAREST), Dart's Fennec.connect(url, token:
jwt, reads: Reads.nearest), and all three have
asRead(). A write sent through asRead() is
refused (status 403), never landed.
Where the file lives
Anywhere the app may write. On iOS the app's Application Support
directory, which is backed up and never shown in the Files app; on Android
context.filesDir, or noBackupFilesDir to keep it
out of a backup; in Flutter path_provider's
getApplicationSupportDirectory(). Beside the file the library
keeps <name>.lock while it is open.
- Open a file once. A second open of the same file, in the app or
in another process -- an app extension sharing the app group's container
-- is refused (
locked): two databases over one file corrupt it. - iOS file protection. A file under the
completeprotection class cannot be read while the device is locked, and a page of a mapped file read then is the app's end, not an error. Keep the default class (complete until first user authentication), or open the file with.inMemory. - The file compacts itself. An update writes the row again at the
file's end; a thread of the library's looks every 5 s and compacts the
file beside your calls once half of it is versions updates and deletes
left behind, and at least 64 MB, so a file you keep updating stays under
about twice what it holds -- sooner once it reaches three quarters of
the memory the device has for its pages, so its reads stay in
memory. The same thread rebuilds a vector index once the entries
updates and deletes left in it outnumber its live ones, beside your
calls, so a collection whose vectors change keeps its memory. Closing
the database stops a rebuild under way rather than wait for it, and a
compact still rebuilding the vector indexes before it writes the file,
so neither holds a close as the app goes to the background more than
a fraction of a second, where a rebuild takes seconds; a compact
already writing the new file finishes first, and the close waits as
long as writing the file's live records takes. Open
with
.noAutoCompact(NO_AUTO_COMPACT,noAutoCompact) to compact, and rebuild, only when you runcompact.
How the file is mapped and locked on a device
The file is mapped, as a server maps its own: the documents stay in it
and the app holds only what it derived from them, the indexes and the
graph. The mapping takes address space past the file's end -- a gigabyte at
least -- for the writes to grow into; where the system refuses that much, as
an iOS app's address space may, the file alone is mapped and mapped again
as it grows. A 32-bit Android device (armeabi-v7a) reads the file into
memory. The lock is a file of its own, held with a record lock
(fcntl), which belongs to the process: a checkpoint renames a
new file over the database, which a lock on the database itself would not
survive, and an flock belongs to the open file, which a child
process started meanwhile shares until it runs its program -- a close and
an open again at that moment were refused.
Durability
Every write is on disk before the call returns: written and fsynced, outside the database's lock, so reads go on meanwhile and writes arriving together share one fsync. On a Mac that fsync is about 4 ms. For a burst of writes, open the file without it and sync when they are done:
| what it does | what it survives | |
|---|---|---|
| a write, by default | written and fsynced before it returns | the device losing power |
noSync, then sync() | writes wait in a buffer, then one fsync | the device losing power, once synced |
flush() | the buffer handed to the system, no fsync: microseconds | the app being killed |
checkpoint() | the file written anew, graphs and all | -- (the next open links nothing) |
close() | saves the graphs, syncs, lets the file go | everything |
A phone ends an app in the background without a word, so an app opened
with noSync calls flush() or sync()
as it goes there -- scenePhase turning to
.background, onStop, AppLifecycleState.paused.
What noSync and a refused write do
With noSync the buffer goes into the file once it holds
64 KB, written by the call that filled it after it has let go of the
database, as a server's buffered writes are: a read beside texts of 1 000
rows of 128 dimensions waits 1.1 to 1.4 ms at p99, where written under the
lock, the disk's waits and all, it waited 2.2 to 6.6 ms and up to 61.
A write the disk refuses stops the database's writes, as on a server:
every later write and sync is refused with io until the file
is opened again, and reads go on. The vectors a database holds are linked
into its graph as they are written; the open links those written since the
graph was last saved. close() saves a graph once the file has
grown three times the graph's record since its last save, so an app that
opens and closes often does not grow its file by a graph each time;
checkpoint() saves everything, and an open after it links
nothing.
The hash, text, ordered and sparse indexes are never in the file: an
open leaves each for the first statement that reads it, which builds it
first -- a @text index of 100 000 products took that search
141 ms. warm() builds them off the main thread as the app
starts, each under the read lock on its own so reads go on beside it, and
answers how many it built; warm(["docs", "orders.status"])
builds those collections' and fields' alone (Kotlin
warm(listOf(...)), Dart warm([...])).
Threads
One database is safe to use from every thread, task or coroutine at
once: a read runs beside other reads, a write takes the database to itself,
a long read — an aggregate, a scan, a text of several reads — pins what it
reads and lets writes land beside it, as a server's does, and a lone
create index or compact is built beside the
database while reads and writes go on. Every binding makes its calls
off the main thread, since any of them may wait -- for the lock, an fsync, a
whole open.
Sizes and speed
| the native library, stripped | size |
|---|---|
| macOS, Apple silicon | 3.17 MB |
| macOS, Intel | 3.45 MB |
| Linux x86-64, with the JNI functions | 3.65 MB |
The sync with a server is 165 to 198 KB of each. Linux's are the
release's libfennec_ffi.so (C on
Linux); iOS's and Android's are measured by CI for every change, in the
run's summary. An app ships the code it links: the XCFramework's static library
is the objects before the app's linker takes what it uses.
| 20 000 × 128, Apple M1 | through the library | fennec-server's handler, in process |
|---|---|---|
| open, checkpointed | 2.9 ms | 2.6 ms |
| a put with its vector, buffered | 4.6 µs | 23.2 µs |
| a put with its vector, fsynced | 4.0 ms | 4.0 ms |
near, limit 10 | 97.8 µs | 151.4 µs |
Method, and the library's timings against the server's
make ffi-bench makes the file through the library and
opens it both ways: the library as an app opens it, the server's handler as
fennec-server serves it, the request built in process with no
socket. The medians of 1 000 calls each. The library is handed a vector as
its f32 bytes, as the bindings hand it; as JSON a buffered put
takes 23.3 µs, the server's time, most of it the vector's digits.
The C ABI
Any language with a C FFI can call the library: the header is
crates/fennec-ffi/include/fennec.h, and make ffi
builds the library for this machine, or TARGET=... for another;
for Linux each release carries it built (C on
Linux).
Every call returns a code -- 0, an error's kind, or the boundary's own:
FENNEC_MISUSE, FENNEC_LOCKED,
FENNEC_PANIC -- and writes its answer or its error as
NUL-terminated JSON, freed with fennec_free_string.
fennec_query takes the parameters that are vectors beside the
JSON as their f32s, each its place and length first; a place
with its top bit set (FENNEC_VECTOR_EXACT) says each
f32 is the number the caller had, and a list of numbers takes
it as it came. An error holding "exact":[places] asks for
those parameters again inside the JSON.
Handles, panics and JNI
A handle is a number, not a pointer: a handle used after its close finds
nothing and is refused, where a pointer would reach freed memory, and a
call holds its database for as long as it runs whatever closes meanwhile.
A panic never crosses the boundary: each call runs under
catch_unwind and answers FENNEC_PANIC with its
message, which is why the library is built to unwind, not to abort. The
Kotlin binding reaches the same calls through JNI functions the library
carries itself (its jni feature): each call answers one
byte[], its code then its JSON, and text goes in as UTF-8
bytes, since JNI's own strings write an emoji as two halves.
C on Linux
Releases after 0.2.0 carry the library built for Linux with glibc, for
x86_64 and arm64, each a tarball listed in the release's
SHA256SUMS: fennec-ffi-v0.2.0-x86_64-unknown-linux-gnu.tar.gz
and fennec-ffi-v0.2.0-aarch64-unknown-linux-gnu.tar.gz.
Each holds the shared library lib/libfennec_ffi.so, the static
lib/libfennec_ffi.a, include/fennec.h and a
pkg-config file for each library, fennec-ffi and
fennec-ffi-static, which find the folder wherever it is
unpacked. A GTK app, a C or C++ program, or any language with a C FFI
links it. It runs on glibc 2.34 or later -- Ubuntu 22.04, Debian 12,
RHEL 9 and the releases after them -- and asks the system for nothing else
but libgcc_s.
TARGET=x86_64-unknown-linux-gnu # or aarch64-unknown-linux-gnu
curl -fLO https://github.com/fennec-db/fennecdb/releases/download/v0.2.0/fennec-ffi-v0.2.0-$TARGET.tar.gz
curl -fLO https://github.com/fennec-db/fennecdb/releases/download/v0.2.0/SHA256SUMS
sha256sum --ignore-missing -c SHA256SUMS
tar -xzf fennec-ffi-v0.2.0-$TARGET.tar.gz
export PKG_CONFIG_PATH=$PWD/fennec-ffi-v0.2.0-$TARGET/lib/pkgconfig
cc notes.c $(pkg-config --cflags --libs fennec-ffi) -o notes # the shared library
cc notes.c $(pkg-config --cflags --libs fennec-ffi-static) -o notes # or the static one, linked in#include <fennec.h>
#include <stdio.h>
#include <string.h>
int main(void) {
const char *path = "notes.fennec";
uint64_t db;
char *out = NULL;
if (fennec_open((const uint8_t *)path, strlen(path), 0, &db, &out, NULL) != FENNEC_OK) {
fprintf(stderr, "%s\n", out);
return 1;
}
const char *text = "create collection if not exists notes (title text); "
"put notes {title: $1}; get notes select title";
const char *params = "[\"from C\"]";
int32_t code = fennec_query(db, (const uint8_t *)text, strlen(text),
(const uint8_t *)params, strlen(params), NULL, 0, &out, NULL);
printf("%d %s\n", code, out);
fennec_free_string(out);
fennec_close(db, NULL, NULL);
return code;
}0 {"kind":"rows","result":{"columns":["title"],"rows":[{"title":"from C"}]}}A program linked to the shared library finds
libfennec_ffi.so where the loader looks: copy it into
/usr/local/lib and run ldconfig, start the program
with LD_LIBRARY_PATH naming the folder's lib, or
link it with -Wl,-rpath,<folder>/lib. The static one is
linked into the program, which then needs nothing beside it. The shared
library carries the JNI functions too, so Kotlin's
com.fennecdb:fennecdb on the JVM loads this file, named by
-Dfennec.library=<folder>/lib/libfennec_ffi.so or
FENNEC_LIBRARY.
How each release checks it
Each architecture's tarball is built on a runner of its own and held
before the release keeps it (tools/linux-ffi-check.sh): its
files, its header the repository's, the shared library's name, its
exports -- the header's functions and the JNI functions alone -- and what
it asks the system for, glibc and libgcc_s and no glibc symbol
newer than 2.34. Then a C program compiled against the header with every
warning an error is linked to each library through its pkg-config file, as
above, and run on the runner and in AlmaLinux 9, whose glibc is 2.34: it
opens a file, writes a row with parameters and reads it back, is refused a
second open of the file, finds the row after a close, and starts a sync and
drives it as a binding does. CI does the same on every change and keeps
the tarballs as the run's artifacts.