pub struct Db { /* private fields */ }Expand description
An open database.
Cheap to clone, and every clone is the same database. A handle taken out of it stays valid for as long as any clone lives.
This build runs in inline mode (15 section 7): the calling thread is the
shard, which is what makes a point read a function call rather than a
message. That is also why a handle does not cross threads yet. The owned
and served modes put the same API on top of yo-shard’s runtime, and they
arrive with it.
Implementations§
Source§impl Db
impl Db
Sourcepub fn map<K: Decode, V: Decode>(&self, name: &str) -> Result<Map<K, V>>
pub fn map<K: Decode, V: Decode>(&self, name: &str) -> Result<Map<K, V>>
Open a map, creating it if this is the first time.
The type is the collection’s shape (15 section 3), so opening the
same name a second time with a different type is an error and not a
surprise later: the shapes are compared, and a mismatch says which
field moved and whether the change is additive or breaking.
§Errors
Code::ShapeMismatch when the name is already a collection of
another shape.
Sourcepub fn strings(&self) -> Strings
pub fn strings(&self) -> Strings
The Redis string keyspace.
The same store a client reaches over RESP, reached without the socket, the parser or the reply (Y23). Not a named collection, because in Redis a string is not one: it is the keyspace itself.
let db = yo::open(yo::MEMORY)?;
assert_eq!(db.strings().incr("hits")?, 1);Sourcepub fn counter(&self, key: impl Into<Vec<u8>>) -> Counter
pub fn counter(&self, key: impl Into<Vec<u8>>) -> Counter
A counter at one key, which is 15 section 2’s db.counter("hits").
Sugar over Db::strings and worth having: a counter is the commonest
thing a string key is, and a handle that holds the key means the key is
spelled once rather than at every call site.
let db = yo::open(yo::MEMORY)?;
let hits = db.counter("hits");
hits.incr()?;
hits.add(9)?;
assert_eq!(hits.get()?, 10);Sourcepub fn sets(&self) -> Sets
pub fn sets(&self) -> Sets
Every Redis set command, with the key as the first argument.
The same store SADD off a socket reaches. Like Db::strings this is
not a named collection, because in Redis a set is not one: it is a key in
the keyspace that happens to hold a set.
let db = yo::open(yo::MEMORY)?;
db.sets().add_many("online", &["alice", "bob"])?;
assert_eq!(db.sets().len_of("online")?, 2);Sourcepub fn set(&self, key: impl Into<Vec<u8>>) -> Set
pub fn set(&self, key: impl Into<Vec<u8>>) -> Set
A set at one key, which is the same sugar Db::counter is.
let db = yo::open(yo::MEMORY)?;
let online = db.set("online");
online.add("alice")?;
assert!(online.contains("alice")?);Sourcepub fn keys(&self) -> Keys
pub fn keys(&self) -> Keys
Every command that works on a key whatever the key holds.
DEL, EXISTS and TYPE, and the whole expiry family. These are the
ones that belong to the keyspace rather than to a type, which is why
they are not on Db::strings or Db::sets: a deadline sits in the
key’s record and does not care what the record points at.
use std::time::Duration;
let db = yo::open(yo::MEMORY)?;
db.set("online").add("alice")?;
db.keys().expire_in("online", Duration::from_secs(60))?;Sourcepub fn collections(&self) -> Result<Vec<String>>
pub fn collections(&self) -> Result<Vec<String>>
The names of the typed collections in this database, in the order they were first opened.
§Errors
Code::Invalid if called from inside a callback that is already
holding this database.
Sourcepub fn shape(&self, name: &str) -> Result<Option<Tag>>
pub fn shape(&self, name: &str) -> Result<Option<Tag>>
The shape of a collection, if it exists.
§Errors
Code::Invalid if called from inside a callback that is already
holding this database.
Sourcepub fn memory_bytes(&self) -> Result<usize>
pub fn memory_bytes(&self) -> Result<usize>
What this database is holding, index and arena together, across the keyspace and every typed collection.
§Errors
Code::Invalid if called from inside a callback that is already
holding this database.
Sourcepub fn reads_the_clock(&self) -> bool
pub fn reads_the_clock(&self) -> bool
Whether this database reads the clock on the data path.
False until something is given a deadline, because until then the
clock’s answer cannot change any reply. 04 section 5 is the reason
this is worth a method: a clock read is tens of nanoseconds against a
budget of a hundred and fifty.