forked from mapbox/tile-count
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mergetool.cpp
215 lines (173 loc) · 4.45 KB
/
mergetool.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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <string>
#include <vector>
#include "header.hpp"
#include "serial.hpp"
#include "merge.hpp"
void submerge(std::vector<std::string> fnames, int out, const char *argv0, int zoom, int cpus, size_t *also_todo, size_t *also_did);
bool quiet = false;
void usage(char **argv) {
fprintf(stderr, "Usage: %s -o merged.count file.count ...\n", argv[0]);
}
void trim(char *s) {
for (; *s != '\0'; s++) {
if (*s == '\n') {
*s = '\0';
break;
}
}
}
void addfiles(std::vector<std::string> &list) {
char s[2000];
while (fgets(s, 2000, stdin)) {
trim(s);
list.push_back(s);
}
}
int main(int argc, char **argv) {
extern int optind;
extern char *optarg;
char *outfile = NULL;
int zoom = 32;
size_t cpus = sysconf(_SC_NPROCESSORS_ONLN);
bool readfiles = false;
int i;
while ((i = getopt(argc, argv, "o:s:qp:F")) != -1) {
switch (i) {
case 's':
zoom = atoi(optarg);
break;
case 'o':
outfile = optarg;
break;
case 'p':
cpus = atoi(optarg);
break;
case 'q':
quiet = true;
break;
case 'F':
readfiles = true;
break;
default:
usage(argv);
exit(EXIT_FAILURE);
}
}
if (zoom < 0 || zoom > 32) {
fprintf(stderr, "%s: zoom (-s) must be in the range 0 to 32, not %d\n", argv[0], zoom);
exit(EXIT_FAILURE);
}
std::vector<std::string> fnames;
for (i = optind; i < argc; i++) {
fnames.push_back(argv[i]);
}
if (readfiles) {
addfiles(fnames);
}
if (fnames.size() == 0) {
usage(argv);
exit(EXIT_FAILURE);
}
int out = open(outfile, O_CREAT | O_TRUNC | O_RDWR, 0777);
if (out < 0) {
perror(outfile);
exit(EXIT_FAILURE);
}
size_t also_todo = 0, also_did = 0;
submerge(fnames, out, argv[0], zoom, cpus, &also_todo, &also_did);
return 0;
}
#define MAX_MERGE 50
void submerge(std::vector<std::string> fnames, int out, const char *argv0, int zoom, int cpus, size_t *also_todo, size_t *also_did) {
std::vector<std::string> todelete;
if (fnames.size() > MAX_MERGE) {
size_t subs = MAX_MERGE;
std::vector<std::string> temps;
std::vector<int> tempfds;
std::vector<std::vector<std::string>> subfnames;
for (size_t i = 0; i < subs; i++) {
char s[2000] = "/tmp/count.XXXXXX";
int fd = mkstemp(s);
if (fd < 0) {
perror("mkstemp");
}
temps.push_back(s);
tempfds.push_back(fd);
subfnames.push_back(std::vector<std::string>());
}
for (size_t i = 0; i < fnames.size(); i++) {
subfnames[i % subs].push_back(fnames[i]);
struct stat st;
if (stat(fnames[i].c_str(), &st) == 0) {
*also_todo += st.st_size / RECORD_BYTES;
}
if ((st.st_size - HEADER_LEN) % RECORD_BYTES != 0) {
fprintf(stderr, "%s: file size not a multiple of record length\n", fnames[i].c_str());
exit(EXIT_FAILURE);
}
}
for (size_t i = 0; i < subs; i++) {
submerge(subfnames[i], tempfds[i], argv0, zoom, cpus, also_todo, also_did);
// submerge will have closed the temp fds
}
fnames = temps;
todelete = temps;
}
size_t nmerges = fnames.size();
struct merge merges[nmerges];
int fds[nmerges];
unsigned char *maps[nmerges];
long long to_sort = 0;
for (size_t i = 0; i < nmerges; i++) {
fds[i] = open(fnames[i].c_str(), O_RDONLY);
if (fds[i] < 0) {
perror(fnames[i].c_str());
exit(EXIT_FAILURE);
}
struct stat st;
if (fstat(fds[i], &st) != 0) {
perror("stat");
exit(EXIT_FAILURE);
}
maps[i] = (unsigned char *) mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fds[i], 0);
if (maps[i] == MAP_FAILED) {
perror(fnames[i].c_str());
exit(EXIT_FAILURE);
}
if (st.st_size < HEADER_LEN || memcmp(maps[i], header_text, HEADER_LEN) != 0) {
fprintf(stderr, "%s:%s: Not a tile-count file\n", argv0, fnames[i].c_str());
exit(EXIT_FAILURE);
}
merges[i].start = HEADER_LEN;
merges[i].end = st.st_size;
merges[i].map = maps[i];
to_sort += st.st_size - HEADER_LEN;
if (close(fds[i]) < 0) {
perror("close");
exit(EXIT_FAILURE);
}
}
if (write(out, header_text, HEADER_LEN) != HEADER_LEN) {
perror("write header");
exit(EXIT_FAILURE);
}
do_merge(merges, nmerges, out, RECORD_BYTES, to_sort / RECORD_BYTES, zoom, quiet, cpus, *also_todo, *also_did);
if (close(out) != 0) {
perror("close");
exit(EXIT_FAILURE);
}
*also_did += to_sort / RECORD_BYTES;
for (size_t i = 0; i < todelete.size(); i++) {
if (unlink(todelete[i].c_str()) < 0) {
perror(todelete[i].c_str());
exit(EXIT_FAILURE);
}
}
}