-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathIdMap.hpp
More file actions
206 lines (170 loc) · 5.39 KB
/
IdMap.hpp
File metadata and controls
206 lines (170 loc) · 5.39 KB
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
#ifndef IDMAP_HPP
#define IDMAP_HPP
#include <iostream>
#include <fstream>
#include <sstream>
#include <map>
#include <sys/file.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
using namespace std;
#define DEFAULT_CHUNK_SIZE 1024
// Possible chunk status
#define CHUNK_NOT_INIT 0
#define CHUNK_OPENED 1
#define CHUNK_BEING_USED 2
#define CHUNK_FULL 3
struct Chunk {
int id;
int status;
int start;
int end;
Chunk(): id(0), status(CHUNK_NOT_INIT), start(0), end(0) {}
};
struct FileLocker {
int fd;
FileLocker(string file) {
fd = open((file + ".lock").c_str(), O_RDWR|O_CREAT, 0666);
if (flock(fd, LOCK_EX) != 0) cerr << "Error while locking " << file << ".lock fd=" << fd << endl;
}
~FileLocker() {
if (flock(fd, LOCK_UN) != 0) cerr << "Error while unlocking fd=" << fd << endl;
close(fd);
}
};
struct IdMap {
map<string, int> id_map;
map<string, int> new_id_map;
Chunk curchunk;
string fname;
string fchunkspool;
int flockd;
IdMap(string filename) {
const char * val = ::getenv("DIN_MAPS");
string prefix = "./";
if ((val == 0) || (strcmp(val,"") == 0)) {
cerr << "DIN_MAPS not set, falling back to current directory" << endl;
} else {
prefix = val;
prefix += "/";
}
fname = prefix + filename;
fchunkspool = fname + ".chunkspool.json";
readMap();
curchunk = Chunk();
reserveNewChunk();
}
~IdMap() {
recycleCurChunk();
}
void readMap() { // thread safe
FileLocker lock(fname);
readMapFromJson(id_map, fname);
}
void saveMap() { // thread safe
FileLocker lock(fname);
readMapFromJson(id_map, fname);
id_map.insert(new_id_map.begin(), new_id_map.end());
writeMapToJson(id_map, fname);
}
int getId(string src) {
if (id_map.count(src) != 0) return id_map[src];
if (new_id_map.count(src) != 0) return new_id_map[src];
if (curchunk.start > curchunk.end) {
reserveNewChunk();
}
int new_id = curchunk.start++;
new_id_map[src] = new_id;
return new_id;
}
inline string s(int i) {
char buf[33];
sprintf(buf, "%d", i);
return string(buf);
}
inline void putChunk(map<string, int> &pool, Chunk &chk) {
string name = "chunk_" + s(chk.id);
pool[name + "_status"] = chk.status;
pool[name + "_start"] = chk.start;
pool[name + "_end"] = chk.end;
}
inline void getChunk(map<string, int> &pool, int id, Chunk &chk) {
string name = "chunk_" + s(id);
chk.id = id;
chk.status = pool[name + "_status"];
chk.start = pool[name + "_start"];
chk.end = pool[name + "_end"];
}
void reserveNewChunk() { // thread safe
FileLocker lock(fchunkspool);
map<string, int> pool;
readMapFromJson(pool, fchunkspool);
if (pool.count("size_of_each_chunk") == 0) {
pool["size_of_each_chunk"] = DEFAULT_CHUNK_SIZE;
}
if (pool.count("latest_chunk_id") == 0) {
pool["latest_chunk_id"] = -1;
}
// update current chunk in the pool
if (curchunk.status != CHUNK_NOT_INIT) {
curchunk.status = curchunk.start > curchunk.end ? CHUNK_FULL : CHUNK_OPENED;
putChunk(pool, curchunk);
}
// try to find an opened chunk (the chunk that is not fully used)
bool foundOpenChunk = false;
for (int i = 0; i <= pool["latest_chunk_id"]; i++) {
getChunk(pool, i, curchunk);
if (curchunk.status == CHUNK_OPENED) {
curchunk.status = CHUNK_BEING_USED;
putChunk(pool, curchunk);
foundOpenChunk = true;
break;
}
}
if (!foundOpenChunk) {
pool["latest_chunk_id"] += 1;
curchunk.id = pool["latest_chunk_id"];
curchunk.status = CHUNK_BEING_USED;
curchunk.start = curchunk.id * pool["size_of_each_chunk"];
curchunk.end = curchunk.start + pool["size_of_each_chunk"] - 1;
putChunk(pool, curchunk);
}
writeMapToJson(pool, fchunkspool);
}
void recycleCurChunk() { // thread safe
FileLocker lock(fchunkspool);
map<string, int> pool;
readMapFromJson(pool, fchunkspool);
// recycle the chunk if it is not fully used
curchunk.status = curchunk.start > curchunk.end ? CHUNK_FULL : CHUNK_OPENED;
putChunk(pool, curchunk);
writeMapToJson(pool, fchunkspool);
}
bool readMapFromJson(map<string, int> &mp, string jsonfile) {
ifstream mapin;
mapin.open(jsonfile);
if (!mapin.is_open()) {
return false;
}
stringstream ss;
string err;
ss << mapin.rdbuf();
json11::Json jsmap;
jsmap = json11::Json::parse(ss.str(), err);
for (auto entry : jsmap.object_items()) {
mp[entry.first] = entry.second.int_value();
}
return true;
}
bool writeMapToJson(map<string, int> &mp, string jsonfile) {
ofstream mapout;
mapout.open(jsonfile, ofstream::trunc);
json11::Json jsmap(mp);
mapout << jsmap.dump();
mapout.flush();
mapout.close();
return true;
}
};
#endif