lance_file/
lib.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright The Lance Authors

pub mod datatypes;
pub mod format;
pub mod page_table;
pub mod reader;
pub mod v2;
pub mod writer;

use format::MAGIC;
pub use lance_encoding::version;

use lance_core::{Error, Result};
use lance_encoding::version::LanceFileVersion;
use lance_io::object_store::ObjectStore;
use object_store::path::Path;
use snafu::{location, Location};

pub async fn determine_file_version(
    store: &ObjectStore,
    path: &Path,
    known_size: Option<usize>,
) -> Result<LanceFileVersion> {
    let size = match known_size {
        None => store.size(path).await.unwrap(),
        Some(size) => size,
    };
    if size < 8 {
        return Err(Error::InvalidInput {
            source: format!(
                "the file {} does not appear to be a lance file (too small)",
                path
            )
            .into(),
            location: location!(),
        });
    }
    let reader = store.open_with_size(path, size).await?;
    let footer = reader.get_range((size - 8)..size).await?;
    if &footer[4..] != MAGIC {
        return Err(Error::InvalidInput {
            source: format!(
                "the file {} does not appear to be a lance file (magic mismatch)",
                path
            )
            .into(),
            location: location!(),
        });
    }
    let major_version = u16::from_le_bytes([footer[0], footer[1]]);
    let minor_version = u16::from_le_bytes([footer[2], footer[3]]);

    LanceFileVersion::try_from_major_minor(major_version as u32, minor_version as u32)
}