|
| 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 | +#include "paimon/common/data/blob_view_struct.h" |
| 17 | + |
| 18 | +#include <utility> |
| 19 | + |
| 20 | +#include "fmt/format.h" |
| 21 | +#include "paimon/common/io/memory_segment_output_stream.h" |
| 22 | +#include "paimon/common/memory/memory_segment_utils.h" |
| 23 | +#include "paimon/common/utils/murmurhash_utils.h" |
| 24 | +#include "paimon/io/byte_array_input_stream.h" |
| 25 | +#include "paimon/io/byte_order.h" |
| 26 | +#include "paimon/io/data_input_stream.h" |
| 27 | +#include "paimon/memory/bytes.h" |
| 28 | +#include "paimon/status.h" |
| 29 | + |
| 30 | +namespace paimon { |
| 31 | +PAIMON_UNIQUE_PTR<Bytes> BlobViewStruct::Serialize(const std::shared_ptr<MemoryPool>& pool) const { |
| 32 | + MemorySegmentOutputStream out(MemorySegmentOutputStream::DEFAULT_SEGMENT_SIZE, pool); |
| 33 | + out.SetOrder(ByteOrder::PAIMON_LITTLE_ENDIAN); |
| 34 | + |
| 35 | + out.WriteValue<int8_t>(kCurrentVersion); |
| 36 | + out.WriteValue<int64_t>(kMagic); |
| 37 | + std::string identifier = identifier_.GetFullName(); |
| 38 | + out.WriteValue<int32_t>(static_cast<int32_t>(identifier.size())); |
| 39 | + auto uri_bytes = std::make_shared<Bytes>(identifier, pool.get()); |
| 40 | + out.WriteBytes(uri_bytes); |
| 41 | + out.WriteValue<int32_t>(field_id_); |
| 42 | + out.WriteValue<int64_t>(row_id_); |
| 43 | + return MemorySegmentUtils::CopyToBytes(out.Segments(), 0, out.CurrentSize(), pool.get()); |
| 44 | +} |
| 45 | + |
| 46 | +Result<std::unique_ptr<BlobViewStruct>> BlobViewStruct::Deserialize(const char* buffer, |
| 47 | + uint64_t size) { |
| 48 | + auto input_stream = std::make_shared<ByteArrayInputStream>(buffer, size); |
| 49 | + DataInputStream in(std::move(input_stream)); |
| 50 | + in.SetOrder(ByteOrder::PAIMON_LITTLE_ENDIAN); |
| 51 | + |
| 52 | + PAIMON_ASSIGN_OR_RAISE(int8_t version, in.ReadValue<int8_t>()); |
| 53 | + if (version != kCurrentVersion) { |
| 54 | + return Status::Invalid(fmt::format( |
| 55 | + "Expecting BlobViewStruct version to be {}, but found {}.", kCurrentVersion, version)); |
| 56 | + } |
| 57 | + PAIMON_ASSIGN_OR_RAISE(int64_t magic, in.ReadValue<int64_t>()); |
| 58 | + if (kMagic != magic) { |
| 59 | + return Status::Invalid( |
| 60 | + fmt::format("Invalid BlobViewStruct: missing magic header. Expected magic: {}, " |
| 61 | + "but found {}", |
| 62 | + kMagic, magic)); |
| 63 | + } |
| 64 | + PAIMON_ASSIGN_OR_RAISE(int32_t length, in.ReadValue<int32_t>()); |
| 65 | + std::string identifier_str(length, '\0'); |
| 66 | + PAIMON_RETURN_NOT_OK(in.Read(identifier_str.data(), identifier_str.size())); |
| 67 | + PAIMON_ASSIGN_OR_RAISE(int32_t field_id, in.ReadValue<int32_t>()); |
| 68 | + PAIMON_ASSIGN_OR_RAISE(int64_t row_id, in.ReadValue<int64_t>()); |
| 69 | + PAIMON_ASSIGN_OR_RAISE(Identifier identifier, Identifier::FromString(identifier_str)); |
| 70 | + return std::make_unique<BlobViewStruct>(identifier, field_id, row_id); |
| 71 | +} |
| 72 | + |
| 73 | +Result<bool> BlobViewStruct::IsBlobViewStruct(const char* buffer, uint64_t size) { |
| 74 | + if (size < kMinViewLength) { |
| 75 | + return false; |
| 76 | + } |
| 77 | + auto input_stream = std::make_shared<ByteArrayInputStream>(buffer, size); |
| 78 | + DataInputStream in(std::move(input_stream)); |
| 79 | + in.SetOrder(ByteOrder::PAIMON_LITTLE_ENDIAN); |
| 80 | + |
| 81 | + PAIMON_ASSIGN_OR_RAISE(int8_t version, in.ReadValue<int8_t>()); |
| 82 | + if (version != kCurrentVersion) { |
| 83 | + return false; |
| 84 | + } |
| 85 | + PAIMON_ASSIGN_OR_RAISE(int64_t magic, in.ReadValue<int64_t>()); |
| 86 | + return kMagic == magic; |
| 87 | +} |
| 88 | + |
| 89 | +std::string BlobViewStruct::ToString() const { |
| 90 | + return fmt::format("BlobViewStruct{{identifier={}, fieldId={}, rowId={}}}", |
| 91 | + identifier_.GetFullName(), field_id_, row_id_); |
| 92 | +} |
| 93 | + |
| 94 | +bool BlobViewStruct::operator==(const BlobViewStruct& other) const { |
| 95 | + if (this == &other) { |
| 96 | + return true; |
| 97 | + } |
| 98 | + return field_id_ == other.field_id_ && row_id_ == other.row_id_ && |
| 99 | + identifier_ == other.identifier_; |
| 100 | +} |
| 101 | + |
| 102 | +bool BlobViewStruct::operator!=(const BlobViewStruct& other) const { |
| 103 | + return !(*this == other); |
| 104 | +} |
| 105 | + |
| 106 | +int32_t BlobViewStruct::HashCode() const { |
| 107 | + int32_t hash = |
| 108 | + MurmurHashUtils::HashUnsafeBytes(reinterpret_cast<const void*>(&field_id_), |
| 109 | + /*offset=*/0, sizeof(field_id_), identifier_.HashCode()); |
| 110 | + return MurmurHashUtils::HashUnsafeBytes(reinterpret_cast<const void*>(&row_id_), |
| 111 | + /*offset=*/0, sizeof(row_id_), hash); |
| 112 | +} |
| 113 | +} // namespace paimon |
0 commit comments