1//===----------------------------------------------------------------------===//
2// DuckDB
3//
4// duckdb/storage/buffer_manager.hpp
5//
6//
7//===----------------------------------------------------------------------===//
8
9#pragma once
10
11#include "duckdb/storage/buffer_manager.hpp"
12#include "duckdb/storage/buffer/buffer_handle.hpp"
13#include "duckdb/storage/block_manager.hpp"
14#include "duckdb/common/file_system.hpp"
15#include "duckdb/storage/buffer/temporary_file_information.hpp"
16#include "duckdb/main/config.hpp"
17
18namespace duckdb {
19
20class Allocator;
21class BufferPool;
22
23class BufferManager {
24 friend class BufferHandle;
25 friend class BlockHandle;
26 friend class BlockManager;
27
28public:
29 BufferManager() {
30 }
31 virtual ~BufferManager() {
32 }
33
34public:
35 static unique_ptr<BufferManager> CreateStandardBufferManager(DatabaseInstance &db, DBConfig &config);
36 virtual BufferHandle Allocate(idx_t block_size, bool can_destroy = true,
37 shared_ptr<BlockHandle> *block = nullptr) = 0;
38 //! Reallocate an in-memory buffer that is pinned.
39 virtual void ReAllocate(shared_ptr<BlockHandle> &handle, idx_t block_size) = 0;
40 virtual BufferHandle Pin(shared_ptr<BlockHandle> &handle) = 0;
41 virtual void Unpin(shared_ptr<BlockHandle> &handle) = 0;
42 //! Returns the currently allocated memory
43 virtual idx_t GetUsedMemory() const = 0;
44 //! Returns the maximum available memory
45 virtual idx_t GetMaxMemory() const = 0;
46 virtual shared_ptr<BlockHandle> RegisterSmallMemory(idx_t block_size);
47 virtual DUCKDB_API Allocator &GetBufferAllocator();
48 virtual DUCKDB_API void ReserveMemory(idx_t size);
49 virtual DUCKDB_API void FreeReservedMemory(idx_t size);
50 //! Set a new memory limit to the buffer manager, throws an exception if the new limit is too low and not enough
51 //! blocks can be evicted
52 virtual void SetLimit(idx_t limit = (idx_t)-1);
53 virtual vector<TemporaryFileInformation> GetTemporaryFiles();
54 virtual const string &GetTemporaryDirectory();
55 virtual void SetTemporaryDirectory(const string &new_dir);
56 virtual DatabaseInstance &GetDatabase();
57 virtual bool HasTemporaryDirectory() const;
58 //! Construct a managed buffer.
59 virtual unique_ptr<FileBuffer> ConstructManagedBuffer(idx_t size, unique_ptr<FileBuffer> &&source,
60 FileBufferType type = FileBufferType::MANAGED_BUFFER);
61 //! Get the underlying buffer pool responsible for managing the buffers
62 virtual BufferPool &GetBufferPool();
63
64 // Static methods
65 DUCKDB_API static BufferManager &GetBufferManager(DatabaseInstance &db);
66 DUCKDB_API static BufferManager &GetBufferManager(ClientContext &context);
67 DUCKDB_API static BufferManager &GetBufferManager(AttachedDatabase &db);
68
69 static idx_t GetAllocSize(idx_t block_size) {
70 return AlignValue<idx_t, Storage::SECTOR_SIZE>(n: block_size + Storage::BLOCK_HEADER_SIZE);
71 }
72
73protected:
74 virtual void PurgeQueue() = 0;
75 virtual void AddToEvictionQueue(shared_ptr<BlockHandle> &handle);
76 virtual void WriteTemporaryBuffer(block_id_t block_id, FileBuffer &buffer);
77 virtual unique_ptr<FileBuffer> ReadTemporaryBuffer(block_id_t id, unique_ptr<FileBuffer> buffer);
78 virtual void DeleteTemporaryFile(block_id_t id);
79};
80
81} // namespace duckdb
82