-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfilepreindexer.hpp
374 lines (313 loc) · 7.89 KB
/
filepreindexer.hpp
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
using namespace std;
#include <iostream>
#include <vector>
#include <fstream>
#include <string>
#include <string.h>
#include <stdlib.h>
#include <time.h>
const int blocksize=512;
#include "datafile.hpp"
#include "tagfile.hpp"
#include "wayfile.hpp"
#include <map>
class OSMWorldPreindex
{
public:
void skip() {
//report_finished();
header << "SKIP:" << object_count << " data:" << currentobject << endl;
currentobject.clear();
}
void debug_start_char(char s, int state)
{
body << "start:" <<s << "\t" << state << endl;
}
void debug_end_string(const char * start,int size)
{
// char buffer[];
cerr << "got end " << size << endl;
/* const char * p=start;
while (p < (p + size -1))
{
body << "debug:" <<*p;
p++;
}
body << endl;
*/
}
void debug_node_char(char s, int state)
{
body << "node:" <<s << "\t" << state << endl;
}
void debug_end_char(char s, int state)
{
body << "end:" << s << "\t" << state << endl;
}
string currentobject;
void add_char(char s)
{
marker++; // count chars read
if (s != '\r'){
currentobject.push_back(s);
}
//body << type <<":" << s << "\t" << state << endl;
}
void debug_end_line(const string &s)
{
body << "end line:" << s << endl;
}
enum element_type_t{
t_none=0,
t_node,
t_way,
t_relation,
} current_element_type,parent_element_type;
struct tm current_tm;
//string laststring;
long int object_count;
long int current_id;
long int parent_id;
long int current_node;
int current_inode;
long int current_way;
int current_iway;
long int current_rel;
int current_irel;
long int current_uid;
long int current_cs;
long int current_ver;
int current_vis;
time_t current_timestamp;
string current_tag_key;
long int marker; // position in the file
DataFile<long int> node_positions;
DataFile<long int> way_positions;
DataFile<long int> rel_positions;
DataFile<long int> node_ids;
DataFile<long int> way_ids;
DataFile<long int> rel_ids;
int scanlines;
long blockcount;
ofstream header;
ofstream footer;
ofstream body;
ofstream debug;
OSMWorldPreindex (const char * dir, long blockcount) :
blockcount(blockcount),
current_id(0),
current_cs(-1),
current_ver(-1),
current_uid(-1),
current_vis(-1),
current_timestamp(-1),
current_element_type(t_none),
parent_element_type(t_none),
marker(0),
// positions
node_positions(dir,blockcount,"node_positions"),
way_positions(dir,blockcount,"way_positions"),
rel_positions(dir,blockcount,"relation_positions"),
//ids
node_ids(dir,blockcount,"node_ids"),
way_ids(dir,blockcount,"way_ids"),
rel_ids(dir,blockcount,"relation_ids"),
// node_tags("node_tags"), // node tags
object_count(0),
scanlines(0)
{
header.open(string(string(dir) + "header.txt").c_str());
body.open(string(string(dir) + "body.txt").c_str());
debug.open(string(string(dir) + "debug.txt").c_str());
footer.open(string(string(dir) + "footer.txt").c_str());
}
element_type_t get_current_element_type (){
return current_element_type;
}
element_type_t get_parent_element_type (){
return parent_element_type;
}
void set_current_element_type_node (){
set_current_element_type_none ();
current_element_type=t_node;
}
void set_current_element_type_way (){
set_current_element_type_none ();
current_element_type=t_way;
}
void set_current_element_type_rel (){
set_current_element_type_none ();
current_element_type=t_relation;
}
void add_node_position(){
node_positions.push_back(marker);
}
void add_way_position(){
way_positions.push_back(marker);
}
void add_rel_position(){
rel_positions.push_back(marker);
}
bool check_counts_nodes() {
return true;
}
void set_current_id(long int id) {
if (id == 0) {
cerr << "there is no id " << endl;
return ; //
}
// cerr << "Setting current id " << id << endl;
switch (current_element_type )
{
case t_node :
case t_way:
case t_relation:
parent_id=0;
break;
default:
// we only care about the parent type if we have a subtype
switch (parent_element_type )
{
case t_node :
case t_way:
case t_relation:
cerr << "Setting parent id to " << parent_id << endl;
parent_id=current_id;
break;
default:
break;
};
break;
};
current_id=id;
int index=0;
// write to disk
switch (get_current_element_type()) {
case t_node:
node_ids.push_back(id);
index=node_ids.count();
//node_index[id]=index; // save the index for referencing
// cerr << "node id:" << id << "idx: "<< index << endl;
current_node=id;
current_inode=index;
break;
case t_way:
way_ids.push_back(id);
index=way_ids.count();
// way_index[id]=index; // save the index for referencing
current_way=id;
current_iway=index;
break;
case t_relation:
rel_ids.push_back(id);
index=rel_ids.count();
// rel_index[id]=index; // save the index for referencing
current_rel=id;
current_irel=index;
break;
default:
break;
};
}
const char * get_element_type_name(element_type_t t) {
const char * element_names[] = {
"none",
"node",
"way",
"relation",
};
return element_names[t];
}
const char * get_current_element_type_name() {
return get_element_type_name(get_current_element_type());
}
const char * get_parent_element_type_name() {
return get_element_type_name(get_parent_element_type());
}
void record_start_position() {
switch (get_current_element_type()) {
case t_none:
// cout << "This should never happen none" << endl;
break;
case t_node:
add_node_position();
break;
case t_way:
add_way_position();
break;
case t_relation:
add_rel_position();
break;
default:
// cout << "This should never happen other:" << get_current_element_type() << endl;
break;
};
}
void set_current_element_type_none (){
switch (current_element_type )
{
case t_node :
case t_way:
case t_relation:
parent_element_type=current_element_type;
break;
default:
break;
};
if (get_current_element_type()!=t_none) {
current_element_type=t_none;
}
object_count++;
}
bool debug_lines() {
return false;
//return true;
}
void report_finished() {
if (object_count <= 0) {
header << "count:" << object_count << " data:" << currentobject << endl;
} else {
body << currentobject << endl;
}
currentobject.clear();
}
//
int scannerstatus(int stat, const char * buffer)
{
// write the footer
footer << currentobject << endl;
currentobject.clear();
if (scanlines++ % 10000 ==0)
{
cerr << "L"<< scanlines << endl;
}
// if (debug_lines())
{
debug << "stat:"<< stat
<< "buffer \"" << buffer
<< "\""<< endl;
}
// reset the data
set_current_element_type_none ();
// check default values
if (!check_counts_nodes())
{
cerr << "error" << endl;
return -1;
}
// laststring =buffer;
return 0;
}
void finish_current_object()
{
report_finished();
if (debug_lines()) {
cerr << "finish current object" << endl;
}
// reset the data
set_current_element_type_none ();
}
void set_action_modify (){}
void set_action_delete (){}
void set_action_create (){}
};