mirror of
https://github.com/felt/tippecanoe.git
synced 2026-10-06 02:15:41 +02:00
Start an abstraction layer for input streams
This commit is contained in:
@@ -476,9 +476,53 @@ void do_read_parallel(char *map, long long len, long long initial_offset, const
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct STREAM {
|
||||||
|
FILE *fp;
|
||||||
|
|
||||||
|
int fclose() {
|
||||||
|
int ret = ::fclose(fp);
|
||||||
|
delete this;
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
int peekc() {
|
||||||
|
int c = getc(fp);
|
||||||
|
if (c != EOF) {
|
||||||
|
ungetc(c, fp);
|
||||||
|
}
|
||||||
|
return c;
|
||||||
|
}
|
||||||
|
|
||||||
|
int fread(char *buf, size_t unit, size_t count) {
|
||||||
|
return ::fread(buf, unit, count, fp);
|
||||||
|
}
|
||||||
|
|
||||||
|
json_pull *json_begin() {
|
||||||
|
return json_begin_file(fp);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
STREAM *streamfdopen(int fd, const char *mode) {
|
||||||
|
FILE *fp = fdopen(fd, mode);
|
||||||
|
|
||||||
|
if (fp == NULL) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
STREAM *s = new STREAM;
|
||||||
|
s->fp = fp;
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
|
||||||
|
STREAM *streamfpopen(FILE *fp) {
|
||||||
|
STREAM *s = new STREAM;
|
||||||
|
s->fp = fp;
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
|
||||||
struct read_parallel_arg {
|
struct read_parallel_arg {
|
||||||
int fd = 0;
|
int fd = 0;
|
||||||
FILE *fp = NULL;
|
STREAM *fp = NULL;
|
||||||
long long offset = 0;
|
long long offset = 0;
|
||||||
long long len = 0;
|
long long len = 0;
|
||||||
std::atomic<int> *is_parsing = NULL;
|
std::atomic<int> *is_parsing = NULL;
|
||||||
@@ -532,7 +576,7 @@ void *run_read_parallel(void *v) {
|
|||||||
if (munmap(map, rpa->len) != 0) {
|
if (munmap(map, rpa->len) != 0) {
|
||||||
perror("munmap source file");
|
perror("munmap source file");
|
||||||
}
|
}
|
||||||
if (fclose(rpa->fp) != 0) {
|
if (rpa->fp->fclose() != 0) {
|
||||||
perror("close source file");
|
perror("close source file");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
@@ -543,7 +587,7 @@ void *run_read_parallel(void *v) {
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
void start_parsing(int fd, FILE *fp, long long offset, long long len, std::atomic<int> *is_parsing, pthread_t *parallel_parser, bool &parser_created, const char *reading, std::vector<struct reader> *readers, std::atomic<long long> *progress_seq, std::set<std::string> *exclude, std::set<std::string> *include, int exclude_all, json_object *filter, int basezoom, int source, std::vector<std::map<std::string, layermap_entry> > &layermaps, int *initialized, unsigned *initial_x, unsigned *initial_y, int maxzoom, std::string layername, bool uses_gamma, std::map<std::string, int> const *attribute_types, int separator, double *dist_sum, size_t *dist_count, bool want_dist, bool filters) {
|
void start_parsing(int fd, STREAM *fp, long long offset, long long len, std::atomic<int> *is_parsing, pthread_t *parallel_parser, bool &parser_created, const char *reading, std::vector<struct reader> *readers, std::atomic<long long> *progress_seq, std::set<std::string> *exclude, std::set<std::string> *include, int exclude_all, json_object *filter, int basezoom, int source, std::vector<std::map<std::string, layermap_entry> > &layermaps, int *initialized, unsigned *initial_x, unsigned *initial_y, int maxzoom, std::string layername, bool uses_gamma, std::map<std::string, int> const *attribute_types, int separator, double *dist_sum, size_t *dist_count, bool want_dist, bool filters) {
|
||||||
// This has to kick off an intermediate thread to start the parser threads,
|
// This has to kick off an intermediate thread to start the parser threads,
|
||||||
// so the main thread can get back to reading the next input stage while
|
// so the main thread can get back to reading the next input stage while
|
||||||
// the intermediate thread waits for the completion of the parser threads.
|
// the intermediate thread waits for the completion of the parser threads.
|
||||||
@@ -1394,7 +1438,7 @@ int read_input(std::vector<source> &sources, char *fname, int maxzoom, int minzo
|
|||||||
|
|
||||||
int read_parallel_this = read_parallel ? '\n' : 0;
|
int read_parallel_this = read_parallel ? '\n' : 0;
|
||||||
|
|
||||||
if (1) {
|
if (!(sources[source].file.size() > 3 && sources[source].file.substr(sources[source].file.size() - 3) == std::string(".gz"))) {
|
||||||
if (fstat(fd, &st) == 0) {
|
if (fstat(fd, &st) == 0) {
|
||||||
off = lseek(fd, 0, SEEK_CUR);
|
off = lseek(fd, 0, SEEK_CUR);
|
||||||
if (off >= 0) {
|
if (off >= 0) {
|
||||||
@@ -1439,7 +1483,7 @@ int read_input(std::vector<source> &sources, char *fname, int maxzoom, int minzo
|
|||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
FILE *fp = fdopen(fd, "r");
|
STREAM *fp = streamfdopen(fd, "r");
|
||||||
if (fp == NULL) {
|
if (fp == NULL) {
|
||||||
perror(sources[layer].file.c_str());
|
perror(sources[layer].file.c_str());
|
||||||
if (close(fd) != 0) {
|
if (close(fd) != 0) {
|
||||||
@@ -1449,10 +1493,7 @@ int read_input(std::vector<source> &sources, char *fname, int maxzoom, int minzo
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
int c = getc(fp);
|
int c = fp->peekc();
|
||||||
if (c != EOF) {
|
|
||||||
ungetc(c, fp);
|
|
||||||
}
|
|
||||||
if (c == 0x1E) {
|
if (c == 0x1E) {
|
||||||
read_parallel_this = 0x1E;
|
read_parallel_this = 0x1E;
|
||||||
}
|
}
|
||||||
@@ -1487,7 +1528,7 @@ int read_input(std::vector<source> &sources, char *fname, int maxzoom, int minzo
|
|||||||
char buf[READ_BUF];
|
char buf[READ_BUF];
|
||||||
int n;
|
int n;
|
||||||
|
|
||||||
while ((n = fread(buf, sizeof(char), READ_BUF, fp)) > 0) {
|
while ((n = fp->fread(buf, sizeof(char), READ_BUF)) > 0) {
|
||||||
fwrite_check(buf, sizeof(char), n, readfp, reading.c_str());
|
fwrite_check(buf, sizeof(char), n, readfp, reading.c_str());
|
||||||
ahead += n;
|
ahead += n;
|
||||||
|
|
||||||
@@ -1506,7 +1547,7 @@ int read_input(std::vector<source> &sources, char *fname, int maxzoom, int minzo
|
|||||||
}
|
}
|
||||||
|
|
||||||
fflush(readfp);
|
fflush(readfp);
|
||||||
start_parsing(readfd, readfp, initial_offset, ahead, &is_parsing, ¶llel_parser, parser_created, reading.c_str(), &readers, &progress_seq, exclude, include, exclude_all, filter, basezoom, layer, layermaps, initialized, initial_x, initial_y, maxzoom, sources[layer].layer, gamma != 0, attribute_types, read_parallel_this, &dist_sum, &dist_count, guess_maxzoom, prefilter != NULL || postfilter != NULL);
|
start_parsing(readfd, streamfpopen(readfp), initial_offset, ahead, &is_parsing, ¶llel_parser, parser_created, reading.c_str(), &readers, &progress_seq, exclude, include, exclude_all, filter, basezoom, layer, layermaps, initialized, initial_x, initial_y, maxzoom, sources[layer].layer, gamma != 0, attribute_types, read_parallel_this, &dist_sum, &dist_count, guess_maxzoom, prefilter != NULL || postfilter != NULL);
|
||||||
|
|
||||||
initial_offset += ahead;
|
initial_offset += ahead;
|
||||||
overall_offset += ahead;
|
overall_offset += ahead;
|
||||||
@@ -1543,7 +1584,7 @@ int read_input(std::vector<source> &sources, char *fname, int maxzoom, int minzo
|
|||||||
fflush(readfp);
|
fflush(readfp);
|
||||||
|
|
||||||
if (ahead > 0) {
|
if (ahead > 0) {
|
||||||
start_parsing(readfd, readfp, initial_offset, ahead, &is_parsing, ¶llel_parser, parser_created, reading.c_str(), &readers, &progress_seq, exclude, include, exclude_all, filter, basezoom, layer, layermaps, initialized, initial_x, initial_y, maxzoom, sources[layer].layer, gamma != 0, attribute_types, read_parallel_this, &dist_sum, &dist_count, guess_maxzoom, prefilter != NULL || postfilter != NULL);
|
start_parsing(readfd, streamfpopen(readfp), initial_offset, ahead, &is_parsing, ¶llel_parser, parser_created, reading.c_str(), &readers, &progress_seq, exclude, include, exclude_all, filter, basezoom, layer, layermaps, initialized, initial_x, initial_y, maxzoom, sources[layer].layer, gamma != 0, attribute_types, read_parallel_this, &dist_sum, &dist_count, guess_maxzoom, prefilter != NULL || postfilter != NULL);
|
||||||
|
|
||||||
if (parser_created) {
|
if (parser_created) {
|
||||||
if (pthread_join(parallel_parser, NULL) != 0) {
|
if (pthread_join(parallel_parser, NULL) != 0) {
|
||||||
@@ -1559,7 +1600,7 @@ int read_input(std::vector<source> &sources, char *fname, int maxzoom, int minzo
|
|||||||
// Plain serial reading
|
// Plain serial reading
|
||||||
|
|
||||||
std::atomic<long long> layer_seq(overall_offset);
|
std::atomic<long long> layer_seq(overall_offset);
|
||||||
json_pull *jp = json_begin_file(fp);
|
json_pull *jp = fp->json_begin();
|
||||||
struct serialization_state sst;
|
struct serialization_state sst;
|
||||||
|
|
||||||
sst.fname = reading.c_str();
|
sst.fname = reading.c_str();
|
||||||
@@ -1591,7 +1632,7 @@ int read_input(std::vector<source> &sources, char *fname, int maxzoom, int minzo
|
|||||||
checkdisk(&readers);
|
checkdisk(&readers);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (fclose(fp) != 0) {
|
if (fp->fclose() != 0) {
|
||||||
perror("fclose input");
|
perror("fclose input");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user