Struct db_dump::Index [−][src]
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
impl<'a> Index<'a>
[src]
pub fn new(db: &'a DbDump) -> Self
[src]
This call does no work up front. Each index is built lazily upon first access through one of the methods below.
pub fn category(&self, id: CategoryId) -> &'a Row
[src]
pub fn krate(&self, id: CrateId) -> &'a Row
[src]
pub fn keyword(&self, id: KeywordId) -> &'a Row
[src]
pub fn team(&self, id: TeamId) -> &'a Row
[src]
pub fn user(&self, id: UserId) -> &'a Row
[src]
pub fn version(&self, id: VersionId) -> &'a Row
[src]
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
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
pub fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,