1#pragma once
2
3#include <memory>
4#include <time.h>
5#include <IO/ReadBufferFromFileBase.h>
6#include "CompressedReadBufferBase.h"
7#include <IO/UncompressedCache.h>
8#include <port/clock.h>
9
10
11namespace DB
12{
13
14
15/** A buffer for reading from a compressed file using the cache of decompressed blocks.
16 * The external cache is passed as an argument to the constructor.
17 * Allows you to increase performance in cases where the same blocks are often read.
18 * Disadvantages:
19 * - in case you need to read a lot of data in a row, but of them only a part is cached, you have to do seek-and.
20 */
21class CachedCompressedReadBuffer : public CompressedReadBufferBase, public ReadBuffer
22{
23private:
24 const std::string path;
25 UncompressedCache * cache;
26 size_t buf_size;
27 size_t estimated_size;
28 size_t aio_threshold;
29
30 std::unique_ptr<ReadBufferFromFileBase> file_in;
31 size_t file_pos;
32
33 /// A piece of data from the cache, or a piece of read data that we put into the cache.
34 UncompressedCache::MappedPtr owned_cell;
35
36 void initInput();
37 bool nextImpl() override;
38
39 /// Passed into file_in.
40 ReadBufferFromFileBase::ProfileCallback profile_callback;
41 clockid_t clock_type {};
42
43public:
44 CachedCompressedReadBuffer(
45 const std::string & path_, UncompressedCache * cache_, size_t estimated_size_, size_t aio_threshold_,
46 size_t buf_size_ = DBMS_DEFAULT_BUFFER_SIZE);
47
48
49 void seek(size_t offset_in_compressed_file, size_t offset_in_decompressed_block);
50
51 void setProfileCallback(const ReadBufferFromFileBase::ProfileCallback & profile_callback_, clockid_t clock_type_ = CLOCK_MONOTONIC_COARSE)
52 {
53 profile_callback = profile_callback_;
54 clock_type = clock_type_;
55 }
56};
57
58}
59