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