home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / gdbm-1.5 / gdbmdefs.h < prev    next >
Encoding:
C/C++ Source or Header  |  1991-02-28  |  7.3 KB  |  201 lines

  1. /* gdbmdefs.h - The include file for dbm.  Defines structure and constants. */
  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. /* Include the standard include files needed for gdbm. */
  31. #include <stdio.h>
  32. #include <sys/types.h>
  33. #include <sys/file.h>
  34. #include <sys/stat.h>
  35. #include "gdbmconst.h"
  36. #include "systems.h"
  37.  
  38. /* The type definitions are next.  */
  39.  
  40. /* The data and key structure.  This structure is defined for compatibility. */
  41.  
  42. typedef struct {
  43.     char *dptr;
  44.     int   dsize;
  45.       } datum;
  46.  
  47.  
  48. /* The available file space is stored in an "avail" table.  The one with
  49.    most activity is contained in the file header. (See below.)  When that
  50.    one filles up, it is split in half and half is pushed on an "avail
  51.    stack."  When the active avail table is empty and the "avail stack" is
  52.    not empty, the top of the stack is popped into the active avail table. */
  53.  
  54. /* The following structure is the element of the avaliable table.  */
  55. typedef struct {
  56.       int   av_size;        /* The size of the available block. */
  57.     long  av_adr;        /* The file address of the available block. */
  58.       } avail_elem;
  59.  
  60. /* This is the actual table. The in-memory images of the avail blocks are
  61.    allocated by malloc using a calculated size.  */
  62. typedef struct {
  63.     int   size;        /* The number of avail elements in the table.*/
  64.     int   count;        /* The number of entries in the table. */
  65.     long  next_block;    /* The file address of the next avail block. */
  66.     avail_elem av_table[1]; /* The table.  Make it look like an array.  */
  67.       } avail_block;
  68.  
  69. /* The dbm file header keeps track of the current location of the hash
  70.    directory and the free space in the file.  */
  71.  
  72. typedef struct {
  73.     long  header_magic;  /* 0x13579ace to make sure the header is good. */
  74.     int   block_size;    /* The  optimal i/o blocksize from stat. */
  75.     long  dir;         /* File address of hash directory table. */
  76.     int   dir_size;         /* Size in bytes of the table.  */
  77.     int   dir_bits;         /* The number of address bits used in the table.*/
  78.     int   bucket_size;   /* Size in bytes of a hash bucket struct. */
  79.     int   bucket_elems;  /* Number of elements in a hash bucket. */
  80.     long  next_block;    /* The next unallocated block address. */
  81.     avail_block avail;   /* This must be last because of the psuedo
  82.                 array in avail.  This avail grows to fill
  83.                 the entire block. */
  84.       }  gdbm_file_header;
  85.  
  86.  
  87. /* The dbm hash bucket element contains the full 31 bit hash value, the
  88.    "pointer" to the key and data (stored together) with their sizes.  It also
  89.    has a small part of the actual key value.  It is used to verify the first
  90.    part of the key has the correct value without having to read the actual
  91.    key. */
  92.  
  93. typedef struct {
  94.     long  hash_value;    /* The complete 31 bit value. */
  95.     char  key_start[SMALL];    /* Up to the first SMALL bytes of the key.  */
  96.     long  data_pointer;    /* The file address of the key record. The
  97.                    data record directly follows the key.  */
  98.     int   key_size;        /* Size of key data in the file. */
  99.     int   data_size;    /* Size of associated data in the file. */
  100.       } bucket_element;
  101.  
  102.  
  103. /* A bucket is a small hash table.  This one consists of a number of
  104.    bucket elements plus some bookkeeping fields.  The number of elements
  105.    depends on the optimum blocksize for the storage device and on a
  106.    parameter given at file creation time.  This bucket takes one block.
  107.    When one of these tables gets full, it is split into two hash buckets.
  108.    The contents are split between them by the use of the first few bits
  109.    of the 31 bit hash function.  The location in a bucket is the hash
  110.    value modulo the size of the bucket.  The in-memory images of the
  111.    buckets are allocated by malloc using a calculated size depending of
  112.    the file system buffer size.  To speed up write, each bucket will have
  113.    BUCKET_AVAIL avail elements with the bucket. */
  114.  
  115. typedef struct {
  116.         int   av_count;           /* The number of bucket_avail entries. */
  117.         avail_elem bucket_avail[BUCKET_AVAIL];  /* Distributed avail. */
  118.     int   bucket_bits;       /* The number of bits used to get here. */
  119.     int   count;           /* The number of element buckets full. */
  120.     bucket_element h_table[1]; /* The table.  Make it look like an array.*/
  121.       } hash_bucket;
  122.  
  123. /* We want to keep from reading buckets as much as possible.  The following is
  124.    to implement a bucket cache.  When full, buckets will be dropped in a
  125.    least recently read from disk order.  */
  126.  
  127. /* To speed up fetching and "sequential" access, we need to implement a
  128.    data cache for key/data pairs read from the file.  To find a key, we
  129.    must exactly match the key from the file.  To reduce overhead, the
  130.    data will be read at the same time.  Both key and data will be stored
  131.    in a data cache.  Each bucket cached will have a one element data
  132.    cache.  */
  133.  
  134. typedef struct {
  135.         long  hash_val;
  136.     int   data_size;
  137.     int   key_size;
  138.     char *dptr;
  139.     int   elem_loc;
  140.       }  data_cache_elem;
  141.  
  142. typedef struct {
  143.         hash_bucket *   ca_bucket;
  144.     long            ca_adr;
  145.     char        ca_changed;   /* Data in the bucket changed. */
  146.     data_cache_elem ca_data;
  147.       } cache_elem;
  148.  
  149.  
  150.  
  151. /* This final structure contains all main memory based information for
  152.    a gdbm file.  This allows multiple gdbm files to be opened at the same
  153.    time by one program. */
  154.  
  155. typedef struct {
  156.     /* Global variables and pointers to dynamic variables used by gdbm.  */
  157.  
  158.       /* The file name. */
  159.     char *name;
  160.  
  161.     /* The reader/writer status. */
  162.     int read_write;
  163.  
  164.     /* The fatal error handling routine. */
  165.     void (*fatal_err) ();
  166.  
  167.     /* The gdbm file descriptor which is set in gdbm_open.  */
  168.     int  desc;
  169.  
  170.     /* The file header holds information about the database. */
  171.     gdbm_file_header *header;
  172.  
  173.     /* The hash table directory from extendible hashing.  See Fagin et al, 
  174.        ACM Trans on Database Systems, Vol 4, No 3. Sept 1979, 315-344 */
  175.     long  *dir;
  176.  
  177.     /* The bucket cache. */
  178.     cache_elem bucket_cache [CACHE_SIZE];
  179.     int last_read;
  180.  
  181.     /* Points to the current hash bucket in the cache. */
  182.     hash_bucket *bucket;
  183.  
  184.     /* The directory entry used to get the current hash bucket. */
  185.     long bucket_dir;
  186.  
  187.     /* Pointer to the current bucket's cache entry. */
  188.     cache_elem *cache_entry;
  189.  
  190.  
  191.     /* Bookkeeping of things that need to be written back at the
  192.        end of an update. */
  193.     char  header_changed;
  194.     char  directory_changed;
  195.     char  bucket_changed;
  196.     char  second_changed;
  197.     
  198.       } gdbm_file_info;
  199.  
  200.  
  201.