forked from mischasan/hx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhx.c
446 lines (369 loc) · 12.3 KB
/
hx.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
// Copyright (C) 2001-2013 Mischa Sandberg <[email protected]>
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License Version 2 as
// published by the Free Software Foundation. You may not use, modify or
// distribute this program under any other version of the GNU General
// Public License.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
//
// IF YOU HAVE NO WAY OF WORKING WITH GPL, CONTACT ME.
//-------------------------------------------------------------------------------
// hx: utilities for implementing HX file access method.
#include "_hx.h"
#include <assert.h>
#include <errno.h>
#include <stdarg.h>
#include <sys/time.h>
#include "util.h" // fls
int hxcrash;
int hxdebug;
char const *hxproc = "";
double hxtime;
int hxversion = HXVERSION;
int
hx_diff(HXFILE const *hp, char const *reca, char const *recb)
{
return hp->diff(reca, recb, hp->udata, hp->uleng);
}
HXHASH
hx_hash(HXFILE const *hp, char const *recp)
{
return hp->hash(recp, hp->udata, hp->uleng);
}
int
hx_load(HXFILE const *hp, char *recp, int recsize, char const *buf)
{
if (!hp->load)
return HXERR_BAD_REQUEST;
int rc = hp->load(recp, recsize, buf, hp->udata, hp->uleng);
return rc > 0 && rc <= hxmaxrec(hp) ? rc : HXERR_BAD_RECORD;
}
int
hx_save(HXFILE const *hp, char const *recp, int reclen,
char *buf, int bufsize)
{
return !hp->save ? HXERR_BAD_REQUEST
: hp->save(recp, reclen, buf, bufsize, hp->udata, hp->uleng);
}
int
hx_test(HXFILE const *hp, char const *recp, int reclen)
{
return hp->test ? hp->test(recp, reclen, hp->udata, hp->uleng)
: HXERR_BAD_REQUEST;
}
int
hxfileno(HXFILE const *hp)
{
return hp ? hp->fileno : -1;
}
int
hxinfo(HXFILE const *hp, char *udata, int usize)
{
if (!hp)
return HXERR_BAD_REQUEST;
memcpy(udata, hp->udata, IMIN(hp->uleng, usize));
return hp->uleng;
}
int
hxmaxrec(HXFILE const *hp)
{
return hp ? (int)(DATASIZE(hp) - sizeof(HXREC) - MIN_INDEX_BYTES(1))
: HXERR_BAD_REQUEST;
}
//--------------|-------|-------------------------------------
// _hxenter: common prologue validations and processing
// for API routines. Check args, initialize HXLOCAL,
// allocate buffers, handle generic lock work.
void
_hxenter(HXLOCAL * locp, HXFILE * hp, char const *recp, int nbufs)
{
assert(nbufs <= MAXHXBUFS);
// The following must be set before any LEAVE:
locp->file = hp;
if (!hp || !hp->diff || !hp->hash)
LEAVE(locp, HXERR_BAD_REQUEST);
locp->mode = F_WRLCK; // the common case
errno = 0;
if (recp)
locp->hash = hx_hash(hp, recp);
// THIS SHOULD NOT BE HERE!
if (!IS_MMAP(hp) && nbufs) {
char *mem = calloc(nbufs, hp->pgsize);
int i;
for (i = 0; i < nbufs; ++i)
locp->buf[i].page = (HXPAGE *) & mem[i * hp->pgsize];
}
}
// _hxfind: search buffer for match against given key.
// Returns (-1), or offset in data[] of HXREC matching (rp).
// *hindp is the hash table position, for incremental hind changes.
int
_hxfind(HXLOCAL * locp, HXBUF const *bufp, HXHASH rechash,
char const *recdata, int *hindp)
{
HXFILE const *hp = locp->file;
COUNT const *hind = HIND_BASE_C(hp, bufp);
int hsize = HIND_SIZE(hp, bufp);
unsigned mask = MASK(hsize);
int i = rechash & mask;
if (i >= hsize)
i &= mask >> 1;
for (; hind[-i]; i = (i ? i : hsize) - 1) {
char const *recp = bufp->data + hind[-i] - 1;
if (rechash == RECHASH(recp) && !hx_diff(hp, recdata, RECDATA(recp)))
break;
}
if (hindp)
*hindp = i;
return hind[-i] - 1;
}
// _hxhead: calculate pgno of head-of-chain for a given record hash.
// This first reverses the hash, since the low bits of the hash
// are now used for in-page indexing.
PAGENO
_hxhead(HXLOCAL const *locp, HXHASH hash)
{
PAGENO pg = REV_HASH(hash) & locp->mask;
return _hxd2f(pg < locp->dpages ? pg : pg & (locp->mask >> 1));
}
// _hxindexed: test that hind[] is correct.
// - every record is indexed
// - the rest of hind[] is zero
int
_hxindexed(HXLOCAL * locp, HXBUF const *bufp)
{
COUNT const *hind = HIND_BASE_C(locp->file, bufp);
int hsize = HIND_SIZE(locp->file, bufp);
COUNT test[hsize], *tend = test + hsize - 1;
memcpy(test, hind - hsize + 1, hsize * sizeof(COUNT));
// Ensure that there are at least hsize/4 zeroes in hind[]:
// _hxfind relies on there being at least 1 zero!
int i = 0, hpos = bufp->recs / 4;
while (i < hsize && hpos > 0)
hpos -= !test[i++];
if (i == hsize)
return 0;
char const *recp, *endp;
FOR_EACH_REC(recp, bufp, endp) {
if (0 > _hxfind(locp, bufp, RECHASH(recp), RECDATA(recp), &hpos))
return 0;
tend[-hpos] = 0;
}
for (i = 0; i < hsize; ++i)
if (tend[-i])
return 0;
return 1;
}
// _hxleave: epilogue clean-up.
// POSIX says free(NULL) is okay ...
HXRET
_hxleave(HXLOCAL * locp)
{
HXFILE *hp = locp->file;
int i, save_errno = errno;
// Handle _hxenter rejecting !hp as a pass-through
if (!hp)
return HXERR_BAD_REQUEST;
if (!hp->hold && locp->mylock)
_hxunlock(locp, 0, 0);
// No point in dumping core if you can't flush anyway.
if (locp->ret >= 0) {
assert(!locp->freed);
assert(!locp->buf[0].page || !DIRTY(&locp->buf[0]));
assert(!locp->buf[1].page || !DIRTY(&locp->buf[1]));
assert(!locp->buf[2].page || !DIRTY(&locp->buf[2]));
}
if (!IS_MMAP(hp))
free(locp->buf[0].page);
else if (hp->mode & HX_MPROTECT)
mprotect(hp->mmap, hp->mlen, PROT_NONE);
if (locp->changed && (hp->mode & HX_FSYNC)
&& locp->ret >= HXOKAY && fsync(hp->fileno))
locp->ret = HXERR_FSYNC;
free(locp->vnext);
free(locp->vprev);
free(locp->vrefs);
free(locp->visit);
free(locp->vtail);
free(locp->recv);
free(locp->membase);
if (locp->tmpmap)
munmap(locp->tmpmap, locp->mapsize);
for (i = locp->nfps; --i >= 0;)
fclose(locp->fp[i]);
errno = save_errno;
return locp->ret;
}
// _hxload: read page into a buffer, setting dependent
// buffer fields.
void
_hxload(HXLOCAL * locp, HXBUF * bufp, PAGENO pgno)
{
HXFILE *hp = locp->file;
off_t pos = (off_t) pgno * hp->pgsize;
assert(!DIRTY(bufp));
assert(pgno < locp->npages);
assert(!
(locp->buf[0].page && locp->buf[0].pgno == pgno &&
DIRTY(&locp->buf[0])));
assert(!
(locp->buf[1].page && locp->buf[1].pgno == pgno &&
DIRTY(&locp->buf[1])));
assert(!
(locp->buf[2].page && locp->buf[2].pgno == pgno &&
DIRTY(&locp->buf[2])));
if (!IS_HEAD(pgno))
_hxlock(locp, pgno, 1);
if (!hp->hold && !_hxislocked(locp, pgno)) {
DEBUG("no lock guarding pgno=%d", pgno);
assert(!"page is not locked");
}
if (IS_MMAP(hp))
bufp->page = (HXPAGE *) & hp->mmap[pos];
else
_hxread(locp, pos, bufp->page, hp->pgsize);
bufp->pgno = pgno;
bufp->next = LDUL(&bufp->page->next);
bufp->used = LDUS(&bufp->page->used);
bufp->recs = LDUS(&bufp->page->recs);
bufp->orig = bufp->used;
bufp->hind = (COUNT *) (bufp->data + DATASIZE(hp)) - 1;
bufp->hsize = HIND_SIZE(hp, bufp);
bufp->hmask = MASK(bufp->hsize);
SCRUB(bufp);
if (!IS_MAP(hp, pgno) && hxdebug > 2) {
char heads[9999];
DEBUG3("load pgno=%u next=%u used=%u recs=%u hsize=%d heads=%s",
pgno, bufp->next, bufp->used, bufp->recs, bufp->hsize,
_hxheads(locp, bufp, heads));
//assert(_hxcheckbuf(locp, bufp) == HXOKAY);
} else {
DEBUG3("load pgno=%u", pgno);
}
if (!(hp->mode & HX_RECOVER) && ((bufp->used > DATASIZE(hp))
|| (pgno && bufp->next && !bufp->used)
|| (pgno && IS_HEAD(bufp->next))
|| (IS_MAP(hp, pgno) &&
!(bufp->data[bufp->used] & 1))
))
LEAVE(locp, HXERR_BAD_FILE);
}
// _hxmap: translate (ovfl) pgno to a bitmap pos(pgno,bitpos).
// "map1" is the pgno of the first map page (after page 0).
PAGENO
_hxmap(HXFILE const *hp, PAGENO pgno, int *bitpos)
{
PAGENO mpg = 0, ppm;
assert(!IS_HEAD(pgno));
if (pgno < hp->map1) {
*bitpos = pgno / HXPGRATE + 8 * hp->uleng;
} else {
ppm = 8 * HXPGRATE * DATASIZE(hp);
mpg = pgno - (pgno - hp->map1) % ppm;
*bitpos = (pgno - mpg) / HXPGRATE % ppm;
DEBUG3("pgno=%u: %u %u", pgno, mpg, *bitpos);
}
DEBUG3("pgno=%u maps to mpg=%u off=%u bit=%u",
pgno, mpg, *bitpos / 8, *bitpos & 7);
return mpg;
}
// _hxpginfo: read the {next,used} page header (only).
// Currently only of use to "hxshare", which forbids MMAP.
PGINFO
_hxpginfo(HXLOCAL * locp, PAGENO pg)
{
PGINFO info;
assert(!locp->file->mmap);
_hxread(locp, (off_t) pg * locp->file->pgsize, &info, sizeof info);
return (PGINFO) {
LDUL(&info.pgno), LDUS(&info.used), LDUS(&info.recs)};
}
// _hxpoint: update loc.head from (npages,hash).
void
_hxpoint(HXLOCAL * locp)
{
HXFILE *hp = locp->file;
PAGENO head = _hxhead(locp, locp->hash);
// This is the first place where we can calc the curr key head.
// Cancel a prior hxhold() for a different key
if (hp->hold && hp->hold != head && !FILE_HELD(hp)) {
_hxunlock(locp, hp->hold, 1);
hp->hold = 0, hp->lockpart = NONE_LOCK;
}
locp->head = head;
}
void
_hxread(HXLOCAL * locp, off_t pos, void *buf, int size)
{
int ret;
assert(pos < (off_t) locp->npages * locp->file->pgsize);
assert(pos + size <= (off_t) locp->npages * locp->file->pgsize);
if (pos != lseek(locp->file->fileno, pos, SEEK_SET))
LEAVE(locp, HXERR_LSEEK);
ret = read(locp->file->fileno, buf, size);
assert(ret == size);
if (size != ret)
LEAVE(locp, HXERR_READ);
}
void
_hxremap(HXLOCAL * locp)
{
HXFILE *hp = locp->file;
off_t mlen = (off_t) hp->pgsize * locp->npages;
int prot = hp->mode & HX_UPDATE ? PROT_WRITE : 0;
// At hxopen, hp->mlen is 0
if (hp->mlen == mlen) {
if (hp->mode & HX_MPROTECT
&& mprotect(hp->mmap, hp->mlen, prot | PROT_READ))
LEAVE(locp, HXERR_MMAP);
} else {
char *mp;
if (hp->mmap) {
# ifdef __USE_GNU
mp = mremap(hp->mmap, hp->mlen, mlen, MREMAP_MAYMOVE);
# else
if (munmap(hp->mmap, hp->mlen))
LEAVE(locp, HXERR_MMAP);
mp = mmap(hp->mmap, mlen, prot | PROT_READ,
MAP_SHARED | MAP_NOCORE, hp->fileno, 0);
#endif
} else {
mp = mmap( /*addr */ NULL, mlen, PROT_READ | prot,
MAP_SHARED | MAP_NOCORE, hp->fileno, 0);
}
DEBUG("mmap %d %s%d -> %d%s", hp->mlen, mlen < hp->mlen ? "" : "+", mlen - hp->mlen, mlen, !hp->mlen || hp->mmap == mp ? "" : " MOVED");
if (!mp || mp == MAP_FAILED)
LEAVE(locp, HXERR_MMAP);
hp->mlen = mlen;
hp->mmap = mp;
HXBUF *bufp = &locp->buf[MAXHXBUFS];
while (--bufp >= locp->buf)
if (bufp->page)
MAP_BUF(hp, bufp);
}
}
void
_hxsize(HXLOCAL * locp)
{
HXFILE *hp = locp->file;
off_t size = lseek(hp->fileno, 0, SEEK_END);
if (size % hp->pgsize || (size /= hp->pgsize) < 2)
LEAVE(locp, HXERR_BAD_FILE);
PAGENO npages = locp->npages, split = SPLIT_PAGE(locp);
locp->npages = size;
locp->dpages = _hxf2d(locp->npages);
locp->mask = MASK(locp->dpages);
if (npages && npages != locp->npages)
DEBUG2("npages changes: %d<%d> to %d<%d> mask=%d",
npages, split, locp->npages, SPLIT_PAGE(locp), locp->mask);
}
//EOF