-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtblmerge2.cc
326 lines (286 loc) · 6.99 KB
/
tblmerge2.cc
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
/*
* tblmerge2: fast table merge - implementation
* Copyright(c) 2010 EURAC, Institute of Genetic Medicine
*/
/*
* Headers
*/
// local headers
#include "shared.hh"
// system headers
#include <map>
using std::map;
using std::make_pair;
// c headers
#include <stdlib.h>
#include <unistd.h>
/*
* Types and constants
*/
typedef map<string, size_t> col_map;
typedef map<string, size_t> key_map;
typedef vector<size_t> key_col;
/*
* Implementation
*/
void
help(char* argv[])
{
cerr << argv[0] << ": bad parameters:\n"
<< "Usage: " << argv[0] << " [-h] key file1 file2 [file3 ...]\n"
<< "Perform a full join on 'key' of two or more CSV files, performing a\n"
<< "comparison of common columns. 'key' can be a comma-separated list of\n"
<< "column names to form unique indexes. CSV files are TAB separated,\n"
<< "containing column labels on the first row. You can change the column\n"
<< "separator by setting the TBLSEP environment variable.\n"
<< "\n"
<< " -v: increase verbosity\n"
<< " -k: keep going on duplicate rows\n"
<< " -h: help summary\n";
}
string
buildKey(const key_col& kc, const vector<fix_string>& row)
{
key_col::const_iterator it = kc.begin();
string buf(row[*it]);
for(++it; it != kc.end(); ++it)
{
buf += '\0';
buf += row[*it];
}
return buf;
}
string
unescape(const string& str, const char c = ',')
{
string buf;
bool esc = false;
foreach_ro(string, p, str)
{
if(*p == '\\' && !esc)
{
esc = true;
continue;
}
if(*p == c && !esc)
buf += '\0';
else
buf += *p;
esc = false;
}
return buf;
}
string
escape(const string& str, const char c = ',')
{
string buf;
foreach_ro(string, it, str)
{
if(*it == '\0')
buf += c;
else if(*it != c)
buf += *it;
else
{
buf += '\\';
buf += c;
}
}
return buf;
}
void
mergeCharMatrix(fix_string_matrix& dst, col_map& dstCm, key_map& dstKm, const key_col& dstKc,
const fix_string_matrix& add, const col_map& addCm, key_map& addKm, const key_col& addKc,
bool keep_going=false)
{
// preallocate all columns on dst
vector<size_t> addDstCm;
foreach_ro(vector<fix_string>, it, add.front())
{
string cname = *it;
col_map::iterator dIt = dstCm.find(cname);
if(dIt != dstCm.end())
addDstCm.push_back(dIt->second);
else
{
// allocate a new (empty) column
size_t ki = dst.front().size();
addDstCm.push_back(ki);
dstCm.insert(make_pair(cname, ki));
dst.front().push_back(*it);
for(fix_string_matrix::iterator dIt = dst.begin() + 1; dIt != dst.end(); ++dIt)
dIt->push_back(fix_string(NULL, 0));
}
}
// iterate on add rows
dst.reserve(dst.size() + add.size());
for(fix_string_matrix::const_iterator it = add.begin() + 1; it != add.end(); ++it)
{
// key lookup
string key = buildKey(addKc, *it);
key_map::iterator dstKIt = dstKm.find(key);
if(dstKIt == dstKm.end())
{
// new row
dstKIt = dstKm.insert(make_pair(key, dst.size())).first;
dst.push_back(vector<fix_string>(dst.front().size(), fix_string(NULL, 0)));
}
// merge row
vector<fix_string>& dstRow = dst[dstKIt->second];
for(size_t addCol = 0; addCol != it->size(); ++addCol)
{
if(!(*it)[addCol].size()) continue;
size_t dstCol = addDstCm[addCol];
if(!dstRow[dstCol].size())
dstRow[dstCol] = (*it)[addCol];
else
{
if((*it)[addCol] != dstRow[dstCol])
{
string cname = add.front()[addCol];
string error = sprintf2("conflicting contents for column \"%s\", key \"%s\"",
cname.c_str(), escape(key).c_str());
if(keep_going)
cerr << error << std::endl;
else
throw runtime_error(error.c_str());
}
}
}
}
}
int
main(int argc, char* argv[]) try
{
int arg;
int verb = 0;
bool keep_going = false;
while((arg = getopt(argc, argv, "vhk")) != -1)
switch(arg)
{
case 'v':
++verb;
break;
case 'k':
keep_going = true;
break;
case 'h':
help(argv);
return EXIT_SUCCESS;
default:
return EXIT_FAILURE;
}
// check args
argc -= optind;
if(argc < 3)
{
help(argv);
return EXIT_FAILURE;
}
// get default separator
char sep = '\t';
const char *envSep = getenv("TBLSEP");
if(envSep && *envSep)
sep = *envSep;
// key
const char* keyArg(argv[optind++]);
vector<string> keys;
tokenize(keys, unescape(keyArg), string("\0", 1), true);
if(!keys.size())
{
cerr << argv[0] << ": no key specified!\n";
return EXIT_FAILURE;
}
// loading stage
// NOTE: the (mapped) memory is never freed after the merge, due to pointers
// to the mmap-ed region being used for the actual storage. This
// results in a very compact memory layout.
fix_string_matrix* m = NULL;
col_map cm;
key_map km;
key_col kc;
do
{
const char* addr;
const char* file(argv[optind++]);
if(verb > 0) cerr << "loading " << file << "...\n";
fix_string_matrix* tmp(mapFixStringMatrix(&addr, file, sep));
if(!tmp->size() || !tmp->front().size())
{
cerr << file << ": file is empty\n";
return EXIT_FAILURE;
}
// build the column map
col_map ctmp;
for(size_t i = 0; i != tmp->front().size(); ++i)
{
const string cname = tmp->front()[i];
if(!ctmp.insert(make_pair(cname, i)).second)
{
cerr << file << ": duplicated column \"" << cname << "\"\n";
return EXIT_FAILURE;
}
}
key_col kcTmp;
foreach_ro(vector<string>, keyIt, keys)
{
col_map::const_iterator ci = ctmp.find(*keyIt);
if(ci == ctmp.end())
{
cerr << file << ": cannot find key column \"" << *keyIt << "\"\n";
if(!keep_going) return EXIT_FAILURE;
}
kcTmp.push_back(ci->second);
}
// build the key map
key_map ktmp;
for(size_t i = 1; i != tmp->size(); ++i)
{
const string key = buildKey(kcTmp, (*tmp)[i]);
if(!ktmp.insert(make_pair(key, i)).second)
{
cerr << file << ": duplicated key \"" << escape(key) << "\"\n";
if(!keep_going) return EXIT_FAILURE;
}
}
if(m)
{
// table merge on the working copy
if(verb > 0) cerr << "merging " << file << "...\n";
try { mergeCharMatrix(*m, cm, km, kc, *tmp, ctmp, ktmp, kcTmp, keep_going); }
catch(const runtime_error& e)
{ throw runtime_error(sprintf2("%s: %s", file, e.what())); }
}
else
{
// setup the destination table
m = tmp;
cm.swap(ctmp);
km.swap(ktmp);
kc.swap(kcTmp);
}
}
while(argv[optind]);
if(!m)
{
cerr << argv[0] << ": not enough files loaded\n";
return EXIT_FAILURE;
}
// output
foreach_ro(fix_string_matrix, it, *m)
{
vector<fix_string>::const_iterator it2 = it->begin();
if(it2->size()) cout << *it2;
for(++it2; it2 != it->end(); ++it2)
{
cout << sep;
if(it2->size()) cout << *it2;
}
cout << '\n';
}
}
catch(runtime_error& e)
{
cerr << e.what() << std::endl;
return EXIT_FAILURE;
}