|
| 1 | +/* |
| 2 | + * Copyright 2024-present Alibaba Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +#include "paimon/core/operation/commit/compacted_changelog_path_resolver.h" |
| 18 | + |
| 19 | +#include <string> |
| 20 | +#include <vector> |
| 21 | + |
| 22 | +#include "paimon/common/utils/path_util.h" |
| 23 | +#include "paimon/common/utils/string_utils.h" |
| 24 | + |
| 25 | +namespace paimon { |
| 26 | + |
| 27 | +bool CompactedChangelogPathResolver::IsCompactedChangelogPath(const std::string& path) { |
| 28 | + const std::string file_name = PathUtil::GetName(path); |
| 29 | + return StringUtils::StartsWith(file_name, "compacted-changelog-"); |
| 30 | +} |
| 31 | + |
| 32 | +std::string CompactedChangelogPathResolver::Resolve(const std::string& path) { |
| 33 | + if (!IsCompactedChangelogPath(path)) { |
| 34 | + return path; |
| 35 | + } |
| 36 | + const std::string file_name = PathUtil::GetName(path); |
| 37 | + |
| 38 | + const size_t dot_pos = file_name.find_last_of('.'); |
| 39 | + if (dot_pos == std::string::npos || dot_pos + 1 >= file_name.size()) { |
| 40 | + return path; |
| 41 | + } |
| 42 | + |
| 43 | + const std::string name_without_ext = file_name.substr(0, dot_pos); |
| 44 | + const std::string format = file_name.substr(dot_pos + 1); |
| 45 | + const size_t dollar_pos = name_without_ext.find('$'); |
| 46 | + if (dollar_pos == std::string::npos || dollar_pos + 1 >= name_without_ext.size()) { |
| 47 | + return path; |
| 48 | + } |
| 49 | + |
| 50 | + const std::string base_name = name_without_ext.substr(0, dollar_pos); |
| 51 | + const std::string suffix = name_without_ext.substr(dollar_pos + 1); |
| 52 | + const std::vector<std::string> split_tokens = StringUtils::Split(suffix, "-", false); |
| 53 | + |
| 54 | + // Real compacted changelog path pattern: ...$bucket-len.ext |
| 55 | + if (split_tokens.size() == 2) { |
| 56 | + return path; |
| 57 | + } |
| 58 | + |
| 59 | + // Fake compacted changelog path pattern: ...$bucket-len-offset-sliceLen.ext |
| 60 | + if (split_tokens.size() < 4) { |
| 61 | + return path; |
| 62 | + } |
| 63 | + |
| 64 | + const std::string& bucket = split_tokens[0]; |
| 65 | + const std::string& total_len = split_tokens[1]; |
| 66 | + const std::string real_file_name = |
| 67 | + base_name + "$" + bucket + "-" + total_len + "." + format; |
| 68 | + |
| 69 | + const std::string parent = PathUtil::GetParentDirPath(path); |
| 70 | + const std::string grand_parent = PathUtil::GetParentDirPath(parent); |
| 71 | + if (parent.empty() || grand_parent.empty()) { |
| 72 | + return path; |
| 73 | + } |
| 74 | + |
| 75 | + return PathUtil::JoinPath(PathUtil::JoinPath(grand_parent, "bucket-" + bucket), |
| 76 | + real_file_name); |
| 77 | +} |
| 78 | + |
| 79 | +} // namespace paimon |
0 commit comments