-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHotcount.hpp
235 lines (184 loc) · 5.57 KB
/
Hotcount.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#ifndef _HOTCOUNT_HPP_
#define _HOTCOUNT_HPP_
#include "BOBHash32.h"
#include <unordered_map>
#include <string.h>
#define CONSTANT_NUMBER 2654435761u
#define CalculateBucketPos(fp) (((fp) * CONSTANT_NUMBER) >> 15)
#define GetCounterVal(val) ((uint32_t)((val) & 0x7FFFFFFF))
#define KEY_LENGTH_4 4
#define KEY_LENGTH_13 13
#define n 400
#define d 4
#define w 352
#define z 16
using namespace std;
struct Coldbucket {
uint8_t val;
} ;
struct Coldpart
{
Coldbucket coldbuckets[n][d][w];
};
struct Hotbucket
{
uint32_t key[z];
uint32_t val[z];
};
struct Hotcount
{
Coldpart coldpart;
alignas(64) Hotbucket hotbuckets[n];
BOBHash32* hash[d] = {NULL};
void Hotcount_update(uint8_t *key,int f);
uint32_t Hotcount_query(uint8_t *key);
void get_heavy_hitters(int threshold, vector<pair<string, int>> & results);
void initialize();
};
Hotcount hotcount;
void Hotcount_destory(Hotcount Hotcount);
int CalculateFP(uint8_t *key, uint32_t &fp)
{
fp = *((uint32_t*)key);
return CalculateBucketPos(fp) % n;
}
void Hotcount::Hotcount_update(uint8_t *key,int f=1)
{
uint32_t *key1;
uint_fast32_t val1;
//hotbucket位置
uint32_t fp;
int pos = CalculateFP(key, fp);
/* find if there has matched bucket */
int matched = -1, empty = -1, min_counter = 0;
uint32_t min_counter_val = GetCounterVal(hotbuckets[pos].val[0]);
for(int i = 0; i <z; i++)
{
if(hotbuckets[pos].key[i] == fp){
matched = i;
break;
}
if(hotbuckets[pos].key[i] == 0 && empty == -1)
empty = i;
if(min_counter_val > GetCounterVal(hotbuckets[pos].val[i])){
min_counter = i;
min_counter_val = GetCounterVal(hotbuckets[pos].val[i]);
}
}
/* if matched */
if(matched != -1){
hotbuckets[pos].val[matched] += f;
}
else{
/* if there has empty bucket */
if(empty != -1){
hotbuckets[pos].key[empty] = fp;
hotbuckets[pos].val[empty] = f;
}
else
{
uint8_t a[d];
uint8_t minnum=255;
for (int i = 0; i < d; i++) {
int index = (hash[i]->run((const char *)key, KEY_LENGTH_4)) % w;
a[i]= coldpart.coldbuckets[pos][i][index].val;
}
uint8_t b,c=0;
for(int i=0;i<d;i++){
if(a[0]==a[i])
b=b+1;
if(b==z)
c=1;
}
if(c==1){
for(int i=0;i<d;i++){
int index = (hash[i]->run((const char *)key, KEY_LENGTH_4)) % w;
coldpart.coldbuckets[pos][i][index].val+=f;
coldpart.coldbuckets[pos][i][index].val = coldpart.coldbuckets[pos][i][index].val < 255 ? coldpart.coldbuckets[pos][i][index].val : 255;
minnum=coldpart.coldbuckets[pos][i][index].val;
}
}
else
{
uint8_t mina=255;
for(int i=0;i<d;i++)
{
mina=min(mina,a[i]);
}
for(int i=0;i<d;i++){
if(a[i]==mina){
int index = (hash[i]->run((const char *)key, KEY_LENGTH_4)) % w;
coldpart.coldbuckets[pos][i][index].val+=f;
coldpart.coldbuckets[pos][i][index].val = coldpart.coldbuckets[pos][i][index].val < 255 ? coldpart.coldbuckets[pos][i][index].val : 255;
minnum = coldpart.coldbuckets[pos][i][index].val;
}
}
}
if(minnum>min_counter_val){
hotbuckets[pos].key[min_counter] = fp;
val1 = min_counter_val;
hotbuckets[pos].val[min_counter] = minnum;
for (int i = 0; i < d; i++) {
int index = (hash[i]->run((const char *)key, KEY_LENGTH_4)) % w;
coldpart.coldbuckets[pos][i][index].val -= minnum;
}
for (int i = 0; i < d; i++) {
int index = (hash[i]->run((const char *)&hotbuckets[pos].key[min_counter], KEY_LENGTH_4)) % w;
coldpart.coldbuckets[pos][i][index].val = val1+coldpart.coldbuckets[pos][i][index].val;
//coldpart.coldbuckets[pos][i][index].val = coldpart.coldbuckets[pos][i][index].val < 255 ? coldpart.coldbuckets[pos][i][index].val : 255;
/*if(coldpart.coldbuckets[pos][i][index].val< val1)
{
coldpart.coldbuckets[pos][i][index].val = val1;
coldpart.coldbuckets[pos][i][index].val = coldpart.coldbuckets[pos][i][index].val < 255 ? coldpart.coldbuckets[pos][i][index].val : 255;
}*/
/*else
{
coldpart.coldbuckets[pos][i][index].val = +val1;
coldpart.coldbuckets[pos][i][index].val = coldpart.coldbuckets[pos][i][index].val < 255 ? coldpart.coldbuckets[pos][i][index].val : 255;
}*/
}
}
}
}
}
uint32_t Hotcount::Hotcount_query(uint8_t *key){
uint32_t fp;
int pos = CalculateFP(key, fp);
for(int i = 0; i < z; i++)
{
if(hotbuckets[pos].key[i] == fp)
return hotbuckets[pos].val[i];
}
uint8_t minnum=255;
for (int i = 0; i < d; i++) {
int index = (hash[i]->run((const char *)key, KEY_LENGTH_4)) % w;
minnum= min(minnum,coldpart.coldbuckets[pos][i][index].val);
}
return minnum;
}
void Hotcount::get_heavy_hitters(int threshold, vector<pair<string, int>> & results)
{
for (int i = 0; i < n; ++i)
for (int j = 0; j < z; ++j)
{
uint32_t key = hotbuckets[i].key[j];
if (hotbuckets[i].val[j] >= threshold) {
results.push_back(make_pair(string((const char*)&key, 4), hotbuckets[i].val[j]));
}
}
}
void Hotcount::initialize()
{
for(int i=0;i<n;i++)
{
for(int j=0;j<z;j++)
{
hotbuckets[i].key[j]=0;
hotbuckets[i].val[j]=0;
}
}
memset(coldpart.coldbuckets,0,n*d*w*sizeof(uint8_t));
for(int i=0;i<d;i++)
hash[i] = new BOBHash32(i + 750);
}
#endif