lance_io/object_store/providers/
memory.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright The Lance Authors
3
4use std::sync::Arc;
5
6use object_store::{memory::InMemory, path::Path};
7use url::Url;
8
9use crate::object_store::{
10    ObjectStore, ObjectStoreParams, ObjectStoreProvider, StorageOptions, DEFAULT_LOCAL_BLOCK_SIZE,
11};
12use lance_core::{error::Result, utils::tokio::get_num_compute_intensive_cpus};
13
14/// Provides a fresh in-memory object store for each call to `new_store`.
15#[derive(Default, Debug)]
16pub struct MemoryStoreProvider;
17
18#[async_trait::async_trait]
19impl ObjectStoreProvider for MemoryStoreProvider {
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(InMemory::new()),
26            scheme: String::from("memory"),
27            block_size,
28            use_constant_size_upload_parts: false,
29            list_is_lexically_ordered: true,
30            io_parallelism: get_num_compute_intensive_cpus(),
31            download_retry_count,
32        })
33    }
34
35    fn extract_path(&self, url: &Url) -> Path {
36        let mut output = String::new();
37        if let Some(domain) = url.domain() {
38            output.push_str(domain);
39        }
40        output.push_str(url.path());
41        Path::from(output)
42    }
43}
44
45#[cfg(test)]
46mod tests {
47    use super::*;
48
49    #[test]
50    fn test_memory_store_path() {
51        let provider = MemoryStoreProvider;
52
53        let url = Url::parse("memory://path/to/file").unwrap();
54        let path = provider.extract_path(&url);
55        let expected_path = Path::from("path/to/file");
56        assert_eq!(path, expected_path);
57    }
58}