Next wave of _63 VMS patches
[p5sagit/p5-mst-13.2.git] / ext / SDBM_File / sdbm / sdbm.c
1 /*
2  * sdbm - ndbm work-alike hashed database library
3  * based on Per-Aake Larson's Dynamic Hashing algorithms. BIT 18 (1978).
4  * author: oz@nexus.yorku.ca
5  * status: public domain.
6  *
7  * core routines
8  */
9
10 #ifndef lint
11 static char rcsid[] = "$Id: sdbm.c,v 1.16 90/12/13 13:01:31 oz Exp $";
12 #endif
13
14 #include "INTERN.h"
15 #include "config.h"
16 #include "sdbm.h"
17 #include "tune.h"
18 #include "pair.h"
19
20 #ifdef I_FCNTL
21 # include <fcntl.h>
22 #endif
23 #ifdef I_SYS_FILE
24 # include <sys/file.h>
25 #endif
26
27 #ifdef I_STRING
28 # include <string.h>
29 #else
30 # include <strings.h>
31 #endif
32
33 /*
34  * externals
35  */
36 #ifndef WIN32
37 #ifndef sun
38 extern int errno;
39 #endif
40
41 extern Malloc_t malloc proto((MEM_SIZE));
42 extern Free_t free proto((Malloc_t));
43 extern Off_t lseek(int, Off_t, int);
44 #endif
45
46 /*
47  * forward
48  */
49 static int getdbit proto((DBM *, long));
50 static int setdbit proto((DBM *, long));
51 static int getpage proto((DBM *, long));
52 static datum getnext proto((DBM *));
53 static int makroom proto((DBM *, long, int));
54
55 /*
56  * useful macros
57  */
58 #define bad(x)          ((x).dptr == NULL || (x).dsize < 0)
59 #define exhash(item)    sdbm_hash((item).dptr, (item).dsize)
60 #define ioerr(db)       ((db)->flags |= DBM_IOERR)
61
62 #define OFF_PAG(off)    (long) (off) * PBLKSIZ
63 #define OFF_DIR(off)    (long) (off) * DBLKSIZ
64
65 static long masks[] = {
66         000000000000, 000000000001, 000000000003, 000000000007,
67         000000000017, 000000000037, 000000000077, 000000000177,
68         000000000377, 000000000777, 000000001777, 000000003777,
69         000000007777, 000000017777, 000000037777, 000000077777,
70         000000177777, 000000377777, 000000777777, 000001777777,
71         000003777777, 000007777777, 000017777777, 000037777777,
72         000077777777, 000177777777, 000377777777, 000777777777,
73         001777777777, 003777777777, 007777777777, 017777777777
74 };
75
76 DBM *
77 sdbm_open(register char *file, register int flags, register int mode)
78 {
79         register DBM *db;
80         register char *dirname;
81         register char *pagname;
82         register int n;
83
84         if (file == NULL || !*file)
85                 return errno = EINVAL, (DBM *) NULL;
86 /*
87  * need space for two seperate filenames
88  */
89         n = strlen(file) * 2 + strlen(DIRFEXT) + strlen(PAGFEXT) + 2;
90
91         if ((dirname = (char *) malloc((unsigned) n)) == NULL)
92                 return errno = ENOMEM, (DBM *) NULL;
93 /*
94  * build the file names
95  */
96         dirname = strcat(strcpy(dirname, file), DIRFEXT);
97         pagname = strcpy(dirname + strlen(dirname) + 1, file);
98         pagname = strcat(pagname, PAGFEXT);
99
100         db = sdbm_prep(dirname, pagname, flags, mode);
101         free((char *) dirname);
102         return db;
103 }
104
105 DBM *
106 sdbm_prep(char *dirname, char *pagname, int flags, int mode)
107 {
108         register DBM *db;
109         struct stat dstat;
110
111         if ((db = (DBM *) malloc(sizeof(DBM))) == NULL)
112                 return errno = ENOMEM, (DBM *) NULL;
113
114         db->flags = 0;
115         db->hmask = 0;
116         db->blkptr = 0;
117         db->keyptr = 0;
118 /*
119  * adjust user flags so that WRONLY becomes RDWR, 
120  * as required by this package. Also set our internal
121  * flag for RDONLY if needed.
122  */
123         if (flags & O_WRONLY)
124                 flags = (flags & ~O_WRONLY) | O_RDWR;
125
126         else if ((flags & 03) == O_RDONLY)
127                 db->flags = DBM_RDONLY;
128 /*
129  * open the files in sequence, and stat the dirfile.
130  * If we fail anywhere, undo everything, return NULL.
131  */
132 #if defined(OS2) || defined(MSDOS) || defined(WIN32)
133         flags |= O_BINARY;
134 #       endif
135         if ((db->pagf = open(pagname, flags, mode)) > -1) {
136                 if ((db->dirf = open(dirname, flags, mode)) > -1) {
137 /*
138  * need the dirfile size to establish max bit number.
139  */
140                         if (fstat(db->dirf, &dstat) == 0) {
141 /*
142  * zero size: either a fresh database, or one with a single,
143  * unsplit data page: dirpage is all zeros.
144  */
145                                 db->dirbno = (!dstat.st_size) ? 0 : -1;
146                                 db->pagbno = -1;
147                                 db->maxbno = dstat.st_size * BYTESIZ;
148
149                                 (void) memset(db->pagbuf, 0, PBLKSIZ);
150                                 (void) memset(db->dirbuf, 0, DBLKSIZ);
151                         /*
152                          * success
153                          */
154                                 return db;
155                         }
156                         (void) close(db->dirf);
157                 }
158                 (void) close(db->pagf);
159         }
160         free((char *) db);
161         return (DBM *) NULL;
162 }
163
164 void
165 sdbm_close(register DBM *db)
166 {
167         if (db == NULL)
168                 errno = EINVAL;
169         else {
170                 (void) close(db->dirf);
171                 (void) close(db->pagf);
172                 free((char *) db);
173         }
174 }
175
176 datum
177 sdbm_fetch(register DBM *db, datum key)
178 {
179         if (db == NULL || bad(key))
180                 return errno = EINVAL, nullitem;
181
182         if (getpage(db, exhash(key)))
183                 return getpair(db->pagbuf, key);
184
185         return ioerr(db), nullitem;
186 }
187
188 int
189 sdbm_delete(register DBM *db, datum key)
190 {
191         if (db == NULL || bad(key))
192                 return errno = EINVAL, -1;
193         if (sdbm_rdonly(db))
194                 return errno = EPERM, -1;
195
196         if (getpage(db, exhash(key))) {
197                 if (!delpair(db->pagbuf, key))
198                         return -1;
199 /*
200  * update the page file
201  */
202                 if (lseek(db->pagf, OFF_PAG(db->pagbno), SEEK_SET) < 0
203                     || write(db->pagf, db->pagbuf, PBLKSIZ) < 0)
204                         return ioerr(db), -1;
205
206                 return 0;
207         }
208
209         return ioerr(db), -1;
210 }
211
212 int
213 sdbm_store(register DBM *db, datum key, datum val, int flags)
214 {
215         int need;
216         register long hash;
217
218         if (db == NULL || bad(key))
219                 return errno = EINVAL, -1;
220         if (sdbm_rdonly(db))
221                 return errno = EPERM, -1;
222
223         need = key.dsize + val.dsize;
224 /*
225  * is the pair too big (or too small) for this database ??
226  */
227         if (need < 0 || need > PAIRMAX)
228                 return errno = EINVAL, -1;
229
230         if (getpage(db, (hash = exhash(key)))) {
231 /*
232  * if we need to replace, delete the key/data pair
233  * first. If it is not there, ignore.
234  */
235                 if (flags == DBM_REPLACE)
236                         (void) delpair(db->pagbuf, key);
237 #ifdef SEEDUPS
238                 else if (duppair(db->pagbuf, key))
239                         return 1;
240 #endif
241 /*
242  * if we do not have enough room, we have to split.
243  */
244                 if (!fitpair(db->pagbuf, need))
245                         if (!makroom(db, hash, need))
246                                 return ioerr(db), -1;
247 /*
248  * we have enough room or split is successful. insert the key,
249  * and update the page file.
250  */
251                 (void) putpair(db->pagbuf, key, val);
252
253                 if (lseek(db->pagf, OFF_PAG(db->pagbno), SEEK_SET) < 0
254                     || write(db->pagf, db->pagbuf, PBLKSIZ) < 0)
255                         return ioerr(db), -1;
256         /*
257          * success
258          */
259                 return 0;
260         }
261
262         return ioerr(db), -1;
263 }
264
265 /*
266  * makroom - make room by splitting the overfull page
267  * this routine will attempt to make room for SPLTMAX times before
268  * giving up.
269  */
270 static int
271 makroom(register DBM *db, long int hash, int need)
272 {
273         long newp;
274         char twin[PBLKSIZ];
275         char *pag = db->pagbuf;
276         char *New = twin;
277         register int smax = SPLTMAX;
278
279         do {
280 /*
281  * split the current page
282  */
283                 (void) splpage(pag, New, db->hmask + 1);
284 /*
285  * address of the new page
286  */
287                 newp = (hash & db->hmask) | (db->hmask + 1);
288
289 /*
290  * write delay, read avoidence/cache shuffle:
291  * select the page for incoming pair: if key is to go to the new page,
292  * write out the previous one, and copy the new one over, thus making
293  * it the current page. If not, simply write the new page, and we are
294  * still looking at the page of interest. current page is not updated
295  * here, as sdbm_store will do so, after it inserts the incoming pair.
296  */
297                 if (hash & (db->hmask + 1)) {
298                         if (lseek(db->pagf, OFF_PAG(db->pagbno), SEEK_SET) < 0
299                             || write(db->pagf, db->pagbuf, PBLKSIZ) < 0)
300                                 return 0;
301                         db->pagbno = newp;
302                         (void) memcpy(pag, New, PBLKSIZ);
303                 }
304                 else if (lseek(db->pagf, OFF_PAG(newp), SEEK_SET) < 0
305                          || write(db->pagf, New, PBLKSIZ) < 0)
306                         return 0;
307
308                 if (!setdbit(db, db->curbit))
309                         return 0;
310 /*
311  * see if we have enough room now
312  */
313                 if (fitpair(pag, need))
314                         return 1;
315 /*
316  * try again... update curbit and hmask as getpage would have
317  * done. because of our update of the current page, we do not
318  * need to read in anything. BUT we have to write the current
319  * [deferred] page out, as the window of failure is too great.
320  */
321                 db->curbit = 2 * db->curbit +
322                         ((hash & (db->hmask + 1)) ? 2 : 1);
323                 db->hmask |= db->hmask + 1;
324
325                 if (lseek(db->pagf, OFF_PAG(db->pagbno), SEEK_SET) < 0
326                     || write(db->pagf, db->pagbuf, PBLKSIZ) < 0)
327                         return 0;
328
329         } while (--smax);
330 /*
331  * if we are here, this is real bad news. After SPLTMAX splits,
332  * we still cannot fit the key. say goodnight.
333  */
334 #ifdef BADMESS
335         (void) write(2, "sdbm: cannot insert after SPLTMAX attempts.\n", 44);
336 #endif
337         return 0;
338
339 }
340
341 /*
342  * the following two routines will break if
343  * deletions aren't taken into account. (ndbm bug)
344  */
345 datum
346 sdbm_firstkey(register DBM *db)
347 {
348         if (db == NULL)
349                 return errno = EINVAL, nullitem;
350 /*
351  * start at page 0
352  */
353         if (lseek(db->pagf, OFF_PAG(0), SEEK_SET) < 0
354             || read(db->pagf, db->pagbuf, PBLKSIZ) < 0)
355                 return ioerr(db), nullitem;
356         db->pagbno = 0;
357         db->blkptr = 0;
358         db->keyptr = 0;
359
360         return getnext(db);
361 }
362
363 datum
364 sdbm_nextkey(register DBM *db)
365 {
366         if (db == NULL)
367                 return errno = EINVAL, nullitem;
368         return getnext(db);
369 }
370
371 /*
372  * all important binary trie traversal
373  */
374 static int
375 getpage(register DBM *db, register long int hash)
376 {
377         register int hbit;
378         register long dbit;
379         register long pagb;
380
381         dbit = 0;
382         hbit = 0;
383         while (dbit < db->maxbno && getdbit(db, dbit))
384                 dbit = 2 * dbit + ((hash & (1 << hbit++)) ? 2 : 1);
385
386         debug(("dbit: %d...", dbit));
387
388         db->curbit = dbit;
389         db->hmask = masks[hbit];
390
391         pagb = hash & db->hmask;
392 /*
393  * see if the block we need is already in memory.
394  * note: this lookaside cache has about 10% hit rate.
395  */
396         if (pagb != db->pagbno) { 
397 /*
398  * note: here, we assume a "hole" is read as 0s.
399  * if not, must zero pagbuf first.
400  */
401                 if (lseek(db->pagf, OFF_PAG(pagb), SEEK_SET) < 0
402                     || read(db->pagf, db->pagbuf, PBLKSIZ) < 0)
403                         return 0;
404                 if (!chkpage(db->pagbuf))
405                         return 0;
406                 db->pagbno = pagb;
407
408                 debug(("pag read: %d\n", pagb));
409         }
410         return 1;
411 }
412
413 static int
414 getdbit(register DBM *db, register long int dbit)
415 {
416         register long c;
417         register long dirb;
418
419         c = dbit / BYTESIZ;
420         dirb = c / DBLKSIZ;
421
422         if (dirb != db->dirbno) {
423                 if (lseek(db->dirf, OFF_DIR(dirb), SEEK_SET) < 0
424                     || read(db->dirf, db->dirbuf, DBLKSIZ) < 0)
425                         return 0;
426                 db->dirbno = dirb;
427
428                 debug(("dir read: %d\n", dirb));
429         }
430
431         return db->dirbuf[c % DBLKSIZ] & (1 << dbit % BYTESIZ);
432 }
433
434 static int
435 setdbit(register DBM *db, register long int dbit)
436 {
437         register long c;
438         register long dirb;
439
440         c = dbit / BYTESIZ;
441         dirb = c / DBLKSIZ;
442
443         if (dirb != db->dirbno) {
444                 if (lseek(db->dirf, OFF_DIR(dirb), SEEK_SET) < 0
445                     || read(db->dirf, db->dirbuf, DBLKSIZ) < 0)
446                         return 0;
447                 db->dirbno = dirb;
448
449                 debug(("dir read: %d\n", dirb));
450         }
451
452         db->dirbuf[c % DBLKSIZ] |= (1 << dbit % BYTESIZ);
453
454         if (dbit >= db->maxbno)
455                 db->maxbno += DBLKSIZ * BYTESIZ;
456
457         if (lseek(db->dirf, OFF_DIR(dirb), SEEK_SET) < 0
458             || write(db->dirf, db->dirbuf, DBLKSIZ) < 0)
459                 return 0;
460
461         return 1;
462 }
463
464 /*
465  * getnext - get the next key in the page, and if done with
466  * the page, try the next page in sequence
467  */
468 static datum
469 getnext(register DBM *db)
470 {
471         datum key;
472
473         for (;;) {
474                 db->keyptr++;
475                 key = getnkey(db->pagbuf, db->keyptr);
476                 if (key.dptr != NULL)
477                         return key;
478 /*
479  * we either run out, or there is nothing on this page..
480  * try the next one... If we lost our position on the
481  * file, we will have to seek.
482  */
483                 db->keyptr = 0;
484                 if (db->pagbno != db->blkptr++)
485                         if (lseek(db->pagf, OFF_PAG(db->blkptr), SEEK_SET) < 0)
486                                 break;
487                 db->pagbno = db->blkptr;
488                 if (read(db->pagf, db->pagbuf, PBLKSIZ) <= 0)
489                         break;
490                 if (!chkpage(db->pagbuf))
491                         break;
492         }
493
494         return ioerr(db), nullitem;
495 }
496