#pragma once #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace DB { using RemoteFileCacheType = LRUResourceCache; class LocalFileHolder { public: explicit LocalFileHolder(RemoteFileCacheType::MappedHolderPtr cache_controller); explicit LocalFileHolder(RemoteFileCacheType::MappedHolderPtr cache_controller, std::unique_ptr original_readbuffer_, BackgroundSchedulePool * thread_pool_); ~LocalFileHolder(); RemoteFileCacheType::MappedHolderPtr file_cache_controller; std::unique_ptr file_buffer; std::unique_ptr original_readbuffer; BackgroundSchedulePool * thread_pool; }; class RemoteReadBuffer : public BufferWithOwnMemory, public WithFileSize { public: explicit RemoteReadBuffer(size_t buff_size); ~RemoteReadBuffer() override = default; static std::unique_ptr create(ContextPtr context, IRemoteFileMetadataPtr remote_file_metadata, std::unique_ptr read_buffer, size_t buff_size, bool is_random_accessed = false); bool nextImpl() override; off_t seek(off_t off, int whence) override; off_t getPosition() override; std::optional tryGetFileSize() override { return remote_file_size; } private: std::unique_ptr local_file_holder; size_t remote_file_size = 0; }; class ExternalDataSourceCache : private boost::noncopyable { public: ~ExternalDataSourceCache(); // global instance static ExternalDataSourceCache & instance(); void initOnce(ContextPtr context, const String & root_dir_, size_t limit_size_, size_t bytes_read_before_flush_); bool isInitialized() const { return initialized; } std::pair, std::unique_ptr> createReader(ContextPtr context, IRemoteFileMetadataPtr remote_file_metadata, std::unique_ptr & read_buffer, bool is_random_accessed); void updateTotalSize(size_t size) { total_size += size; } protected: ExternalDataSourceCache(); private: // Root directory of local cache for remote filesystem. Strings root_dirs; size_t local_cache_bytes_read_before_flush = 0; std::atomic initialized = false; std::atomic total_size; std::mutex mutex; std::unique_ptr lru_caches; LoggerPtr log = getLogger("ExternalDataSourceCache"); String calculateLocalPath(IRemoteFileMetadataPtr meta) const; BackgroundSchedulePool::TaskHolder recover_task_holder; void recoverTask(); }; }