diff options
Diffstat (limited to 'dbm/sdbm')
-rw-r--r-- | dbm/sdbm/sdbm.c | 584 | ||||
-rw-r--r-- | dbm/sdbm/sdbm_hash.c | 63 | ||||
-rw-r--r-- | dbm/sdbm/sdbm_lock.c | 79 | ||||
-rw-r--r-- | dbm/sdbm/sdbm_pair.c | 320 | ||||
-rw-r--r-- | dbm/sdbm/sdbm_pair.h | 40 | ||||
-rw-r--r-- | dbm/sdbm/sdbm_private.h | 84 | ||||
-rw-r--r-- | dbm/sdbm/sdbm_tune.h | 40 |
7 files changed, 1210 insertions, 0 deletions
diff --git a/dbm/sdbm/sdbm.c b/dbm/sdbm/sdbm.c new file mode 100644 index 0000000..a62b009 --- /dev/null +++ b/dbm/sdbm/sdbm.c @@ -0,0 +1,584 @@ +/* Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* + * sdbm - ndbm work-alike hashed database library + * based on Per-Aake Larson's Dynamic Hashing algorithms. BIT 18 (1978). + * author: oz@nexus.yorku.ca + * ex-public domain, ported to APR for Apache 2 + * core routines + */ + +#include "apr.h" +#include "apr_file_io.h" +#include "apr_strings.h" +#include "apr_errno.h" +#include "apr_sdbm.h" + +#include "sdbm_tune.h" +#include "sdbm_pair.h" +#include "sdbm_private.h" + +#include <string.h> /* for memset() */ +#include <stdlib.h> /* for malloc() and free() */ + +/* + * forward + */ +static int getdbit (apr_sdbm_t *, long); +static apr_status_t setdbit(apr_sdbm_t *, long); +static apr_status_t getpage(apr_sdbm_t *db, long, int, int); +static apr_status_t getnext(apr_sdbm_datum_t *key, apr_sdbm_t *db); +static apr_status_t makroom(apr_sdbm_t *, long, int); + +/* + * useful macros + */ +#define bad(x) ((x).dptr == NULL || (x).dsize <= 0) +#define exhash(item) sdbm_hash((item).dptr, (item).dsize) + +#define OFF_PAG(off) (apr_off_t) (off) * PBLKSIZ +#define OFF_DIR(off) (apr_off_t) (off) * DBLKSIZ + +static const long masks[] = { + 000000000000, 000000000001, 000000000003, 000000000007, + 000000000017, 000000000037, 000000000077, 000000000177, + 000000000377, 000000000777, 000000001777, 000000003777, + 000000007777, 000000017777, 000000037777, 000000077777, + 000000177777, 000000377777, 000000777777, 000001777777, + 000003777777, 000007777777, 000017777777, 000037777777, + 000077777777, 000177777777, 000377777777, 000777777777, + 001777777777, 003777777777, 007777777777, 017777777777 +}; + +const apr_sdbm_datum_t sdbm_nullitem = { NULL, 0 }; + +static apr_status_t database_cleanup(void *data) +{ + apr_sdbm_t *db = data; + + /* + * Can't rely on apr_sdbm_unlock, since it will merely + * decrement the refcnt if several locks are held. + */ + if (db->flags & (SDBM_SHARED_LOCK | SDBM_EXCLUSIVE_LOCK)) + (void) apr_file_unlock(db->dirf); + (void) apr_file_close(db->dirf); + (void) apr_file_close(db->pagf); + free(db); + + return APR_SUCCESS; +} + +static apr_status_t prep(apr_sdbm_t **pdb, const char *dirname, const char *pagname, + apr_int32_t flags, apr_fileperms_t perms, apr_pool_t *p) +{ + apr_sdbm_t *db; + apr_status_t status; + + *pdb = NULL; + + db = malloc(sizeof(*db)); + memset(db, 0, sizeof(*db)); + db->pagbno = -1L; + + db->pool = p; + + /* + * adjust user flags so that WRONLY becomes RDWR, + * as required by this package. Also set our internal + * flag for RDONLY if needed. + */ + if (!(flags & APR_FOPEN_WRITE)) { + db->flags |= SDBM_RDONLY; + } + + /* + * adjust the file open flags so that we handle locking + * on our own (don't rely on any locking behavior within + * an apr_file_t, in case it's ever introduced, and set + * our own flag. + */ + if (flags & APR_FOPEN_SHARELOCK) { + db->flags |= SDBM_SHARED; + flags &= ~APR_FOPEN_SHARELOCK; + } + + flags |= APR_FOPEN_BINARY | APR_FOPEN_READ; + + /* + * open the files in sequence, and stat the dirfile. + * If we fail anywhere, undo everything, return NULL. + */ + + if ((status = apr_file_open(&db->dirf, dirname, flags, perms, p)) + != APR_SUCCESS) + goto error; + + if ((status = apr_file_open(&db->pagf, pagname, flags, perms, p)) + != APR_SUCCESS) + goto error; + + if ((status = apr_sdbm_lock(db, (db->flags & SDBM_RDONLY) + ? APR_FLOCK_SHARED + : APR_FLOCK_EXCLUSIVE)) + != APR_SUCCESS) + goto error; + + /* apr_pcalloc zeroed the buffers + * apr_sdbm_lock stated the dirf->size and invalidated the cache + */ + + /* + * if we are opened in SHARED mode, unlock ourself + */ + if (db->flags & SDBM_SHARED) + if ((status = apr_sdbm_unlock(db)) != APR_SUCCESS) + goto error; + + /* make sure that we close the database at some point */ + apr_pool_cleanup_register(p, db, database_cleanup, apr_pool_cleanup_null); + + /* Done! */ + *pdb = db; + return APR_SUCCESS; + +error: + if (db->dirf && db->pagf) + (void) apr_sdbm_unlock(db); + if (db->dirf != NULL) + (void) apr_file_close(db->dirf); + if (db->pagf != NULL) { + (void) apr_file_close(db->pagf); + } + free(db); + return status; +} + +APU_DECLARE(apr_status_t) apr_sdbm_open(apr_sdbm_t **db, const char *file, + apr_int32_t flags, + apr_fileperms_t perms, apr_pool_t *p) +{ + char *dirname = apr_pstrcat(p, file, APR_SDBM_DIRFEXT, NULL); + char *pagname = apr_pstrcat(p, file, APR_SDBM_PAGFEXT, NULL); + + return prep(db, dirname, pagname, flags, perms, p); +} + +APU_DECLARE(apr_status_t) apr_sdbm_close(apr_sdbm_t *db) +{ + return apr_pool_cleanup_run(db->pool, db, database_cleanup); +} + +APU_DECLARE(apr_status_t) apr_sdbm_fetch(apr_sdbm_t *db, apr_sdbm_datum_t *val, + apr_sdbm_datum_t key) +{ + apr_status_t status; + + if (db == NULL || bad(key)) + return APR_EINVAL; + + if ((status = apr_sdbm_lock(db, APR_FLOCK_SHARED)) != APR_SUCCESS) + return status; + + if ((status = getpage(db, exhash(key), 0, 1)) == APR_SUCCESS) { + *val = getpair(db->pagbuf, key); + /* ### do we want a not-found result? */ + } + + (void) apr_sdbm_unlock(db); + + return status; +} + +static apr_status_t write_page(apr_sdbm_t *db, const char *buf, long pagno) +{ + apr_status_t status; + apr_off_t off = OFF_PAG(pagno); + + if ((status = apr_file_seek(db->pagf, APR_SET, &off)) == APR_SUCCESS) + status = apr_file_write_full(db->pagf, buf, PBLKSIZ, NULL); + + return status; +} + +APU_DECLARE(apr_status_t) apr_sdbm_delete(apr_sdbm_t *db, + const apr_sdbm_datum_t key) +{ + apr_status_t status; + + if (db == NULL || bad(key)) + return APR_EINVAL; + if (apr_sdbm_rdonly(db)) + return APR_EINVAL; + + if ((status = apr_sdbm_lock(db, APR_FLOCK_EXCLUSIVE)) != APR_SUCCESS) + return status; + + if ((status = getpage(db, exhash(key), 0, 1)) == APR_SUCCESS) { + if (!delpair(db->pagbuf, key)) + /* ### should we define some APRUTIL codes? */ + status = APR_EGENERAL; + else + status = write_page(db, db->pagbuf, db->pagbno); + } + + (void) apr_sdbm_unlock(db); + + return status; +} + +APU_DECLARE(apr_status_t) apr_sdbm_store(apr_sdbm_t *db, apr_sdbm_datum_t key, + apr_sdbm_datum_t val, int flags) +{ + int need; + register long hash; + apr_status_t status; + + if (db == NULL || bad(key)) + return APR_EINVAL; + if (apr_sdbm_rdonly(db)) + return APR_EINVAL; + need = key.dsize + val.dsize; + /* + * is the pair too big (or too small) for this database ?? + */ + if (need < 0 || need > PAIRMAX) + return APR_EINVAL; + + if ((status = apr_sdbm_lock(db, APR_FLOCK_EXCLUSIVE)) != APR_SUCCESS) + return status; + + if ((status = getpage(db, (hash = exhash(key)), 0, 1)) == APR_SUCCESS) { + + /* + * if we need to replace, delete the key/data pair + * first. If it is not there, ignore. + */ + if (flags == APR_SDBM_REPLACE) + (void) delpair(db->pagbuf, key); + else if (!(flags & APR_SDBM_INSERTDUP) && duppair(db->pagbuf, key)) { + status = APR_EEXIST; + goto error; + } + /* + * if we do not have enough room, we have to split. + */ + if (!fitpair(db->pagbuf, need)) + if ((status = makroom(db, hash, need)) != APR_SUCCESS) + goto error; + /* + * we have enough room or split is successful. insert the key, + * and update the page file. + */ + (void) putpair(db->pagbuf, key, val); + + status = write_page(db, db->pagbuf, db->pagbno); + } + +error: + (void) apr_sdbm_unlock(db); + + return status; +} + +/* + * makroom - make room by splitting the overfull page + * this routine will attempt to make room for SPLTMAX times before + * giving up. + */ +static apr_status_t makroom(apr_sdbm_t *db, long hash, int need) +{ + long newp; + char twin[PBLKSIZ]; + char *pag = db->pagbuf; + char *new = twin; + register int smax = SPLTMAX; + apr_status_t status; + + do { + /* + * split the current page + */ + (void) splpage(pag, new, db->hmask + 1); + /* + * address of the new page + */ + newp = (hash & db->hmask) | (db->hmask + 1); + + /* + * write delay, read avoidence/cache shuffle: + * select the page for incoming pair: if key is to go to the new page, + * write out the previous one, and copy the new one over, thus making + * it the current page. If not, simply write the new page, and we are + * still looking at the page of interest. current page is not updated + * here, as sdbm_store will do so, after it inserts the incoming pair. + */ + if (hash & (db->hmask + 1)) { + if ((status = write_page(db, db->pagbuf, db->pagbno)) + != APR_SUCCESS) + return status; + + db->pagbno = newp; + (void) memcpy(pag, new, PBLKSIZ); + } + else { + if ((status = write_page(db, new, newp)) != APR_SUCCESS) + return status; + } + + if ((status = setdbit(db, db->curbit)) != APR_SUCCESS) + return status; + /* + * see if we have enough room now + */ + if (fitpair(pag, need)) + return APR_SUCCESS; + /* + * try again... update curbit and hmask as getpage would have + * done. because of our update of the current page, we do not + * need to read in anything. BUT we have to write the current + * [deferred] page out, as the window of failure is too great. + */ + db->curbit = 2 * db->curbit + + ((hash & (db->hmask + 1)) ? 2 : 1); + db->hmask |= db->hmask + 1; + + if ((status = write_page(db, db->pagbuf, db->pagbno)) + != APR_SUCCESS) + return status; + + } while (--smax); + + /* + * if we are here, this is real bad news. After SPLTMAX splits, + * we still cannot fit the key. say goodnight. + */ +#if 0 + (void) write(2, "sdbm: cannot insert after SPLTMAX attempts.\n", 44); +#endif + /* ### ENOSPC not really appropriate but better than nothing */ + return APR_ENOSPC; + +} + +/* Reads 'len' bytes from file 'f' at offset 'off' into buf. + * 'off' is given relative to the start of the file. + * If 'create' is asked and EOF is returned while reading, this is taken + * as success (i.e. a cleared buffer is returned). + */ +static apr_status_t read_from(apr_file_t *f, void *buf, + apr_off_t off, apr_size_t len, + int create) +{ + apr_status_t status; + + if ((status = apr_file_seek(f, APR_SET, &off)) != APR_SUCCESS || + ((status = apr_file_read_full(f, buf, len, NULL)) != APR_SUCCESS)) { + /* if EOF is reached, pretend we read all zero's */ + if (status == APR_EOF && create) { + memset(buf, 0, len); + status = APR_SUCCESS; + } + } + + return status; +} + +/* + * the following two routines will break if + * deletions aren't taken into account. (ndbm bug) + */ +APU_DECLARE(apr_status_t) apr_sdbm_firstkey(apr_sdbm_t *db, + apr_sdbm_datum_t *key) +{ + apr_status_t status; + + if ((status = apr_sdbm_lock(db, APR_FLOCK_SHARED)) != APR_SUCCESS) + return status; + + /* + * start at page 0 + */ + if ((status = getpage(db, 0, 1, 1)) == APR_SUCCESS) { + db->blkptr = 0; + db->keyptr = 0; + status = getnext(key, db); + } + + (void) apr_sdbm_unlock(db); + + return status; +} + +APU_DECLARE(apr_status_t) apr_sdbm_nextkey(apr_sdbm_t *db, + apr_sdbm_datum_t *key) +{ + apr_status_t status; + + if ((status = apr_sdbm_lock(db, APR_FLOCK_SHARED)) != APR_SUCCESS) + return status; + + status = getnext(key, db); + + (void) apr_sdbm_unlock(db); + + return status; +} + +/* + * all important binary tree traversal + */ +static apr_status_t getpage(apr_sdbm_t *db, long hash, int by_num, int create) +{ + apr_status_t status; + register long pagb; + + if (by_num) { + pagb = hash; + } + else { + register int hbit = 0; + register long dbit = 0; + + while (dbit < db->maxbno && getdbit(db, dbit)) + dbit = 2 * dbit + ((hash & (1 << hbit++)) ? 2 : 1); + debug(("dbit: %d...", dbit)); + + db->curbit = dbit; + db->hmask = masks[hbit]; + + pagb = hash & db->hmask; + } + + /* + * see if the block we need is already in memory. + * note: this lookaside cache has about 10% hit rate. + */ + if (pagb != db->pagbno) { + /* + * note: here, we assume a "hole" is read as 0s. + * if not, must zero pagbuf first. + * ### joe: this assumption was surely never correct? but + * ### we make it so in read_from anyway. + */ + if ((status = read_from(db->pagf, db->pagbuf, + OFF_PAG(pagb), PBLKSIZ, + create)) != APR_SUCCESS) + return status; + + if (!chkpage(db->pagbuf)) + return APR_ENOSPC; /* ### better error? */ + + db->pagbno = pagb; + + debug(("pag read: %d\n", pagb)); + } + return APR_SUCCESS; +} + +static int getdbit(apr_sdbm_t *db, long dbit) +{ + register long c; + register long dirb; + + c = dbit / BYTESIZ; + dirb = c / DBLKSIZ; + + if (dirb != db->dirbno) { + if (read_from(db->dirf, db->dirbuf, + OFF_DIR(dirb), DBLKSIZ, + 1) != APR_SUCCESS) + return 0; + + db->dirbno = dirb; + + debug(("dir read: %d\n", dirb)); + } + + return db->dirbuf[c % DBLKSIZ] & (1 << dbit % BYTESIZ); +} + +static apr_status_t setdbit(apr_sdbm_t *db, long dbit) +{ + register long c; + register long dirb; + apr_status_t status; + apr_off_t off; + + c = dbit / BYTESIZ; + dirb = c / DBLKSIZ; + + if (dirb != db->dirbno) { + if ((status = read_from(db->dirf, db->dirbuf, + OFF_DIR(dirb), DBLKSIZ, + 1)) != APR_SUCCESS) + return status; + + db->dirbno = dirb; + + debug(("dir read: %d\n", dirb)); + } + + db->dirbuf[c % DBLKSIZ] |= (1 << dbit % BYTESIZ); + + if (dbit >= db->maxbno) + db->maxbno += DBLKSIZ * BYTESIZ; + + off = OFF_DIR(dirb); + if ((status = apr_file_seek(db->dirf, APR_SET, &off)) == APR_SUCCESS) + status = apr_file_write_full(db->dirf, db->dirbuf, DBLKSIZ, NULL); + + return status; +} + +/* +* getnext - get the next key in the page, and if done with +* the page, try the next page in sequence +*/ +static apr_status_t getnext(apr_sdbm_datum_t *key, apr_sdbm_t *db) +{ + apr_status_t status; + for (;;) { + db->keyptr++; + *key = getnkey(db->pagbuf, db->keyptr); + if (key->dptr != NULL) + return APR_SUCCESS; + /* + * we either run out, or there is nothing on this page.. + * try the next one... If we lost our position on the + * file, we will have to seek. + */ + db->blkptr++; + db->keyptr = 0; + + /* ### EOF acceptable here too? */ + if ((status = getpage(db, db->blkptr, 1, 0)) != APR_SUCCESS) + return status; + } + + /* NOTREACHED */ +} + + +APU_DECLARE(int) apr_sdbm_rdonly(apr_sdbm_t *db) +{ + /* ### Should we return true if the first lock is a share lock, + * to reflect that apr_sdbm_store and apr_sdbm_delete will fail? + */ + return (db->flags & SDBM_RDONLY) != 0; +} + diff --git a/dbm/sdbm/sdbm_hash.c b/dbm/sdbm/sdbm_hash.c new file mode 100644 index 0000000..e4d7517 --- /dev/null +++ b/dbm/sdbm/sdbm_hash.c @@ -0,0 +1,63 @@ +/* Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* + * sdbm - ndbm work-alike hashed database library + * based on Per-Aake Larson's Dynamic Hashing algorithms. BIT 18 (1978). + * author: oz@nexus.yorku.ca + * status: ex-public domain. keep it that way. + * + * hashing routine + */ + +#include "apr_sdbm.h" +#include "sdbm_private.h" + +/* + * polynomial conversion ignoring overflows + * [this seems to work remarkably well, in fact better + * then the ndbm hash function. Replace at your own risk] + * use: 65599 nice. + * 65587 even better. + */ +long sdbm_hash(const char *str, int len) +{ + register unsigned long n = 0; + +#define DUFF /* go ahead and use the loop-unrolled version */ +#ifdef DUFF + +#define HASHC n = *str++ + 65599 * n + + if (len > 0) { + register int loop = (len + 8 - 1) >> 3; + + switch(len & (8 - 1)) { + case 0: do { + HASHC; case 7: HASHC; + case 6: HASHC; case 5: HASHC; + case 4: HASHC; case 3: HASHC; + case 2: HASHC; case 1: HASHC; + } while (--loop); + } + + } +#else + while (len--) + n = *str++ + 65599 * n; +#endif + return n; +} diff --git a/dbm/sdbm/sdbm_lock.c b/dbm/sdbm/sdbm_lock.c new file mode 100644 index 0000000..7d62ffd --- /dev/null +++ b/dbm/sdbm/sdbm_lock.c @@ -0,0 +1,79 @@ +/* Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "apr_file_info.h" +#include "apr_file_io.h" +#include "apr_sdbm.h" + +#include "sdbm_private.h" +#include "sdbm_tune.h" + +/* NOTE: this function may block until it acquires the lock */ +APU_DECLARE(apr_status_t) apr_sdbm_lock(apr_sdbm_t *db, int type) +{ + apr_status_t status; + int lock_type = type & APR_FLOCK_TYPEMASK; + + if (!(lock_type == APR_FLOCK_SHARED || lock_type == APR_FLOCK_EXCLUSIVE)) + return APR_EINVAL; + + if (db->flags & SDBM_EXCLUSIVE_LOCK) { + ++db->lckcnt; + return APR_SUCCESS; + } + else if (db->flags & SDBM_SHARED_LOCK) { + /* + * Cannot promote a shared lock to an exlusive lock + * in a cross-platform compatibile manner. + */ + if (type == APR_FLOCK_EXCLUSIVE) + return APR_EINVAL; + ++db->lckcnt; + return APR_SUCCESS; + } + /* + * zero size: either a fresh database, or one with a single, + * unsplit data page: dirpage is all zeros. + */ + if ((status = apr_file_lock(db->dirf, type)) == APR_SUCCESS) + { + apr_finfo_t finfo; + if ((status = apr_file_info_get(&finfo, APR_FINFO_SIZE, db->dirf)) + != APR_SUCCESS) { + (void) apr_file_unlock(db->dirf); + return status; + } + + SDBM_INVALIDATE_CACHE(db, finfo); + + ++db->lckcnt; + if (type == APR_FLOCK_SHARED) + db->flags |= SDBM_SHARED_LOCK; + else if (type == APR_FLOCK_EXCLUSIVE) + db->flags |= SDBM_EXCLUSIVE_LOCK; + } + return status; +} + +APU_DECLARE(apr_status_t) apr_sdbm_unlock(apr_sdbm_t *db) +{ + if (!(db->flags & (SDBM_SHARED_LOCK | SDBM_EXCLUSIVE_LOCK))) + return APR_EINVAL; + if (--db->lckcnt > 0) + return APR_SUCCESS; + db->flags &= ~(SDBM_SHARED_LOCK | SDBM_EXCLUSIVE_LOCK); + return apr_file_unlock(db->dirf); +} diff --git a/dbm/sdbm/sdbm_pair.c b/dbm/sdbm/sdbm_pair.c new file mode 100644 index 0000000..50d7965 --- /dev/null +++ b/dbm/sdbm/sdbm_pair.c @@ -0,0 +1,320 @@ +/* Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* + * sdbm - ndbm work-alike hashed database library + * based on Per-Aake Larson's Dynamic Hashing algorithms. BIT 18 (1978). + * author: oz@nexus.yorku.ca + * status: ex-public domain. + * + * page-level routines + */ + +#include "apr_sdbm.h" + +#include "sdbm_tune.h" +#include "sdbm_pair.h" +#include "sdbm_private.h" + +#include <string.h> /* for memset() */ + + +#define exhash(item) sdbm_hash((item).dptr, (item).dsize) + +/* + * forward + */ +static int seepair(char *, int, char *, int); + +/* + * page format: + * +------------------------------+ + * ino | n | keyoff | datoff | keyoff | + * +------------+--------+--------+ + * | datoff | - - - ----> | + * +--------+---------------------+ + * | F R E E A R E A | + * +--------------+---------------+ + * | <---- - - - | data | + * +--------+-----+----+----------+ + * | key | data | key | + * +--------+----------+----------+ + * + * calculating the offsets for free area: if the number + * of entries (ino[0]) is zero, the offset to the END of + * the free area is the block size. Otherwise, it is the + * nth (ino[ino[0]]) entry's offset. + */ + +int +fitpair(pag, need) +char *pag; +int need; +{ + register int n; + register int off; + register int avail; + register short *ino = (short *) pag; + + off = ((n = ino[0]) > 0) ? ino[n] : PBLKSIZ; + avail = off - (n + 1) * sizeof(short); + need += 2 * sizeof(short); + + debug(("avail %d need %d\n", avail, need)); + + return need <= avail; +} + +void +putpair(pag, key, val) +char *pag; +apr_sdbm_datum_t key; +apr_sdbm_datum_t val; +{ + register int n; + register int off; + register short *ino = (short *) pag; + + off = ((n = ino[0]) > 0) ? ino[n] : PBLKSIZ; +/* + * enter the key first + */ + off -= key.dsize; + (void) memcpy(pag + off, key.dptr, key.dsize); + ino[n + 1] = off; +/* + * now the data + */ + off -= val.dsize; + (void) memcpy(pag + off, val.dptr, val.dsize); + ino[n + 2] = off; +/* + * adjust item count + */ + ino[0] += 2; +} + +apr_sdbm_datum_t +getpair(pag, key) +char *pag; +apr_sdbm_datum_t key; +{ + register int i; + register int n; + apr_sdbm_datum_t val; + register short *ino = (short *) pag; + + if ((n = ino[0]) == 0) + return sdbm_nullitem; + + if ((i = seepair(pag, n, key.dptr, key.dsize)) == 0) + return sdbm_nullitem; + + val.dptr = pag + ino[i + 1]; + val.dsize = ino[i] - ino[i + 1]; + return val; +} + +int +duppair(pag, key) +char *pag; +apr_sdbm_datum_t key; +{ + register short *ino = (short *) pag; + return ino[0] > 0 && seepair(pag, ino[0], key.dptr, key.dsize) > 0; +} + +apr_sdbm_datum_t +getnkey(pag, num) +char *pag; +int num; +{ + apr_sdbm_datum_t key; + register int off; + register short *ino = (short *) pag; + + num = num * 2 - 1; + if (ino[0] == 0 || num > ino[0]) + return sdbm_nullitem; + + off = (num > 1) ? ino[num - 1] : PBLKSIZ; + + key.dptr = pag + ino[num]; + key.dsize = off - ino[num]; + + return key; +} + +int +delpair(pag, key) +char *pag; +apr_sdbm_datum_t key; +{ + register int n; + register int i; + register short *ino = (short *) pag; + + if ((n = ino[0]) == 0) + return 0; + + if ((i = seepair(pag, n, key.dptr, key.dsize)) == 0) + return 0; +/* + * found the key. if it is the last entry + * [i.e. i == n - 1] we just adjust the entry count. + * hard case: move all data down onto the deleted pair, + * shift offsets onto deleted offsets, and adjust them. + * [note: 0 < i < n] + */ + if (i < n - 1) { + register int m; + register char *dst = pag + (i == 1 ? PBLKSIZ : ino[i - 1]); + register char *src = pag + ino[i + 1]; + register short zoo = (short) (dst - src); + + debug(("free-up %d ", zoo)); +/* + * shift data/keys down + */ + m = ino[i + 1] - ino[n]; + +#undef DUFF /* just use memmove. it should be plenty fast. */ +#ifdef DUFF +#define MOVB *--dst = *--src + + if (m > 0) { + register int loop = (m + 8 - 1) >> 3; + + switch (m & (8 - 1)) { + case 0: do { + MOVB; case 7: MOVB; + case 6: MOVB; case 5: MOVB; + case 4: MOVB; case 3: MOVB; + case 2: MOVB; case 1: MOVB; + } while (--loop); + } + } +#else + dst -= m; + src -= m; + memmove(dst, src, m); +#endif + +/* + * adjust offset index up + */ + while (i < n - 1) { + ino[i] = ino[i + 2] + zoo; + i++; + } + } + ino[0] -= 2; + return 1; +} + +/* + * search for the key in the page. + * return offset index in the range 0 < i < n. + * return 0 if not found. + */ +static int +seepair(pag, n, key, siz) +char *pag; +register int n; +register char *key; +register int siz; +{ + register int i; + register int off = PBLKSIZ; + register short *ino = (short *) pag; + + for (i = 1; i < n; i += 2) { + if (siz == off - ino[i] && + memcmp(key, pag + ino[i], siz) == 0) + return i; + off = ino[i + 1]; + } + return 0; +} + +void +splpage(pag, new, sbit) +char *pag; +char *new; +long sbit; +{ + apr_sdbm_datum_t key; + apr_sdbm_datum_t val; + + register int n; + register int off = PBLKSIZ; + char cur[PBLKSIZ]; + register short *ino = (short *) cur; + + (void) memcpy(cur, pag, PBLKSIZ); + (void) memset(pag, 0, PBLKSIZ); + (void) memset(new, 0, PBLKSIZ); + + n = ino[0]; + for (ino++; n > 0; ino += 2) { + key.dptr = cur + ino[0]; + key.dsize = off - ino[0]; + val.dptr = cur + ino[1]; + val.dsize = ino[0] - ino[1]; +/* + * select the page pointer (by looking at sbit) and insert + */ + (void) putpair((exhash(key) & sbit) ? new : pag, key, val); + + off = ino[1]; + n -= 2; + } + + debug(("%d split %d/%d\n", ((short *) cur)[0] / 2, + ((short *) new)[0] / 2, + ((short *) pag)[0] / 2)); +} + +/* + * check page sanity: + * number of entries should be something + * reasonable, and all offsets in the index should be in order. + * this could be made more rigorous. + */ +int +chkpage(pag) +char *pag; +{ + register int n; + register int off; + register short *ino = (short *) pag; + + if ((n = ino[0]) < 0 || n > PBLKSIZ / sizeof(short)) + return 0; + + if (n > 0) { + off = PBLKSIZ; + for (ino++; n > 0; ino += 2) { + if (ino[0] < 0 || ino[0] > off || + ino[1] < 0 || ino[1] > off || + ino[1] > ino[0]) + return 0; + off = ino[1]; + n -= 2; + } + } + return 1; +} diff --git a/dbm/sdbm/sdbm_pair.h b/dbm/sdbm/sdbm_pair.h new file mode 100644 index 0000000..222c5e1 --- /dev/null +++ b/dbm/sdbm/sdbm_pair.h @@ -0,0 +1,40 @@ +/* Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef SDBM_PAIR_H +#define SDBM_PAIR_H + +/* Mini EMBED (pair.c) */ +#define chkpage apu__sdbm_chkpage +#define delpair apu__sdbm_delpair +#define duppair apu__sdbm_duppair +#define fitpair apu__sdbm_fitpair +#define getnkey apu__sdbm_getnkey +#define getpair apu__sdbm_getpair +#define putpair apu__sdbm_putpair +#define splpage apu__sdbm_splpage + +int fitpair(char *, int); +void putpair(char *, apr_sdbm_datum_t, apr_sdbm_datum_t); +apr_sdbm_datum_t getpair(char *, apr_sdbm_datum_t); +int delpair(char *, apr_sdbm_datum_t); +int chkpage (char *); +apr_sdbm_datum_t getnkey(char *, int); +void splpage(char *, char *, long); +int duppair(char *, apr_sdbm_datum_t); + +#endif /* SDBM_PAIR_H */ + diff --git a/dbm/sdbm/sdbm_private.h b/dbm/sdbm/sdbm_private.h new file mode 100644 index 0000000..f5d1ae0 --- /dev/null +++ b/dbm/sdbm/sdbm_private.h @@ -0,0 +1,84 @@ +/* Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* + * sdbm - ndbm work-alike hashed database library + * based on Per-Ake Larson's Dynamic Hashing algorithms. BIT 18 (1978). + * author: oz@nexus.yorku.ca + */ + +#ifndef SDBM_PRIVATE_H +#define SDBM_PRIVATE_H + +#include "apr.h" +#include "apr_pools.h" +#include "apr_file_io.h" +#include "apr_errno.h" /* for apr_status_t */ + +#if 0 +/* if the block/page size is increased, it breaks perl apr_sdbm_t compatibility */ +#define DBLKSIZ 16384 +#define PBLKSIZ 8192 +#define PAIRMAX 8008 /* arbitrary on PBLKSIZ-N */ +#else +#define DBLKSIZ 4096 +#define PBLKSIZ 1024 +#define PAIRMAX 1008 /* arbitrary on PBLKSIZ-N */ +#endif +#define SPLTMAX 10 /* maximum allowed splits */ + +/* for apr_sdbm_t.flags */ +#define SDBM_RDONLY 0x1 /* data base open read-only */ +#define SDBM_SHARED 0x2 /* data base open for sharing */ +#define SDBM_SHARED_LOCK 0x4 /* data base locked for shared read */ +#define SDBM_EXCLUSIVE_LOCK 0x8 /* data base locked for write */ + +struct apr_sdbm_t { + apr_pool_t *pool; + apr_file_t *dirf; /* directory file descriptor */ + apr_file_t *pagf; /* page file descriptor */ + apr_int32_t flags; /* status/error flags, see below */ + long maxbno; /* size of dirfile in bits */ + long curbit; /* current bit number */ + long hmask; /* current hash mask */ + long blkptr; /* current block for nextkey */ + int keyptr; /* current key for nextkey */ + long blkno; /* current page to read/write */ + long pagbno; /* current page in pagbuf */ + char pagbuf[PBLKSIZ]; /* page file block buffer */ + long dirbno; /* current block in dirbuf */ + char dirbuf[DBLKSIZ]; /* directory file block buffer */ + int lckcnt; /* number of calls to sdbm_lock */ +}; + + +#define sdbm_hash apu__sdbm_hash +#define sdbm_nullitem apu__sdbm_nullitem + +extern const apr_sdbm_datum_t sdbm_nullitem; + +long sdbm_hash(const char *str, int len); + +/* + * zero the cache + */ +#define SDBM_INVALIDATE_CACHE(db, finfo) \ + do { db->dirbno = (!finfo.size) ? 0 : -1; \ + db->pagbno = -1; \ + db->maxbno = (long)(finfo.size * BYTESIZ); \ + } while (0); + +#endif /* SDBM_PRIVATE_H */ diff --git a/dbm/sdbm/sdbm_tune.h b/dbm/sdbm/sdbm_tune.h new file mode 100644 index 0000000..9bf3d09 --- /dev/null +++ b/dbm/sdbm/sdbm_tune.h @@ -0,0 +1,40 @@ +/* Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* + * sdbm - ndbm work-alike hashed database library + * tuning and portability constructs [not nearly enough] + * author: oz@nexus.yorku.ca + */ + +#ifndef SDBM_TUNE_H +#define SDBM_TUNE_H + +#include "apr_errno.h" + +/* ### this might be better off as sizeof(char *) */ +#define BYTESIZ 8 + +/* + * misc + */ +#ifdef DEBUG +#define debug(x) printf x +#else +#define debug(x) +#endif + +#endif /* SDBM_TUNE_H */ |