diff options
author | Sergey Poznyakoff <gray@gnu.org.ua> | 2016-07-08 15:25:25 +0300 |
---|---|---|
committer | Sergey Poznyakoff <gray@gnu.org.ua> | 2016-07-08 15:35:13 +0300 |
commit | 27b5d96eec6d43eed834c9f1492b1f3d38bf60ec (patch) | |
tree | ac20224e957ad7688ec31d03e6080c09ab9fc4c9 /src | |
parent | 4aef6b36f862e46723403bc6422ac47058a5ef19 (diff) | |
download | gdbm-27b5d96eec6d43eed834c9f1492b1f3d38bf60ec.tar.gz gdbm-27b5d96eec6d43eed834c9f1492b1f3d38bf60ec.tar.bz2 |
Improve _gdbm_findkey interface.
* doc/gdbm.texi: Document gdbm_exists in detail.
* src/findkey.c (_gdbm_findkey): return parameters can be
NULL.
* src/gdbmexists.c (gdbm_exists): Remove unnecessary
local variables. Reset gdbm_errno to 0 if the key was
not found.
* src/gdbmdelete.c (gdbm_delete): Remove unnecessary
local variables.
* src/gdbmfetch.c: Likewise.
* src/gdbmseq.c: Likewise.
* src/gdbmstore.c: Likewise.
Diffstat (limited to 'src')
-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 |
6 files changed, 32 insertions, 29 deletions
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; |