-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkeyFile.cpp
178 lines (148 loc) · 4.44 KB
/
keyFile.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
/*
* keyFile.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 <iostream>
#include <cassert>
#include "keyFile.hpp"
using namespace std;
namespace fujimap_tool{
static const char* TMPKF = "tmp.kf";
KeyFile::KeyFile() : fns_(TMPKF), num_(0), maxID_(0){
initWorkingFile(fns_.c_str());
}
KeyFile::~KeyFile(){
}
size_t KeyFile::getNum() const{
return num_;
}
int KeyFile::clear(){
num_ = 0;
buffers_.clear();
nextPointers_.clear();
firstPointers_.clear();
buffers_.resize(maxID_);
nextPointers_.resize(maxID_);
firstPointers_.resize(maxID_);
ofstream ofs(fns_.c_str());
ofs.write((const char*)(&maxID_), sizeof(uint64_t));
if (!ofs){
return -1;
}
return 0;
}
int KeyFile::initWorkingFile(const char* fn){
fns_ = fn;
ofstream ofs(fns_.c_str());
ofs.write((const char*)(&maxID_), sizeof(uint64_t));
if (!ofs){
return -1;
}
return 0;
}
void KeyFile::initMaxID(const uint64_t maxID__){
maxID_ = maxID__;
buffers_.resize(maxID_);
nextPointers_.resize(maxID_);
firstPointers_.resize(maxID_);
}
int KeyFile::write(const uint64_t id, const char* kbuf, const size_t klen,
const uint64_t value){
assert(id < maxID_);
vector<pair<string, uint64_t> >& v(buffers_[id]);
string s;
s.assign(kbuf, klen);
v.push_back(make_pair(s, value));
if (v.size() >= BLOCKSIZE){
fstream ofs(fns_.c_str(), std::ios::in | std::ios::out);
ofs.seekp(0, ios::end);
uint64_t curPos = ofs.tellp();
if (firstPointers_[id] == 0){
firstPointers_[id] = curPos;
} else {
ofs.seekp(nextPointers_[id], ios::beg);
ofs.write((const char*)(&curPos), sizeof(uint64_t));
if (!ofs){
return -1;
}
ofs.seekp(curPos, ios::beg);
if (!ofs){
return -1;
}
}
uint64_t vnum_ = static_cast<uint64_t>(v.size());
ofs.write((const char*)(&vnum_), sizeof(uint64_t));
for (size_t i = 0; i < v.size(); ++i){
uint64_t klen = v[i].first.size();
ofs.write((const char*)(&klen), sizeof(uint64_t));
ofs.write((const char*)(v[i].first.c_str()), klen);
ofs.write((const char*)(&v[i].second), sizeof(uint64_t));
}
nextPointers_[id] = static_cast<uint64_t>(ofs.tellp());
uint64_t dummy = 0;
ofs.write((const char*)(&dummy), sizeof(uint64_t));
if (!ofs){
return -1;
}
vector<pair<string, uint64_t> >().swap(v);
}
++num_;
return 0;
}
int KeyFile::read(const uint64_t id, vector<pair<std::string, uint64_t> >& kvs){
uint64_t readPos = firstPointers_[id];
ifstream ifs(fns_.c_str());
size_t blockNum_ = 0;
while (readPos != 0){
blockNum_++;
ifs.seekg(readPos, ios::beg);
uint64_t vnum_ = 0;
ifs.read((char*)(&vnum_), sizeof(uint64_t));
if (!ifs) return -1;
for (uint64_t i = 0; i < vnum_; ++i){
uint64_t klen = 0;
ifs.read((char*)(&klen), sizeof(uint64_t));
if (!ifs) return -1;
std::string s;
s.resize(klen);
ifs.read((char*)(&s[0]), klen);
if (!ifs) return -1;
uint64_t value = 0;
ifs.read((char*)(&value), sizeof(uint64_t));
if (!ifs) return -1;
kvs.push_back(make_pair(s, value));
}
ifs.read((char*)(&readPos), sizeof(uint64_t));
if (!ifs){
cerr << "read error" << endl;
return -1;
}
if (!ifs) return -1;
}
for (size_t i = 0; i < buffers_[id].size(); ++i){
kvs.push_back(buffers_[id][i]);
}
return 0;
}
}