home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Professional / OS2PRO194.ISO / os2 / prgramer / rcs / sources / gdbmstor.c < prev    next >
C/C++ Source or Header  |  1992-01-19  |  6KB  |  175 lines

  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  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 1, 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.         phone:  (206) 676-3035
  27.  
  28. *************************************************************************/
  29.  
  30.  
  31. #include "gdbmdefs.h"
  32. #include "gdbmerrno.h"
  33.  
  34. extern gdbm_error gdbm_errno;    /* gdbm's error variable. */
  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.   long new_hash_val;        /* The new hash value. */
  57.   int  elem_loc;        /* The location in hash bucket. */
  58.   long file_adr;        /* The address of new space in the file.  */
  59.   long file_pos;        /* The position after a lseek. */
  60.   int  num_bytes;        /* Used for error detection. */
  61.   long free_adr;        /* For keeping track of a freed section. */
  62.   int  free_size;
  63.  
  64.   char *write_data;        /* To write both key and data in 1 call. */
  65.   char *src;            /* Used to prepare write_data. */
  66.   char *dst;            /* Used to prepare write_data. */
  67.   int   cnt;            /* Counter for loops to fill write_data. */
  68.   int   new_size;        /* Used in allocating space. */
  69.   char *temp;            /* Used in _gdbm_findkey call. */
  70.  
  71.  
  72.   /* First check to make sure this guy is a writer. */
  73.   if (dbf->read_write != GDBM_WRITER)
  74.     {
  75.       gdbm_errno = GDBM_READER_CANT_STORE;
  76.       return -1;
  77.     }
  78.  
  79.   /* Check for illegal data values.  A NULL dptr field is illegal because
  80.      NULL dptr returned by a lookup procedure indicates an error. */
  81.   if ((key.dptr == NULL) || (content.dptr == NULL))
  82.     {
  83.       gdbm_errno = GDBM_ILLEGAL_DATA;
  84.       return -1;
  85.     }
  86.  
  87.   /* Look for the key in the file.
  88.      A side effect loads the correct bucket and calculates the hash value. */
  89.   elem_loc = _gdbm_findkey (dbf, key, &temp, &new_hash_val);
  90.  
  91.  
  92.   /* Did we find the item? */
  93.   if (elem_loc != -1)
  94.     {
  95.       if (flags == GDBM_REPLACE)
  96.     {
  97.       /* Just replace the data. */
  98.       free_adr = dbf->bucket->h_table[elem_loc].data_pointer;
  99.       free_size = dbf->bucket->h_table[elem_loc].key_size
  100.                   + dbf->bucket->h_table[elem_loc].data_size;
  101.       _gdbm_free (dbf, free_adr, free_size);
  102.     }
  103.       else
  104.     {
  105.       gdbm_errno = GDBM_CANNOT_REPLACE;
  106.       return 1;
  107.     }
  108.     }
  109.  
  110.  
  111.   /* Get the file address for the new space.
  112.      (Current bucket's free space is first place to look.) */
  113.   new_size = key.dsize+content.dsize;
  114.   file_adr = _gdbm_alloc (dbf, new_size);
  115.  
  116.   /* If this is a new entry in the bucket, we need to do special things. */
  117.   if (elem_loc == -1)
  118.     {
  119.       if (dbf->bucket->count == dbf->header->bucket_elems)
  120.     {
  121.       /* Split the current bucket. */
  122.       _gdbm_split_bucket (dbf, new_hash_val);
  123.     }
  124.  
  125.       /* Find space to insert into bucket and set elem_loc to that place. */
  126.       elem_loc = (int) (new_hash_val % dbf->header->bucket_elems);
  127.       while (dbf->bucket->h_table[elem_loc].hash_value != -1)
  128.     {  elem_loc = (elem_loc + 1) % dbf->header->bucket_elems; }
  129.  
  130.       /* We now have another element in the bucket.  Add the new information.*/
  131.       dbf->bucket->count += 1;
  132.       dbf->bucket->h_table[elem_loc].hash_value = new_hash_val;
  133.       bcopy (key.dptr, dbf->bucket->h_table[elem_loc].key_start,
  134.          (SMALL < key.dsize ? SMALL : key.dsize));
  135.     }
  136.  
  137.  
  138.   /* Update current bucket data pointer and sizes. */
  139.   dbf->bucket->h_table[elem_loc].data_pointer = file_adr;
  140.   dbf->bucket->h_table[elem_loc].key_size = key.dsize;
  141.   dbf->bucket->h_table[elem_loc].data_size = content.dsize;
  142.  
  143.   /* Prepare write_data. The key is written first.  */
  144.   write_data = (char *) alloca (key.dsize+content.dsize);
  145. #ifdef OS2
  146.   if (write_data == (char *) 0)
  147.     {
  148.       fprintf (stderr, "Error: alloca() failed in gdbm (%s).\n", __FILE__);
  149.       exit (-2);
  150.     }
  151. #endif /* OS2 */
  152.   dst = write_data;
  153.   src = key.dptr;
  154.   for (cnt=0; cnt < key.dsize; cnt++)
  155.     * (dst++) = * (src++);
  156.   src = content.dptr;
  157.   for (cnt=0; cnt < content.dsize; cnt++)
  158.     * (dst++) = * (src++);
  159.  
  160.   /* Write the data to the file. */
  161.   file_pos = lseek (dbf->desc, file_adr, L_SET);
  162.   if (file_pos != file_adr) _gdbm_fatal (dbf, "lseek error");
  163.   num_bytes = write (dbf->desc, write_data, key.dsize+content.dsize);
  164.   if (num_bytes != key.dsize+content.dsize) _gdbm_fatal (dbf, "write error");
  165.  
  166.   /* Current bucket has changed. */
  167.   dbf->cache_entry->ca_changed = TRUE;
  168.   dbf->bucket_changed = TRUE;
  169.  
  170.   /* Write everything that is needed to the disk. */
  171.   _gdbm_end_update (dbf);
  172.   return 0;
  173.  
  174. }
  175.