Struct db_dump::Index

source ·
pub struct Index<'a> { /* private fields */ }
Expand description

Lazy index of those tables that have a unique ID column as primary key.

Example

This example prints the top 5 most downloaded crates in each of the top 20 most popular categories.

use db_dump::categories::CategoryId;
use db_dump::crates::CrateId;
use db_dump::DbDump;
use std::cmp::Reverse;
use std::collections::BTreeMap as Map;

fn main() -> db_dump::Result<()> {
    let mut db = DbDump::default();
    let ref mut crates = db.crates;
    let mut categories = Vec::new();
    let mut crates_by_category = Map::<CategoryId, Vec<CrateId>>::new();
    db_dump::Loader::new()
        .crates(|row| crates.push(row))
        .categories(|row| categories.push(row))
        .crates_categories(|row| {
            crates_by_category
                .entry(row.category_id)
                .or_default()
                .push(row.crate_id)
        })
        .load("./db-dump.tar.gz")?;

    // Lazy index to perform lookups by crate id.
    let index = db.index();

    // Sort categories descending by number of crates, to print most popular
    // categories first.
    categories.sort_unstable_by_key(|category| Reverse(category.crates_cnt));

    for category in categories.iter().take(20) {
        // Get the list of crates in this category.
        let mut crates = crates_by_category.remove(&category.id).unwrap_or_default();

        // Sort crates list by download count descending.
        crates.sort_unstable_by_key(|&id| Reverse(index.krate(id).downloads));

        // Print top 5 most downloaded crates in category.
        print!("{}", category.slug);
        for crate_id in crates.into_iter().take(5) {
            print!(",{}", index.krate(crate_id).name);
        }
        println!();
    }

    Ok(())
}

Implementations§

This call does no work up front. Each index is built lazily upon first access through one of the methods below.

Examples found in repository?
src/index.rs (line 18)
17
18
19
    pub fn index<'a>(&'a self) -> Index<'a> {
        Index::new(self)
    }
Examples found in repository?
src/index.rs (line 199)
198
199
200
    pub fn lookup<'a>(self, index: &Index<'a>) -> &'a crate::categories::Row {
        index.category(self)
    }
Examples found in repository?
src/index.rs (line 205)
204
205
206
    pub fn lookup<'a>(self, index: &Index<'a>) -> &'a crate::crates::Row {
        index.krate(self)
    }
Examples found in repository?
src/index.rs (line 211)
210
211
212
    pub fn lookup<'a>(self, index: &Index<'a>) -> &'a crate::keywords::Row {
        index.keyword(self)
    }
Examples found in repository?
src/index.rs (line 217)
216
217
218
    pub fn lookup<'a>(self, index: &Index<'a>) -> &'a crate::teams::Row {
        index.team(self)
    }
Examples found in repository?
src/index.rs (line 223)
222
223
224
    pub fn lookup<'a>(self, index: &Index<'a>) -> &'a crate::users::Row {
        index.user(self)
    }
Examples found in repository?
src/index.rs (line 229)
228
229
230
    pub fn lookup<'a>(self, index: &Index<'a>) -> &'a crate::versions::Row {
        index.version(self)
    }

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.