Skip to main content

lance_io/object_store/providers/
memory.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright The Lance Authors
3
4use std::{collections::HashMap, sync::Arc};
5
6use crate::object_store::{
7    ObjectStore, ObjectStoreParams, ObjectStoreProvider, StorageOptions,
8    DEFAULT_CLOUD_IO_PARALLELISM, DEFAULT_LOCAL_BLOCK_SIZE, DEFAULT_MAX_IOP_SIZE,
9};
10use lance_core::error::Result;
11use object_store::{memory::InMemory, path::Path};
12use url::Url;
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().cloned().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            max_iop_size: *DEFAULT_MAX_IOP_SIZE,
29            use_constant_size_upload_parts: false,
30            list_is_lexically_ordered: true,
31            io_parallelism: DEFAULT_CLOUD_IO_PARALLELISM,
32            download_retry_count,
33            io_tracker: Default::default(),
34            store_prefix: self
35                .calculate_object_store_prefix(&base_path, params.storage_options())?,
36        })
37    }
38
39    fn extract_path(&self, url: &Url) -> Result<Path> {
40        let mut output = String::new();
41        if let Some(domain) = url.domain() {
42            output.push_str(domain);
43        }
44        output.push_str(url.path());
45        Ok(Path::from(output))
46    }
47
48    fn calculate_object_store_prefix(
49        &self,
50        _url: &Url,
51        _storage_options: Option<&HashMap<String, String>>,
52    ) -> Result<String> {
53        Ok("memory".to_string())
54    }
55}
56
57#[cfg(test)]
58mod tests {
59    use super::*;
60
61    #[test]
62    fn test_memory_store_path() {
63        let provider = MemoryStoreProvider;
64
65        let url = Url::parse("memory://path/to/file").unwrap();
66        let path = provider.extract_path(&url).unwrap();
67        let expected_path = Path::from("path/to/file");
68        assert_eq!(path, expected_path);
69    }
70
71    #[test]
72    fn test_calculate_object_store_prefix() {
73        let provider = MemoryStoreProvider;
74        assert_eq!(
75            "memory",
76            provider
77                .calculate_object_store_prefix(&Url::parse("memory://etc").unwrap(), None)
78                .unwrap()
79        );
80    }
81}