forked from osm2pgsql-dev/osm2pgsql
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnode-persistent-cache.c
738 lines (666 loc) · 24.7 KB
/
node-persistent-cache.c
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
#define _LARGEFILE64_SOURCE /* See feature_test_macrors(7) */
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <limits.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <math.h>
#include "osmtypes.h"
#include "output.h"
#include "node-persistent-cache.h"
#include "node-ram-cache.h"
#include "binarysearcharray.h"
#ifdef __APPLE__
#define lseek64 lseek
#else
#ifndef HAVE_LSEEK64
#if SIZEOF_OFF_T == 8
#define lseek64 lseek
#else
#error Flat nodes cache requires a 64 bit capable seek
#endif
#endif
#endif
static int node_cache_fd;
static const char * node_cache_fname;
static int append_mode;
struct persistentCacheHeader cacheHeader;
static struct ramNodeBlock writeNodeBlock; /* larger node block for more efficient initial sequential writing of node cache */
static struct ramNodeBlock * readNodeBlockCache;
static struct binary_search_array * readNodeBlockCacheIdx;
static int scale;
static int cache_already_written = 0;
static void writeout_dirty_nodes(osmid_t id)
{
int i;
if (writeNodeBlock.dirty > 0)
{
if (lseek64(node_cache_fd,
(writeNodeBlock.block_offset << WRITE_NODE_BLOCK_SHIFT)
* sizeof(struct ramNode)
+ sizeof(struct persistentCacheHeader), SEEK_SET) < 0) {
fprintf(stderr, "Failed to seek to correct position in node cache: %s\n",
strerror(errno));
exit_nicely();
};
if (write(node_cache_fd, writeNodeBlock.nodes,
WRITE_NODE_BLOCK_SIZE * sizeof(struct ramNode))
< WRITE_NODE_BLOCK_SIZE * sizeof(struct ramNode))
{
fprintf(stderr, "Failed to write out node cache: %s\n",
strerror(errno));
exit_nicely();
}
cacheHeader.max_initialised_id = ((writeNodeBlock.block_offset + 1)
<< WRITE_NODE_BLOCK_SHIFT) - 1;
writeNodeBlock.used = 0;
writeNodeBlock.dirty = 0;
if (lseek64(node_cache_fd, 0, SEEK_SET) < 0) {
fprintf(stderr, "Failed to seek to correct position in node cache: %s\n",
strerror(errno));
exit_nicely();
};
if (write(node_cache_fd, &cacheHeader,
sizeof(struct persistentCacheHeader))
!= sizeof(struct persistentCacheHeader))
{
fprintf(stderr, "Failed to update persistent cache header: %s\n",
strerror(errno));
exit_nicely();
}
if (fsync(node_cache_fd) < 0) {
fprintf(stderr, "Info: Node cache could not be guaranteeded to be made durable. fsync failed: %s\n",
strerror(errno));
};
}
if (id < 0)
{
for (i = 0; i < READ_NODE_CACHE_SIZE; i++)
{
if (readNodeBlockCache[i].dirty)
{
if (lseek64(node_cache_fd,
(readNodeBlockCache[i].block_offset
<< READ_NODE_BLOCK_SHIFT)
* sizeof(struct ramNode)
+ sizeof(struct persistentCacheHeader),
SEEK_SET) < 0) {
fprintf(stderr, "Failed to seek to correct position in node cache: %s\n",
strerror(errno));
exit_nicely();
};
if (write(node_cache_fd, readNodeBlockCache[i].nodes,
READ_NODE_BLOCK_SIZE * sizeof(struct ramNode))
< READ_NODE_BLOCK_SIZE * sizeof(struct ramNode))
{
fprintf(stderr, "Failed to write out node cache: %s\n",
strerror(errno));
exit_nicely();
}
}
readNodeBlockCache[i].dirty = 0;
}
}
}
static void ramNodes_clear(struct ramNode * nodes, int size)
{
int i;
for (i = 0; i < size; i++)
{
#ifdef FIXED_POINT
nodes[i].lon = INT_MIN;
nodes[i].lat = INT_MIN;
#else
nodes[i].lon = NAN;
nodes[i].lat = NAN;
#endif
}
}
/**
* Find the cache block with the lowest usage count for replacement
*/
static int persistent_cache_replace_block()
{
int min_used = INT_MAX;
int block_id = -1;
int i;
for (i = 0; i < READ_NODE_CACHE_SIZE; i++)
{
if (readNodeBlockCache[i].used < min_used)
{
min_used = readNodeBlockCache[i].used;
block_id = i;
}
}
if (min_used > 0)
{
for (i = 0; i < READ_NODE_CACHE_SIZE; i++)
{
if (readNodeBlockCache[i].used > 1)
{
readNodeBlockCache[i].used--;
}
}
}
return block_id;
}
/**
* Find cache block number by block_offset
*/
static int persistent_cache_find_block(osmid_t block_offset)
{
int idx = binary_search_get(readNodeBlockCacheIdx, block_offset);
return idx;
}
/**
* Initialise the persistent cache with NaN values to identify which IDs are valid or not
*/
static void persistent_cache_expand_cache(osmid_t block_offset)
{
osmid_t i;
struct ramNode * dummyNodes = malloc(
READ_NODE_BLOCK_SIZE * sizeof(struct ramNode));
if (!dummyNodes) {
fprintf(stderr, "Out of memory: Could not allocate node structure during cache expansion\n");
exit_nicely();
}
ramNodes_clear(dummyNodes, READ_NODE_BLOCK_SIZE);
/* Need to expand the persistent node cache */
if (lseek64(node_cache_fd,
cacheHeader.max_initialised_id * sizeof(struct ramNode)
+ sizeof(struct persistentCacheHeader), SEEK_SET) < 0) {
fprintf(stderr, "Failed to seek to correct position in node cache: %s\n",
strerror(errno));
exit_nicely();
};
for (i = cacheHeader.max_initialised_id >> READ_NODE_BLOCK_SHIFT;
i <= block_offset; i++)
{
if (write(node_cache_fd, dummyNodes,
READ_NODE_BLOCK_SIZE * sizeof(struct ramNode))
< READ_NODE_BLOCK_SIZE * sizeof(struct ramNode))
{
fprintf(stderr, "Failed to expand persistent node cache: %s\n",
strerror(errno));
exit_nicely();
}
}
cacheHeader.max_initialised_id = ((block_offset + 1)
<< READ_NODE_BLOCK_SHIFT) - 1;
if (lseek64(node_cache_fd, 0, SEEK_SET) < 0) {
fprintf(stderr, "Failed to seek to correct position in node cache: %s\n",
strerror(errno));
exit_nicely();
};
if (write(node_cache_fd, &cacheHeader, sizeof(struct persistentCacheHeader))
!= sizeof(struct persistentCacheHeader))
{
fprintf(stderr, "Failed to update persistent cache header: %s\n",
strerror(errno));
exit_nicely();
}
free(dummyNodes);
fsync(node_cache_fd);
}
static void persistent_cache_nodes_prefetch_async(osmid_t id)
{
#ifdef HAVE_POSIX_FADVISE
osmid_t block_offset = id >> READ_NODE_BLOCK_SHIFT;
osmid_t block_id = persistent_cache_find_block(block_offset);
if (block_id < 0)
{ /* The needed block isn't in cache already, so initiate loading */
writeout_dirty_nodes(id);
/* Make sure the node cache is correctly initialised for the block that will be read */
if (cacheHeader.max_initialised_id
< ((block_offset + 1) << READ_NODE_BLOCK_SHIFT))
persistent_cache_expand_cache(block_offset);
if (posix_fadvise(node_cache_fd, (block_offset << READ_NODE_BLOCK_SHIFT) * sizeof(struct ramNode)
+ sizeof(struct persistentCacheHeader), READ_NODE_BLOCK_SIZE * sizeof(struct ramNode),
POSIX_FADV_WILLNEED | POSIX_FADV_RANDOM) != 0) {
fprintf(stderr, "Info: async prefetch of node cache failed. This might reduce performance\n");
};
}
#endif
}
/**
* Load block offset in a synchronous way.
*/
static int persistent_cache_load_block(osmid_t block_offset)
{
int block_id = persistent_cache_replace_block();
if (readNodeBlockCache[block_id].dirty)
{
if (lseek64(node_cache_fd,
(readNodeBlockCache[block_id].block_offset
<< READ_NODE_BLOCK_SHIFT) * sizeof(struct ramNode)
+ sizeof(struct persistentCacheHeader), SEEK_SET) < 0) {
fprintf(stderr, "Failed to seek to correct position in node cache: %s\n",
strerror(errno));
exit_nicely();
};
if (write(node_cache_fd, readNodeBlockCache[block_id].nodes,
READ_NODE_BLOCK_SIZE * sizeof(struct ramNode))
< READ_NODE_BLOCK_SIZE * sizeof(struct ramNode))
{
fprintf(stderr, "Failed to write out node cache: %s\n",
strerror(errno));
exit_nicely();
}
readNodeBlockCache[block_id].dirty = 0;
}
binary_search_remove(readNodeBlockCacheIdx,
readNodeBlockCache[block_id].block_offset);
ramNodes_clear(readNodeBlockCache[block_id].nodes, READ_NODE_BLOCK_SIZE);
readNodeBlockCache[block_id].block_offset = block_offset;
readNodeBlockCache[block_id].used = READ_NODE_CACHE_SIZE;
/* Make sure the node cache is correctly initialised for the block that will be read */
if (cacheHeader.max_initialised_id
< ((block_offset + 1) << READ_NODE_BLOCK_SHIFT))
{
persistent_cache_expand_cache(block_offset);
}
/* Read the block into cache */
if (lseek64(node_cache_fd,
(block_offset << READ_NODE_BLOCK_SHIFT) * sizeof(struct ramNode)
+ sizeof(struct persistentCacheHeader), SEEK_SET) < 0) {
fprintf(stderr, "Failed to seek to correct position in node cache: %s\n",
strerror(errno));
exit_nicely();
};
if (read(node_cache_fd, readNodeBlockCache[block_id].nodes,
READ_NODE_BLOCK_SIZE * sizeof(struct ramNode))
!= READ_NODE_BLOCK_SIZE * sizeof(struct ramNode))
{
fprintf(stderr, "Failed to read from node cache: %s\n",
strerror(errno));
exit(1);
}
binary_search_add(readNodeBlockCacheIdx,
readNodeBlockCache[block_id].block_offset, block_id);
return block_id;
}
static void persisten_cache_nodes_set_create_writeout_block()
{
if (write(node_cache_fd, writeNodeBlock.nodes,
WRITE_NODE_BLOCK_SIZE * sizeof(struct ramNode))
< WRITE_NODE_BLOCK_SIZE * sizeof(struct ramNode))
{
fprintf(stderr, "Failed to write out node cache: %s\n",
strerror(errno));
exit_nicely();
}
#ifdef HAVE_SYNC_FILE_RANGE
/* writing out large files can cause trouble on some operating systems.
* For one, if to much dirty data is in RAM, the whole OS can stall until
* enough dirty data is written out which can take a while. It can also interfere
* with outher disk caching operations and might push things out to swap. By forcing the OS to
* immediately write out the data and blocking after a while, we ensure that no more
* than a couple of 10s of MB are dirty in RAM at a time.
* Secondly, the nodes are stored in an additional ram cache during import. Keeping the
* node cache file in buffer cache therefore duplicates the data wasting 16GB of ram.
* Therefore tell the OS not to cache the node-persistent-cache during initial import.
* */
if (sync_file_range(node_cache_fd, writeNodeBlock.block_offset*WRITE_NODE_BLOCK_SIZE * sizeof(struct ramNode) +
sizeof(struct persistentCacheHeader), WRITE_NODE_BLOCK_SIZE * sizeof(struct ramNode),
SYNC_FILE_RANGE_WRITE) < 0) {
fprintf(stderr, "Info: Sync_file_range writeout has an issue. This shouldn't be anything to worry about.: %s\n",
strerror(errno));
};
if (writeNodeBlock.block_offset > 16) {
if(sync_file_range(node_cache_fd, (writeNodeBlock.block_offset - 16)*WRITE_NODE_BLOCK_SIZE * sizeof(struct ramNode) +
sizeof(struct persistentCacheHeader), WRITE_NODE_BLOCK_SIZE * sizeof(struct ramNode),
SYNC_FILE_RANGE_WAIT_BEFORE | SYNC_FILE_RANGE_WRITE | SYNC_FILE_RANGE_WAIT_AFTER) < 0) {
fprintf(stderr, "Info: Sync_file_range block has an issue. This shouldn't be anything to worry about.: %s\n",
strerror(errno));
}
#ifdef HAVE_POSIX_FADVISE
if (posix_fadvise(node_cache_fd, (writeNodeBlock.block_offset - 16)*WRITE_NODE_BLOCK_SIZE * sizeof(struct ramNode) +
sizeof(struct persistentCacheHeader), WRITE_NODE_BLOCK_SIZE * sizeof(struct ramNode), POSIX_FADV_DONTNEED) !=0 ) {
fprintf(stderr, "Info: Posix_fadvise failed. This shouldn't be anything to worry about.: %s\n",
strerror(errno));
};
#endif
}
#endif
}
static int persistent_cache_nodes_set_create(osmid_t id, double lat, double lon)
{
osmid_t block_offset = id >> WRITE_NODE_BLOCK_SHIFT;
int i;
if (cache_already_written)
return 0;
if (writeNodeBlock.block_offset != block_offset)
{
if (writeNodeBlock.dirty)
{
persisten_cache_nodes_set_create_writeout_block();
writeNodeBlock.used = 0;
writeNodeBlock.dirty = 0;
/* After writing out the node block, the file pointer is at the next block level */
writeNodeBlock.block_offset++;
cacheHeader.max_initialised_id = (writeNodeBlock.block_offset
<< WRITE_NODE_BLOCK_SHIFT) - 1;
}
if (writeNodeBlock.block_offset > block_offset)
{
fprintf(stderr,
"ERROR: Block_offset not in sequential order: %" PRIdOSMID "%" PRIdOSMID "\n",
writeNodeBlock.block_offset, block_offset);
exit_nicely();
}
/* We need to fill the intermediate node cache with node nodes to identify which nodes are valid */
for (i = writeNodeBlock.block_offset; i < block_offset; i++)
{
ramNodes_clear(writeNodeBlock.nodes, WRITE_NODE_BLOCK_SIZE);
persisten_cache_nodes_set_create_writeout_block();
}
ramNodes_clear(writeNodeBlock.nodes, WRITE_NODE_BLOCK_SIZE);
writeNodeBlock.used = 0;
writeNodeBlock.block_offset = block_offset;
}
#ifdef FIXED_POINT
writeNodeBlock.nodes[id & WRITE_NODE_BLOCK_MASK].lat = DOUBLE_TO_FIX(lat);
writeNodeBlock.nodes[id & WRITE_NODE_BLOCK_MASK].lon = DOUBLE_TO_FIX(lon);
#else
writeNodeBlock.nodes[id & WRITE_NODE_BLOCK_MASK].lat = lat;
writeNodeBlock.nodes[id & WRITE_NODE_BLOCK_MASK].lon = lon;
#endif
writeNodeBlock.used++;
writeNodeBlock.dirty = 1;
return 0;
}
static int persistent_cache_nodes_set_append(osmid_t id, double lat, double lon)
{
osmid_t block_offset = id >> READ_NODE_BLOCK_SHIFT;
int block_id = persistent_cache_find_block(block_offset);
if (block_id < 0)
block_id = persistent_cache_load_block(block_offset);
#ifdef FIXED_POINT
if (isnan(lat) && isnan(lon))
{
readNodeBlockCache[block_id].nodes[id & READ_NODE_BLOCK_MASK].lat =
INT_MIN;
readNodeBlockCache[block_id].nodes[id & READ_NODE_BLOCK_MASK].lon =
INT_MIN;
}
else
{
readNodeBlockCache[block_id].nodes[id & READ_NODE_BLOCK_MASK].lat =
DOUBLE_TO_FIX(lat);
readNodeBlockCache[block_id].nodes[id & READ_NODE_BLOCK_MASK].lon =
DOUBLE_TO_FIX(lon);
}
#else
readNodeBlockCache[block_id].nodes[id & READ_NODE_BLOCK_MASK].lat = lat;
readNodeBlockCache[block_id].nodes[id & READ_NODE_BLOCK_MASK].lon = lon;
#endif
readNodeBlockCache[block_id].used++;
readNodeBlockCache[block_id].dirty = 1;
return 1;
}
int persistent_cache_nodes_set(osmid_t id, double lat, double lon)
{
return append_mode ?
persistent_cache_nodes_set_append(id, lat, lon) :
persistent_cache_nodes_set_create(id, lat, lon);
}
int persistent_cache_nodes_get(struct osmNode *out, osmid_t id)
{
osmid_t block_offset = id >> READ_NODE_BLOCK_SHIFT;
osmid_t block_id = persistent_cache_find_block(block_offset);
if (block_id < 0)
{
writeout_dirty_nodes(id);
block_id = persistent_cache_load_block(block_offset);
}
readNodeBlockCache[block_id].used++;
#ifdef FIXED_POINT
if ((readNodeBlockCache[block_id].nodes[id & READ_NODE_BLOCK_MASK].lat
== INT_MIN)
&& (readNodeBlockCache[block_id].nodes[id & READ_NODE_BLOCK_MASK].lon
== INT_MIN))
{
return 1;
}
else
{
out->lat =
FIX_TO_DOUBLE(readNodeBlockCache[block_id].nodes[id & READ_NODE_BLOCK_MASK].lat);
out->lon =
FIX_TO_DOUBLE(readNodeBlockCache[block_id].nodes[id & READ_NODE_BLOCK_MASK].lon);
return 0;
}
#else
if ((isnan(readNodeBlockCache[block_id].nodes[id & READ_NODE_BLOCK_MASK].lat)) &&
(isnan(readNodeBlockCache[block_id].nodes[id & READ_NODE_BLOCK_MASK].lon)))
{
return 1;
}
else
{
out->lat = readNodeBlockCache[block_id].nodes[id & READ_NODE_BLOCK_MASK].lat;
out->lon = readNodeBlockCache[block_id].nodes[id & READ_NODE_BLOCK_MASK].lon;
return 0;
}
#endif
return 0;
}
int persistent_cache_nodes_get_list(struct osmNode *nodes, osmid_t *ndids,
int nd_count)
{
int count = 0;
int i;
for (i = 0; i < nd_count; i++)
{
/* Check cache first */
if (ram_cache_nodes_get(&nodes[i], ndids[i]) == 0)
{
count++;
}
else
{
nodes[i].lat = NAN;
nodes[i].lon = NAN;
}
}
if (count == nd_count)
return count;
for (i = 0; i < nd_count; i++)
{
/* In order to have a higher OS level I/O queue depth
issue posix_fadvise(WILLNEED) requests for all I/O */
if (isnan(nodes[i].lat) && isnan(nodes[i].lon))
persistent_cache_nodes_prefetch_async(ndids[i]);
}
for (i = 0; i < nd_count; i++)
{
if ((isnan(nodes[i].lat) && isnan(nodes[i].lon))
&& (persistent_cache_nodes_get(&(nodes[i]), ndids[i]) == 0))
count++;
}
if (count < nd_count)
{
int j = 0;
for (i = 0; i < nd_count; i++)
{
if (!isnan(nodes[i].lat))
{
nodes[j].lat = nodes[i].lat;
nodes[j].lon = nodes[i].lon;
j++;
}
}
for (i = count; i < nd_count; i++)
{
nodes[i].lat = NAN;
nodes[i].lon = NAN;
}
}
return count;
}
void init_node_persistent_cache(const struct output_options *options, int append)
{
int i, err;
scale = options->scale;
append_mode = append;
node_cache_fname = options->flat_node_file;
fprintf(stderr, "Mid: loading persistent node cache from %s\n",
node_cache_fname);
readNodeBlockCacheIdx = init_search_array(READ_NODE_CACHE_SIZE);
/* Setup the file for the node position cache */
if (append_mode)
{
node_cache_fd = open(node_cache_fname, O_RDWR, S_IRUSR | S_IWUSR);
if (node_cache_fd < 0)
{
fprintf(stderr, "Failed to open node cache file: %s\n",
strerror(errno));
exit_nicely();
}
}
else
{
if (cache_already_written)
{
node_cache_fd = open(node_cache_fname, O_RDWR, S_IRUSR | S_IWUSR);
}
else
{
node_cache_fd = open(node_cache_fname, O_RDWR | O_CREAT | O_TRUNC,
S_IRUSR | S_IWUSR);
}
if (node_cache_fd < 0)
{
fprintf(stderr, "Failed to create node cache file: %s\n",
strerror(errno));
exit_nicely();
}
if (lseek64(node_cache_fd, 0, SEEK_SET) < 0) {
fprintf(stderr, "Failed to seek to correct position in node cache: %s\n",
strerror(errno));
exit_nicely();
};
if (cache_already_written == 0)
{
#ifdef HAVE_POSIX_FALLOCATE
if ((err = posix_fallocate(node_cache_fd, 0,
sizeof(struct ramNode) * MAXIMUM_INITIAL_ID)) != 0)
{
if (err == ENOSPC) {
fprintf(stderr, "Failed to allocate space for node cache file: No space on disk\n");
} else if (err == EFBIG) {
fprintf(stderr, "Failed to allocate space for node cache file: File is too big\n");
} else {
fprintf(stderr, "Failed to allocate space for node cache file: Internal error %i\n", err);
}
close(node_cache_fd);
exit_nicely();
}
fprintf(stderr, "Allocated space for persistent node cache file\n");
#endif
writeNodeBlock.nodes = malloc(
WRITE_NODE_BLOCK_SIZE * sizeof(struct ramNode));
if (!writeNodeBlock.nodes) {
fprintf(stderr, "Out of memory: Failed to allocate node writeout buffer\n");
exit_nicely();
}
ramNodes_clear(writeNodeBlock.nodes, WRITE_NODE_BLOCK_SIZE);
writeNodeBlock.block_offset = 0;
writeNodeBlock.used = 0;
writeNodeBlock.dirty = 0;
cacheHeader.format_version = PERSISTENT_CACHE_FORMAT_VERSION;
cacheHeader.id_size = sizeof(osmid_t);
cacheHeader.max_initialised_id = 0;
if (lseek64(node_cache_fd, 0, SEEK_SET) < 0) {
fprintf(stderr, "Failed to seek to correct position in node cache: %s\n",
strerror(errno));
exit_nicely();
};
if (write(node_cache_fd, &cacheHeader,
sizeof(struct persistentCacheHeader))
!= sizeof(struct persistentCacheHeader))
{
fprintf(stderr, "Failed to write persistent cache header: %s\n",
strerror(errno));
exit_nicely();
}
}
}
if (lseek64(node_cache_fd, 0, SEEK_SET) < 0) {
fprintf(stderr, "Failed to seek to correct position in node cache: %s\n",
strerror(errno));
exit_nicely();
};
if (read(node_cache_fd, &cacheHeader, sizeof(struct persistentCacheHeader))
!= sizeof(struct persistentCacheHeader))
{
fprintf(stderr, "Failed to read persistent cache header: %s\n",
strerror(errno));
exit_nicely();
}
if (cacheHeader.format_version != PERSISTENT_CACHE_FORMAT_VERSION)
{
fprintf(stderr, "Persistent cache header is wrong version\n");
exit_nicely();
}
if (cacheHeader.id_size != sizeof(osmid_t))
{
fprintf(stderr, "Persistent cache header is wrong id type\n");
exit_nicely();
}
fprintf(stderr,"Maximum node in persistent node cache: %" PRIdOSMID "\n", cacheHeader.max_initialised_id);
readNodeBlockCache = malloc(
READ_NODE_CACHE_SIZE * sizeof(struct ramNodeBlock));
if (!readNodeBlockCache) {
fprintf(stderr, "Out of memory: Failed to allocate node read cache\n");
exit_nicely();
}
for (i = 0; i < READ_NODE_CACHE_SIZE; i++)
{
readNodeBlockCache[i].nodes = malloc(
READ_NODE_BLOCK_SIZE * sizeof(struct ramNode));
if (!readNodeBlockCache[i].nodes) {
fprintf(stderr, "Out of memory: Failed to allocate node read cache\n");
exit_nicely();
}
readNodeBlockCache[i].block_offset = -1;
readNodeBlockCache[i].used = 0;
readNodeBlockCache[i].dirty = 0;
}
}
void shutdown_node_persistent_cache()
{
int i;
writeout_dirty_nodes(-1);
if (lseek64(node_cache_fd, 0, SEEK_SET) < 0) {
fprintf(stderr, "Failed to seek to correct position in node cache: %s\n",
strerror(errno));
exit_nicely();
};
if (write(node_cache_fd, &cacheHeader, sizeof(struct persistentCacheHeader))
!= sizeof(struct persistentCacheHeader))
{
fprintf(stderr, "Failed to update persistent cache header: %s\n",
strerror(errno));
exit_nicely();
}
fprintf(stderr,"Maximum node in persistent node cache: %" PRIdOSMID "\n", cacheHeader.max_initialised_id);
fsync(node_cache_fd);
if (close(node_cache_fd) != 0)
{
fprintf(stderr, "Failed to close node cache file: %s\n",
strerror(errno));
}
for (i = 0; i < READ_NODE_CACHE_SIZE; i++)
{
free(readNodeBlockCache[i].nodes);
}
shutdown_search_array(&readNodeBlockCacheIdx);
free(readNodeBlockCache);
readNodeBlockCache = NULL;
}