Skip to main content

concept_graph/
read.rs

1//! Opening a side file for a structure to be assembled from.
2//!
3//! Every structure of a built artifact is read once, from front to back, into
4//! the form it is served from. Reading the file into a `Vec` first puts the
5//! whole file in memory beside the structure it becomes, and the allocator
6//! does not hand those pages back the moment the `Vec` drops (#322), so a
7//! reader assembles from a buffered file instead. No FHIR or SNOMED CT
8//! specification governs this: our own design.
9
10use std::fs::File;
11use std::io::BufReader;
12use std::path::Path;
13
14/// How much of a side file a reader holds at a time.
15///
16/// Wide enough that a structure of hundreds of megabytes is assembled in a few
17/// thousand reads, small enough that the buffer is never the figure a resident
18/// measurement reports.
19pub const BUFFER_BYTES: usize = 1 << 20;
20
21/// Opens `path` buffered, for a structure to be assembled from.
22///
23/// # Errors
24///
25/// Returns the I/O error from opening the file.
26pub fn buffered(path: &Path) -> std::io::Result<BufReader<File>> {
27    Ok(BufReader::with_capacity(BUFFER_BYTES, File::open(path)?))
28}