// Copyright 2025 PingCAP, Inc. // // Licensed 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. #include #include #include namespace DB::DM::InvertedIndex { template void Block::serialize(WriteBuffer & write_buf) const { writeIntBinary(static_cast(entries.size()), write_buf); // write all values first for (const auto & entry : entries) { writeIntBinary(entry.value, write_buf); writeIntBinary(static_cast(entry.row_ids.size()), write_buf); } // write all row_ids for (const auto & entry : entries) { const auto & row_ids = entry.row_ids; write_buf.write(reinterpret_cast(row_ids.data()), row_ids.size() * sizeof(RowID)); } } template void Block::deserialize(Block & block, ReadBuffer & read_buf) { UInt32 size; readIntBinary(size, read_buf); block.entries.resize(size); for (UInt32 i = 0; i < size; ++i) { T value; readIntBinary(value, read_buf); UInt32 row_ids_size; readIntBinary(row_ids_size, read_buf); block.entries[i].value = value; block.entries[i].row_ids.resize(row_ids_size); } for (UInt32 i = 0; i < size; ++i) { auto & entry = block.entries[i]; read_buf.readStrict(reinterpret_cast(entry.row_ids.data()), entry.row_ids.size() * sizeof(RowID)); } } template void Block::search(BitmapFilterPtr & bitmap_filter, ReadBuffer & read_buf, T key) { UInt32 size; readIntBinary(size, read_buf); UInt32 seek_offset = size * (sizeof(T) + sizeof(UInt32)); for (UInt32 i = 0; i < size; ++i) { T value; readIntBinary(value, read_buf); UInt32 row_ids_size; readIntBinary(row_ids_size, read_buf); seek_offset -= (sizeof(T) + sizeof(UInt32)); if (value == key) { // ignore the rest values and previous row_ids read_buf.ignore(seek_offset); RowIDs row_ids(row_ids_size); read_buf.readStrict(reinterpret_cast(row_ids.data()), row_ids_size * sizeof(RowID)); bitmap_filter->set(row_ids, nullptr); return; } seek_offset += row_ids_size * sizeof(RowID); } } template void Block::searchRange(BitmapFilterPtr & bitmap_filter, ReadBuffer & read_buf, T begin, T end) { UInt32 read_count = read_buf.count(); UInt32 size; readIntBinary(size, read_buf); UInt32 acc_row_ids_size = 0; UInt32 start_offset = 0; UInt32 end_offset = 0; for (UInt32 i = 0; i < size; ++i) { T value; readIntBinary(value, read_buf); UInt32 row_ids_size; readIntBinary(row_ids_size, read_buf); if (value >= begin && value <= end && start_offset == 0) start_offset = sizeof(UInt32) + size * (sizeof(T) + sizeof(UInt32)) + acc_row_ids_size * sizeof(RowID); acc_row_ids_size += row_ids_size; if (value >= begin && value <= end) end_offset = sizeof(UInt32) + size * (sizeof(T) + sizeof(UInt32)) + acc_row_ids_size * sizeof(RowID); if (value > end) break; } if (start_offset == 0) return; read_count = read_buf.count() - read_count; read_buf.ignore(start_offset - read_count); RowIDs row_ids((end_offset - start_offset) / sizeof(RowID)); read_buf.readStrict(reinterpret_cast(row_ids.data()), row_ids.size() * sizeof(RowID)); bitmap_filter->set(row_ids, nullptr); } template void MetaEntry::serialize(WriteBuffer & write_buf) const { writeIntBinary(offset, write_buf); writeIntBinary(size, write_buf); writeIntBinary(min, write_buf); writeIntBinary(max, write_buf); } template void MetaEntry::deserialize(MetaEntry & entry, ReadBuffer & read_buf) { readIntBinary(entry.offset, read_buf); readIntBinary(entry.size, read_buf); readIntBinary(entry.min, read_buf); readIntBinary(entry.max, read_buf); } template void Meta::serialize(WriteBuffer & write_buf) const { writeIntBinary(static_cast(sizeof(T)), write_buf); writeIntBinary(static_cast(entries.size()), write_buf); for (const auto & entry : entries) entry.serialize(write_buf); } template void Meta::deserialize(Meta & meta, ReadBuffer & read_buf) { UInt8 type_size; readIntBinary(type_size, read_buf); RUNTIME_CHECK(type_size == sizeof(T)); UInt32 size; readIntBinary(size, read_buf); meta.entries.resize(size); for (auto & entry : meta.entries) MetaEntry::deserialize(entry, read_buf); } template struct Block; template struct Block; template struct Block; template struct Block; template struct Block; template struct Block; template struct Block; template struct Block; template struct MetaEntry; template struct MetaEntry; template struct MetaEntry; template struct MetaEntry; template struct MetaEntry; template struct MetaEntry; template struct MetaEntry; template struct MetaEntry; template struct Meta; template struct Meta; template struct Meta; template struct Meta; template struct Meta; template struct Meta; template struct Meta; template struct Meta; } // namespace DB::DM::InvertedIndex