1 /* gdbmdelete.c - Remove the key and its associated data from the database. */
3 /* This file is part of GDBM, the GNU data base manager.
4 Copyright (C) 1990, 1991, 1993, 2007, 2011, 2013 Free Software Foundation,
7 GDBM is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
12 GDBM is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GDBM. If not, see <http://www.gnu.org/licenses/>. */
20 /* Include system configuration before all else. */
25 /* Remove the KEYed item and the KEY from the database DBF. The file on disk
26 is updated to reflect the structure of the new database before returning
27 from this procedure. */
30 gdbm_delete (GDBM_FILE dbf, datum key)
32 int elem_loc; /* The location in the current hash bucket. */
33 int last_loc; /* Last location emptied by the delete. */
34 int home; /* Home position of an item. */
35 bucket_element elem; /* The element to be deleted. */
36 char *find_data; /* Return pointer from findkey. */
37 int hash_val; /* Returned by findkey. */
38 off_t free_adr; /* Temporary stroage for address and size. */
41 /* First check to make sure this guy is a writer. */
42 if (dbf->read_write == GDBM_READER)
44 gdbm_errno = GDBM_READER_CANT_DELETE;
48 /* Initialize the gdbm_errno variable. */
49 gdbm_errno = GDBM_NO_ERROR;
52 elem_loc = _gdbm_findkey (dbf, key, &find_data, &hash_val);
55 gdbm_errno = GDBM_ITEM_NOT_FOUND;
59 /* Save the element. */
60 elem = dbf->bucket->h_table[elem_loc];
62 /* Delete the element. */
63 dbf->bucket->h_table[elem_loc].hash_value = -1;
66 /* Move other elements to guarantee that they can be found. */
68 elem_loc = (elem_loc + 1) % dbf->header->bucket_elems;
69 while (elem_loc != last_loc
70 && dbf->bucket->h_table[elem_loc].hash_value != -1)
72 home = dbf->bucket->h_table[elem_loc].hash_value
73 % dbf->header->bucket_elems;
74 if ( (last_loc < elem_loc && (home <= last_loc || home > elem_loc))
75 || (last_loc > elem_loc && home <= last_loc && home > elem_loc))
78 dbf->bucket->h_table[last_loc] = dbf->bucket->h_table[elem_loc];
79 dbf->bucket->h_table[elem_loc].hash_value = -1;
82 elem_loc = (elem_loc + 1) % dbf->header->bucket_elems;
85 /* Free the file space. */
86 free_adr = elem.data_pointer;
87 free_size = elem.key_size + elem.data_size;
88 _gdbm_free (dbf, free_adr, free_size);
91 dbf->bucket_changed = TRUE;
93 /* Clear out the data cache for the current bucket. */
94 if (dbf->cache_entry->ca_data.dptr != NULL)
96 free (dbf->cache_entry->ca_data.dptr);
97 dbf->cache_entry->ca_data.dptr = NULL;
99 dbf->cache_entry->ca_data.hash_val = -1;
100 dbf->cache_entry->ca_data.key_size = 0;
101 dbf->cache_entry->ca_data.elem_loc = -1;
104 _gdbm_end_update (dbf);