Skip to main content

lance_file/
lib.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright The Lance Authors
3
4pub mod datatypes;
5pub mod format;
6pub(crate) mod io;
7pub mod reader;
8pub mod testing;
9pub mod version;
10pub mod versions;
11pub mod writer;
12
13#[cfg(test)]
14mod compatibility_tests;
15
16pub use io::LanceEncodingsIo;
17
18use format::MAGIC;
19use lance_core::{Error, Result};
20use lance_io::object_store::ObjectStore;
21use object_store::path::Path;
22use version::ConcreteFileVersion;
23
24pub async fn determine_file_version(
25    store: &ObjectStore,
26    path: &Path,
27    known_size: Option<usize>,
28) -> Result<ConcreteFileVersion> {
29    let size = match known_size {
30        None => usize::try_from(store.size(path).await?).map_err(|_| {
31            Error::invalid_input(format!("file {} is too large for this platform", path))
32        })?,
33        Some(size) => size,
34    };
35    if size < 8 {
36        return Err(Error::invalid_input_source(
37            format!(
38                "the file {} does not appear to be a lance file (too small)",
39                path
40            )
41            .into(),
42        ));
43    }
44    let reader = store.open_with_size(path, size).await?;
45    let footer = reader.get_range((size - 8)..size).await?;
46    if &footer[4..] != MAGIC {
47        return Err(Error::invalid_input_source(
48            format!(
49                "the file {} does not appear to be a lance file (magic mismatch)",
50                path
51            )
52            .into(),
53        ));
54    }
55    let major_version = u16::from_le_bytes([footer[0], footer[1]]);
56    let minor_version = u16::from_le_bytes([footer[2], footer[3]]);
57
58    ConcreteFileVersion::from_footer_numbers(major_version, minor_version)
59}