1 | /* gdbmstore.c - Add a new key/data pair to the database. */ |
---|
2 | |
---|
3 | /* This file is part of GDBM, the GNU data base manager, by Philip A. Nelson. |
---|
4 | Copyright (C) 1990, 1991, 1993 Free Software Foundation, Inc. |
---|
5 | |
---|
6 | GDBM is free software; you can redistribute it and/or modify |
---|
7 | it under the terms of the GNU General Public License as published by |
---|
8 | the Free Software Foundation; either version 2, or (at your option) |
---|
9 | any later version. |
---|
10 | |
---|
11 | GDBM is distributed in the hope that it will be useful, |
---|
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
14 | GNU General Public License for more details. |
---|
15 | |
---|
16 | You should have received a copy of the GNU General Public License |
---|
17 | along with GDBM; see the file COPYING. If not, write to |
---|
18 | the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
19 | |
---|
20 | You may contact the author by: |
---|
21 | e-mail: phil@cs.wwu.edu |
---|
22 | us-mail: Philip A. Nelson |
---|
23 | Computer Science Department |
---|
24 | Western Washington University |
---|
25 | Bellingham, WA 98226 |
---|
26 | |
---|
27 | *************************************************************************/ |
---|
28 | |
---|
29 | |
---|
30 | /* include system configuration before all else. */ |
---|
31 | #include "autoconf.h" |
---|
32 | |
---|
33 | #include "gdbmdefs.h" |
---|
34 | #include "gdbmerrno.h" |
---|
35 | |
---|
36 | |
---|
37 | /* Add a new element to the database. CONTENT is keyed by KEY. The |
---|
38 | file on disk is updated to reflect the structure of the new database |
---|
39 | before returning from this procedure. The FLAGS define the action to |
---|
40 | take when the KEY is already in the database. The value GDBM_REPLACE |
---|
41 | asks that the old data be replaced by the new CONTENT. The value |
---|
42 | GDBM_INSERT asks that an error be returned and no action taken. A |
---|
43 | return value of 0 means no errors. A return value of -1 means that |
---|
44 | the item was not stored in the data base because the caller was not an |
---|
45 | official writer. A return value of 0 means that the item was not stored |
---|
46 | because the argument FLAGS was GDBM_INSERT and the KEY was already in |
---|
47 | the database. */ |
---|
48 | |
---|
49 | int |
---|
50 | gdbm_store (dbf, key, content, flags) |
---|
51 | gdbm_file_info *dbf; |
---|
52 | datum key; |
---|
53 | datum content; |
---|
54 | int flags; |
---|
55 | { |
---|
56 | int new_hash_val; /* The new hash value. */ |
---|
57 | int elem_loc; /* The location in hash bucket. */ |
---|
58 | off_t file_adr; /* The address of new space in the file. */ |
---|
59 | off_t file_pos; /* The position after a lseek. */ |
---|
60 | int num_bytes; /* Used for error detection. */ |
---|
61 | off_t free_adr; /* For keeping track of a freed section. */ |
---|
62 | int free_size; |
---|
63 | int new_size; /* Used in allocating space. */ |
---|
64 | char *temp; /* Used in _gdbm_findkey call. */ |
---|
65 | |
---|
66 | |
---|
67 | /* First check to make sure this guy is a writer. */ |
---|
68 | if (dbf->read_write == GDBM_READER) |
---|
69 | { |
---|
70 | gdbm_errno = GDBM_READER_CANT_STORE; |
---|
71 | return -1; |
---|
72 | } |
---|
73 | |
---|
74 | /* Check for illegal data values. A NULL dptr field is illegal because |
---|
75 | NULL dptr returned by a lookup procedure indicates an error. */ |
---|
76 | if ((key.dptr == NULL) || (content.dptr == NULL)) |
---|
77 | { |
---|
78 | gdbm_errno = GDBM_ILLEGAL_DATA; |
---|
79 | return -1; |
---|
80 | } |
---|
81 | |
---|
82 | /* Initialize the gdbm_errno variable. */ |
---|
83 | gdbm_errno = GDBM_NO_ERROR; |
---|
84 | |
---|
85 | /* Look for the key in the file. |
---|
86 | A side effect loads the correct bucket and calculates the hash value. */ |
---|
87 | elem_loc = _gdbm_findkey (dbf, key, &temp, &new_hash_val); |
---|
88 | |
---|
89 | /* Initialize these. */ |
---|
90 | file_adr = 0; |
---|
91 | new_size = key.dsize + content.dsize; |
---|
92 | |
---|
93 | /* Did we find the item? */ |
---|
94 | if (elem_loc != -1) |
---|
95 | { |
---|
96 | if (flags == GDBM_REPLACE) |
---|
97 | { |
---|
98 | /* Just replace the data. */ |
---|
99 | free_adr = dbf->bucket->h_table[elem_loc].data_pointer; |
---|
100 | free_size = dbf->bucket->h_table[elem_loc].key_size |
---|
101 | + dbf->bucket->h_table[elem_loc].data_size; |
---|
102 | if (free_size != new_size) |
---|
103 | { |
---|
104 | _gdbm_free (dbf, free_adr, free_size); |
---|
105 | } |
---|
106 | else |
---|
107 | { |
---|
108 | /* Just reuse the same address! */ |
---|
109 | file_adr = free_adr; |
---|
110 | } |
---|
111 | } |
---|
112 | else |
---|
113 | { |
---|
114 | gdbm_errno = GDBM_CANNOT_REPLACE; |
---|
115 | return 1; |
---|
116 | } |
---|
117 | } |
---|
118 | |
---|
119 | |
---|
120 | /* Get the file address for the new space. |
---|
121 | (Current bucket's free space is first place to look.) */ |
---|
122 | if (file_adr == 0) |
---|
123 | { |
---|
124 | file_adr = _gdbm_alloc (dbf, new_size); |
---|
125 | } |
---|
126 | |
---|
127 | /* If this is a new entry in the bucket, we need to do special things. */ |
---|
128 | if (elem_loc == -1) |
---|
129 | { |
---|
130 | if (dbf->bucket->count == dbf->header->bucket_elems) |
---|
131 | { |
---|
132 | /* Split the current bucket. */ |
---|
133 | _gdbm_split_bucket (dbf, new_hash_val); |
---|
134 | } |
---|
135 | |
---|
136 | /* Find space to insert into bucket and set elem_loc to that place. */ |
---|
137 | elem_loc = new_hash_val % dbf->header->bucket_elems; |
---|
138 | while (dbf->bucket->h_table[elem_loc].hash_value != -1) |
---|
139 | { elem_loc = (elem_loc + 1) % dbf->header->bucket_elems; } |
---|
140 | |
---|
141 | /* We now have another element in the bucket. Add the new information.*/ |
---|
142 | dbf->bucket->count += 1; |
---|
143 | dbf->bucket->h_table[elem_loc].hash_value = new_hash_val; |
---|
144 | bcopy (key.dptr, dbf->bucket->h_table[elem_loc].key_start, |
---|
145 | (SMALL < key.dsize ? SMALL : key.dsize)); |
---|
146 | } |
---|
147 | |
---|
148 | |
---|
149 | /* Update current bucket data pointer and sizes. */ |
---|
150 | dbf->bucket->h_table[elem_loc].data_pointer = file_adr; |
---|
151 | dbf->bucket->h_table[elem_loc].key_size = key.dsize; |
---|
152 | dbf->bucket->h_table[elem_loc].data_size = content.dsize; |
---|
153 | |
---|
154 | /* Write the data to the file. */ |
---|
155 | file_pos = lseek (dbf->desc, file_adr, L_SET); |
---|
156 | if (file_pos != file_adr) _gdbm_fatal (dbf, "lseek error"); |
---|
157 | num_bytes = write (dbf->desc, key.dptr, key.dsize); |
---|
158 | if (num_bytes != key.dsize) _gdbm_fatal (dbf, "write error"); |
---|
159 | num_bytes = write (dbf->desc, content.dptr, content.dsize); |
---|
160 | if (num_bytes != content.dsize) _gdbm_fatal (dbf, "write error"); |
---|
161 | |
---|
162 | /* Current bucket has changed. */ |
---|
163 | dbf->cache_entry->ca_changed = TRUE; |
---|
164 | dbf->bucket_changed = TRUE; |
---|
165 | |
---|
166 | /* Write everything that is needed to the disk. */ |
---|
167 | _gdbm_end_update (dbf); |
---|
168 | return 0; |
---|
169 | |
---|
170 | } |
---|