source: main/tags/2.26/gsdl/packages/wingdbm/gdbmstore.c@ 28875

Last change on this file since 28875 was 18, checked in by sjboddie, 26 years ago

Added windows gdbm and mg versions

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 5.8 KB
Line 
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: [email protected]
22 us-mail: Philip A. Nelson
23 Computer Science Department
24 Western Washington University
25 Bellingham, WA 98226
26
27*************************************************************************/
28
29
30/* AIX demands this be the very first thing in the file. */
31#if !defined(__GNUC__) && defined(_AIX)
32 #pragma alloca
33#endif
34
35/* include system configuration before all else. */
36#include "autoconf.h"
37
38#include "gdbmdefs.h"
39#include "gdbmerrno.h"
40
41
42/* Add a new element to the database. CONTENT is keyed by KEY. The
43 file on disk is updated to reflect the structure of the new database
44 before returning from this procedure. The FLAGS define the action to
45 take when the KEY is already in the database. The value GDBM_REPLACE
46 asks that the old data be replaced by the new CONTENT. The value
47 GDBM_INSERT asks that an error be returned and no action taken. A
48 return value of 0 means no errors. A return value of -1 means that
49 the item was not stored in the data base because the caller was not an
50 official writer. A return value of 0 means that the item was not stored
51 because the argument FLAGS was GDBM_INSERT and the KEY was already in
52 the database. */
53
54int
55gdbm_store (dbf, key, content, flags)
56 gdbm_file_info *dbf;
57 datum key;
58 datum content;
59 int flags;
60{
61 word_t new_hash_val; /* The new hash value. */
62 int elem_loc; /* The location in hash bucket. */
63 off_t file_adr; /* The address of new space in the file. */
64 off_t file_pos; /* The position after a lseek. */
65 int num_bytes; /* Used for error detection. */
66 off_t free_adr; /* For keeping track of a freed section. */
67 int free_size;
68
69 /* char *write_data; */ /* To write both key and data in 1 call. */
70 /* char *src; */ /* Used to prepare write_data. */
71 /* char *dst; */ /* Used to prepare write_data. */
72 /* int cnt; */ /* Counter for loops to fill write_data. */
73 int new_size; /* Used in allocating space. */
74 char *temp; /* Used in _gdbm_findkey call. */
75
76
77 /* First check to make sure this guy is a writer. */
78 if (dbf->read_write != GDBM_WRITER)
79 {
80 gdbm_errno = GDBM_READER_CANT_STORE;
81 return -1;
82 }
83
84 /* Check for illegal data values. A NULL dptr field is illegal because
85 NULL dptr returned by a lookup procedure indicates an error. */
86 if ((key.dptr == NULL) || (content.dptr == NULL))
87 {
88 gdbm_errno = GDBM_ILLEGAL_DATA;
89 return -1;
90 }
91
92 /* Initialize the gdbm_errno variable. */
93 gdbm_errno = GDBM_NO_ERROR;
94
95 /* Look for the key in the file.
96 A side effect loads the correct bucket and calculates the hash value. */
97 elem_loc = _gdbm_findkey (dbf, key, &temp, &new_hash_val);
98
99
100 /* Did we find the item? */
101 if (elem_loc != -1)
102 {
103 if (flags == GDBM_REPLACE)
104 {
105 /* Just replace the data. */
106 free_adr = dbf->bucket->h_table[elem_loc].data_pointer;
107 free_size = dbf->bucket->h_table[elem_loc].key_size
108 + dbf->bucket->h_table[elem_loc].data_size;
109 _gdbm_free (dbf, free_adr, free_size);
110 }
111 else
112 {
113 gdbm_errno = GDBM_CANNOT_REPLACE;
114 return 1;
115 }
116 }
117
118
119 /* Get the file address for the new space.
120 (Current bucket's free space is first place to look.) */
121 new_size = key.dsize+content.dsize;
122 file_adr = _gdbm_alloc (dbf, new_size);
123
124 /* If this is a new entry in the bucket, we need to do special things. */
125 if (elem_loc == -1)
126 {
127 if (dbf->bucket->count == dbf->header->bucket_elems)
128 {
129 /* Split the current bucket. */
130 _gdbm_split_bucket (dbf, new_hash_val);
131 }
132
133 /* Find space to insert into bucket and set elem_loc to that place. */
134 elem_loc = new_hash_val % dbf->header->bucket_elems;
135 while (dbf->bucket->h_table[elem_loc].hash_value != -1)
136 { elem_loc = (elem_loc + 1) % dbf->header->bucket_elems; }
137
138 /* We now have another element in the bucket. Add the new information.*/
139 dbf->bucket->count += 1;
140 dbf->bucket->h_table[elem_loc].hash_value = new_hash_val;
141 bcopy (key.dptr, dbf->bucket->h_table[elem_loc].key_start,
142 (SMALL < key.dsize ? SMALL : key.dsize));
143 }
144
145
146 /* Update current bucket data pointer and sizes. */
147 dbf->bucket->h_table[elem_loc].data_pointer = file_adr;
148 dbf->bucket->h_table[elem_loc].key_size = key.dsize;
149 dbf->bucket->h_table[elem_loc].data_size = content.dsize;
150
151 /* Write the data to the file. */
152 file_pos = lseek (dbf->desc, file_adr, L_SET);
153 if (file_pos != file_adr) _gdbm_fatal (dbf, "lseek error");
154 num_bytes = write (dbf->desc, key.dptr, key.dsize);
155 if (num_bytes != key.dsize) _gdbm_fatal (dbf, "write error");
156 num_bytes = write (dbf->desc, content.dptr, content.dsize);
157 if (num_bytes != content.dsize) _gdbm_fatal (dbf, "write error");
158
159 /* Current bucket has changed. */
160 dbf->cache_entry->ca_changed = TRUE;
161 dbf->bucket_changed = TRUE;
162
163 /* Write everything that is needed to the disk. */
164 _gdbm_end_update (dbf);
165 return 0;
166
167}
Note: See TracBrowser for help on using the repository browser.