1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
//! The async API: the same store, files and engines as the blocking API, its
//! calls that wait returning a future to `.await`.
//!
//! `aio` is async I/O, as redis-rs names it. Its futures wake through
//! [`std::task::Waker`], so tokio, async-std, smol or an executor of the
//! program's own runs them: TinyStore brings no runtime, and nothing here
//! needs a `spawn`.
//!
//! ```no_run
//! # async fn example() -> tinystore::Result<()> {
//! use tinystore::Options;
//! use tinystore::aio::Store;
//!
//! let store = Store::open("data", Options::default())?; // opening stays a plain call
//! let stock = store.bucket::<i64>("stock").open()?; // and so does opening a handle
//! stock.set("sku-1", &10).await?;
//! let left = store.tx(move |tx| tx.with(&stock).get("sku-1")).await?; // the transaction is awaited
//! store.close().await?;
//! # Ok(())
//! # }
//! ```
//!
//! [`Store`] opens, crosses to and from the blocking [`crate::Store`], runs a
//! transaction and closes, and every engine's handles open from it, each in a
//! module of the engine's name with its blocking builder's options: [`kv`],
//! [`config`], [`locks`], [`channels`], [`jobs`], [`sql`], [`blobs`],
//! [`search`], [`records`], [`metrics`] and [`backup`]. A bucket, a queue or
//! a table's index kept in a database opens from [`sql::Database`]. Opening
//! a store or a handle stays a plain call, made once at start, since a first
//! open may migrate a file.
//!
//! # One store, two APIs
//!
//! [`Store::from`] takes a blocking [`crate::Store`], and [`Store::blocking`]
//! gives it back: each is a cheap clone of the same store, its engines and
//! its files, never a second store. A command-line tool can call the blocking
//! API while a server beside it awaits, on one store.
//!
//! # Where the work runs
//!
//! | A call | Runs | Its future dropped once polled |
//! |-------------------|------------------------------------------------------|---------------------------------------|
//! | a write | queued at its file's writer in the first poll | commits all the same; its answer goes |
//! | a point read | first poll if a reader is idle, else the pool | answered then, or stops as a read |
//! | a page or a query | on the store's pool | stops, and its reader goes back |
//! | a wait | a waiter in the engine, on no thread | leaves; nothing else changes |
//! | a hold of a lock | a waiter in its key's line, then its work | leaves the line, or lets the key go |
//! | a stream | woken by a change, or a page at a time | stops |
//! | a worker | its handlers in its future, claims on its own thread | the abort: see [`jobs`] |
//! | `store.tx` | its closure, blocking, on the store's pool | runs to its end |
//! | `close` | on a thread the call starts | closes all the same |
//!
//! A write is a key's `set`, a job's `add`, a statement's `exec` or an
//! append; a point read a key's `get`, a file's `head` or an index's `get`;
//! a page or a query a bucket's `list`, a database's `all` or a search; a
//! wait a `once` run's for the run of its key under way or a tail's next
//! batch; a stream a channel's messages, a config's or a queue's watch, or a
//! walk over every key, job, file or record. An index's `ready` waits so too,
//! and looks at the index's status on the pool each time the index wakes it.
//! A metrics write and a backup run blocking on the pool, to their end, and a
//! config's `get` and a channel's `publish` are plain calls, in memory. Each
//! engine's module says which of its calls are which.
//!
//! None commits, syncs the disk or waits on the thread that polls its
//! future, so an executor's thread never waits on the disk. The pool is the
//! store's own threads, at most sixteen, started as calls need them, each
//! ended after a minute without work and all of them stopped when the store
//! closes. A commit that finds all sixteen busy runs on a thread started for
//! it, since they may all wait for what it frees: the memory its writes hold.
//!
//! # Dropping a future
//!
//! A future does nothing before its first poll: one made and dropped unpolled
//! reads and writes nothing. After that:
//!
//! - **A read whose future is dropped stops**: its statement is interrupted
//! and its reader goes back at once, as a [`CancellationToken`]'s cancel
//! stops it. `tokio::time::timeout(d, call)` stops a read this way when it
//! expires, and a token around the call that makes a future,
//! `token.run(|| api.list().count())`, still stops it, `Cancelled`.
//! - **A write that was polled commits** whether or not anyone still awaits
//! it, since a grouped commit cannot stop half-way: only its answer is lost.
//! To know that a write happened, await it.
//! - A transaction that was polled runs to its end, as a write does.
//!
//! [`CancellationToken`]: crate::CancellationToken
//!
//! On a node that copies a file from its home, a write passed on to the home
//! waits for its answer on a thread of its own rather than the pool's, and so
//! does a `once` run's claim of its key there, which the home answers once
//! the run under way ends: a pool held by such waits would stall every other
//! call.
//!
//! # A transaction's closure is blocking
//!
//! [`Store::tx`] runs its closure on the store's pool, never on the
//! executor's thread: it holds its file's writer, so it must not wait for the
//! program's awaits. Inside, calls go through `tx.with(&handle)` and are
//! blocking, as in [`crate::Store::tx`]. A panic in the closure rolls the
//! transaction back and is resumed at the `.await`.
//!
//! # Errors and answers
//!
//! Every call answers as its blocking twin does, with the same
//! [`Error`](crate::Error) and [`ErrorKind`](crate::ErrorKind). A call made
//! once its store closed, or still waiting for the pool as it closed, fails
//! `Closed`. An awaited call from inside a transaction's closure, through a
//! `block_on`, fails `Invalid`, as a blocking call around a transaction does.
//!
//! # Closing
//!
//! `store.close().await` closes the store on a thread the call starts, which
//! waits for background work, the handlers running and every engine's last
//! writes. A store whose last handle is dropped closes on the dropping
//! thread, which may be the executor's: await `close` instead.
pub use Store;