home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 1 / FFMCD01.bin / useful / dist / gnu / gdbm / gdbm-1.6-amiga / bucket.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-19  |  10.9 KB  |  319 lines

  1. /* bucket.c - The routines for playing with hash buckets. */
  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 "gdbmdefs.h"
  31.  
  32.  
  33. /* Initializing a new hash buckets sets all bucket entries to -1 hash value. */
  34. void
  35. _gdbm_new_bucket (dbf, bucket, bits)
  36.      gdbm_file_info *dbf;
  37.      hash_bucket *bucket;
  38.      int bits;
  39. {
  40.   int index;
  41.   
  42.   /* Initialize the avail block. */
  43.   bucket->av_count = 0;
  44.  
  45.   /* Set the information fields first. */
  46.   bucket->bucket_bits = bits;
  47.   bucket->count = 0;
  48.   
  49.   /* Initialize all bucket elements. */
  50.   for (index = 0; index < dbf->header->bucket_elems; index++)
  51.     bucket->h_table[index].hash_value = -1;
  52. }
  53.  
  54.  
  55.  
  56. /* Find a bucket for DBF that is pointed to by the bucket directory from
  57.    location DIR_INDEX.   The bucket cache is first checked to see if it
  58.    is already in memory.  If not, a bucket may be tossed to read the new
  59.    bucket.  In any case, the requested bucket is make the "current" bucket
  60.    and dbf->bucket points to the correct bucket. */
  61. int
  62. _gdbm_get_bucket (dbf, dir_index)
  63.      gdbm_file_info *dbf;
  64.      long dir_index;
  65. {
  66.   long  bucket_adr;    /* The address of the correct hash bucket.  */
  67.   int   num_bytes;    /* The number of bytes read. */
  68.   long  file_pos;    /* The return address for lseek. */
  69.   int   index;        /* Loop index. */
  70.  
  71.   /* Initial set up. */
  72.   dbf->bucket_dir = dir_index;
  73.   bucket_adr = dbf->dir [dir_index];
  74.   
  75.   /* Is that one is not alread current, we must find it. */
  76.   if (dbf->cache_entry->ca_adr != bucket_adr)
  77.     {
  78.       /* Look in the cache. */
  79.       for (index = 0; index < CACHE_SIZE; index++)
  80.     if (dbf->bucket_cache[index].ca_adr == bucket_adr)
  81.       {
  82.         dbf->bucket = dbf->bucket_cache[index].ca_bucket;
  83.         dbf->cache_entry = &dbf->bucket_cache[index];
  84.         return;
  85.       }
  86.  
  87.       /* It is not in the cache, read it from the disk. */
  88.       dbf->last_read = (dbf->last_read + 1) % CACHE_SIZE;
  89.       if (dbf->bucket_cache[dbf->last_read].ca_changed)
  90.     _gdbm_write_bucket (dbf, &dbf->bucket_cache[dbf->last_read]);
  91.       dbf->bucket_cache[dbf->last_read].ca_adr = bucket_adr;
  92.       dbf->bucket = dbf->bucket_cache[dbf->last_read].ca_bucket;
  93.       dbf->cache_entry = &dbf->bucket_cache[dbf->last_read];
  94.       dbf->cache_entry->ca_data.elem_loc = -1;
  95.       dbf->cache_entry->ca_changed = FALSE;
  96.  
  97.       /* Read the bucket. */
  98.       file_pos = lseek (dbf->desc, bucket_adr, L_SET);
  99.       if (file_pos != bucket_adr)
  100.     _gdbm_fatal (dbf, "lseek error");
  101.  
  102.       num_bytes = read (dbf->desc, dbf->bucket, dbf->header->bucket_size);
  103.       if (num_bytes != dbf->header->bucket_size)
  104.     _gdbm_fatal (dbf, "read error");
  105.     }
  106.  
  107.   return;
  108. }
  109.  
  110.  
  111. /* Split the current bucket.  This includes moving all items in the bucket to
  112.    a new bucket.  This doesn't require any disk reads because all hash values
  113.    are stored in the buckets.  Splitting the current bucket may require
  114.    doubling the size of the hash directory.  */
  115. void
  116. _gdbm_split_bucket (dbf, next_insert)
  117.      gdbm_file_info *dbf;
  118.      int next_insert;
  119. {
  120.   hash_bucket *bucket[2];     /* Pointers to the new buckets. */
  121.  
  122.   int          new_bits;    /* The number of bits for the new buckets. */
  123.   int           cache_0;        /* Location in the cache for the buckets. */
  124.   int           cache_1;
  125.   long           adr_0;        /* File address of the new bucket 0. */
  126.   long           adr_1;        /* File address of the new bucket 1. */
  127.   avail_elem   old_bucket;    /* Avail Struct for the old bucket. */
  128.  
  129.   long         dir_start0;    /* Used in updating the directory. */
  130.   long         dir_start1;
  131.   long         dir_end;
  132.  
  133.   long          *new_dir;        /* Pointer to the new directory. */
  134.   long         dir_adr;     /* Address of the new directory. */
  135.   long         dir_size;    /* Size of the new directory. */
  136.   long         old_adr[31];     /* Address of the old directories. */
  137.   long         old_size[31];     /* Size of the old directories. */
  138.   int           old_count;    /* Number of old directories. */
  139.  
  140.   int          index;        /* Used in array indexing. */
  141.   int          index1;        /* Used in array indexing. */
  142.   int          elem_loc;    /* Location in new bucket to put element. */
  143.   bucket_element *old_el;    /* Pointer into the old bucket. */
  144.   int           select;        /* Used to index bucket during movement. */
  145.  
  146.  
  147.   /* No directories are yet old. */
  148.   old_count = 0;
  149.  
  150.   while (dbf->bucket->count == dbf->header->bucket_elems)
  151.     {
  152.       /* Initialize the "new" buckets in the cache. */
  153.       do
  154.     {
  155.       dbf->last_read = (dbf->last_read + 1) % CACHE_SIZE;
  156.       cache_0 = dbf->last_read;
  157.     }      
  158.       while (dbf->bucket_cache[cache_0].ca_bucket == dbf->bucket);
  159.       bucket[0] = dbf->bucket_cache[cache_0].ca_bucket;
  160.       if (dbf->bucket_cache[cache_0].ca_changed)
  161.     _gdbm_write_bucket (dbf, &dbf->bucket_cache[cache_0]);
  162.       do
  163.     {
  164.       dbf->last_read = (dbf->last_read + 1) % CACHE_SIZE;
  165.       cache_1 = dbf->last_read;
  166.     }      
  167.       while (dbf->bucket_cache[cache_1].ca_bucket == dbf->bucket);
  168.       bucket[1] = dbf->bucket_cache[cache_1].ca_bucket;
  169.       if (dbf->bucket_cache[cache_1].ca_changed)
  170.     _gdbm_write_bucket (dbf, &dbf->bucket_cache[cache_1]);
  171.       new_bits = dbf->bucket->bucket_bits+1;
  172.       _gdbm_new_bucket (dbf, bucket[0], new_bits);
  173.       _gdbm_new_bucket (dbf, bucket[1], new_bits);
  174.       adr_0 = _gdbm_alloc (dbf, dbf->header->bucket_size); 
  175.       dbf->bucket_cache[cache_0].ca_adr = adr_0;
  176.       adr_1 = _gdbm_alloc (dbf, dbf->header->bucket_size);
  177.       dbf->bucket_cache[cache_1].ca_adr = adr_1;
  178.  
  179.       
  180.       
  181.       /* Double the directory size if necessary. */
  182.       if (dbf->header->dir_bits == dbf->bucket->bucket_bits)
  183.     {
  184.       dir_size = dbf->header->dir_size * 2;
  185.       dir_adr  = _gdbm_alloc (dbf, dir_size);
  186.       new_dir  = (long *) malloc (dir_size);
  187.       if (new_dir == NULL) _gdbm_fatal (dbf, "malloc error");
  188.       for (index = 0; index < dbf->header->dir_size/sizeof (int); index++)
  189.         {
  190.           new_dir[2*index]   = dbf->dir[index];
  191.           new_dir[2*index+1] = dbf->dir[index];
  192.         }
  193.       
  194.       /* Update header. */
  195.       old_adr[old_count] = dbf->header->dir;
  196.       dbf->header->dir = dir_adr;
  197.       old_size[old_count] = dbf->header->dir_size;
  198.       dbf->header->dir_size = dir_size;
  199.       dbf->header->dir_bits = new_bits;
  200.       old_count++;
  201.       
  202.       /* Now update dbf.  */
  203.       dbf->header_changed = TRUE;
  204.       dbf->bucket_dir *= 2;
  205.       free (dbf->dir);
  206.       dbf->dir = new_dir;
  207.     }
  208.  
  209.       /* Copy all elements in dbf->bucket into the new buckets. */
  210.       for (index = 0; index < dbf->header->bucket_elems; index++)
  211.     {
  212.       old_el = & (dbf->bucket->h_table[index]);
  213.       select = (old_el->hash_value >> (31-new_bits)) & 1;
  214.       elem_loc = old_el->hash_value % dbf->header->bucket_elems;
  215.       while (bucket[select]->h_table[elem_loc].hash_value != -1)
  216.         elem_loc = (elem_loc + 1) % dbf->header->bucket_elems;
  217.       bucket[select]->h_table[elem_loc] = *old_el;
  218.       bucket[select]->count += 1;
  219.     }
  220.       
  221.       /* Allocate avail space for the bucket[1]. */
  222.       bucket[1]->bucket_avail[0].av_adr
  223.     = _gdbm_alloc (dbf, dbf->header->block_size);
  224.       bucket[1]->bucket_avail[0].av_size = dbf->header->block_size;
  225.       bucket[1]->av_count = 1;
  226.       
  227.       /* Copy the avail elements in dbf->bucket to bucket[0]. */
  228.       bucket[0]->av_count = dbf->bucket->av_count;
  229.       index = 0;
  230.       index1 = 0;
  231.       if (bucket[0]->av_count == BUCKET_AVAIL)
  232.     {
  233.       /* The avail is full, move the first one to bucket[1]. */
  234.       _gdbm_put_av_elem (dbf->bucket->bucket_avail[0],
  235.                  bucket[1]->bucket_avail,
  236.                  &bucket[1]->av_count);
  237.       index = 1;
  238.       bucket[0]->av_count --;
  239.     }
  240.       for (; index < dbf->bucket->av_count; index++)
  241.     {
  242.       bucket[0]->bucket_avail[index1++] = dbf->bucket->bucket_avail[index];
  243.     }
  244.       
  245.       /* Update the directory.  We have new file addresses for both buckets. */
  246.       dir_start1 = (dbf->bucket_dir >> (dbf->header->dir_bits - new_bits)) | 1;
  247.       dir_end = (dir_start1 + 1) << (dbf->header->dir_bits - new_bits);
  248.       dir_start1 = dir_start1 << (dbf->header->dir_bits - new_bits);
  249.       dir_start0 = dir_start1 - (dir_end - dir_start1);
  250.       for (index = dir_start0; index < dir_start1; index++)
  251.     dbf->dir[index] = adr_0;
  252.       for (index = dir_start1; index < dir_end; index++)
  253.     dbf->dir[index] = adr_1;
  254.       
  255.       
  256.       /* Set changed flags. */
  257.       dbf->bucket_cache[cache_0].ca_changed = TRUE;
  258.       dbf->bucket_cache[cache_1].ca_changed = TRUE;
  259.       dbf->bucket_changed = TRUE;
  260.       dbf->directory_changed = TRUE;
  261.       dbf->second_changed = TRUE;
  262.       
  263.       /* Update the cache! */
  264.       dbf->bucket_dir = next_insert >> (31-dbf->header->dir_bits);
  265.       
  266.       /* Invalidate old cache entry. */
  267.       old_bucket.av_adr  = dbf->cache_entry->ca_adr;
  268.       old_bucket.av_size = dbf->header->bucket_size;
  269.       dbf->cache_entry->ca_adr = 0;
  270.       dbf->cache_entry->ca_changed = FALSE;
  271.       
  272.       /* Set dbf->bucket to the proper bucket. */
  273.       if (dbf->dir[dbf->bucket_dir] == adr_0)
  274.     {
  275.       dbf->bucket = bucket[0];
  276.       dbf->cache_entry = &dbf->bucket_cache[cache_0];
  277.       _gdbm_put_av_elem (old_bucket,
  278.                  bucket[1]->bucket_avail,
  279.                  &bucket[1]->av_count);
  280.     }
  281.       else
  282.     {
  283.       dbf->bucket = bucket[1];
  284.       dbf->cache_entry = &dbf->bucket_cache[cache_1];
  285.       _gdbm_put_av_elem (old_bucket,
  286.                  bucket[0]->bucket_avail,
  287.                  &bucket[0]->av_count);
  288.     }
  289.       
  290.     }
  291.  
  292.   /* Get rid of old directories. */
  293.   for (index = 0; index < old_count; index++)
  294.     _gdbm_free (dbf, old_adr[index], old_size[index]);
  295. }
  296.  
  297.  
  298. /* The only place where a bucket is written.  CA_ENTRY is the
  299.    cache entry containing the bucket to be written. */
  300.  
  301. _gdbm_write_bucket (dbf, ca_entry)
  302.      gdbm_file_info *dbf;
  303.      cache_elem *ca_entry;
  304. {
  305.   int  num_bytes;    /* The return value for write. */
  306.   long file_pos;    /* The return value for lseek. */
  307.   
  308.   file_pos = lseek (dbf->desc, ca_entry->ca_adr, L_SET);
  309.   if (file_pos != ca_entry->ca_adr)
  310.     _gdbm_fatal (dbf, "lseek error");
  311.   num_bytes = write (dbf->desc, ca_entry->ca_bucket, dbf->header->bucket_size);
  312.   if (num_bytes != dbf->header->bucket_size)
  313.     _gdbm_fatal (dbf, "write error");
  314.   ca_entry->ca_changed = FALSE;
  315.   ca_entry->ca_data.hash_val = -1;
  316.   ca_entry->ca_data.elem_loc = -1;
  317.   
  318. }
  319.