-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBucket.h
70 lines (59 loc) · 2.09 KB
/
Bucket.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
#pragma once
#include <iostream>
#include <memory>
#include <atomic>
#include <mutex>
using namespace std;
//------------------------------------------------
// BYTE_MSB[n] returns the position of the most
// significant bit. If no bits are set (n = 0) it
// returns 0. Otherwise the positions are 1 ... 8
// from low to high, so e.g. n = 13 returns 4:
//
// bits: 0 0 0 0 1 1 0 1
// position: 8 7 6 5 [4] 3 2 1
//
static const char BYTE_MSB[] = {
0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4,
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8
};
class Bucket {
public:
Bucket(std::string name, uint32_t start, uint32_t max, uint32_t mf) :
name_(name), start_(start), max_(max), multiplication_factor_(mf) {
Init();
}
void Init();
bool Insert(uint64_t number);
bool Insert(uint64_t number, int req_cnt, int req_size);
bool Clear();
bool Dump();
void SetLogFrequencyWithReqCount(uint64_t req_cnt);
std::string GetName() { return name_; }
private:
std::string name_;
uint32_t start_{0};
uint32_t max_;
uint32_t multiplication_factor_{2};
vector<vector<int>> bucket_;
uint64_t log_frequency_{512};
std::atomic<uint64_t> req_cnt_{0};
std::atomic<uint64_t> req_size_{0};
std::mutex mutex_;
uint32_t max_batch_size_{128};
//unit milliseconds
};