Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pola-rs
GitHub Repository: pola-rs/polars
Path: blob/main/crates/polars-io/src/file_cache/file_fetcher.rs
8424 views
1
use polars_error::{PolarsError, PolarsResult};
2
use polars_utils::pl_path::PlRefPath;
3
4
use super::metadata::FileVersion;
5
use super::utils::last_modified_u64;
6
use crate::cloud::PolarsObjectStore;
7
use crate::pl_async;
8
9
pub trait FileFetcher: Send + Sync {
10
fn get_uri(&self) -> &PlRefPath;
11
fn fetch_metadata(&self) -> PolarsResult<RemoteMetadata>;
12
/// Fetches the object to a `local_path`.
13
fn fetch(&self, local_path: &std::path::Path) -> PolarsResult<()>;
14
fn fetches_as_symlink(&self) -> bool;
15
}
16
17
pub struct RemoteMetadata {
18
pub size: u64,
19
pub(super) version: FileVersion,
20
}
21
22
/// A struct that fetches data from local disk and stores it into the `cache`.
23
/// Mostly used for debugging, it only ever gets called if `POLARS_FORCE_ASYNC` is set.
24
pub(super) struct LocalFileFetcher {
25
path: PlRefPath,
26
}
27
28
impl LocalFileFetcher {
29
pub(super) fn from_uri(uri: PlRefPath) -> Self {
30
debug_assert_eq!(
31
std::fs::canonicalize(uri.as_str())
32
.ok()
33
.and_then(|x| PlRefPath::try_from_pathbuf(x).ok())
34
.as_ref(),
35
Some(&uri),
36
);
37
38
Self { path: uri }
39
}
40
}
41
42
impl FileFetcher for LocalFileFetcher {
43
fn get_uri(&self) -> &PlRefPath {
44
&self.path
45
}
46
47
fn fetches_as_symlink(&self) -> bool {
48
#[cfg(target_family = "unix")]
49
{
50
true
51
}
52
#[cfg(not(target_family = "unix"))]
53
{
54
false
55
}
56
}
57
58
fn fetch_metadata(&self) -> PolarsResult<RemoteMetadata> {
59
let metadata = std::fs::metadata(&self.path).map_err(PolarsError::from)?;
60
61
Ok(RemoteMetadata {
62
size: metadata.len(),
63
version: FileVersion::Timestamp(last_modified_u64(&metadata)),
64
})
65
}
66
67
fn fetch(&self, local_path: &std::path::Path) -> PolarsResult<()> {
68
#[cfg(target_family = "unix")]
69
{
70
std::os::unix::fs::symlink(&self.path, local_path).map_err(PolarsError::from)
71
}
72
#[cfg(not(target_family = "unix"))]
73
{
74
std::fs::copy(&self.path, local_path).map_err(PolarsError::from)?;
75
Ok(())
76
}
77
}
78
}
79
80
pub(super) struct CloudFileFetcher {
81
pub(super) uri: PlRefPath,
82
pub(super) cloud_path: object_store::path::Path,
83
pub(super) object_store: PolarsObjectStore,
84
}
85
86
impl FileFetcher for CloudFileFetcher {
87
fn get_uri(&self) -> &PlRefPath {
88
&self.uri
89
}
90
91
fn fetches_as_symlink(&self) -> bool {
92
false
93
}
94
95
fn fetch_metadata(&self) -> PolarsResult<RemoteMetadata> {
96
let metadata =
97
pl_async::get_runtime().block_in_place_on(self.object_store.head(&self.cloud_path))?;
98
99
Ok(RemoteMetadata {
100
size: metadata.size as u64,
101
version: metadata
102
.e_tag
103
.map(|x| FileVersion::ETag(blake3::hash(x.as_bytes()).to_hex()[..32].to_string()))
104
.unwrap_or_else(|| {
105
FileVersion::Timestamp(metadata.last_modified.timestamp_millis() as u64)
106
}),
107
})
108
}
109
110
fn fetch(&self, local_path: &std::path::Path) -> PolarsResult<()> {
111
pl_async::get_runtime().block_in_place_on(async {
112
let file = &mut tokio::fs::OpenOptions::new()
113
.write(true)
114
.truncate(true)
115
.open(local_path)
116
.await
117
.map_err(PolarsError::from)?;
118
119
self.object_store.download(&self.cloud_path, file).await
120
})
121
}
122
}
123
124