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 previous;
8pub mod reader;
9pub mod testing;
10pub mod writer;
11
12pub use io::LanceEncodingsIo;
13
14use format::MAGIC;
15pub use lance_encoding::version;
16
17use lance_core::{Error, Result};
18use lance_encoding::version::LanceFileVersion;
19use lance_io::object_store::ObjectStore;
20use object_store::path::Path;
21
22pub async fn determine_file_version(
23    store: &ObjectStore,
24    path: &Path,
25    known_size: Option<usize>,
26) -> Result<LanceFileVersion> {
27    let size = match known_size {
28        None => store.size(path).await.unwrap() as usize,
29        Some(size) => size,
30    };
31    if size < 8 {
32        return Err(Error::invalid_input_source(
33            format!(
34                "the file {} does not appear to be a lance file (too small)",
35                path
36            )
37            .into(),
38        ));
39    }
40    let reader = store.open_with_size(path, size).await?;
41    let footer = reader.get_range((size - 8)..size).await?;
42    if &footer[4..] != MAGIC {
43        return Err(Error::invalid_input_source(
44            format!(
45                "the file {} does not appear to be a lance file (magic mismatch)",
46                path
47            )
48            .into(),
49        ));
50    }
51    let major_version = u16::from_le_bytes([footer[0], footer[1]]);
52    let minor_version = u16::from_le_bytes([footer[2], footer[3]]);
53
54    LanceFileVersion::try_from_major_minor(major_version as u32, minor_version as u32)
55}