Reference / KV API for Rust
KV API for Rust
Every public type, trait and function of the KV engine in tinystore::kv and tinystore::aio::kv, generated from its source. The KV guide explains how to use them, and the Bun and Node, Python and Go pages list the same API.
Store
impl StoreStore.bucket
pub fn bucket<V: Value>(&self, name: &str) -> BucketBuilder<V>A bucket of values of type V by key, in the store's kv.db; the engine
opens with the first handle.
Store.counters
pub fn counters(&self, name: &str) -> CountersBuilderNumbers by key that add up, in the store's kv.db: attempts, hits, uses.
Store.onceNew in 0.13.1
pub fn once<V: Value>(&self, name: &str) -> OnceBuilder<V>Functions run once a key, their answers kept in the store's kv.db: a request sent twice gets the first one's answer instead of doing its work again, as an idempotency key promises.
Store.quota
pub fn quota(&self, name: &str) -> QuotaBuilderUses a key may make in windows that each reset on their own, in the
store's kv.db: store.quota("ai").window("daily", 100, DAY).
Store.rate_limit
pub fn rate_limit(&self, name: &str) -> RateLimitBuilderRequests let through at a steady rate a key, with room for a burst, in
the store's kv.db: store.rate_limit("api").rate(100, Duration::from_secs(1)).
aio::Store
impl aio::Storeaio::Store.bucket
pub fn bucket<V: Value>(&self, name: &str) -> BucketBuilder<V>A bucket of values of type V by key, in the store's kv.db, whose
calls are awaited.
aio::Store.counters
pub fn counters(&self, name: &str) -> CountersBuilderNumbers by key that add up, in the store's kv.db, whose calls are awaited.
aio::Store.once
pub fn once<V: Value>(&self, name: &str) -> OnceBuilder<V>Functions run once a key, their answers kept in the store's kv.db, as
crate::Store::once keeps them, the functions here async.
aio::Store.quota
pub fn quota(&self, name: &str) -> QuotaBuilderUses a key may make in windows that each reset on their own, in the store's kv.db, whose calls are awaited.
aio::Store.rate_limit
pub fn rate_limit(&self, name: &str) -> RateLimitBuilderRequests let through at a steady rate a key, with room for a burst, in the store's kv.db, whose calls are awaited.
Allowance
pub struct Allowance {
pub ok: bool,
pub left: u64,
pub retry_at: Option<SystemTime>,
pub windows: Vec<Window>,
}What a rate limit or a quota answers a request: whether it passed, and what is left.
Allowance.window
pub fn window(&self, name: &str) -> Option<&Window>The window named name, of a quota's answer.
Window
pub struct Window {
pub name: String,
pub used: u64,
pub limit: u64,
pub left: u64,
pub resets_at: Option<SystemTime>,
}One window of a quota, as a key stands in it.
All
pub struct All<V> { /* private fields */ }Every key of a branch, a page at a time.
Bucket
pub struct Bucket<V> { /* private fields */ }Values of one type by key. A handle is cheap to clone and to keep; under
gives the same bucket seen from a branch.
Bucket.set
pub fn set(&self, key: impl Key, value: &V) -> Result<()>Writes value under key. A key that is there keeps its ttl; a new one
takes the bucket's, and an idle key lives its span from the write.
Bucket.create
pub fn create(&self, key: impl Key, value: &V) -> Result<bool>Writes value only when key is not there, and says whether it did; an
expired key is not there. A key that is there is false, never an error.
Bucket.update
pub fn update(&self, key: impl Key, change: impl FnMut(Option<V>) -> Option<V>) -> Result<Option<V>>Writes what change makes of the value under key without losing a
write made meanwhile: it reads the key, calls change with its value,
or None where there is none, and writes what change returns at the
version it read, or creates the key. When another write came first it
reads again and calls change again, eight tries at most, then fails
Conflict; so change may run more than once, and must change
nothing else. A change that returns None writes nothing. It answers
the value written, or the one there when nothing was.
A key that is there keeps its expiry, and one it creates takes the
bucket's, as set and create do.
let pinned = store.bucket::<Vec<i64>>("pinned-courses").open()?;
// a phone and a laptop pinning at once both keep their course
pinned.update("97516", |courses| {
let mut courses = courses.unwrap_or_default();
courses.push(2518);
Some(courses)
})?;Bucket.key
pub fn key(&self, key: impl Key) -> KeyCall<'_, V>A call on key with what it asks beside its value, a new expiry or a
version the key must still be at, run by its last step:
sessions.key(&token).if_version(seen).set(&next).
Bucket.get
pub fn get(&self, key: impl Key) -> Result<Option<V>>Bucket.entry
pub fn entry(&self, key: impl Key) -> Result<Option<Entry<V>>>The value with its version and expiry.
Bucket.take
pub fn take(&self, key: impl Key) -> Result<Option<V>>Reads key and removes it in one commit: of two callers taking the same
key, one gets the value. A value that no longer reads as the bucket's
type is Corrupt, and stays.
Bucket.list
pub fn list(&self, limit: usize, after: Option<&str>) -> Result<Page<V>>One page of this branch's own keys, after after when it continues
another; limit keys at most, one at least, and never more than
PAGE_KEYS.
Bucket.all
pub fn all(&self) -> All<V>Every key of this branch, a page at a time. No snapshot is held between pages, so a slow loop keeps no reader, and a key written meanwhile may or may not be met.
Bucket.under
pub fn under(&self, owner: impl Key) -> Bucket<V>The same bucket seen from the branch owner names under this one:
sessions.under(user_id).clear() signs a user out everywhere.
Bucket.has
pub fn has(&self, key: impl Key) -> Result<bool>Bucket.count
pub fn count(&self) -> Result<u64>How many keys all would walk now: this branch's own live keys, not
those of the branches under it. An expired key not yet swept and a
cleared one are not counted, and an idle key is counted without being
renewed. It reads 65,536 keys a snapshot and holds none between
them, so a key written meanwhile may or may not be counted.
Bucket.delete
pub fn delete(&self, key: impl Key) -> Result<bool>Removes key and says whether a live key was there.
Bucket.expire
pub fn expire(&self, key: impl Key, ttl: Duration) -> Result<bool>Gives a live key a new expiry, ttl from now, as Redis's EXPIRE does,
keeping its value and version; says whether the key was there.
Bucket.clear
pub fn clear(&self) -> Result<()>Removes every key of this branch and of the branches under it, at once for every reader, however many there are.
Bucket.reseal
pub fn reseal(&self) -> Result<u64>Seals again with the store's sealing key every value of this branch,
and of the branches under it, that an older key sealed, and says how
many: once every value of the bucket is, an older key can leave the
store's options. It writes a batch of values a commit, each keeping
its version and its expiry. A value sealed with a key the store does
not hold stops it, Invalid, naming that key: the batches before it
stay sealed again, and a reseal after the key is given goes on.
Bucket.add
pub fn add(&self, key: impl Key) -> Result<bool>Adds key to a bucket of keys alone, a set, and says whether it was new:
seen.add(event_id) is false for an event already handled.
BucketBuilder
pub struct BucketBuilder<V> { /* private fields */ }A bucket being opened: its name, its type, how its keys expire, whether its values are encrypted.
BucketBuilder.ttl
pub fn ttl(self, ttl: Duration) -> SelfKeys expire this long after they are written, unless a write says otherwise.
BucketBuilder.idle
pub fn idle(self, idle: Duration) -> SelfKeys expire this long after they were last read or written.
BucketBuilder.encrypted
pub fn encrypted(self) -> SelfKeeps every value sealed with the store's encryption key, as a password or a token is kept: the file holds what nobody reads without the key, and a value copied under another key does not open. A key is not sealed, so a secret is a value and never a key. A name keeps whether it is encrypted: it opens the same way every time.
let passwords = store.bucket::<String>("source-passwords").encrypted().open()?;
passwords.set("source-42", &"hunter2".to_owned())?;BucketBuilder.open
pub fn open(self) -> Result<Bucket<V>>Entry
pub struct Entry<V> {
pub key: String,
pub value: V,
pub version: Version,
pub expires_at: Option<SystemTime>,
}A key's value with what a conditional write needs.
PAGE_KEYS
pub const PAGE_KEYS: usizeKeys a page holds at most.
Page
pub struct Page<V> {
pub entries: Vec<Entry<V>>,
pub next: Option<String>,
}One page of a branch's keys, in the byte order of their text.
Version
pub struct Version(_)The revision of kv.db that wrote a value. It never repeats, across deletes, expiry and restarts, and it compares only for equality.
Counters
pub struct Counters { /* private fields */ }Numbers by key that only add up; a counter that is not there holds 0. Every handle on a name counts the same way, in the file or in memory.
Counters.under
pub fn under(&self, owner: impl Key) -> CountersThe same counters seen from the branch owner names under these.
Counters.add
pub fn add(&self, key: impl Key, n: i64) -> Result<i64>Adds n to the counter under key and returns what it holds now. A
counter that is not there starts from 0, its ttl from now; a sum past
the range of an i64 is Limit and changes nothing.
Counters.get
pub fn get(&self, key: impl Key) -> Result<i64>What the counter under key holds; 0 when it is not there.
Counters.delete
pub fn delete(&self, key: impl Key) -> Result<bool>Removes the counter under key and says whether it was there.
Counters.clear
pub fn clear(&self) -> Result<()>Removes every counter of this branch and of the branches under it, at once for every reader, however many there are.
CountersBuilder
pub struct CountersBuilder { /* private fields */ }Counters being opened: their name, how long a counter lasts, and whether they are kept in memory between flushes.
CountersBuilder.ttl
pub fn ttl(self, ttl: Duration) -> SelfA counter lasts this long from its first add, then starts again from 0: the adds after the first do not move it.
CountersBuilder.flush_every
pub fn flush_every(self, span: Duration) -> SelfKeeps the counters in memory and writes what changed once a span, so that an add costs no commit and a crash loses at most the last span. Without it each add is written before it returns.
CountersBuilder.open
pub fn open(self) -> Result<Counters>Maintenance
pub struct Maintenance {
pub expired: usize,
pub cleared: usize,
pub renewed: usize,
pub flushed: usize,
}What one maintenance pass did.
Fresh
pub struct Fresh<V> { /* private fields */ }Once's answers, each answered as it is while younger than fresh, then
answered at once while one new answer is made in the background, until
keep ends:
let (fresh, keep) = (Duration::from_mins(10), Duration::from_hours(12));
let books = store.once::<String>("cabinet-books").fresh(fresh).keep(keep).open()?;
let user = 7;
let answer = books.fetch(user, move || book(user))?;
println!("{} made {:?} ago, stale: {}", answer.value, answer.age, answer.stale);| The kept answer | What the caller gets | What is built |
|---|---|---|
younger than fresh | it, at once | nothing |
between fresh and keep | it at once, stale | one new answer in the background |
past keep, or none | the new answer | one, which every caller of the key waits for |
A build in the background runs on a thread the store keeps for them, four
at most at once and none of those its other calls run on, so its function
is Send + 'static; its answer replaces the kept one and starts both
terms again, and its error keeps the kept one until keep ends, while no
other build of its key starts for a tenth of fresh, a second at least
and a minute at most. One build of a key runs at a time across every
client of the store; on a node that copies kv.db, one a node.
Fresh.fetch
pub fn fetch<E, W>(&self, key: impl Key, work: W) -> Result<Fetched<V>, E>
where
E: From<Error> + Send + 'static,
W: FnOnce() -> Result<V, E> + Send + 'static,The answer kept under key, with how old it is, or work's once
none is kept, which is then kept, as Once::run keeps it. A kept
answer older than fresh is answered at once, stale, and work
makes the new one in the background, unless a build of the key is
under way or failed within its pause.
Fresh.run
pub fn run<E, W>(&self, key: impl Key, work: W) -> Result<V, E>
where
E: From<Error> + Send + 'static,
W: FnOnce() -> Result<V, E> + Send + 'static,Fresh::fetch's value alone.
Fresh.get
pub fn get(&self, key: impl Key) -> Result<Option<V>>The answer kept under key, stale or not.
Fresh.delete
pub fn delete(&self, key: impl Key) -> Result<bool>Forgets the answer kept under key, so that the next run runs again;
says whether there was one. A build of it in the background under way
meanwhile keeps nothing.
Fresh.clear
pub fn clear(&self) -> Result<()>Removes every answer of this branch and of the branches under it, in one commit. A build in the background under way meanwhile keeps nothing; a run of a key that had none keeps its answer once it ends.
Fresh.reseal
pub fn reseal(&self) -> Result<u64>Seals again with the store's sealing key every answer an older key
sealed, as Once::reseal does.
Fresh.under
pub fn under(&self, owner: impl Key) -> Fresh<V>The same answers seen from the branch owner names under these.
FreshBuilder
pub struct FreshBuilder<V> { /* private fields */ }Answers being opened with fresh: their name and type, how long one is
answered without making a new one, and how long one is kept at all.
FreshBuilder.keepNew in 0.13.1
pub fn keep(self, keep: Duration) -> SelfHow long an answer is kept from when it was made; a day unless said.
FreshBuilder.encrypted
pub fn encrypted(self) -> SelfKeeps every answer sealed with the store's encryption key, as
OnceBuilder::encrypted says.
FreshBuilder.openNew in 0.13.1
pub fn open(self) -> Result<Fresh<V>>Opens the answers; a fresh of zero, or as long as keep or longer,
is Invalid.
KeyCall
pub struct KeyCall<'a, V> { /* private fields */ }A call on one key with what it asks beside its value, run by its last step:
sessions.key(&token).if_version(seen).set(&next).
KeyCall.ttl
pub fn ttl(self, ttl: Duration) -> SelfThe key expires ttl from now, for set, create and expire.
KeyCall.expires_at
pub fn expires_at(self, time: SystemTime) -> SelfThe key expires at time, for set, create and expire.
KeyCall.if_version
pub fn if_version(self, version: Version) -> SelfThe call applies only to a live key still at version: a key written,
expired or deleted since is a Conflict.
KeyCall.set
pub fn set(self, value: &V) -> Result<()>KeyCall.create
pub fn create(self, value: &V) -> Result<bool>Writes only when the key is not there, and says whether it did.
KeyCall.take
pub fn take(self) -> Result<Option<V>>KeyCall.delete
pub fn delete(self) -> Result<bool>Removes the key and says whether a live key was there.
KeyCall.expire
pub fn expire(self) -> Result<bool>Gives a live key the expiry ttl or expires_at named, keeping its
value and version, and says whether it was there.
Fetched
pub struct Fetched<V> {
pub value: V,
pub age: Duration,
pub stale: bool,
}An answer with how old it is: stale once it is older than the fresh
its answers opened with, and never for answers opened without one.
Once
pub struct Once<V> { /* private fields */ }Functions run at most once a key, their answers kept: a charge, an import, a webhook's effect, done once however often the request arrives.
Once.under
pub fn under(&self, owner: impl Key) -> Once<V>The same answers seen from the branch owner names under these.
Once.fetch
pub fn fetch<E: From<Error>>(
&self,
key: impl Key,
work: impl FnOnce() -> Result<V, E>,
) -> Result<Fetched<V>, E>Once::run's answer with how old it is: no older than keep, and
never stale, since these answers were opened without fresh.
let charges = store.once::<String>("charges").open()?;
let receipt = charges.fetch("key-7f", || Ok::<_, tinystore::Error>("receipt 1".to_owned()))?;
println!("{} made {:?} ago", receipt.value, receipt.age);Once.run
pub fn run<E: From<Error>>(
&self,
key: impl Key,
work: impl FnOnce() -> Result<V, E>,
) -> Result<V, E>The answer kept under key, or work's, which is then kept.
While another run of the key is under way, in this process or through its server, this one waits for it and returns its answer. An error keeps nothing, so the next run runs again; an answer meant to be kept, a card declined, is a value rather than an error. An answer that could not be kept is still returned, to this caller and to those that waited for this run, and the failure logged.
Once.clear
pub fn clear(&self) -> Result<()>Removes every answer of this branch and of the branches under it, in one commit, so that the next run of each key runs again. A run under way meanwhile keeps its answer once it ends.
Once.get
pub fn get(&self, key: impl Key) -> Result<Option<V>>The answer kept under key.
Once.reseal
pub fn reseal(&self) -> Result<u64>Seals again with the store's sealing key every answer of this branch,
and of the branches under it, that an older key sealed, and says how
many, as Bucket::reseal does an encrypted bucket's values; answers
opened without encrypted are Invalid.
Once.delete
pub fn delete(&self, key: impl Key) -> Result<bool>Forgets the answer kept under key, so that the next run runs again;
says whether there was one.
OnceBuilder
pub struct OnceBuilder<V> { /* private fields */ }Answers being opened: their name and type, how long one is kept, and whether they are encrypted.
OnceBuilder.keep
pub fn keep(self, keep: Duration) -> SelfHow long an answer is kept from when it was made; a day unless said. After that, a run of its key runs again.
OnceBuilder.encryptedNew in 0.13.1
pub fn encrypted(self) -> SelfKeeps every answer sealed with the store's encryption key, as an encrypted bucket keeps its values, for answers nobody should read in the file: a statement, a grade book. A name keeps whether it is encrypted: it opens the same way every time.
let books = store.once::<String>("grade-books").encrypted().open()?;
let book = books.run("student-1", || Ok::<_, tinystore::Error>("grades of student 1".to_owned()))?;OnceBuilder.freshNew in 0.13.1
pub fn fresh(self, fresh: Duration) -> FreshBuilder<V>How long an answer is answered without making a new one, shorter than
keep: past it, the kept answer is answered at once and one new one
is made in the background, as crate::kv::Fresh says.
OnceBuilder.open
pub fn open(self) -> Result<Once<V>>Key
pub trait KeyWhat names a key, an owner or a folder: text, or an integer spelled in
decimal, so that 42 and "42" are one key.
Key.text
fn text(&self) -> Cow<'_, str>Quota
pub struct Quota { /* private fields */ }Uses a key may make in each of its windows, such as 100 messages every five
hours and 300 a week, counted by one allow.
A window starts at a key's first use after the last one ended and lasts its span, so each key resets on its own:
window("session", 100, 5 h): first use 13:42 → resets at 18:42;
a use at 19:07 starts the next, which resets at 00:07An allow counts in every window or in none, in one durable write: two
quotas asked one after the other would leave the first counted when the
second refused.
Quota.under
pub fn under(&self, owner: impl Key) -> QuotaThe same quota seen from the branch owner names under this one, whose
keys are counted apart.
Quota.allow
pub fn allow(&self, key: impl Key) -> Result<Allowance>Uses one of each of key's windows, or none when one has no room left.
retry_at is when the windows that refused reset.
Quota.allow_n
pub fn allow_n(&self, key: impl Key, n: u64) -> Result<Allowance>Uses n of each of key's windows at once, or none when one has no
room for n. More than a window's limit never passes, and is Invalid.
Quota.peek
pub fn peek(&self, key: impl Key) -> Result<Allowance>key's windows, using nothing: ok says whether one more use would
pass now.
Quota.refund
pub fn refund(&self, key: impl Key) -> Result<()>Gives one use back to each of key's windows that has not reset since.
Quota.refund_n
pub fn refund_n(&self, key: impl Key, n: u64) -> Result<()>Gives n uses back, never below nothing.
Quota.reset
pub fn reset(&self, key: impl Key) -> Result<()>Forgets key's windows, so that its next use starts each anew.
QuotaBuilder
pub struct QuotaBuilder { /* private fields */ }A quota being opened: its name and its windows.
QuotaBuilder.window
pub fn window(self, name: &str, limit: u64, span: Duration) -> SelfLets a key use up to limit every span from its first use. A name is
a lowercase letter, then up to 31 of them, digits and '_'; a quota has
one to eight windows.
QuotaBuilder.open
pub fn open(self) -> Result<Quota>RateLimit
pub struct RateLimit { /* private fields */ }Requests let through at a steady rate a key, with room for a burst.
It is the generic cell rate algorithm: a key holds one time, when its next request is due, so no window has an edge that lets twice the rate through, and a key costs one number.
rate(3, 3 s), burst 3: three pass at once, and the fourth may pass 1 s laterThe times live in memory and reach kv.db every second and at close: a crash forgets at most the last second, which lets at most one burst more through. A key whose time has come is as one never seen.
RateLimit.under
pub fn under(&self, owner: impl Key) -> RateLimitThe same rate limit seen from the branch owner names under this one,
whose keys are counted apart.
RateLimit.allow
pub fn allow(&self, key: impl Key) -> Result<Allowance>Asks for one request of key.
RateLimit.allow_n
pub fn allow_n(&self, key: impl Key, n: u64) -> Result<Allowance>Asks for n requests of key at once: all of them pass or none does.
More than the burst never passes, and is Invalid.
RateLimit.peek
pub fn peek(&self, key: impl Key) -> Result<Allowance>Whether one request of key would pass now, using nothing.
RateLimit.reset
pub fn reset(&self, key: impl Key) -> Result<()>Forgets key's requests, so that its next one has the whole burst.
RateLimitBuilder
pub struct RateLimitBuilder { /* private fields */ }A rate limit being opened: its name, its rate and its burst.
RateLimitBuilder.rate
pub fn rate(self, count: u64, per: Duration) -> SelfLets count requests a key through every per.
RateLimitBuilder.burst
pub fn burst(self, burst: u64) -> SelfHow many requests may pass at once before the rate holds the next; the rate's count unless said.
RateLimitBuilder.open
pub fn open(self) -> Result<RateLimit>TxBucket
pub struct TxBucket<'a, V> { /* private fields */ }A bucket's calls inside a transaction.
TxBucket.get
pub fn get(&self, key: impl Key) -> Result<Option<V>>TxBucket.entry
pub fn entry(&self, key: impl Key) -> Result<Option<Entry<V>>>TxBucket.has
pub fn has(&self, key: impl Key) -> Result<bool>TxBucket.set
pub fn set(&self, key: impl Key, value: &V) -> Result<()>TxBucket.create
pub fn create(&self, key: impl Key, value: &V) -> Result<bool>TxBucket.take
pub fn take(&self, key: impl Key) -> Result<Option<V>>Takes key; a value that no longer reads as the bucket's type is
Corrupt, and stays, and the transaction goes on.
TxBucket.delete
pub fn delete(&self, key: impl Key) -> Result<bool>TxBucket.expire
pub fn expire(&self, key: impl Key, ttl: Duration) -> Result<bool>TxBucket.key
pub fn key(&self, key: impl Key) -> KeyCall<'a, V>A call on key with what it asks beside its value, inside the
transaction: tx.with(&sessions).key(&token).if_version(seen).set(&next).
TxBucket.clear
pub fn clear(&self) -> Result<()>Removes every key of the bucket's branch and of the branches under it.
TxBucket.list
pub fn list(&self, limit: usize, after: Option<&str>) -> Result<Page<V>>One page of the branch's own keys, as Bucket::list reads it.
TxCounters
pub struct TxCounters<'a> { /* private fields */ }Counters' calls inside a transaction. Counters kept in memory join no
transaction: their calls through it are Invalid.
TxCounters.add
pub fn add(&self, key: impl Key, n: i64) -> Result<i64>TxCounters.get
pub fn get(&self, key: impl Key) -> Result<i64>TxCounters.delete
pub fn delete(&self, key: impl Key) -> Result<bool>Bytes
pub struct Bytes(pub Vec<u8>)Bytes kept as they are rather than as a JSON list of numbers.
Value
pub trait ValueA type a bucket can keep: anything serde reads and writes.
maintain
pub fn maintain(store: &Store) -> Result<Maintenance>Writes what kv keeps in memory, then deletes what expired and what large clears hid. The store runs it every minute, and writes memory every second; a store opened without background work leaves both to the caller.
aio::kv::All
pub struct All<V> { /* private fields */ }Every key of a branch, a page at a time: a Stream of entries, which
ends once the last page is read, or after the error that stopped it, a
store closed included.
aio::kv::Bucket
pub struct Bucket<V> { /* private fields */ }Values of one type by key, their calls awaited: the same bucket as
crate::kv::Bucket, cheap to clone and to keep.
aio::kv::Bucket.set
pub fn set(&self, key: impl Key, value: &V) -> impl Future<Output = Result<()>> + Send + 'staticWrites value under key, as crate::kv::Bucket::set does. The
value is encoded when the call is made.
aio::kv::Bucket.create
pub fn create(
&self,
key: impl Key,
value: &V,
) -> impl Future<Output = Result<bool>> + Send + 'staticWrites value only when key is not there, and says whether it did.
aio::kv::Bucket.update
pub fn update(
&self,
key: impl Key,
change: impl FnMut(Option<V>) -> Option<V> + Send + 'static,
) -> impl Future<Output = Result<Option<V>>> + Send + 'staticWrites what change makes of the value under key without losing a
write made meanwhile, as crate::kv::Bucket::update does: another
write that came first has it read again and call change again, eight
tries at most, so change must change nothing else. It answers the
value written, or the one there when change returns None.
Each read runs as get's, change in the poll on the thread that
polls the future, and each write is queued as set's. Once polled,
the try submitted commits whether or not the future is still awaited;
a try after a conflict is made only while it is polled.
aio::kv::Bucket.key
pub fn key(&self, key: impl Key) -> KeyCall<'_, V>A call on key with what it asks beside its value, run by its last
step: sessions.key(&token).if_version(seen).set(&next).await.
aio::kv::Bucket.get
pub fn get(&self, key: impl Key) -> impl Future<Output = Result<Option<V>>> + Send + 'staticThe value under key. A key of an idle bucket read in its last
minute is renewed before the read answers: that read runs on the
store's pool, where it may wait for the writer.
aio::kv::Bucket.entry
pub fn entry(
&self,
key: impl Key,
) -> impl Future<Output = Result<Option<Entry<V>>>> + Send + 'staticThe value with its version and expiry, read as get reads it.
aio::kv::Bucket.has
pub fn has(&self, key: impl Key) -> impl Future<Output = Result<bool>> + Send + 'staticWhether key is there, read as get reads it.
aio::kv::Bucket.take
pub fn take(&self, key: impl Key) -> impl Future<Output = Result<Option<V>>> + Send + 'staticReads key and removes it in one commit: of two callers taking the
same key, one gets the value. A value that no longer reads as the
bucket's type is Corrupt, and stays.
aio::kv::Bucket.delete
pub fn delete(&self, key: impl Key) -> impl Future<Output = Result<bool>> + Send + 'staticRemoves key and says whether a live key was there.
aio::kv::Bucket.expire
pub fn expire(
&self,
key: impl Key,
ttl: Duration,
) -> impl Future<Output = Result<bool>> + Send + 'staticGives a live key a new expiry, ttl from now, keeping its value and
version; says whether the key was there.
aio::kv::Bucket.clear
pub fn clear(&self) -> impl Future<Output = Result<()>> + Send + 'staticRemoves every key of this branch and of the branches under it, at once for every reader, however many there are.
aio::kv::Bucket.reseal
pub fn reseal(&self) -> impl Future<Output = Result<u64>> + Send + 'staticSeals again with the store's sealing key what an older key sealed, as
crate::kv::Bucket::reseal does, on the store's pool: its batches
commit one after another, and once polled it runs to its end.
aio::kv::Bucket.list
pub fn list(
&self,
limit: usize,
after: Option<&str>,
) -> impl Future<Output = Result<Page<V>>> + Send + 'staticOne page of this branch's own keys, after after when it continues
another, as crate::kv::Bucket::list reads it.
aio::kv::Bucket.count
pub fn count(&self) -> impl Future<Output = Result<u64>> + Send + 'staticHow many keys all would walk now, as crate::kv::Bucket::count
counts them.
aio::kv::Bucket.all
pub fn all(&self) -> All<V>Every key of this branch, a page at a time, as a Stream. No
snapshot is held between pages, so a slow loop keeps no reader, and a
key written meanwhile may or may not be met. Dropped, it stops the
page under way and reads no more.
aio::kv::Bucket.under
pub fn under(&self, owner: impl Key) -> Bucket<V>The same bucket seen from the branch owner names under this one:
sessions.under(user_id).clear().await signs a user out everywhere.
aio::kv::Bucket.blocking
pub fn blocking(&self) -> &Bucket<V>The same bucket, for its blocking calls.
aio::kv::Bucket.add
pub fn add(&self, key: impl Key) -> impl Future<Output = Result<bool>> + Send + 'staticAdds key to a bucket of keys alone, a set, and says whether it was
new: seen.add(event_id).await is false for an event already handled.
aio::kv::BucketBuilder
pub struct BucketBuilder<V> { /* private fields */ }A bucket being opened, as crate::kv::BucketBuilder is: its name, its
type, how its keys expire, whether its values are encrypted.
aio::kv::BucketBuilder.ttl
pub fn ttl(self, ttl: Duration) -> SelfKeys expire this long after they are written, unless a write says otherwise.
aio::kv::BucketBuilder.idle
pub fn idle(self, idle: Duration) -> SelfKeys expire this long after they were last read or written.
aio::kv::BucketBuilder.encrypted
pub fn encrypted(self) -> SelfKeeps every value sealed with the store's encryption key, as
crate::kv::BucketBuilder::encrypted does.
aio::kv::BucketBuilder.open
pub fn open(self) -> Result<Bucket<V>>Opens the bucket: a plain call, made once at start, since the engine's first handle may migrate kv.db.
aio::kv::KeyCall
pub struct KeyCall<'a, V> { /* private fields */ }A call on one key with what it asks beside its value, run by its last
step, awaited: sessions.key(&token).if_version(seen).set(&next).await.
aio::kv::KeyCall.ttl
pub fn ttl(self, ttl: Duration) -> SelfThe key expires ttl from now, for set, create and expire.
aio::kv::KeyCall.expires_at
pub fn expires_at(self, time: SystemTime) -> SelfThe key expires at time, for set, create and expire.
aio::kv::KeyCall.if_version
pub fn if_version(self, version: Version) -> SelfThe call applies only to a live key still at version: a key written,
expired or deleted since is a Conflict.
aio::kv::KeyCall.set
pub fn set(self, value: &V) -> impl Future<Output = Result<()>> + Send + 'staticaio::kv::KeyCall.create
pub fn create(self, value: &V) -> impl Future<Output = Result<bool>> + Send + 'staticWrites only when the key is not there, and says whether it did.
aio::kv::KeyCall.take
pub fn take(self) -> impl Future<Output = Result<Option<V>>> + Send + 'staticaio::kv::KeyCall.delete
pub fn delete(self) -> impl Future<Output = Result<bool>> + Send + 'staticRemoves the key and says whether a live key was there.
aio::kv::KeyCall.expire
pub fn expire(self) -> impl Future<Output = Result<bool>> + Send + 'staticGives a live key the expiry ttl or expires_at named, keeping its
value and version, and says whether it was there.
aio::kv::Counters
pub struct Counters { /* private fields */ }Numbers by key that only add up, their calls awaited: the same counters
as crate::kv::Counters.
What the builder chose decides where a call runs, never the call: counters written each add queue at kv.db's writer, and counters kept in memory step there in the first poll.
On a node that copies kv.db, an add of counters written each add is queued
on that node and answered at once, on the store's pool, and a get reads
the node's copy there. Every other add, and each delete and clear,
passes on to the file's home and waits for its answer on a thread of its
own.
aio::kv::Counters.add
pub fn add(&self, key: impl Key, n: i64) -> impl Future<Output = Result<i64>> + Send + 'staticAdds n to the counter under key and returns what it holds now, as
crate::kv::Counters::add does. Counters kept in memory answer in
the first poll, unless a key memory does not hold yet is read or
memory must flush first: that step runs on the store's pool, and once
polled it counts whether or not its future is still awaited.
aio::kv::Counters.get
pub fn get(&self, key: impl Key) -> impl Future<Output = Result<i64>> + Send + 'staticWhat the counter under key holds; 0 when it is not there.
aio::kv::Counters.delete
pub fn delete(&self, key: impl Key) -> impl Future<Output = Result<bool>> + Send + 'staticRemoves the counter under key and says whether it was there.
aio::kv::Counters.clear
pub fn clear(&self) -> impl Future<Output = Result<()>> + Send + 'staticRemoves every counter of this branch and of the branches under it. Counters kept in memory hold that memory across the clear's commit, so their clear runs on the store's pool, to its end once polled.
aio::kv::Counters.under
pub fn under(&self, owner: impl Key) -> CountersThe same counters seen from the branch owner names under these.
aio::kv::Counters.blocking
pub fn blocking(&self) -> &CountersThe same counters, for their blocking calls.
aio::kv::CountersBuilder
pub struct CountersBuilder { /* private fields */ }Counters being opened, as crate::kv::CountersBuilder are: their
name, how long a counter lasts, and whether they are kept in memory
between flushes.
aio::kv::CountersBuilder.ttl
pub fn ttl(self, ttl: Duration) -> SelfA counter lasts this long from its first add, then starts again from 0.
aio::kv::CountersBuilder.flush_every
pub fn flush_every(self, span: Duration) -> SelfKeeps the counters in memory and writes what changed once a span: an add is then a step in memory, answered in its first poll.
aio::kv::CountersBuilder.open
pub fn open(self) -> Result<Counters>Opens the counters: a plain call, made once at start.
aio::kv::Build
pub type Build = Pin<Box<dyn Future<Output = ()> + Send + 'static>>A build in the background of a stale answer, as the program's runtime
runs it: tokio::spawn(build).
aio::kv::Fresh
pub struct Fresh<V> { /* private fields */ }Once's answers opened with fresh, awaited: the same answers and terms
as crate::kv::Fresh, whose builds in the background run where spawn
puts them.
let books = store
.once::<String>("cabinet-books")
.fresh(Duration::from_mins(10))
.keep(Duration::from_hours(12))
.spawn(|build| drop(tokio::spawn(build)))
.open()?;
let answer = books.fetch(7, || book(7)).await?; // at once, stale or not, once one is keptaio::kv::Fresh.fetch
pub fn fetch<K, E, W, F>(
&self,
key: K,
work: W,
) -> impl Future<Output = Result<Fetched<V>, E>> + Send + use<K, V, E, W, F>
where
K: Key,
E: From<Error> + Send + 'static,
W: FnOnce() -> F + Send + 'static,
F: Future<Output = Result<V, E>> + Send + 'static,The answer kept under key, with how old it is, or the answer of the
future work gives once none is kept, as crate::kv::Fresh::fetch
gives it. A kept answer older than fresh is answered at once,
stale, and work's future, given to spawn, makes the new one,
unless a build of the key is under way or failed within its pause.
aio::kv::Fresh.run
pub fn run<K, E, W, F>(
&self,
key: K,
work: W,
) -> impl Future<Output = Result<V, E>> + Send + use<K, V, E, W, F>
where
K: Key,
E: From<Error> + Send + 'static,
W: FnOnce() -> F + Send + 'static,
F: Future<Output = Result<V, E>> + Send + 'static,Fresh::fetch's value alone.
aio::kv::Fresh.get
pub fn get(&self, key: impl Key) -> impl Future<Output = Result<Option<V>>> + Send + 'staticThe answer kept under key, stale or not.
aio::kv::Fresh.delete
pub fn delete(&self, key: impl Key) -> impl Future<Output = Result<bool>> + Send + 'staticForgets the answer kept under key; says whether there was one.
aio::kv::Fresh.clear
pub fn clear(&self) -> impl Future<Output = Result<()>> + Send + 'staticRemoves every answer of this branch and of the branches under it, in
one commit, as crate::kv::Fresh::clear does.
aio::kv::Fresh.reseal
pub fn reseal(&self) -> impl Future<Output = Result<u64>> + Send + 'staticSeals again every answer an older key sealed, as
crate::kv::Once::reseal does.
aio::kv::Fresh.under
pub fn under(&self, owner: impl Key) -> Fresh<V>The same answers seen from the branch owner names under these.
aio::kv::Fresh.blocking
pub fn blocking(&self) -> &Fresh<V>The same answers, for their blocking calls.
aio::kv::FreshBuilder
pub struct FreshBuilder<V> { /* private fields */ }Answers being opened with fresh, as crate::kv::FreshBuilder are,
and where their builds in the background run.
aio::kv::FreshBuilder.keep
pub fn keep(self, keep: Duration) -> SelfHow long an answer is kept from when it was made; a day unless said.
aio::kv::FreshBuilder.encrypted
pub fn encrypted(self) -> SelfKeeps every answer sealed with the store's encryption key, as
crate::kv::OnceBuilder::encrypted says.
aio::kv::FreshBuilder.spawn
pub fn spawn(self, spawn: impl Fn(Build) + Send + Sync + 'static) -> SelfWhere a build in the background runs: .spawn(|build| { tokio::spawn(build); }).
A build dropped before its end keeps nothing and lets its key go.
aio::kv::FreshBuilder.open
pub fn open(self) -> Result<Fresh<V>>Opens the answers: a plain call, made once at start. Without spawn
it is Invalid, since a stale answer would have nowhere to be made.
aio::kv::Once
pub struct Once<V> { /* private fields */ }Functions run at most once a key, their answers kept, the functions
async: the same answers as crate::kv::Once, whose runs and those of
the blocking API wait for one another.
aio::kv::Once.run
pub fn run<K, E, W, F>(
&self,
key: K,
work: W,
) -> impl Future<Output = Result<V, E>> + Send + use<K, V, E, W, F>
where
K: Key,
E: From<Error> + Send,
W: FnOnce() -> F + Send,
F: Future<Output = Result<V, E>> + Send,The answer kept under key, or the answer of the future work
gives, which is then kept, as crate::kv::Once::run keeps it:
let charges = store.once::<String>("charges").open()?;
let order = "order-7".to_owned();
let receipt = charges.run("key-7f", || charge(&order)).await?; // charged once, however often it is sentWhile another run of the key is under way, in this process or through its server, this one waits for it with no thread held, and returns its answer. On a node that copies kv.db, the run claims its key at the file's home, which waits for the run under way there, and this run waits for the home's answer on a thread of its own.
work is called once the key is claimed, and its future is polled
inside this one's. The future is Send, and 'static when key,
work and its future are, as tokio::spawn asks.
Dropped while it waits, the run leaves and nothing changes; while its
function runs, the key is let go and nothing is kept, so the next run
runs again; once the function answered, its answer is kept all the
same and the key let go after it. A run of a key inside its own run,
awaited or blocking, is Invalid.
aio::kv::Once.fetch
pub fn fetch<K, E, W, F>(
&self,
key: K,
work: W,
) -> impl Future<Output = Result<Fetched<V>, E>> + Send + use<K, V, E, W, F>
where
K: Key,
E: From<Error> + Send,
W: FnOnce() -> F + Send,
F: Future<Output = Result<V, E>> + Send,Once::run's answer with how old it is, as
crate::kv::Once::fetch gives it.
aio::kv::Once.get
pub fn get(&self, key: impl Key) -> impl Future<Output = Result<Option<V>>> + Send + 'staticThe answer kept under key.
aio::kv::Once.delete
pub fn delete(&self, key: impl Key) -> impl Future<Output = Result<bool>> + Send + 'staticForgets the answer kept under key, so that the next run runs again;
says whether there was one.
aio::kv::Once.clear
pub fn clear(&self) -> impl Future<Output = Result<()>> + Send + 'staticRemoves every answer of this branch and of the branches under it, in
one commit, as crate::kv::Once::clear does.
aio::kv::Once.reseal
pub fn reseal(&self) -> impl Future<Output = Result<u64>> + Send + 'staticSeals again every answer an older key sealed, as
crate::kv::Once::reseal does.
aio::kv::Once.under
pub fn under(&self, owner: impl Key) -> Once<V>The same answers seen from the branch owner names under these.
aio::kv::Once.blocking
pub fn blocking(&self) -> &Once<V>The same answers, for their blocking calls.
aio::kv::OnceBuilder
pub struct OnceBuilder<V> { /* private fields */ }Answers being opened, as crate::kv::OnceBuilder are: their name and
type, and how long one is kept.
aio::kv::OnceBuilder.keep
pub fn keep(self, keep: Duration) -> SelfHow long an answer is kept from when it was made; a day unless said.
aio::kv::OnceBuilder.encrypted
pub fn encrypted(self) -> SelfKeeps every answer sealed with the store's encryption key, as
crate::kv::OnceBuilder::encrypted says.
aio::kv::OnceBuilder.fresh
pub fn fresh(self, fresh: Duration) -> FreshBuilder<V>How long an answer is answered without making a new one, shorter than
keep, as crate::kv::OnceBuilder::fresh says; awaited answers
opened so also take where their builds in the background run.
aio::kv::OnceBuilder.open
pub fn open(self) -> Result<Once<V>>Opens the answers: a plain call, made once at start.
aio::kv::Quota
pub struct Quota { /* private fields */ }Uses a key may make in each of its windows, their calls awaited: the same
quota as crate::kv::Quota. An allow counts in every window or in
none, in one durable write.
aio::kv::Quota.allow
pub fn allow(&self, key: impl Key) -> impl Future<Output = Result<Allowance>> + Send + 'staticUses one of each of key's windows, or none when one has no room left.
aio::kv::Quota.allow_n
pub fn allow_n(
&self,
key: impl Key,
n: u64,
) -> impl Future<Output = Result<Allowance>> + Send + 'staticUses n of each of key's windows at once, or none when one has no
room for n. More than a window's limit never passes, and is Invalid.
aio::kv::Quota.peek
pub fn peek(&self, key: impl Key) -> impl Future<Output = Result<Allowance>> + Send + 'statickey's windows, using nothing: ok says whether one more use would
pass now.
aio::kv::Quota.refund
pub fn refund(&self, key: impl Key) -> impl Future<Output = Result<()>> + Send + 'staticGives one use back to each of key's windows that has not reset since.
aio::kv::Quota.refund_n
pub fn refund_n(&self, key: impl Key, n: u64) -> impl Future<Output = Result<()>> + Send + 'staticGives n uses back, never below nothing.
aio::kv::Quota.reset
pub fn reset(&self, key: impl Key) -> impl Future<Output = Result<()>> + Send + 'staticForgets key's windows, so that its next use starts each anew.
aio::kv::Quota.under
pub fn under(&self, owner: impl Key) -> QuotaThe same quota seen from the branch owner names under this one,
whose keys are counted apart.
aio::kv::Quota.blocking
pub fn blocking(&self) -> &QuotaThe same quota, for its blocking calls.
aio::kv::QuotaBuilder
pub struct QuotaBuilder { /* private fields */ }A quota being opened, as crate::kv::QuotaBuilder is: its name and its
windows.
aio::kv::QuotaBuilder.window
pub fn window(self, name: &str, limit: u64, span: Duration) -> SelfLets a key use up to limit every span from its first use.
aio::kv::QuotaBuilder.open
pub fn open(self) -> Result<Quota>Opens the quota: a plain call, made once at start.
aio::kv::RateLimit
pub struct RateLimit { /* private fields */ }Requests let through at a steady rate a key, with room for a burst, their
calls awaited: the same rate limit as crate::kv::RateLimit.
Its times live in memory, so each call is a step there, answered in its
first poll. A key memory does not hold yet is read from kv.db first, on
the store's pool when the read would wait in the poll, and an allow or
a reset polled once applies whether or not its future is still awaited.
On a node that copies kv.db, allow and reset pass on to the file's
home and wait for its answer on a thread of its own, and peek reads the
node's copy.
aio::kv::RateLimit.allow
pub fn allow(&self, key: impl Key) -> impl Future<Output = Result<Allowance>> + Send + 'staticAsks for one request of key.
aio::kv::RateLimit.allow_n
pub fn allow_n(
&self,
key: impl Key,
n: u64,
) -> impl Future<Output = Result<Allowance>> + Send + 'staticAsks for n requests of key at once: all of them pass or none does.
More than the burst never passes, and is Invalid.
aio::kv::RateLimit.peek
pub fn peek(&self, key: impl Key) -> impl Future<Output = Result<Allowance>> + Send + 'staticWhether one request of key would pass now, using nothing.
aio::kv::RateLimit.reset
pub fn reset(&self, key: impl Key) -> impl Future<Output = Result<()>> + Send + 'staticForgets key's requests, so that its next one has the whole burst.
aio::kv::RateLimit.under
pub fn under(&self, owner: impl Key) -> RateLimitThe same rate limit seen from the branch owner names under this one,
whose keys are counted apart.
aio::kv::RateLimit.blocking
pub fn blocking(&self) -> &RateLimitThe same rate limit, for its blocking calls.
aio::kv::RateLimitBuilder
pub struct RateLimitBuilder { /* private fields */ }A rate limit being opened, as crate::kv::RateLimitBuilder is: its
name, its rate and its burst.
aio::kv::RateLimitBuilder.rate
pub fn rate(self, count: u64, per: Duration) -> SelfLets count requests a key through every per.
aio::kv::RateLimitBuilder.burst
pub fn burst(self, burst: u64) -> SelfHow many requests may pass at once before the rate holds the next; the rate's count unless said.
aio::kv::RateLimitBuilder.open
pub fn open(self) -> Result<RateLimit>Opens the rate limit: a plain call, made once at start.