-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuffer_pool.hpp
More file actions
56 lines (43 loc) · 1.62 KB
/
buffer_pool.hpp
File metadata and controls
56 lines (43 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#ifndef CACHE_BUFFER_POOL_HPP
#define CACHE_BUFFER_POOL_HPP
#include "shard.hpp"
#include <atomic>
#include <memory>
#include <string>
#include <thread>
#include <vector>
namespace cache {
/** Configuration for BufferPool: shard count and eviction limit per shard. */
struct BufferPoolConfig {
int nShards = defaultNShards;
int maxEntriesPerShard = defaultMaxEntriesPerShard;
};
/** Sharded buffer pool: file-backed page cache with per-shard locking and a background flusher. */
class BufferPool {
public:
/** Opens or creates the file at \a path and builds a pool with \a config. Starts the flusher thread. Returns nullptr if the file cannot be opened. */
static std::unique_ptr<BufferPool> New(const std::string& path,
BufferPoolConfig config = {});
/** Writes \a buf to the page at file offset \a offset. Returns false on error. */
bool Put(const PageBuf& buf, int offset);
/** Reads the page at file offset \a offset into \a buf. Returns false on error. */
bool Get(PageBuf& buf, int offset);
/** Starts the background thread that flushes dirty pages every 5 seconds. */
void startFlusher();
/** Stops the flusher thread (blocks until it exits). */
void stopFlusher();
/** Stops the flusher and closes the file. */
~BufferPool();
private:
/** Hashes \a key for shard selection. */
static unsigned hash(int key);
/** Loop run by the flusher thread: sleep 5s, then flush all shards. */
void flusher();
int fd_{-1};
int nShards_{0};
std::vector<std::unique_ptr<Shard>> shards_;
std::thread flusherThread_;
std::atomic<bool> stopFlusher_{false};
};
}
#endif