|
| 1 | +/* |
| 2 | + * Copyright 2026-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/disk/file_io_channel.h" |
| 18 | + |
| 19 | +#include <iomanip> |
| 20 | +#include <sstream> |
| 21 | +#include <utility> |
| 22 | + |
| 23 | +#include "paimon/common/utils/path_util.h" |
| 24 | + |
| 25 | +namespace paimon { |
| 26 | + |
| 27 | +std::string FileIOChannel::GenerateRandomHexString(std::mt19937* random) { |
| 28 | + std::uniform_int_distribution<int32_t> dist(0, 255); |
| 29 | + std::ostringstream hex_stream; |
| 30 | + hex_stream << std::hex << std::setfill('0'); |
| 31 | + for (int32_t i = 0; i < kRandomBytesLength; ++i) { |
| 32 | + hex_stream << std::setw(2) << dist(*random); |
| 33 | + } |
| 34 | + return hex_stream.str(); |
| 35 | +} |
| 36 | + |
| 37 | +FileIOChannel::ID::ID(const std::string& path) : path_(path) {} |
| 38 | + |
| 39 | +FileIOChannel::ID::ID(const std::string& base_path, std::mt19937* random) |
| 40 | + : path_(PathUtil::JoinPath(base_path, GenerateRandomHexString(random) + ".channel")) {} |
| 41 | + |
| 42 | +FileIOChannel::ID::ID(const std::string& base_path, const std::string& prefix, std::mt19937* random) |
| 43 | + : path_(PathUtil::JoinPath(base_path, |
| 44 | + prefix + "-" + GenerateRandomHexString(random) + ".channel")) {} |
| 45 | + |
| 46 | +const std::string& FileIOChannel::ID::GetPath() const { |
| 47 | + return path_; |
| 48 | +} |
| 49 | + |
| 50 | +bool FileIOChannel::ID::operator==(const ID& other) const { |
| 51 | + return path_ == other.path_; |
| 52 | +} |
| 53 | + |
| 54 | +bool FileIOChannel::ID::operator!=(const ID& other) const { |
| 55 | + return !(*this == other); |
| 56 | +} |
| 57 | + |
| 58 | +size_t FileIOChannel::ID::Hash::operator()(const ID& id) const { |
| 59 | + return std::hash<std::string>{}(id.path_); |
| 60 | +} |
| 61 | + |
| 62 | +FileIOChannel::Enumerator::Enumerator(const std::string& base_path, std::mt19937* random) |
| 63 | + : path_(base_path), name_prefix_(GenerateRandomHexString(random)) {} |
| 64 | + |
| 65 | +FileIOChannel::ID FileIOChannel::Enumerator::Next() { |
| 66 | + std::ostringstream filename; |
| 67 | + filename << name_prefix_ << "." << std::setfill('0') << std::setw(6) << (local_counter_++) |
| 68 | + << ".channel"; |
| 69 | + |
| 70 | + std::string full_path = PathUtil::JoinPath(path_, filename.str()); |
| 71 | + return ID(full_path); |
| 72 | +} |
| 73 | + |
| 74 | +} // namespace paimon |
0 commit comments