Skip to main content

FileView

Struct FileView 

Source
pub struct FileView<'a> { /* private fields */ }
Expand description

A read-only, zero-copy view over a .verit file’s bytes (typically an mmap). open validates the header, footer, schema section, and every index entry up front, so each later get is a bounds-free slice.

Implementations§

Source§

impl<'a> FileView<'a>

Source

pub fn open(buf: &'a [u8]) -> Result<FileView<'a>>

Open a file image, recovering the newest valid commit.

Follows spec §7.2: scan back from the end over 8-aligned offsets for a footer whose trailing magic, CRC, and self-referential length all agree. The first hit is the newest generation, so an intact file resolves on the first candidate and a file torn mid-append transparently opens at the previous generation — the crash-safety guarantee, exercised as a read.

Source

pub fn len(&self) -> usize

Number of live records.

Source

pub fn is_empty(&self) -> bool

Source

pub fn generation(&self) -> u64

The commit counter of the generation this view resolved to. A value lower than expected after a crash means the torn commit was rolled back.

Source

pub fn footer(&self) -> Footer

The authoritative footer.

Source

pub fn file_len(&self) -> u64

The committed extent. Bytes at or beyond this are uncommitted debris and carry no meaning.

Source

pub fn next_record_id(&self) -> u64

The id the next appended record will take. Every live record’s id is strictly below this, and no id at or above it has ever been used.

Source

pub fn has_record_checksums(&self) -> bool

Whether this file carries a CRC-32 per record (OPT_RECORD_CRC).

Source

pub fn record_checksum(&self, i: usize) -> Option<u32>

The stored CRC-32 for record i, or None when the file carries none.

The array ends exactly at index_offset, in the space a reader without the feature already skips — which is what makes the feature optional rather than a format change.

Source

pub fn verify_checksums(&self) -> Result<usize>

Verify every record against its stored checksum.

Returns the number checked — Ok(0) for a file that carries none, which is not an error: checksums are optional, and their absence is a property of the file, not a fault. A mismatch is Error::ChecksumMismatch, naming the record’s id rather than its position.

Source

pub fn schemas(&self) -> &SchemaRegistry

The file’s schema section, decoded. Every schema needed to read every record is here — this is what “self-contained” means concretely.

Source

pub fn record(&self, i: usize) -> Result<Record>

Index entry i.

Source

pub fn get(&self, i: usize) -> Result<&'a [u8]>

The raw message bytes of record i, borrowing the file image. Hand the result straight to Message::parse.

Source

pub fn find_by_id(&self, id: u64) -> Option<usize>

The position of the record with this id, or None if it is not live.

A binary search: ids ascend with the index (appends are monotonic and neither removal nor compaction reorders), and open proved it. No secondary structure, and nothing outside the index bytes is touched.

Source

pub fn get_by_id(&self, id: u64) -> Result<&'a [u8]>

The bytes of the record with this id.

Source

pub fn records_after(&self, id: u64) -> impl Iterator<Item = Record> + '_

Every record whose id is greater than id, in order — a tailing reader’s “everything since my checkpoint”.

Pass the last id you processed; pass 0 for the whole file. The search for the starting position is logarithmic, so polling a large file is cheap even when nothing has changed.

Source

pub fn schema_id(&self, i: usize) -> Result<u128>

The schema id of record i, read from the index — no record bytes are touched, so filtering a large mapped file by type stays in one contiguous region instead of paging the whole file in.

Source

pub fn schema(&self, i: usize) -> Result<&Schema>

The writer schema of record i, from the file’s own schema section.

Source

pub fn message(&self, i: usize) -> Result<Message<'a>>

Parse record i into a Message, zero-copy over the file image.

Source

pub fn resolver_for(&self, i: usize, reader: &Schema) -> Result<Resolver>

A Resolver reading record i’s writer schema into reader — the schema-evolution payoff at rest. A record written years ago under an older schema resolves into today’s type, because the file kept the writer schema alongside it.

Builds a resolver on every call. Resolution is meant to be paid once per schema pair, not once per record, so calling this inside a loop over a large file repeats identical work. Use resolvers there — it resolves each distinct schema in the file once and hands back a lookup.

Source

pub fn resolvers(&self, reader: &Schema) -> Resolvers

Resolve every schema this file’s records use into reader, once each, and return the lookup to use across the whole file.

This is the shape the “paid once per schema pair” promise actually needs: hoist it out of the loop, then ask it per record.

let file = FileView::open(&bytes).unwrap();
let resolvers = file.resolvers(&schema);          // once
for i in 0..file.len() {
    let resolver = resolvers.for_record(&file, i).unwrap();
    let root = file.message(i).unwrap().root(resolver).unwrap();
    assert_eq!(root.get_i32(1).unwrap(), Some(7));
}

A file may hold records this reader cannot interpret — a mixed-schema file read by a type that only covers one of them. Those simply do not appear in the lookup, and Resolvers::for_record reports them as incompatible rather than the whole call failing, so a reader can walk a mixed file and skip what is not for it.

Source

pub fn dump_json(&self, i: usize) -> Result<String>

Render record i as JSON using only this file’s bytes.

Source

pub fn iter(&self) -> impl Iterator<Item = &'a [u8]> + '_

Iterate over each record’s bytes in order.

Source

pub fn records(&self) -> impl Iterator<Item = Record> + '_

Iterate over the index entries in order.

Trait Implementations§

Source§

impl<'a> Clone for FileView<'a>

Source§

fn clone(&self) -> FileView<'a>

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<'a> Debug for FileView<'a>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a> Freeze for FileView<'a>

§

impl<'a> RefUnwindSafe for FileView<'a>

§

impl<'a> Send for FileView<'a>

§

impl<'a> Sync for FileView<'a>

§

impl<'a> Unpin for FileView<'a>

§

impl<'a> UnsafeUnpin for FileView<'a>

§

impl<'a> UnwindSafe for FileView<'a>

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.