-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathkmvcontainer.h
232 lines (195 loc) · 6.33 KB
/
kmvcontainer.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
//
// (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_KMV_CONTAINER_H
#define MIMIR_KMV_CONTAINER_H
#include "config.h"
#include "hashbucket.h"
#include "container.h"
#include "interface.h"
#include "serializer.h"
namespace MIMIR_NS {
template <typename KeyType, typename ValType>
class KMVItem;
template <typename KeyType, typename ValType>
class KMVContainer
{
public:
KMVContainer(int keycount, int valcount, int hashscale)
{
this->keycount = keycount;
this->valcount = valcount;
kmvcount = 0;
keyarray = (char *) mem_aligned_malloc(MEMPAGE_SIZE, MAX_RECORD_SIZE);
h = new HashBucket<ReducerVal>(hashscale, true);
ser = new Serializer<KeyType, ValType>(keycount, valcount);
mem_bytes = 0;
}
virtual ~KMVContainer()
{
delete ser;
delete h;
mem_aligned_free(keyarray);
PROFILER_RECORD_COUNT(COUNTER_MAX_KMV_PAGES, this->mem_bytes, OPMAX);
}
virtual int open()
{
kmv = new KMVItem<KeyType, ValType>(keycount, valcount);
h->open();
return 0;
}
virtual void close()
{
delete kmv;
h->close();
}
virtual KMVItem<KeyType, ValType> *read()
{
HashBucket<ReducerVal>::HashEntry *entry = h->next();
if (entry != NULL) {
kmv->set_entry(entry);
return kmv;
}
return NULL;
}
virtual uint64_t get_record_count() { return kmvcount; }
void convert(Readable<KeyType, ValType> *kv)
{
int valbytes = 0;
typename SafeType<KeyType>::type key[keycount];
typename SafeType<ValType>::type val[valcount];
ReducerVal *rdc_val = NULL;
LOG_PRINT(DBG_GEN, "MapReduce: convert start.\n");
kv->open();
while ((kv->read(key, val)) == true) {
keybytes = ser->key_to_bytes(key, keyarray, MAX_RECORD_SIZE);
if ((rdc_val = h->findEntry(keyarray, keybytes)) == NULL) {
valbytes = ser->get_val_bytes(val);
ReducerVal tmpval;
tmpval.valbytes = valbytes;
h->insertEntry(keyarray, keybytes, &tmpval);
}
else {
rdc_val->valbytes += valbytes;
}
}
kv->close();
std::vector<HashBucket<ReducerVal>::HashEntry *> entries;
h->open();
HashBucket<ReducerVal>::HashEntry *entry = NULL;
int64_t totalsize = 0;
while ((entry = h->next()) != NULL) {
totalsize += entry->val.valbytes;
entries.push_back(entry);
if (totalsize >= DATA_PAGE_SIZE) {
char *buffer
= (char *) mem_aligned_malloc(MEMPAGE_SIZE, totalsize);
mem_bytes += totalsize;
buffers.push_back(buffer);
int64_t bufoff = 0;
for (size_t i = 0; i < entries.size(); i++) {
entries[i]->val.values_start = buffer + bufoff;
entries[i]->val.values_end = buffer + bufoff;
bufoff += entries[i]->val.valbytes;
}
entries.clear();
totalsize = 0;
}
}
h->close();
char *buffer = (char *) mem_aligned_malloc(MEMPAGE_SIZE, totalsize);
mem_bytes += totalsize;
buffers.push_back(buffer);
int64_t bufoff = 0;
for (size_t i = 0; i < entries.size(); i++) {
entries[i]->val.values_start = buffer + bufoff;
entries[i]->val.values_end = buffer + bufoff;
bufoff += entries[i]->val.valbytes;
}
kv->open();
while ((kv->read(key, val)) == true) {
keybytes = ser->key_to_bytes(key, keyarray, MAX_RECORD_SIZE);
if ((rdc_val = h->findEntry(keyarray, keybytes)) != NULL) {
int vsize = ser->val_to_bytes(val, rdc_val->values_end,
MAX_RECORD_SIZE);
rdc_val->values_end += vsize;
}
else {
LOG_ERROR("Cannot find key=%s in convert!\n", keyarray);
}
}
kv->close();
PROFILER_RECORD_COUNT(COUNTER_MAX_KMVS, (uint64_t)(h->get_nunique()),
OPMAX);
LOG_PRINT(DBG_GEN, "MapReduce: convert end (KMVs=%ld).\n",
h->get_nunique());
}
private:
HashBucket<ReducerVal> *h;
uint64_t kmvcount;
int keycount, valcount;
char *keyarray;
int keybytes;
std::vector<char *> buffers;
KMVItem<KeyType, ValType> *kmv;
Serializer<KeyType, ValType> *ser;
uint64_t mem_bytes;
};
template <typename KeyType, typename ValType>
class KMVItem : public Readable<KeyType, ValType>
{
public:
KMVItem(int keycount, int valcount)
{
this->keycount = keycount;
this->valcount = valcount;
this->entry = NULL;
this->valptr = NULL;
ser = new Serializer<KeyType, ValType>(keycount, valcount);
}
~KMVItem() { delete ser; }
void set_entry(HashBucket<ReducerVal>::HashEntry *entry)
{
this->entry = entry;
}
virtual int open()
{
valptr = entry->val.values_start;
return true;
}
virtual void close() { return; }
virtual int seek(DB_POS pos)
{
if (pos == DB_START) {
valptr = entry->val.values_start;
}
else if (pos == DB_END) {
valptr = entry->val.values_end;
}
return true;
}
virtual int read(KeyType *key, ValType *val)
{
if (valptr == entry->val.values_end) {
valptr = entry->val.values_start;
return false;
}
ser->key_from_bytes(key, entry->key, entry->keysize);
int vsize = ser->val_from_bytes(val, valptr,
(int) (entry->val.values_end - valptr));
valptr += vsize;
return true;
}
uint64_t get_record_count() { return 0; }
private:
HashBucket<ReducerVal>::HashEntry *entry;
char *valptr;
int keycount, valcount;
Serializer<KeyType, ValType> *ser;
};
} // namespace MIMIR_NS
#endif