-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfujimap.cpp
408 lines (341 loc) · 10.6 KB
/
fujimap.cpp
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/*
* fujimap.cpp
* Copyright (c) 2010 Daisuke Okanohara All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
#include <string.h>
#include <algorithm>
#include <fstream>
#include <cassert>
#include "fujimap.hpp"
#include "smaz.h"
using namespace std;
namespace fujimap_tool{
Fujimap::Fujimap() : isComp(false), companionSize(0), compData(NULL), seed_(0x123456), fpLen_(FPLEN), tmpN_(TMPN),
keyBlockN_(KEYBLOCK), et_(BINARY) {
kf_.initMaxID(keyBlockN_);
}
Fujimap::~Fujimap(){
}
void Fujimap::initSeed(const uint64_t seed){
seed_ = seed;
}
uint64_t Fujimap::getSeed() {
return seed_;
}
void Fujimap::initFP(const uint64_t fpLen){
fpLen_ = fpLen;
}
void Fujimap::initTmpN(const uint64_t tmpN){
tmpN_ = tmpN;
}
void Fujimap::initKeyBlockN(const uint64_t keyBlockN){
keyBlockN_ = keyBlockN;
kf_.initMaxID(keyBlockN_);
}
int Fujimap::initWorkingFile(const char* fn){
if (kf_.initWorkingFile(fn) == -1){
what_ << "initWorkingFile erorr: " << fn;
return -1;
}
return 0;
}
void Fujimap::initEncodeType(const EncodeType et){
et_ = et;
}
void Fujimap::setString(const char* kbuf, const size_t klen,
const char* vbuf, const size_t vlen,
const bool searchable){
setInteger(kbuf, klen, getCode(string(vbuf, vlen)), searchable);
}
void Fujimap::setInteger(const char* kbuf, const size_t klen,
const uint64_t code, const bool searchable){
if (searchable){
tmpEdges_[string(kbuf, klen)] = code;
if (tmpEdges_.size() == tmpN_){
build();
}
} else {
uint64_t id = getBlockID(kbuf, klen);
kf_.write(id, kbuf, klen, code);
}
}
uint64_t Fujimap::getCode(const std::string& value){
map<string, uint64_t>::const_iterator it = val2code_.find(value);
if (it != val2code_.end()){
return it->second;
} else {
uint32_t code = static_cast<uint32_t>(val2code_.size());
val2code_[value] = code;
code2val_.push_back(value);
return code;
}
}
bool kvsEq(const pair<string, uint64_t>& v1,
const pair<string, uint64_t>& v2){
return v1.first == v2.first; // ignore second
}
bool kvsComp(const pair<string, uint64_t>& v1,
const pair<string, uint64_t>& v2){
return v1.first < v2.first; // ignore second
}
uint64_t Fujimap::getBlockID(const char* kbuf, const size_t klen) const{
return hash(kbuf, klen) % keyBlockN_;
}
int Fujimap::build(){
for (map<string, uint64_t>::const_iterator it = tmpEdges_.begin();
it != tmpEdges_.end(); ++it){
uint64_t id = getBlockID(it->first.c_str(), it->first.size());
kf_.write(id, it->first.c_str(), it->first.size(), it->second);
}
tmpEdges_.clear();
vector<FujimapBlock> cur;
for (uint64_t i = 0; i < keyBlockN_; ++i){
vector<pair<string, uint64_t> > kvs;
if (kf_.read(i, kvs) == -1){
what_ << "kf read error" << endl;
return -1;
}
FujimapBlock fb;
if (build_(kvs, fb) == -1){
return -1;
}
cur.push_back(fb);
}
kf_.clear();
fbs_.push_back(cur);
return 0;
}
int Fujimap::build_(vector<pair<string, uint64_t> >& kvs,
FujimapBlock& fb){
reverse(kvs.begin(), kvs.end());
stable_sort(kvs.begin(), kvs.end(), kvsComp);
kvs.erase(unique(kvs.begin(), kvs.end(), kvsEq), kvs.end());
if (kvs.size() == 0){
return 0; // not build
}
for (int i = 0; i < 20; ++i){
vector<KeyEdge> keyEdges;
for (size_t i = 0; i < kvs.size(); ++i){
keyEdges.push_back(KeyEdge(kvs[i].first.c_str(), kvs[i].first.size(),
kvs[i].second, seed_));
}
if (fb.build(keyEdges, seed_, fpLen_, et_) == 0){
break;
}
seed_ += 777;
}
return 0;
}
size_t Fujimap::getKeyNum() const{
size_t keyNum = tmpEdges_.size() + kf_.getNum();
for (size_t i = 0; i < fbs_.size(); ++i){
for (size_t j = 0; j < fbs_[i].size(); ++j){
keyNum += fbs_[i][j].getKeyNum();
}
}
return keyNum;
}
size_t Fujimap::getWorkingSize() const {
size_t workingSize = 0;
for (size_t i = 0; i < fbs_.size(); ++i){
for (size_t j = 0; j < fbs_[i].size(); ++j){
workingSize += fbs_[i][j].getWorkingSize();
}
}
return workingSize;
}
uint64_t Fujimap::getFpLen() const {
return fpLen_;
}
EncodeType Fujimap::getEncodeType() const {
return et_;
}
string Fujimap::getEncodeTypeStr() const {
if (et_ == BINARY){
return string("binary");
} else if (et_ == GAMMA){
return string("gamma");
} else {
return string("undefined");
}
}
const char* Fujimap::getString(const char* kbuf, const size_t klen, size_t& vlen) const {
uint64_t ret = getInteger(kbuf, klen);
if (ret != NOTFOUND && ret < code2val_.size()){
vlen = code2val_[ret].size();
return code2val_[ret].c_str();
} else {
return NULL; // NOTFOUND;
}
}
uint64_t Fujimap::getInteger_(const char* kbuf, const size_t klen) const {
string s(kbuf, klen);
map<string, uint64_t>::const_iterator it = tmpEdges_.find(string(kbuf, klen));
if (it != tmpEdges_.end()){
return it->second;
}
const uint64_t id = getBlockID(kbuf, klen);
for (vector< vector<FujimapBlock> >::const_reverse_iterator it2 = fbs_.rbegin();
it2 != fbs_.rend(); ++it2){
KeyEdge ke(kbuf, klen, 0, (*it2)[id].getSeed());
uint64_t ret = (*it2)[id].getVal(ke);
if (ret != NOTFOUND){
return ret;
}
}
return NOTFOUND;
}
uint64_t Fujimap::getInteger(const char* kbuf, const size_t klen) const {
uint64_t i = getInteger_(kbuf, klen);
if (i == NOTFOUND) return NOTFOUND;
if (!isComp) return i;
i--;
char smaz[5000];
int len = decodeInteger((const unsigned char*)(compData + i));
cout << "decodeing offset " << i << " with length " << len << endl;
int lenSize = intLength(len);
uint64_t offset = i + lenSize;
if (offset < companionSize && len <= (klen*2)) {
size_t out = smaz_decompress((char*)(compData + offset), len, smaz, sizeof(smaz)-100);
smaz[out]=0;
smaz[4999]=0;
cout << "decompressed '" << smaz << "' len: " << out << " original len " << klen << endl;
if (out == klen && strcmp(smaz, kbuf)==0) {
return decodeInteger(compData + len + offset);
}
}
return NOTFOUND;
}
int Fujimap::load(const char* index){
ifstream ifs(index);
if (!ifs){
what_ << "cannot open " << index << endl;
return -1;
}
uint64_t code2valSize = 0;
ifs.read((char*)(&code2valSize), sizeof(code2valSize));
code2val_.resize(code2valSize);
for (uint64_t i = 0; i < code2valSize; ++i){
loadString(code2val_[i], ifs);
}
for (size_t i = 0; i < code2val_.size(); ++i){
val2code_[code2val_[i]] = i;
}
ifs.read((char*)(&seed_), sizeof(seed_));
ifs.read((char*)(&fpLen_), sizeof(fpLen_));
ifs.read((char*)(&tmpN_), sizeof(tmpN_));
uint64_t tmpEdgeSize = 0;
ifs.read((char*)(&tmpEdgeSize), sizeof(tmpEdgeSize));
for (uint64_t i = 0; i < tmpEdgeSize; ++i){
string s;
loadString(s, ifs);
uint32_t code = 0;
ifs.read((char*)(&code), sizeof(uint32_t));
tmpEdges_[s] = code;
}
uint64_t fbs_Size = 0;
ifs.read((char*)(&fbs_Size), sizeof(fbs_Size));
fbs_.resize(fbs_Size);
for (size_t i = 0; i < fbs_.size(); ++i){
uint64_t fbs_InSize = 0;
ifs.read((char*)(&fbs_InSize), sizeof(fbs_InSize));
fbs_[i].resize(fbs_InSize);
for (size_t j = 0; j < fbs_[i].size(); ++j){
fbs_[i][j].load(ifs);
}
}
if (!ifs){
what_ << "read error " << index << endl;
return -1;
}
return 0;
}
int Fujimap::load(const char* index, const char* comp) {
int i = load(index);
isComp = true;
ifstream compis;
compis.open(comp, ios::binary| ios::ate);
if (!compis){
cerr << "Unable to open companion file for read " << comp << endl;
return -1;
}
companionSize = compis.tellg();
cout << "loading companion of " << companionSize << " bytes" << endl;
compData = new unsigned char [companionSize];
compis.seekg (0, ios::beg);
compis.read ((char*)compData, companionSize);
compis.close();
return i;
}
void Fujimap::saveString(const std::string& s, ofstream& ofs) const{
uint64_t len = static_cast<uint64_t>(s.size());
ofs.write((const char*)(&len), sizeof(len));
ofs.write(s.c_str(), len);
}
void Fujimap::loadString(std::string& s, ifstream& ifs) const{
uint64_t len = 0;
ifs.read((char*)(&len), sizeof(len));
s.resize(len);
ifs.read((char*)(&s[0]), len);
}
int Fujimap::save(const char* index){
ofstream ofs(index);
if (!ofs){
what_ << "cannot open " << index << endl;
return -1;
}
uint64_t code2valSize = static_cast<uint64_t>(code2val_.size());
ofs.write((const char*)(&code2valSize), sizeof(uint64_t));
for (uint64_t i = 0; i < code2valSize; ++i){
saveString(code2val_[i], ofs);
}
ofs.write((const char*)(&seed_), sizeof(seed_));
ofs.write((const char*)(&fpLen_), sizeof(fpLen_));
ofs.write((const char*)(&tmpN_), sizeof(tmpN_));
uint64_t tmpEdgeSize = static_cast<uint64_t>(tmpEdges_.size());
ofs.write((const char*)(&tmpEdgeSize), sizeof(tmpEdgeSize));
for (map<string, uint64_t>::const_iterator it = tmpEdges_.begin();
it != tmpEdges_.end(); ++it){
saveString(it->first, ofs);
ofs.write((const char*)(&it->second), sizeof(it->second));
}
uint64_t fbs_Size = static_cast<uint64_t>(fbs_.size());
ofs.write((const char*)(&fbs_Size), sizeof(fbs_Size));
for (size_t i = 0; i < fbs_.size(); ++i){
uint64_t fbs_InSize = fbs_[i].size();
ofs.write((const char*)(&fbs_InSize), sizeof(fbs_InSize));
for (size_t j = 0; j < fbs_[i].size(); ++j){
fbs_[i][j].save(ofs);
}
}
if (!ofs){
what_ << "write error " << index << endl;
return -1;
}
return 0;
}
std::string Fujimap::what() const{
return what_.str();
}
}