Skip to main content

YcdIndex

Struct YcdIndex 

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

An in-memory index over a contiguous set of YCD files.

Building the index reads every file’s header once and stores the resulting metadata. Subsequent read_digits calls use binary search to locate the relevant file(s) and seek directly to the target block, skipping every other file entirely.

§Stale-index detection

Each time a file is actually read, its current file_size and last-modified time are compared against the snapshot taken at build time. If any difference is detected, read_digits returns io::ErrorKind::InvalidData with an explicit “stale index” message. There is no silent fallback to a full-scan path.

§Example

use std::io;
use ycd_reader::YcdIndex;

fn main() -> io::Result<()> {
    let files = [
        "Pi - Dec - Chudnovsky - 0.ycd",
        "Pi - Dec - Chudnovsky - 1.ycd",
    ];

    // Build the index once (reads all headers).
    let index = YcdIndex::build(&files)?;

    // Fast random-access — only the relevant file(s) are opened.
    let digits = index.read_digits(999_995, 20)?;
    assert_eq!(digits, "45815130927562832084");

    Ok(())
}

Implementations§

Source§

impl YcdIndex

Source

pub fn build<P: AsRef<Path>>(files: &[P]) -> Result<Self>

Build an index from an ordered, contiguous slice of YCD file paths.

Every file’s header is read and validated (base-10 constraint, contiguity, no duplicates, no gaps). On success the index is ready for read_digits calls.

§Errors

Returns the same errors as YcdFileUtil::read_digits for header and continuity problems. Additionally returns InvalidInput when files is empty.

Source

pub fn rebuild<P: AsRef<Path>>(&mut self, files: &[P]) -> Result<()>

Discard the current index and rebuild it from a new file list.

On success self is replaced with the freshly built index. If the rebuild fails, self is left unchanged.

Source

pub fn len(&self) -> usize

Returns the number of YCD files in the index.

Source

pub fn is_empty(&self) -> bool

Returns true if the index contains no files.

Source

pub fn entries(&self) -> &[YcdIndexEntry]

Returns a slice of all index entries in digit order.

Source

pub fn read_digits( &self, one_based_start_position: usize, length: usize, ) -> Result<String>

Read exactly length decimal digits starting at 1-based position one_based_start_position.

Binary search locates the first file that contains the start position. Only the file(s) actually needed are opened; all other files are skipped entirely.

Before reading each file, its current file_size and last-modified time are compared against the index snapshot. A mismatch returns io::ErrorKind::InvalidData with an “index is stale” message.

§Errors
Conditionio::ErrorKind
Index is empty, position 0, or length 0InvalidInput
Start or end position outside the indexed rangeInvalidInput
start + length overflows usizeInvalidData
File size or mtime differs from index snapshotInvalidData
File does not existNotFound
Payload truncated within logical rangeUnexpectedEof
Output string pre-allocation failureOther

Trait Implementations§

Source§

impl Clone for YcdIndex

Source§

fn clone(&self) -> YcdIndex

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for YcdIndex

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.