[−][src]Trait unqlite::Cursor
Cursor iterator interfaces.
Cursors provide a mechanism by which you can iterate over the records in a database. Using cursors, you can seek, fetch, move, and delete database records.
To iterate over database records, from the first record to the last, simply call first
to get the first valid cursor and loop to the next:
use unqlite::{UnQLite, Cursor}; let unqlite = UnQLite::create_temp(); let mut entry = unqlite.first(); loop { if entry.is_none() { break; } let record = entry.expect("valid entry"); println!("{:?}", record.key_value()); entry = record.next(); }
To iterate over database records, from the last record to the first, just replace first
as
last
, call prev
instead of next()
on entry
.
You can also use cursors to search for records and start the iteration process from there.
To do that, start from seek
method.
To retrieve record key/value from a valid cursor, just use like:
let entry = ...; // Get the cursor entry let key = entry.key(); // Key only let value = entry.value(); // Value only let (key, value) = entry.key_value(); // Key-Value pair
To delete a record from the database using the cursor interface, simply point to the target
record using seek
and call delete
on the Entry
object.
A rusty Iterator
style would perform in a short time.
Required methods
fn first(&self) -> Option<Entry>
Returns the first entry.
fn last(&self) -> Option<Entry>
Retruns the last entry.
fn seek<K: AsRef<[u8]>>(&self, key: K, pos: Direction) -> Option<Entry>
Seek an entry by key
.
The pos
Direction
options:
- Exact: If the record exists, the cursor is left pointing to it,
otherwise return
None
. - Le: The cursor is left pointing to the largest key in the database that is smaller
than
key
, If the database contains no keys smaller thankey
, it returnsNone
. - Ge: Oppsite to Le, it returns the smallest
Entry
in the database that is larger thankey
.If the database contains no keys smaller thankey
, returnNone
.