diff options
-rw-r--r-- | doc/gdbm.texi | 10 | ||||
-rw-r--r-- | src/findkey.c | 34 | ||||
-rw-r--r-- | src/gdbmdelete.c | 6 | ||||
-rw-r--r-- | src/gdbmexists.c | 11 | ||||
-rw-r--r-- | src/gdbmfetch.c | 3 | ||||
-rw-r--r-- | src/gdbmseq.c | 4 | ||||
-rw-r--r-- | src/gdbmstore.c | 3 |
7 files changed, 40 insertions, 31 deletions
diff --git a/doc/gdbm.texi b/doc/gdbm.texi index 6fad9d9..4c7c5c0 100644 --- a/doc/gdbm.texi +++ b/doc/gdbm.texi @@ -464,8 +464,14 @@ else You may also search for a particular key without retrieving it: @deftypefn {gdbm interface} int gdbm_exists (GDBM_FILE @var{dbf}, datum @var{key}) -Returns @samp{true} (@samp{1}) if the @var{key} exists in @var{dbf} -and @samp{false} (@samp{0}) otherwise. +Checks whether the @var{key} exists in the database @var{dbf}. + +If @var{key} is found, returns @samp{true} (@samp{1}). If it is not +found, returns @samp{false} (@samp{0}) and sets @code{gdbm_errno} to +@samp{GDBM_NO_ERROR} (@samp{0}). + +On error, returns @samp{0} and sets @code{gdbm_errno} to a +non-@samp{0} error code. The parameters are: diff --git a/src/findkey.c b/src/findkey.c index b3ebe0b..b4b3bd2 100644 --- a/src/findkey.c +++ b/src/findkey.c @@ -69,47 +69,52 @@ _gdbm_read_entry (GDBM_FILE dbf, int elem_loc) return data_ca->dptr; } - - /* Find the KEY in the file and get ready to read the associated data. The return value is the location in the current hash bucket of the KEY's - entry. If it is found, a pointer to the data and the key are returned - in DPTR. If it is not found, the value -1 is returned and gdbm_errno is - set to GDBM_ITEM_NOT_FOUND. Since find key computes the hash value of key, - that value is returned in NEW_HASH_VAL. */ + entry. If it is found, additional data are returned as follows: + + If RET_DPTR is not NULL, a pointer to the actual data is stored in it. + If RET_HASH_VAL is not NULL, it is assigned the actual hash value. + + If KEY is not found, the value -1 is returned and gdbm_errno is + set to GDBM_ITEM_NOT_FOUND. */ int -_gdbm_findkey (GDBM_FILE dbf, datum key, char **dptr, int *new_hash_val) +_gdbm_findkey (GDBM_FILE dbf, datum key, char **ret_dptr, int *ret_hash_val) { int bucket_hash_val; /* The hash value from the bucket. */ + int new_hash_val; /* Computed hash value for the key */ char *file_key; /* The complete key as stored in the file. */ int elem_loc; /* The location in the bucket. */ int home_loc; /* The home location in the bucket. */ int key_size; /* Size of the key on the file. */ /* Compute hash value and load proper bucket. */ - *new_hash_val = _gdbm_hash (key); - _gdbm_get_bucket (dbf, *new_hash_val>> (31-dbf->header->dir_bits)); + new_hash_val = _gdbm_hash (key); + if (ret_hash_val) + *ret_hash_val = new_hash_val; + _gdbm_get_bucket (dbf, new_hash_val>> (31-dbf->header->dir_bits)); /* Is the element the last one found for this bucket? */ if (dbf->cache_entry->ca_data.elem_loc != -1 - && *new_hash_val == dbf->cache_entry->ca_data.hash_val + && new_hash_val == dbf->cache_entry->ca_data.hash_val && dbf->cache_entry->ca_data.key_size == key.dsize && dbf->cache_entry->ca_data.dptr != NULL && memcmp (dbf->cache_entry->ca_data.dptr, key.dptr, key.dsize) == 0) { /* This is it. Return the cache pointer. */ - *dptr = dbf->cache_entry->ca_data.dptr+key.dsize; + if (ret_dptr) + *ret_dptr = dbf->cache_entry->ca_data.dptr+key.dsize; return dbf->cache_entry->ca_data.elem_loc; } /* It is not the cached value, search for element in the bucket. */ - elem_loc = *new_hash_val % dbf->header->bucket_elems; + elem_loc = new_hash_val % dbf->header->bucket_elems; home_loc = elem_loc; bucket_hash_val = dbf->bucket->h_table[elem_loc].hash_value; while (bucket_hash_val != -1) { key_size = dbf->bucket->h_table[elem_loc].key_size; - if (bucket_hash_val != *new_hash_val + if (bucket_hash_val != new_hash_val || key_size != key.dsize || memcmp (dbf->bucket->h_table[elem_loc].key_start, key.dptr, (SMALL < key_size ? SMALL : key_size)) != 0) @@ -128,7 +133,8 @@ _gdbm_findkey (GDBM_FILE dbf, datum key, char **dptr, int *new_hash_val) if (memcmp (file_key, key.dptr, key_size) == 0) { /* This is the item. */ - *dptr = file_key+key.dsize; + if (ret_dptr) + *ret_dptr = file_key + key.dsize; return elem_loc; } else diff --git a/src/gdbmdelete.c b/src/gdbmdelete.c index b308c51..4396b14 100644 --- a/src/gdbmdelete.c +++ b/src/gdbmdelete.c @@ -33,9 +33,7 @@ gdbm_delete (GDBM_FILE dbf, datum key) int last_loc; /* Last location emptied by the delete. */ int home; /* Home position of an item. */ bucket_element elem; /* The element to be deleted. */ - char *find_data; /* Return pointer from findkey. */ - int hash_val; /* Returned by findkey. */ - off_t free_adr; /* Temporary stroage for address and size. */ + off_t free_adr; /* Temporary storage for address and size. */ int free_size; /* First check to make sure this guy is a writer. */ @@ -49,7 +47,7 @@ gdbm_delete (GDBM_FILE dbf, datum key) gdbm_errno = GDBM_NO_ERROR; /* Find the item. */ - elem_loc = _gdbm_findkey (dbf, key, &find_data, &hash_val); + elem_loc = _gdbm_findkey (dbf, key, NULL, NULL); if (elem_loc == -1) return -1; diff --git a/src/gdbmexists.c b/src/gdbmexists.c index c162037..9bbd308 100644 --- a/src/gdbmexists.c +++ b/src/gdbmexists.c @@ -27,8 +27,11 @@ int gdbm_exists (GDBM_FILE dbf, datum key) { - char *find_data; /* Dummy */ - int hash_val; /* Dummy */ - - return (_gdbm_findkey (dbf, key, &find_data, &hash_val) >= 0); + if (_gdbm_findkey (dbf, key, NULL, NULL) < 0) + { + if (gdbm_errno == GDBM_ITEM_NOT_FOUND) + gdbm_errno = GDBM_NO_ERROR; + return 0; + } + return 1; } diff --git a/src/gdbmfetch.c b/src/gdbmfetch.c index f9a2e91..a765c52 100644 --- a/src/gdbmfetch.c +++ b/src/gdbmfetch.c @@ -32,7 +32,6 @@ gdbm_fetch (GDBM_FILE dbf, datum key) datum return_val; /* The return value. */ int elem_loc; /* The location in the bucket. */ char *find_data; /* Returned from find_key. */ - int hash_val; /* Returned from find_key. */ /* Set the default return value. */ return_val.dptr = NULL; @@ -42,7 +41,7 @@ gdbm_fetch (GDBM_FILE dbf, datum key) gdbm_errno = GDBM_NO_ERROR; /* Find the key and return a pointer to the data. */ - elem_loc = _gdbm_findkey (dbf, key, &find_data, &hash_val); + elem_loc = _gdbm_findkey (dbf, key, &find_data, NULL); /* Copy the data if the key was found. */ if (elem_loc >= 0) diff --git a/src/gdbmseq.c b/src/gdbmseq.c index 114cf11..0742383 100644 --- a/src/gdbmseq.c +++ b/src/gdbmseq.c @@ -120,8 +120,6 @@ gdbm_nextkey (GDBM_FILE dbf, datum key) { datum return_val; /* The return value. */ int elem_loc; /* The location in the bucket. */ - char *find_data; /* Data pointer returned by _gdbm_findkey. */ - int hash_val; /* Returned by _gdbm_findkey. */ /* Initialize the gdbm_errno variable. */ gdbm_errno = GDBM_NO_ERROR; @@ -137,7 +135,7 @@ gdbm_nextkey (GDBM_FILE dbf, datum key) } /* Find the key. */ - elem_loc = _gdbm_findkey (dbf, key, &find_data, &hash_val); + elem_loc = _gdbm_findkey (dbf, key, NULL, NULL); if (elem_loc == -1) return return_val; /* Find the next key. */ diff --git a/src/gdbmstore.c b/src/gdbmstore.c index 3cb7560..3ba882e 100644 --- a/src/gdbmstore.c +++ b/src/gdbmstore.c @@ -45,7 +45,6 @@ gdbm_store (GDBM_FILE dbf, datum key, datum content, int flags) off_t free_adr; /* For keeping track of a freed section. */ int free_size; int new_size; /* Used in allocating space. */ - char *temp; /* Used in _gdbm_findkey call. */ int rc; /* First check to make sure this guy is a writer. */ @@ -68,7 +67,7 @@ gdbm_store (GDBM_FILE dbf, datum key, datum content, int flags) /* Look for the key in the file. A side effect loads the correct bucket and calculates the hash value. */ - elem_loc = _gdbm_findkey (dbf, key, &temp, &new_hash_val); + elem_loc = _gdbm_findkey (dbf, key, NULL, &new_hash_val); /* Initialize these. */ file_adr = 0; |