lance_io/object_store/providers/
local.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright The Lance Authors
3
4use std::sync::Arc;
5
6use object_store::{local::LocalFileSystem, path::Path};
7use url::Url;
8
9use crate::object_store::{
10    ObjectStore, ObjectStoreParams, ObjectStoreProvider, StorageOptions, DEFAULT_LOCAL_BLOCK_SIZE,
11    DEFAULT_LOCAL_IO_PARALLELISM,
12};
13use lance_core::error::Result;
14
15#[derive(Default, Debug)]
16pub struct FileStoreProvider;
17
18#[async_trait::async_trait]
19impl ObjectStoreProvider for FileStoreProvider {
20    async fn new_store(&self, base_path: Url, params: &ObjectStoreParams) -> Result<ObjectStore> {
21        let block_size = params.block_size.unwrap_or(DEFAULT_LOCAL_BLOCK_SIZE);
22        let storage_options = StorageOptions(params.storage_options.clone().unwrap_or_default());
23        let download_retry_count = storage_options.download_retry_count();
24        Ok(ObjectStore {
25            inner: Arc::new(LocalFileSystem::new()),
26            scheme: base_path.scheme().to_owned(),
27            block_size,
28            use_constant_size_upload_parts: false,
29            list_is_lexically_ordered: false,
30            io_parallelism: DEFAULT_LOCAL_IO_PARALLELISM,
31            download_retry_count,
32        })
33    }
34
35    fn extract_path(&self, url: &Url) -> object_store::path::Path {
36        url.to_file_path()
37            .ok()
38            .and_then(|p| Path::from_absolute_path(p).ok())
39            .unwrap_or_else(|| Path::from(url.path()))
40    }
41}
42
43#[cfg(test)]
44mod tests {
45    use crate::object_store::uri_to_url;
46
47    use super::*;
48
49    #[test]
50    fn test_file_store_path() {
51        let provider = FileStoreProvider;
52
53        let cases = [
54            ("file:///", ""),
55            ("file:///usr/local/bin", "usr/local/bin"),
56            ("file-object-store:///path/to/file", "path/to/file"),
57            ("file:///path/to/foo/../bar", "path/to/bar"),
58        ];
59
60        for (uri, expected_path) in cases {
61            let url = uri_to_url(uri).unwrap();
62            let path = provider.extract_path(&url);
63            assert_eq!(path.as_ref(), expected_path, "uri: '{}'", uri);
64        }
65    }
66
67    #[test]
68    #[cfg(windows)]
69    fn test_file_store_path_windows() {
70        let provider = FileStoreProvider;
71
72        let cases = [
73            (
74                "C:\\Users\\ADMINI~1\\AppData\\Local\\",
75                "C:/Users/ADMINI~1/AppData/Local",
76            ),
77            (
78                "C:\\Users\\ADMINI~1\\AppData\\Local\\..\\",
79                "C:/Users/ADMINI~1/AppData",
80            ),
81        ];
82
83        for (uri, expected_path) in cases {
84            let url = uri_to_url(uri).unwrap();
85            let path = provider.extract_path(&url);
86            assert_eq!(path.as_ref(), expected_path);
87        }
88    }
89}