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§

source§

impl<'a> Index<'a>

source

pub fn new(db: &'a DbDump) -> Self

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

source

pub fn category(&self, id: CategoryId) -> &'a Row

source

pub fn krate(&self, id: CrateId) -> &'a Row

source

pub fn keyword(&self, id: KeywordId) -> &'a Row

source

pub fn team(&self, id: TeamId) -> &'a Row

source

pub fn user(&self, id: UserId) -> &'a Row

source

pub fn version(&self, id: VersionId) -> &'a Row

Auto Trait Implementations§

§

impl<'a> RefUnwindSafe for Index<'a>

§

impl<'a> Send for Index<'a>

§

impl<'a> Sync for Index<'a>

§

impl<'a> Unpin for Index<'a>

§

impl<'a> UnwindSafe for Index<'a>

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

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

source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.