Examples

Indexes

View source
agdb example: creating indexes and looking up elements by an indexed key

What it shows

The example creates indexes on the username and token keys, inserts 100 users as typed User structs linked to a users root and then finds one user by looking up its username in the index instead of searching the graph. The result is converted straight back into a User.

Run it

cargo run -p agdb --example indexes

Creating indexes

An index is created for a key. It can be created before or after the data is inserted - an index created later indexes all existing values of that key as well:

// Create two indexes, one for username and one for token. They can just as well
// be created later and will index all existing data as well.
db.exec_mut(QueryBuilder::insert().index("username").query())?;
db.exec_mut(QueryBuilder::insert().index("token").query())?;

Looking up by index

The index search finds the element with the given value directly. This is useful when the data is too uniform to be split up on the graph - for example users looked up by their username or logged-in users looked up by a token:

let user: User = db
    .exec(
        QueryBuilder::select()
            .values(User::db_keys())
            .search()
            .index("username")
            .value("user50")
            .query(),
    )?
    .try_into()?;

println!("{user:?}");

Learn more