Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add per-table stats #674

Merged
merged 4 commits into from
Sep 6, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/multimap_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use std::mem;
use std::mem::size_of;
use std::ops::{RangeBounds, RangeFull};
use std::sync::{Arc, Mutex};
use crate::table::TableStats;

// Verify all the checksums in the tree, including any Dynamic collection subtrees
pub(crate) fn verify_tree_and_subtree_checksums(
Expand Down Expand Up @@ -922,6 +923,19 @@ impl<'db, 'txn, K: RedbKey + 'static, V: RedbKey + 'static> ReadableMultimapTabl
Ok(MultimapRange::new(inner, self.mem))
}

fn stats(&self) -> Result<TableStats> {
let tree_stats = self.tree.stats()?;

Ok(TableStats {
tree_height: tree_stats.tree_height,
leaf_pages: tree_stats.leaf_pages,
branch_pages: tree_stats.branch_pages,
stored_leaf_bytes: tree_stats.stored_leaf_bytes,
metadata_bytes: tree_stats.metadata_bytes,
fragmented_bytes: tree_stats.fragmented_bytes,
})
}

/// Returns the number of key-value pairs in the table
fn len(&self) -> Result<u64> {
let mut count = 0;
Expand Down Expand Up @@ -963,6 +977,9 @@ pub trait ReadableMultimapTable<K: RedbKey + 'static, V: RedbKey + 'static>: Sea
K: 'a,
KR: Borrow<K::SelfType<'a>> + 'a;

/// Retrieves information about storage usage for the table
fn stats(&self) -> Result<TableStats>;

fn len(&self) -> Result<u64>;

fn is_empty(&self) -> Result<bool>;
Expand Down Expand Up @@ -1025,6 +1042,19 @@ impl<'txn, K: RedbKey + 'static, V: RedbKey + 'static> ReadableMultimapTable<K,
Ok(MultimapRange::new(inner, self.mem))
}

fn stats(&self) -> Result<TableStats> {
let tree_stats = self.tree.stats()?;

Ok(TableStats {
tree_height: tree_stats.tree_height,
leaf_pages: tree_stats.leaf_pages,
branch_pages: tree_stats.branch_pages,
stored_leaf_bytes: tree_stats.stored_leaf_bytes,
metadata_bytes: tree_stats.metadata_bytes,
fragmented_bytes: tree_stats.fragmented_bytes,
})
}

fn len(&self) -> Result<u64> {
let mut count = 0;
for item in self.iter()? {
Expand Down
73 changes: 73 additions & 0 deletions src/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,50 @@ use std::borrow::Borrow;
use std::ops::RangeBounds;
use std::sync::{Arc, Mutex};

/// Informational storage stats about a table
#[derive(Debug)]
pub struct TableStats {
pub(crate) tree_height: u32,
pub(crate) leaf_pages: u64,
pub(crate) branch_pages: u64,
pub(crate) stored_leaf_bytes: u64,
pub(crate) metadata_bytes: u64,
pub(crate) fragmented_bytes: u64,
}

impl TableStats {
/// Maximum traversal distance to reach the deepest (key, value) pair in the table
pub fn tree_height(&self) -> u32 {
self.tree_height
}

/// Number of leaf pages that store user data
pub fn leaf_pages(&self) -> u64 {
self.leaf_pages
}

/// Number of branch pages in the btree that store user data
pub fn branch_pages(&self) -> u64 {
self.branch_pages
}

/// Number of bytes consumed by keys and values that have been inserted.
/// Does not include indexing overhead
pub fn stored_bytes(&self) -> u64 {
self.stored_leaf_bytes
}

/// Number of bytes consumed by keys in internal branch pages, plus other metadata
pub fn metadata_bytes(&self) -> u64 {
self.metadata_bytes
}

/// Number of bytes consumed by fragmentation, both in data pages and internal metadata tables
pub fn fragmented_bytes(&self) -> u64 {
self.fragmented_bytes
}
}

/// A table containing key-value mappings
pub struct Table<'db, 'txn, K: RedbKey + 'static, V: RedbValue + 'static> {
name: String,
Expand Down Expand Up @@ -174,6 +218,19 @@ impl<'db, 'txn, K: RedbKey + 'static, V: RedbValue + 'static> ReadableTable<K, V
self.tree.range(&range).map(Range::new)
}

fn stats(&self) -> Result<TableStats> {
let tree_stats = self.tree.stats()?;

Ok(TableStats {
tree_height: tree_stats.tree_height,
leaf_pages: tree_stats.leaf_pages,
branch_pages: tree_stats.branch_pages,
stored_leaf_bytes: tree_stats.stored_leaf_bytes,
metadata_bytes: tree_stats.metadata_bytes,
fragmented_bytes: tree_stats.fragmented_bytes,
})
}

fn len(&self) -> Result<u64> {
self.tree.len()
}
Expand Down Expand Up @@ -235,6 +292,9 @@ pub trait ReadableTable<K: RedbKey + 'static, V: RedbValue + 'static>: Sealed {
K: 'a,
KR: Borrow<K::SelfType<'a>> + 'a;

/// Retrieves information about storage usage for the table
fn stats(&self) -> Result<TableStats>;

/// Returns the number of entries in the table
fn len(&self) -> Result<u64>;

Expand Down Expand Up @@ -282,6 +342,19 @@ impl<'txn, K: RedbKey + 'static, V: RedbValue + 'static> ReadableTable<K, V>
self.tree.range(&range).map(Range::new)
}

fn stats(&self) -> Result<TableStats> {
let tree_stats = self.tree.stats()?;

Ok(TableStats {
tree_height: tree_stats.tree_height,
leaf_pages: tree_stats.leaf_pages,
branch_pages: tree_stats.branch_pages,
stored_leaf_bytes: tree_stats.stored_leaf_bytes,
metadata_bytes: tree_stats.metadata_bytes,
fragmented_bytes: tree_stats.fragmented_bytes,
})
}

fn len(&self) -> Result<u64> {
self.tree.len()
}
Expand Down
9 changes: 9 additions & 0 deletions src/tree_store/btree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,15 @@ impl<'a, K: RedbKey, V: RedbValue> Btree<'a, K, V> {
Ok(count)
}

pub(crate) fn stats(&self) -> Result<BtreeStats> {
btree_stats(
self.root.map(|(p, _)| p),
self.mem,
K::fixed_width(),
V::fixed_width(),
)
}

#[allow(dead_code)]
pub(crate) fn print_debug(&self, include_values: bool) -> Result {
if let Some((p, _)) = self.root {
Expand Down