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>
impl<'a> Index<'a>
sourcepub fn new(db: &'a DbDump) -> Self
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.
pub fn category(&self, id: CategoryId) -> &'a Row
pub fn krate(&self, id: CrateId) -> &'a Row
pub fn keyword(&self, id: KeywordId) -> &'a Row
pub fn team(&self, id: TeamId) -> &'a Row
pub fn user(&self, id: UserId) -> &'a Row
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more