1//===----------------------------------------------------------------------===//
2// DuckDB
3//
4// duckdb/common/compressed_file_system.hpp
5//
6//
7//===----------------------------------------------------------------------===//
8
9#pragma once
10
11#include "duckdb/common/common.hpp"
12#include "duckdb/common/file_system.hpp"
13
14namespace duckdb {
15class CompressedFile;
16
17struct StreamData {
18 // various buffers & pointers
19 bool write = false;
20 bool refresh = false;
21 unsafe_unique_array<data_t> in_buff;
22 unsafe_unique_array<data_t> out_buff;
23 data_ptr_t out_buff_start = nullptr;
24 data_ptr_t out_buff_end = nullptr;
25 data_ptr_t in_buff_start = nullptr;
26 data_ptr_t in_buff_end = nullptr;
27
28 idx_t in_buf_size = 0;
29 idx_t out_buf_size = 0;
30};
31
32struct StreamWrapper {
33 DUCKDB_API virtual ~StreamWrapper();
34
35 DUCKDB_API virtual void Initialize(CompressedFile &file, bool write) = 0;
36 DUCKDB_API virtual bool Read(StreamData &stream_data) = 0;
37 DUCKDB_API virtual void Write(CompressedFile &file, StreamData &stream_data, data_ptr_t buffer,
38 int64_t nr_bytes) = 0;
39 DUCKDB_API virtual void Close() = 0;
40};
41
42class CompressedFileSystem : public FileSystem {
43public:
44 DUCKDB_API int64_t Read(FileHandle &handle, void *buffer, int64_t nr_bytes) override;
45 DUCKDB_API int64_t Write(FileHandle &handle, void *buffer, int64_t nr_bytes) override;
46
47 DUCKDB_API void Reset(FileHandle &handle) override;
48
49 DUCKDB_API int64_t GetFileSize(FileHandle &handle) override;
50
51 DUCKDB_API bool OnDiskFile(FileHandle &handle) override;
52 DUCKDB_API bool CanSeek() override;
53
54 DUCKDB_API virtual unique_ptr<StreamWrapper> CreateStream() = 0;
55 DUCKDB_API virtual idx_t InBufferSize() = 0;
56 DUCKDB_API virtual idx_t OutBufferSize() = 0;
57};
58
59class CompressedFile : public FileHandle {
60public:
61 DUCKDB_API CompressedFile(CompressedFileSystem &fs, unique_ptr<FileHandle> child_handle_p, const string &path);
62 DUCKDB_API ~CompressedFile() override;
63
64 CompressedFileSystem &compressed_fs;
65 unique_ptr<FileHandle> child_handle;
66 //! Whether the file is opened for reading or for writing
67 bool write = false;
68 StreamData stream_data;
69
70public:
71 DUCKDB_API void Initialize(bool write);
72 DUCKDB_API int64_t ReadData(void *buffer, int64_t nr_bytes);
73 DUCKDB_API int64_t WriteData(data_ptr_t buffer, int64_t nr_bytes);
74 DUCKDB_API void Close() override;
75
76private:
77 unique_ptr<StreamWrapper> stream_wrapper;
78};
79
80} // namespace duckdb
81