-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSSTable.h
103 lines (87 loc) · 2.62 KB
/
SSTable.h
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#ifndef LSM_KV__SSTABLE_H_
#define LSM_KV__SSTABLE_H_
#include <algorithm>
#include <bitset>
#include <fstream>
#include <utility>
#include "MurmurHash3.h"
#include "SkipList.h"
// Its ascending attribute must be promised by code
using SSTableDic = vector<pair<uint64_t, string>>;
using SSTableIndex = pair<uint64_t, int>;
class SSTableHeader {
public:
uint64_t timeStamp{}, length{}, minKey{}, maxKey{};
SSTableHeader();
explicit SSTableHeader(uint64_t _timeStamp, uint64_t _size, uint64_t _minKey,
uint64_t _maxKey);
SSTableHeader(const SSTableHeader &other);
};
class BloomFilter {
private:
bitset<10240 * 8> bits;
public:
explicit BloomFilter(const SkipList &memTable);
explicit BloomFilter(const SSTableDic &dic);
explicit BloomFilter(ifstream &in);
BloomFilter();
void write(ofstream &out);
bool exists(uint64_t key) const;
};
class SSTableCache {
private:
SSTableHeader header;
vector<SSTableIndex> index;
BloomFilter bloomFilter;
const string fileName;
public:
explicit SSTableCache(const SkipList &memTable, uint64_t timeStamp,
string _fileName);
explicit SSTableCache(const SSTableDic &dic, uint64_t timeStamp,
string _fileName);
explicit SSTableCache(string _fileName);
string get(uint64_t key) const;
SSTableHeader getHeader() const;
};
class SSTable {
public:
// utils
static void write64(ofstream &out, uint64_t n);
static void write32(ofstream &out, uint32_t n);
static void read64(ifstream &in, uint64_t &n);
static void read32(ifstream &in, int &n);
/**
* convert a mem table to a SSTable file
* @param memTable mem table
* @param fileName file name
*/
static void toSSTable(const SkipList &memTable, const string &fileName,
uint64_t timeStamp);
/**
* convert a mem table to a SSTable file
* @param memTable mem table
* @param fileName file name
*/
static void toSSTable(const SSTableDic &dic, const string &fileName,
uint64_t timeStamp);
/**
* read the timeStamp from a SSTable file
* @param fileName file name
* @return timeStamp of SSTable
*/
static SSTableHeader readHeader(const string &fileName);
/**
* read dictionary from a SSTable file
* @param fileName file name
* @param dic reference to dic
*/
static void readDic(const string &fileName, SSTableDic &dic);
/**
* get value from a SSTable file
* @param fileName file name
* @param key key
* @return value, empty string if not found
*/
static string get(const string &fileName, uint64_t key);
};
#endif // LSM_KV__SSTABLE_H_