BlockIndex

Struct BlockIndex 

Source
pub struct BlockIndex { /* private fields */ }
Expand description

Complete index for a VBINSEQ file

A BlockIndex contains metadata about a VBINSEQ file and all of its blocks, enabling efficient random access and parallel processing. It consists of an IndexHeader and a collection of BlockRange entries, one for each block in the file.

The index can be created by scanning a VBINSEQ file or loaded from a previously created index file. Once loaded, it provides information about block locations, sizes, and record counts.

§Examples

use binseq::vbq::{BlockIndex, MmapReader};
use std::path::Path;

// Create an index from a VBINSEQ file
let vbq_path = Path::new("example.vbq");
let index = BlockIndex::from_vbq(vbq_path).unwrap();

// Save the index for future use
let index_path = Path::new("example.vbq.vqi");
index.save_to_path(index_path).unwrap();

// Use the index with a reader for parallel processing
let reader = MmapReader::new(vbq_path).unwrap();
println!("File contains {} blocks", index.n_blocks());

Implementations§

Source§

impl BlockIndex

Source

pub fn new(header: IndexHeader) -> Self

Creates a new empty block index with the specified header

§Parameters
  • header - The index header containing metadata about the indexed file
§Returns

A new empty BlockIndex instance

Source

pub fn n_blocks(&self) -> usize

Returns the number of blocks in the indexed file

§Returns

The number of blocks in the VBINSEQ file described by this index

§Examples
use binseq::vbq::BlockIndex;
use std::path::Path;

let index = BlockIndex::from_path(Path::new("example.vbq.vqi")).unwrap();
println!("The file contains {} blocks", index.n_blocks());
Source

pub fn save_to_path<P: AsRef<Path>>(&self, path: P) -> Result<()>

Writes the collection of BlockRange to a file Saves the index to a file

This writes the index header and all block ranges to a file, which can be loaded later to avoid rescanning the VBINSEQ file. The index is compressed to reduce storage space.

§Parameters
  • path - The path where the index file should be saved
§Returns
  • Ok(()) - If the index was successfully saved
  • Err(_) - If an error occurred during saving
§Examples
use binseq::vbq::BlockIndex;
use std::path::Path;

// Create an index from a VBINSEQ file
let index = BlockIndex::from_vbq(Path::new("example.vbq")).unwrap();

// Save it for future use
index.save_to_path(Path::new("example.vbq.vqi")).unwrap();
Source

pub fn write_bytes<W: Write>(&self, writer: &mut W) -> Result<()>

Write the index to an output buffer

Source

pub fn write_range<W: Write>(&self, writer: &mut W) -> Result<()>

Write the collection of BlockRange to an output handle Writes all block ranges to the provided writer

This method is used internally by save_to_path to write the block ranges to an index file. It can also be used to serialize an index to any destination that implements Write.

§Parameters
  • writer - The destination to write the block ranges to
§Returns
  • Ok(()) - If all block ranges were successfully written
  • Err(_) - If an error occurred during writing
Source

pub fn from_vbq<P: AsRef<Path>>(path: P) -> Result<Self>

Creates a new index by scanning a VBINSEQ file

This method memory-maps the specified VBINSEQ file and scans it block by block to create an index. The index can then be saved to a file for future use, enabling efficient random access without rescanning the file.

§Parameters
  • path - Path to the VBINSEQ file to index
§Returns
  • Ok(Self) - A new BlockIndex containing information about all blocks in the file
  • Err(_) - If an error occurred during file opening, validation, or scanning
§Examples
use binseq::vbq::BlockIndex;
use std::path::Path;

// Create an index from a VBINSEQ file
let index = BlockIndex::from_vbq(Path::new("example.vbq")).unwrap();

// Save the index for future use
index.save_to_path(Path::new("example.vbq.vqi")).unwrap();

// Get statistics about the file
println!("File contains {} blocks", index.n_blocks());

// Analyze the record distribution
if let Some(last_range) = index.ranges().last() {
    println!("Total records: {}", last_range.cumulative_records);
    println!("Average records per block: {}",
             last_range.cumulative_records as f64 / index.n_blocks() as f64);
}
§Notes

This method uses memory mapping for efficiency, which allows the operating system to load only the needed portions of the file into memory as they are accessed.

Source

pub fn from_path<P: AsRef<Path>>(path: P) -> Result<Self>

Reads an index from a path

§Panics

Panics if the path is not a valid UTF-8 string.

Source

pub fn from_bytes(bytes: &[u8]) -> Result<Self>

Source

pub fn ranges(&self) -> &[BlockRange]

Get a reference to the internal ranges Returns a reference to the collection of block ranges

This provides access to the metadata for all blocks in the indexed file, which can be used for operations like parallel processing or random access.

§Returns

A slice containing all BlockRange entries in this index

§Examples
use binseq::vbq::BlockIndex;
use std::path::Path;

let index = BlockIndex::from_path(Path::new("example.vbq.vqi")).unwrap();

// Examine the ranges to determine which blocks to process
for (i, range) in index.ranges().iter().enumerate() {
    println!("Block {}: {} records at offset {}",
             i, range.block_records, range.start_offset);
}
Source

pub fn pprint(&self)

Source

pub fn num_records(&self) -> usize

Returns the total number of records in the dataset

Trait Implementations§

Source§

impl Clone for BlockIndex

Source§

fn clone(&self) -> BlockIndex

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for BlockIndex

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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 T
where 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

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 T
where U: TryFrom<T>,

Source§

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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V