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;
21use snafu::location;
22
23pub async fn determine_file_version(
24    store: &ObjectStore,
25    path: &Path,
26    known_size: Option<usize>,
27) -> Result<LanceFileVersion> {
28    let size = match known_size {
29        None => store.size(path).await.unwrap() as usize,
30        Some(size) => size,
31    };
32    if size < 8 {
33        return Err(Error::InvalidInput {
34            source: format!(
35                "the file {} does not appear to be a lance file (too small)",
36                path
37            )
38            .into(),
39            location: location!(),
40        });
41    }
42    let reader = store.open_with_size(path, size).await?;
43    let footer = reader.get_range((size - 8)..size).await?;
44    if &footer[4..] != MAGIC {
45        return Err(Error::InvalidInput {
46            source: format!(
47                "the file {} does not appear to be a lance file (magic mismatch)",
48                path
49            )
50            .into(),
51            location: location!(),
52        });
53    }
54    let major_version = u16::from_le_bytes([footer[0], footer[1]]);
55    let minor_version = u16::from_le_bytes([footer[2], footer[3]]);
56
57    LanceFileVersion::try_from_major_minor(major_version as u32, minor_version as u32)
58}