// Copyright 2023 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. #pragma once #include #include #include #include #include namespace DB::DM { using ColId = DB::ColumnID; using PackId = size_t; using PackRange = std::pair; using PackRanges = std::vector; enum class ColumnCacheType { ExtraColumnCache, DataSharingCache, }; // ColumnCache is a thread-safe cache for columns, the entry is one pack. class ColumnCache : private boost::noncopyable { public: enum class Strategy { Memory, Disk, Unknown }; explicit ColumnCache(ColumnCacheType type_ = ColumnCacheType::ExtraColumnCache) : type(type_) {} using RangeWithStrategy = std::pair; using RangeWithStrategys = std::vector; RangeWithStrategys getReadStrategy(size_t start_pack_idx, size_t pack_count, ColId column_id); static RangeWithStrategys getCleanReadStrategy( size_t start_pack_idx, size_t pack_count, const std::vector & clean_read_pack_idx); void tryPutColumn(size_t pack_id, ColId column_id, const ColumnPtr & column, size_t rows_offset, size_t rows_count); using ColumnCacheElement = std::pair>; ColumnCacheElement getColumn(size_t pack_id, ColId column_id); void delColumn(ColId column_id, size_t upper_pack_id); void clear() { column_caches.withExclusive([](auto & column_caches) { column_caches.clear(); }); } private: static RangeWithStrategys getReadStrategyImpl( size_t start_pack_idx, size_t pack_count, ColId column_id, std::function is_hit); bool isPackInCache(PackId pack_id, ColId column_id); private: struct ColumnCacheEntry { std::unordered_map columns; size_t rows_offset; size_t rows_count; }; SharedMutexProtected> column_caches; const ColumnCacheType type; }; using ColumnCachePtr = std::shared_ptr; using ColumnCachePtrs = std::vector; using RangeWithStrategy = ColumnCache::RangeWithStrategy; using RangeWithStrategys = ColumnCache::RangeWithStrategys; using ColumnCacheElement = ColumnCache::ColumnCacheElement; } // namespace DB::DM