-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.h
75 lines (56 loc) · 1.69 KB
/
index.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
#pragma once
#include <ctime>
#include <map>
#include <vector>
#include <string>
#include <memory>
#include "filter.h"
class IndexNode {
public:
IndexNode(uint64_t offset,
uint64_t length,
std::time_t timestamp,
bool deleted
) : offset_(offset),
length_(length),
timestamp_(timestamp),
deleted_(deleted) {}
[[nodiscard]] uint64_t get_offset() const { return offset_; }
[[nodiscard]] uint64_t get_length() const { return length_; }
[[nodiscard]] std::time_t get_timestamp() const { return timestamp_; }
[[nodiscard]] bool is_deleted() const { return deleted_; }
private:
friend class Index;
uint64_t offset_;
uint64_t length_;
std::time_t timestamp_;
bool deleted_;
};
typedef std::map<uint64_t, std::shared_ptr<IndexNode>> IndexTree; // key -> node info
typedef std::map<uint64_t, std::shared_ptr<IndexTree>, std::greater<>> IndexLevel; // filename -> index tree
class Index {
public:
Index(const std::string &dir);
~Index();
void get(uint64_t key,
int &level,
uint64_t &filename,
uint64_t &offset,
uint64_t &length,
bool &deleted) const;
void put(uint64_t key,
int level,
const std::string &filename,
uint64_t offset,
uint64_t length,
std::time_t timestamp,
bool deleted);
bool find(uint64_t key);
void reset();
void recover(Filter &filter);
IndexLevel &get_level(size_t level) { return levels[level]; }
private:
const std::string &dir_;
std::vector<IndexLevel> levels;
const int maxLevel = 20;
};