forked from OSGeo/grass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache.c
336 lines (260 loc) · 9.45 KB
/
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
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/types.h>
#include <unistd.h>
#include "raster3d_intern.h"
/*---------------------------------------------------------------------------*/
static int cacheRead_readFun(int tileIndex, void *tileBuf, void *closure)
{
RASTER3D_Map *map = closure;
if (!Rast3d_read_tile(map, tileIndex, tileBuf, map->typeIntern)) {
Rast3d_error("cacheRead_readFun: error in Rast3d_read_tile");
return 0;
}
return 1;
}
/*---------------------------------------------------------------------------*/
static int initCacheRead(RASTER3D_Map *map, int nCached)
{
map->cache =
Rast3d_cache_new_read(nCached, map->tileSize * map->numLengthIntern,
map->nTiles, cacheRead_readFun, map);
if (map->cache == NULL) {
Rast3d_error("initCacheRead: error in Rast3d_cache_new_read");
return 0;
}
return 1;
}
/*---------------------------------------------------------------------------*/
/*
the map->index array is (ab)used to store the positions of the tiles in the
file-cash. we can do this since we maintain the invariant for every tile
that it is either in no file (index == -1) or in either the output-file
(index >= 0) or the cash-file (index <= -2). to convert the file-position in
the cash-file into an index we use the following function:
index = - (fileposition + 2)
symmetrically, we use
fileposition = - (index + 2)
to convert from index to the fileposition.
*/
/*---------------------------------------------------------------------------*/
static int cacheWrite_readFun(int tileIndex, void *tileBuf, void *closure)
{
RASTER3D_Map *map = closure;
int index;
size_t nBytes;
size_t offs, offsLast;
ssize_t res;
long int pos;
pos = map->index[tileIndex];
/* tile has already been flushed onto output file or does not exist yet */
if (pos >=
-1) { /* note, Rast3d_read_tile takes care of the case pos == -1 */
Rast3d_read_tile(map, tileIndex, tileBuf, map->typeIntern);
return 1;
}
/* tile is in cache file */
pos = -pos - 2; /* pos is shifted by 2 to avoid 0 and -1 */
nBytes = map->tileSize * map->numLengthIntern;
offs = pos * (nBytes + sizeof(int));
/* seek tile and read it into buffer */
if (lseek(map->cacheFD, offs, SEEK_SET) == -1) {
Rast3d_error("cacheWrite_readFun: can't position file");
return 0;
}
if ((res = read(map->cacheFD, tileBuf, nBytes)) < 0 ||
(size_t)res != nBytes) {
Rast3d_error("cacheWrite_readFun: can't read file");
return 0;
}
/* remove it from index */
map->index[tileIndex] = -1;
/* if it is the last tile in the file we are done */
/* map->cachePosLast tells us the position of the last tile in the file */
if (map->cachePosLast == pos) {
map->cachePosLast--;
return 1;
}
/* otherwise we move the last tile in the file into the position of */
/* the tile we just read and update the hash information */
offsLast = map->cachePosLast * (nBytes + sizeof(int));
if (lseek(map->cacheFD, offsLast, SEEK_SET) == -1) {
Rast3d_error("cacheWrite_readFun: can't position file");
return 0;
}
if ((res = read(map->cacheFD, xdr, nBytes + sizeof(int))) < 0 ||
(size_t)res != nBytes + sizeof(int)) {
Rast3d_error("cacheWrite_readFun: can't read file");
return 0;
}
if (lseek(map->cacheFD, offs, SEEK_SET) == -1) {
Rast3d_error("cacheWrite_readFun: can't position file");
return 0;
}
if ((res = write(map->cacheFD, xdr, nBytes + sizeof(int))) < 0 ||
(size_t)res != nBytes + sizeof(int)) {
Rast3d_error("cacheWrite_readFun: can't write file");
return 0;
}
index = *((int *)((unsigned char *)xdr + nBytes));
map->index[index] = -pos - 2;
map->cachePosLast--;
return 1;
}
/*---------------------------------------------------------------------------*/
static int cacheWrite_writeFun(int tileIndex, const void *tileBuf,
void *closure)
{
RASTER3D_Map *map = closure;
size_t nBytes;
size_t offs;
ssize_t res;
if (map->index[tileIndex] != -1)
return 1;
map->cachePosLast++;
nBytes = map->tileSize * map->numLengthIntern;
offs = map->cachePosLast * (nBytes + sizeof(int));
if (lseek(map->cacheFD, offs, SEEK_SET) == -1) {
Rast3d_error("cacheWrite_writeFun: can't position file");
return 0;
}
if ((res = write(map->cacheFD, tileBuf, nBytes)) < 0 ||
(size_t)res != nBytes) {
Rast3d_error("cacheWrite_writeFun: can't write file");
return 0;
}
if (write(map->cacheFD, &tileIndex, sizeof(int)) != sizeof(int)) {
Rast3d_error("cacheWrite_writeFun: can't write file");
return 0;
}
map->index[tileIndex] = -map->cachePosLast - 2;
return 1;
}
/*---------------------------------------------------------------------------*/
static int disposeCacheWrite(RASTER3D_Map *map)
{
if (map->cacheFD >= 0) {
if (close(map->cacheFD) != 0) {
Rast3d_error("disposeCacheWrite: could not close file");
return 0;
}
remove(map->cacheFileName);
Rast3d_free(map->cacheFileName);
}
Rast3d_cache_dispose(map->cache);
return 1;
}
/*---------------------------------------------------------------------------*/
static int initCacheWrite(RASTER3D_Map *map, int nCached)
{
map->cacheFileName = G_tempfile();
map->cacheFD = open(map->cacheFileName, O_RDWR | O_CREAT | O_TRUNC, 0666);
if (map->cacheFD < 0) {
Rast3d_error("initCacheWrite: could not open file");
return 0;
}
map->cachePosLast = -1;
map->cache = Rast3d_cache_new(nCached, map->tileSize * map->numLengthIntern,
map->nTiles, cacheWrite_writeFun, map,
cacheWrite_readFun, map);
if (map->cache == NULL) {
disposeCacheWrite(map);
Rast3d_error("initCacheWrite: error in Rast3d_cache_new");
return 0;
}
return 1;
}
/*---------------------------------------------------------------------------*/
int Rast3d_init_cache(RASTER3D_Map *map, int nCached)
{
if (map->operation == RASTER3D_READ_DATA) {
if (!initCacheRead(map, nCached)) {
Rast3d_error("Rast3d_init_cache: error in initCacheRead");
return 0;
}
return 1;
}
if (!initCacheWrite(map, nCached)) {
Rast3d_error("Rast3d_init_cache: error in initCacheWrite");
return 0;
}
return 1;
}
/*---------------------------------------------------------------------------*/
static int disposeCacheRead(RASTER3D_Map *map)
{
Rast3d_cache_dispose(map->cache);
return 1;
}
/*---------------------------------------------------------------------------*/
int Rast3d_dispose_cache(RASTER3D_Map *map)
{
if (map->operation == RASTER3D_READ_DATA) {
if (!disposeCacheRead(map)) {
Rast3d_error("Rast3d_dispose_cache: error in disposeCacheRead");
return 0;
}
return 1;
}
if (!disposeCacheWrite(map)) {
Rast3d_error("Rast3d_dispose_cache: error in disposeCacheWrite");
return 0;
}
return 1;
}
/*---------------------------------------------------------------------------*/
static int cacheFlushFun(int tileIndex, const void *tileBuf, void *closure)
{
RASTER3D_Map *map = closure;
if (!Rast3d_write_tile(map, tileIndex, tileBuf, map->typeIntern)) {
Rast3d_error("cacheFlushFun: error in Rast3d_write_tile");
return 0;
}
return 1;
}
/*---------------------------------------------------------------------------*/
int Rast3d_flush_all_tiles(RASTER3D_Map *map)
{
size_t tileIndex, nBytes;
size_t offs;
if (map->operation == RASTER3D_READ_DATA) {
if (!Rast3d_cache_remove_all(map->cache)) {
Rast3d_error(
"Rast3d_flush_all_tiles: error in Rast3d_cache_remove_all");
return 0;
}
return 1;
}
/* make cache write into output file instead of cache file */
Rast3d_cache_set_remove_fun(map->cache, cacheFlushFun, map);
/* first flush all the tiles which are in the file cache */
nBytes = map->tileSize * map->numLengthIntern;
while (map->cachePosLast >= 0) {
offs = map->cachePosLast * (nBytes + sizeof(int)) + nBytes;
if (lseek(map->cacheFD, offs, SEEK_SET) == -1) {
Rast3d_error("Rast3d_flush_all_tiles: can't position file");
return 0;
}
if (read(map->cacheFD, &tileIndex, sizeof(int)) != sizeof(int)) {
Rast3d_error("Rast3d_flush_all_tiles: can't read file");
return 0;
}
if (!Rast3d_cache_load(map->cache, tileIndex)) {
Rast3d_error("Rast3d_flush_all_tiles: error in Rast3d_cache_load");
return 0;
}
if (!Rast3d_cache_flush(map->cache, tileIndex)) {
Rast3d_error("Rast3d_flush_all_tiles: error in Rast3d_cache_flush");
return 0;
}
}
/* then flush all the tiles which remain in the non-file cache */
if (!Rast3d_cache_flush_all(map->cache)) {
Rast3d_error("Rast3d_flush_all_tiles: error in Rast3d_cache_flush_all");
return 0;
}
/* now the cache should write into the cache file again */
Rast3d_cache_set_remove_fun(map->cache, cacheWrite_writeFun, map);
return 1;
}