Path: blob/main/crates/polars-io/src/file_cache/metadata.rs
6939 views
use std::path::Path;1use std::sync::Arc;23use serde::{Deserialize, Serialize};45#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]6pub(super) enum FileVersion {7Timestamp(u64),8ETag(String),9Uninitialized,10}1112#[derive(Debug)]13pub enum LocalCompareError {14LastModifiedMismatch { expected: u64, actual: u64 },15SizeMismatch { expected: u64, actual: u64 },16DataFileReadError(std::io::Error),17}1819pub type LocalCompareResult = Result<(), LocalCompareError>;2021/// Metadata written to a file used to track state / synchronize across processes.22#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]23pub(super) struct EntryMetadata {24pub(super) uri: Arc<str>,25pub(super) local_last_modified: u64,26pub(super) local_size: u64,27pub(super) remote_version: FileVersion,28/// TTL since last access, in seconds.29pub(super) ttl: u64,30}3132impl std::fmt::Display for LocalCompareError {33fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {34match self {35Self::LastModifiedMismatch { expected, actual } => write!(36f,37"last modified time mismatch: expected {expected}, found {actual}"38),39Self::SizeMismatch { expected, actual } => {40write!(f, "size mismatch: expected {expected}, found {actual}")41},42Self::DataFileReadError(err) => {43write!(f, "failed to read local file metadata: {err}")44},45}46}47}4849impl EntryMetadata {50pub(super) fn new(uri: Arc<str>, ttl: u64) -> Self {51Self {52uri,53local_last_modified: 0,54local_size: 0,55remote_version: FileVersion::Uninitialized,56ttl,57}58}5960pub(super) fn compare_local_state(&self, data_file_path: &Path) -> LocalCompareResult {61let metadata = match std::fs::metadata(data_file_path) {62Ok(v) => v,63Err(e) => return Err(LocalCompareError::DataFileReadError(e)),64};6566let local_last_modified = super::utils::last_modified_u64(&metadata);67let local_size = metadata.len();6869if local_last_modified != self.local_last_modified {70Err(LocalCompareError::LastModifiedMismatch {71expected: self.local_last_modified,72actual: local_last_modified,73})74} else if local_size != self.local_size {75Err(LocalCompareError::SizeMismatch {76expected: self.local_size,77actual: local_size,78})79} else {80Ok(())81}82}8384pub(super) fn try_write<W: std::io::Write>(&self, writer: &mut W) -> serde_json::Result<()> {85serde_json::to_writer(writer, self)86}8788pub(super) fn try_from_reader<R: std::io::Read>(reader: &mut R) -> serde_json::Result<Self> {89serde_json::from_reader(reader)90}91}929394