-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathkvcontainer.h
311 lines (266 loc) · 8.82 KB
/
kvcontainer.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
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
//
// (c) 2016 by University of Delaware, Argonne National Laboratory, San Diego
// Supercomputer Center, National University of Defense Technology,
// National Supercomputer Center in Guangzhou, and Sun Yat-sen University.
//
// See COPYRIGHT in top-level directory.
//
#ifndef MIMIR_KV_CONTAINER_H
#define MIMIR_KV_CONTAINER_H
#include <stdio.h>
#include <stdlib.h>
#include <set>
#include "container.h"
#include "containeriter.h"
#include "interface.h"
#include "serializer.h"
#include "stat.h"
namespace MIMIR_NS {
template <typename KeyType, typename ValType>
class KVContainer : virtual public BaseDatabase<KeyType, ValType>
{
public:
KVContainer(int keycount, int valcount)
: BaseObject(true), BaseDatabase<KeyType, ValType>()
{
this->keycount = keycount;
this->valcount = valcount;
//this->bincount = bincount;
pageid = 0;
pageoff = 0;
ptr = NULL;
kvsize = 0;
kvcount = 0;
kvmem = 0;
gbmem = 0;
pagesize = DATA_PAGE_SIZE;
min_kvsize = MAX_RECORD_SIZE;
max_kvsize = 0;
ser = new Serializer<KeyType, ValType>(keycount, valcount);
if (std::is_pointer<KeyType>::value
|| std::is_pointer<ValType>::value) {
ispointer = true;
}
else {
ispointer = false;
}
LOG_PRINT(DBG_DATA, "KVContainer create.\n");
}
virtual ~KVContainer()
{
delete ser;
for (size_t i = 0; i < pages.size(); i++) {
mem_aligned_free(pages[i].buffer);
BaseDatabase<KeyType, ValType>::mem_bytes -= pagesize;
}
LOG_PRINT(DBG_DATA, "KVContainer destory.\n");
}
virtual int open()
{
pageid = 0;
pageoff = 0;
ptr = NULL;
kvsize = 0;
LOG_PRINT(DBG_DATA, "KVContainer open.\n");
return true;
}
virtual void close()
{
garbage_collection();
LOG_PRINT(DBG_DATA, "KVContainer close.\n");
return;
}
virtual int seek(DB_POS pos)
{
if (pos == DB_START) {
pageid = 0;
pageoff = 0;
ptr = NULL;
kvsize = 0;
}
else if (pos == DB_END) {
if (pages.size() != 0) {
pageid = pages.size() - 1;
pageoff = pages[pageid].datasize;
}
else {
pageid = 0;
pageoff = 0;
}
ptr = NULL;
kvsize = 0;
}
return true;
}
virtual int read(KeyType *key, ValType *val)
{
while (pageid < pages.size()
&& (int) pageoff >= (int) pages[pageid].datasize) {
pageid++;
pageoff = 0;
}
if (pageid >= pages.size()) {
LOG_PRINT(DBG_DATA, "slice length=%ld\n", slices.size());
return false;
}
ptr = pages[pageid].buffer + pageoff;
kvsize = this->ser->kv_from_bytes(
key, val, ptr, (int) (pages[pageid].datasize - pageoff));
pageoff += kvsize;
return true;
}
virtual int write(KeyType *key, ValType *val)
{
if (pageid >= pages.size()) {
pageid = add_page();
}
ptr = pages[pageid].buffer + pages[pageid].datasize;
kvsize = this->ser->kv_to_bytes(
key, val, ptr, (int) (pagesize - pages[pageid].datasize));
if (kvsize == -1) {
pageid = add_page();
ptr = pages[pageid].buffer + pages[pageid].datasize;
kvsize = this->ser->kv_to_bytes(
key, val, ptr, (int) (pagesize - pages[pageid].datasize));
if (kvsize == -1)
LOG_ERROR("Error: KV size (%d) is larger than one page (%ld)\n",
kvsize, pagesize);
}
pages[pageid].datasize += kvsize;
kvcount += 1;
kvmem += kvsize;
if (kvsize > max_kvsize) max_kvsize = kvsize;
if (kvsize < min_kvsize) min_kvsize = kvsize;
return true;
}
virtual int remove()
{
if (ptr == NULL) return false;
slices[ptr] = kvsize;
kvcount -= 1;
kvmem -= kvsize;
gbmem += kvsize;
return true;
}
virtual uint64_t get_record_count() { return kvcount; }
void print(int rank, int size)
{
size_t count = 0;
for (unsigned i = 0; i < slices.bucket_count(); ++i) {
size_t bucket_size = slices.bucket_size(i);
if (bucket_size == 0) {
count++;
}
else {
count += bucket_size;
}
}
fprintf(stdout,
"%d[%d] KVinfo: page count=%ld, kvcount=%ld, kvmem=%ld, "
"gbmem=%ld, memuse=%ld, membytes=%ld,slices=%ld,%ld,%ld\n",
rank, size, pages.size(), kvcount, kvmem, gbmem,
get_mem_usage(), BaseDatabase<KeyType, ValType>::mem_bytes,
slices.size(), slices.bucket_count(), count);
}
protected:
uint64_t add_page()
{
Page page;
page.datasize = 0;
page.buffer = (char *) mem_aligned_malloc(MEMPAGE_SIZE, pagesize);
pages.push_back(page);
BaseDatabase<KeyType, ValType>::mem_bytes += pagesize;
PROFILER_RECORD_COUNT(COUNTER_MAX_KV_PAGES, this->mem_bytes, OPMAX);
return pages.size() - 1;
}
virtual void garbage_collection()
{
typename SafeType<KeyType>::ptrtype key = NULL;
typename SafeType<ValType>::ptrtype val = NULL;
size_t dst_pid = 0, src_pid = 0;
Page *dst_page = NULL, *src_page = NULL;
int64_t dst_off = 0, src_off = 0;
if (!(this->slices.empty())) {
LOG_PRINT(DBG_GEN, "KVContainer garbage collection: slices=%ld\n",
this->slices.size());
if (kvcount == 0) {
for (auto iter : pages) {
mem_aligned_free(iter.buffer);
BaseDatabase<KeyType, ValType>::mem_bytes -= pagesize;
}
pages.clear();
//this->slices.clear();
std::unordered_map<char *, int> empty;
this->slices.swap(empty);
gbmem = 0;
return;
}
if (dst_pid < pages.size()) dst_page = &pages[dst_pid++];
while (src_pid < pages.size()) {
src_page = &pages[src_pid++];
src_off = 0;
while (src_off < src_page->datasize) {
char *src_buf = src_page->buffer + src_off;
std::unordered_map<char *, int>::iterator slice
= this->slices.find(src_buf);
if (slice != this->slices.end()) {
src_off += slice->second;
}
else {
int kvsize = this->ser->kv_from_bytes(
&key, &val, src_buf,
(int) (src_page->datasize - src_off));
if (dst_page != src_page || dst_off != src_off) {
if (dst_off + kvsize > this->pagesize) {
dst_page->datasize = dst_off;
dst_page = &pages[dst_pid++];
dst_off = 0;
}
for (int kk = 0; kk < kvsize; kk++) {
dst_page->buffer[dst_off + kk]
= src_page->buffer[src_off + kk];
}
}
src_off += kvsize;
dst_off += kvsize;
}
}
if (src_page == dst_page && src_off == dst_off) {
dst_page = &pages[dst_pid++];
dst_off = 0;
}
}
if (dst_page != NULL) dst_page->datasize = dst_off;
pageid = dst_pid;
pageoff = dst_off;
while (dst_pid < pages.size()) {
auto iter = pages.back();
mem_aligned_free(iter.buffer);
BaseDatabase<KeyType, ValType>::mem_bytes -= pagesize;
pages.pop_back();
}
//this->slices.clear();
std::unordered_map<char *, int> empty;
this->slices.swap(empty);
gbmem = 0;
}
}
int64_t pagesize;
size_t pageid;
uint64_t pageoff;
std::vector<Page> pages;
char *ptr;
int kvsize;
int keycount, valcount;
uint64_t kvcount;
uint64_t kvmem;
uint64_t gbmem;
//uint32_t bincount;
int max_kvsize;
int min_kvsize;
bool ispointer;
std::unordered_map<char *, int> slices;
Serializer<KeyType, ValType> *ser;
};
} // namespace MIMIR_NS
#endif