|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +//! Tag manager for reading tag metadata using FileIO. |
| 19 | +//! |
| 20 | +//! Reference: [org.apache.paimon.utils.TagManager](https://github.com/apache/paimon/blob/master/paimon-core/src/main/java/org/apache/paimon/utils/TagManager.java) |
| 21 | +//! and [pypaimon.tag.tag_manager.TagManager](https://github.com/apache/paimon/blob/master/paimon-python/pypaimon/tag/tag_manager.py). |
| 22 | +
|
| 23 | +use crate::io::FileIO; |
| 24 | +use crate::spec::Snapshot; |
| 25 | + |
| 26 | +const TAG_DIR: &str = "tag"; |
| 27 | +const TAG_PREFIX: &str = "tag-"; |
| 28 | + |
| 29 | +/// Manager for tag files using unified FileIO. |
| 30 | +/// |
| 31 | +/// Tags are named snapshots stored as JSON files at `{table_path}/tag/tag-{name}`. |
| 32 | +/// The tag file format is identical to a Snapshot JSON file. |
| 33 | +/// |
| 34 | +/// Reference: [org.apache.paimon.utils.TagManager](https://github.com/apache/paimon/blob/master/paimon-core/src/main/java/org/apache/paimon/utils/TagManager.java) |
| 35 | +#[derive(Debug, Clone)] |
| 36 | +pub struct TagManager { |
| 37 | + file_io: FileIO, |
| 38 | + table_path: String, |
| 39 | +} |
| 40 | + |
| 41 | +impl TagManager { |
| 42 | + pub fn new(file_io: FileIO, table_path: String) -> Self { |
| 43 | + Self { |
| 44 | + file_io, |
| 45 | + table_path, |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + /// Path to the tag directory (e.g. `table_path/tag`). |
| 50 | + pub fn tag_directory(&self) -> String { |
| 51 | + format!("{}/{}", self.table_path, TAG_DIR) |
| 52 | + } |
| 53 | + |
| 54 | + /// Path to the tag file for the given name (e.g. `tag/tag-my_tag`). |
| 55 | + pub fn tag_path(&self, tag_name: &str) -> String { |
| 56 | + format!("{}/{}{}", self.tag_directory(), TAG_PREFIX, tag_name) |
| 57 | + } |
| 58 | + |
| 59 | + /// Check if a tag exists. |
| 60 | + pub async fn tag_exists(&self, tag_name: &str) -> crate::Result<bool> { |
| 61 | + let path = self.tag_path(tag_name); |
| 62 | + let input = self.file_io.new_input(&path)?; |
| 63 | + input.exists().await |
| 64 | + } |
| 65 | + |
| 66 | + /// Get the snapshot for a tag, or None if the tag file does not exist. |
| 67 | + /// |
| 68 | + /// Tag files are JSON with the same schema as Snapshot. |
| 69 | + /// Reads directly and catches NotFound to avoid a separate exists() IO round-trip. |
| 70 | + pub async fn get(&self, tag_name: &str) -> crate::Result<Option<Snapshot>> { |
| 71 | + let path = self.tag_path(tag_name); |
| 72 | + let input = self.file_io.new_input(&path)?; |
| 73 | + let bytes = match input.read().await { |
| 74 | + Ok(b) => b, |
| 75 | + Err(crate::Error::IoUnexpected { ref source, .. }) |
| 76 | + if source.kind() == opendal::ErrorKind::NotFound => |
| 77 | + { |
| 78 | + return Ok(None); |
| 79 | + } |
| 80 | + Err(e) => return Err(e), |
| 81 | + }; |
| 82 | + let snapshot: Snapshot = |
| 83 | + serde_json::from_slice(&bytes).map_err(|e| crate::Error::DataInvalid { |
| 84 | + message: format!("tag '{tag_name}' JSON invalid: {e}"), |
| 85 | + source: Some(Box::new(e)), |
| 86 | + })?; |
| 87 | + Ok(Some(snapshot)) |
| 88 | + } |
| 89 | +} |
0 commit comments