codope commented on code in PR #395: URL: https://github.com/apache/hudi-rs/pull/395#discussion_r2452318890
########## crates/core/src/timeline/loader.rs: ########## @@ -0,0 +1,172 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +use crate::config::internal::HudiInternalConfig::{ + TimelineArchivedReadEnabled, TimelineArchivedUnavailableBehavior, +}; +use crate::config::table::HudiTableConfig::ArchiveLogFolder; +use crate::config::HudiConfigs; +use crate::error::CoreError; +use crate::metadata::HUDI_METADATA_DIR; +use crate::storage::Storage; +use crate::timeline::instant::Instant; +use crate::timeline::lsm_tree::LSM_TIMELINE_DIR; +use crate::timeline::selector::TimelineSelector; +use crate::Result; +use log::debug; +use std::sync::Arc; + +#[derive(Debug, Clone)] +pub enum TimelineLoader { + LayoutOneActive(Arc<Storage>), + LayoutOneArchived(Arc<Storage>), + LayoutTwoActive(Arc<Storage>), + LayoutTwoArchived(Arc<Storage>), +} + +impl TimelineLoader { + pub async fn load_instants( + &self, + selector: &TimelineSelector, + desc: bool, + ) -> Result<Vec<Instant>> { + match self { + TimelineLoader::LayoutOneActive(storage) => { + let files = storage.list_files(Some(HUDI_METADATA_DIR)).await?; + let mut instants = Vec::with_capacity(files.len() / 3); + + for file_info in files { + match selector.try_create_instant(file_info.name.as_str()) { + Ok(instant) => instants.push(instant), + Err(e) => { + debug!( + "Instant not created from file {:?} due to: {:?}", + file_info, e + ); + } + } + } + + instants.sort_unstable(); + instants.shrink_to_fit(); + + if desc { + Ok(instants.into_iter().rev().collect()) + } else { + Ok(instants) + } + } + TimelineLoader::LayoutTwoActive(storage) => { + let files = storage.list_files(Some(LSM_TIMELINE_DIR)).await?; + let mut instants = Vec::new(); + + for file_info in files { + if file_info.name.starts_with("history/") || file_info.name.ends_with(".crc") { + continue; + } + match selector.try_create_instant(file_info.name.as_str()) { + Ok(instant) => instants.push(instant), + Err(e) => { + debug!( + "Instant not created from file {:?} due to: {:?}", + file_info, e + ); + } + } + } + + instants.sort_unstable(); + instants.shrink_to_fit(); + + if desc { + Ok(instants.into_iter().rev().collect()) + } else { + Ok(instants) + } + } + _ => Err(CoreError::Unsupported( + "Loading from this timeline layout is not implemented yet.".to_string(), + )), + } + } + + pub(crate) async fn load_archived_instants( + &self, + selector: &TimelineSelector, + desc: bool, + ) -> Result<Vec<Instant>> { + // Config wiring: if archived read not enabled, return behavior based on policy + let storage = match self { + TimelineLoader::LayoutOneArchived(storage) + | TimelineLoader::LayoutTwoArchived(storage) => storage.clone(), + _ => return Ok(Vec::new()), // Active loaders don't have archived parts + }; + + let configs: Arc<HudiConfigs> = storage.hudi_configs.clone(); + let enabled = configs + .get_or_default(TimelineArchivedReadEnabled) + .to::<bool>(); + if !enabled { + let behavior = configs + .get_or_default(TimelineArchivedUnavailableBehavior) + .to::<String>() + .to_ascii_lowercase(); + return match behavior.as_str() { + "error" => Err(CoreError::Unsupported( + "Archived timeline read is disabled; shorten time range or enable archived read" + .to_string(), + )), + _ => Ok(Vec::new()), // continue silently with empty archived + }; + } + + match self { + TimelineLoader::LayoutOneArchived(storage) => { + // Resolve archive folder from configs or fallback + let archive_dir = configs + .try_get(ArchiveLogFolder) + .map(|v| v.to::<String>()) + .unwrap_or_else(|| ".hoodie/archived".to_string()); Review Comment: used `get_or_default` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
