-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathBlockUtils.hpp
178 lines (132 loc) · 4.21 KB
/
BlockUtils.hpp
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#ifndef BLOCKMETA_H
#define BLOCKMETA_H
#include <stdint.h>
#include <boost/filesystem.hpp>
#include <boost/function.hpp>
#include <cstdio>
#include <limits>
#include <vector>
#include "base/Endian.hpp"
#include "third_party/ulid.hpp"
namespace tsdb {
namespace block {
extern const std::string INDEX_FILE_NAME;
extern const std::string META_FILE_NAME;
class BlockStats {
public:
uint64_t num_samples;
uint64_t num_series;
uint64_t num_chunks;
uint64_t num_tombstones;
uint64_t num_bytes;
BlockStats()
: num_samples(0),
num_series(0),
num_chunks(0),
num_tombstones(0),
num_bytes(0) {}
bool operator==(const BlockStats &b) const {
return (b.num_samples == num_samples) && (b.num_series == num_series) &&
(b.num_chunks == num_chunks) &&
(b.num_tombstones == num_tombstones) && (b.num_bytes == num_bytes);
}
};
class BlockDesc {
public:
ulid::ULID ulid_;
int64_t max_time;
int64_t min_time;
BlockDesc()
: ulid_(0),
max_time(std::numeric_limits<int64_t>::min()),
min_time(std::numeric_limits<int64_t>::max()) {}
BlockDesc(const ulid::ULID &ulid_, int64_t min_time, int64_t max_time)
: ulid_(ulid_), min_time(min_time), max_time(max_time) {}
};
class BlockMetaCompaction {
public:
int level;
std::vector<ulid::ULID> sources;
std::vector<BlockDesc> parents;
bool failed;
bool deletable;
BlockMetaCompaction() : level(0), failed(0), deletable(0) {}
};
class BlockMeta {
public:
ulid::ULID ulid_;
int64_t max_time;
int64_t min_time;
BlockStats stats;
BlockMetaCompaction compaction;
int version;
BlockMeta()
: ulid_(0),
max_time(std::numeric_limits<int64_t>::min()),
min_time(std::numeric_limits<int64_t>::max()),
version(1) {}
BlockMeta(const ulid::ULID &ulid_, int64_t min_time, int64_t max_time)
: ulid_(ulid_), min_time(min_time), max_time(max_time), version(1) {}
};
class BlockMetas {
private:
std::deque<BlockMeta> metas;
public:
BlockMetas() = default;
BlockMetas(const std::deque<BlockMeta> &metas) : metas(metas) {}
BlockMetas(const std::initializer_list<BlockMeta> &metas)
: metas(metas.begin(), metas.end()) {}
void push_back(const BlockMeta &meta) { metas.push_back(meta); }
void pop_back() { metas.pop_back(); }
void clear() { metas.clear(); }
int size() const { return metas.size(); }
void sort_by_min_time() {
std::sort(metas.begin(), metas.end(),
[](const BlockMeta &lhs, const BlockMeta &rhs) {
return lhs.min_time < rhs.min_time;
});
}
BlockMeta &operator[](int i) { return metas[i]; }
BlockMeta operator[](int i) const { return metas[i]; }
const BlockMeta &at(int i) const { return metas[i]; }
const BlockMeta &back() const { return metas.back(); }
const BlockMeta &front() const { return metas.front(); }
};
class DirMeta {
public:
std::string dir;
std::shared_ptr<BlockMeta> meta;
DirMeta() = default;
DirMeta(const std::string &dir, const std::shared_ptr<BlockMeta> &meta)
: dir(dir), meta(meta) {}
};
class DirMetas {
private:
std::deque<DirMeta> metas;
public:
DirMetas() = default;
DirMetas(const std::deque<DirMeta> &metas) : metas(metas) {}
DirMetas(const std::initializer_list<DirMeta> &metas)
: metas(metas.begin(), metas.end()) {}
void push_back(const DirMeta &meta) { metas.push_back(meta); }
void pop_back() { metas.pop_back(); }
void clear() { metas.clear(); }
int size() const { return metas.size(); }
bool empty() const { return metas.empty(); }
void sort_by_min_time() {
std::sort(metas.begin(), metas.end(),
[](const DirMeta &lhs, const DirMeta &rhs) {
return lhs.meta->min_time < rhs.meta->min_time;
});
}
DirMeta &operator[](int i) { return metas[i]; }
DirMeta operator[](int i) const { return metas[i]; }
const DirMeta &at(int i) const { return metas[i]; }
const DirMeta &back() const { return metas.back(); }
const DirMeta &front() const { return metas.front(); }
};
std::pair<BlockMeta, bool> read_block_meta(const std::string &dir);
bool write_block_meta(const std::string &dir, const BlockMeta &meta);
} // namespace block
} // namespace tsdb
#endif