home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 22 gnu / 22-gnu.zip / db02_src.zip / defs.h < prev    next >
C/C++ Source or Header  |  1994-02-22  |  8KB  |  337 lines

  1. /**************************************************************************
  2.  * Source Id :
  3.  *
  4.  * $Id: defs.h,v 1.29 1993/11/05 12:45:05 kevinl Exp $
  5.  *-------------------------------------------------------------------------
  6.  * Project Notes :
  7.  *
  8.  *  Diamond Base
  9.  *  ============
  10.  *    A solid database implementation, spurred on by the continuing
  11.  *  Metal (Lead) Base saga.
  12.  *
  13.  *  Project Team :
  14.  *      A. Davison
  15.  *      K. Lentin
  16.  *      D. Platt
  17.  *
  18.  *    Project Commenced : 05-02-1993
  19.  *
  20.  *-------------------------------------------------------------------------
  21.  *  Module Notes :
  22.  *
  23.  *  Common types and definitions
  24.  *
  25.  *
  26.  *  Original Author : Dunno. Andy started it. Kev finished it.
  27.  *
  28.  *-------------------------------------------------------------------------
  29.  * Revision History:
  30.  *
  31.  * $Log: defs.h,v $
  32.  * Revision 1.29  1993/11/05  12:45:05    kevinl
  33.  * Fixed problems that CC picked up (and some multi bugs)
  34.  *
  35.  * Revision 1.28  1993/11/04  22:24:32    darrenp
  36.  * Avoided prototype for strcasesmp for linux as it already has one.
  37.  *
  38.  * Revision 1.27  1993/11/03  10:04:46    kevinl
  39.  * Added ichar and utils.{h,cc}
  40.  *
  41.  * Revision 1.26  1993/10/24  15:35:08    kevinl
  42.  * Fix for strcasecmp vs stricmp
  43.  *
  44.  * Revision 1.25  1993/10/18  11:19:12    kevinl
  45.  * Added strNcpy
  46.  *
  47.  * Revision 1.24  1993/10/05  07:31:29    kevinl
  48.  * Had problems with some sytems #define'ing TRUE and FALSE
  49.  *
  50.  * Revision 1.23  1993/06/23  05:21:22    kevinl
  51.  * Mallocs are now in angular brackets
  52.  *
  53.  * Revision 1.22  1993/05/11  14:44:50    kevinl
  54.  * Added version number output
  55.  *
  56.  * Revision 1.21  1993/05/03  23:33:53    kevinl
  57.  * long aligned data
  58.  *
  59.  * Revision 1.20  1993/05/03  01:33:03    kevinl
  60.  * Cosmetic (mainly) changes for CC
  61.  *
  62.  * Revision 1.19  1993/05/01  14:39:19    kevinl
  63.  * Got rid of ints
  64.  *
  65.  * Revision 1.18  1993/04/30  07:45:45    kevinl
  66.  * Some optimizing
  67.  *
  68.  * Revision 1.17  1993/04/29  07:22:23    kevinl
  69.  * Removed some bucket::'s
  70.  *
  71.  * Revision 1.16  1993/04/28  11:30:15    kevinl
  72.  * Fixed up inlines
  73.  *
  74.  * Revision 1.15  1993/04/27  10:31:23    darrenp
  75.  * Added BOOL_DEFINED flag.
  76.  *
  77.  * Revision 1.14  1993/04/26  00:35:30    kevinl
  78.  * Comments
  79.  *
  80.  * Revision 1.13  1993/04/15  07:58:17    kevinl
  81.  * Added findIndex, del and empty
  82.  *
  83.  * Revision 1.12  1993/04/04  23:58:41    kevinl
  84.  * Added get & setParent
  85.  *
  86.  * Revision 1.11  1993/03/26  06:16:38    darrenp
  87.  * standardized error codes.
  88.  *
  89.  * Revision 1.10  1993/03/21  23:35:44    kevinl
  90.  * Added typedefs for unsigned types
  91.  * Changd pKeyType to a char*
  92.  *
  93.  * Revision 1.9  1993/03/17  14:28:34  kevinl
  94.  * newKeyNum argument in bucket constructors
  95.  * changed some int data members to long
  96.  *
  97.  * Revision 1.8  1993/03/15  14:19:33  kevinl
  98.  * Fixed up BORLANDC probs
  99.  * Changed some int's to long
  100.  * added empty bucket() constructor
  101.  * added setParent
  102.  *
  103.  * Revision 1.7  1993/02/17  12:15:16  kevinl
  104.  * Fixed pointer alignment problems by adding getLink and setLink
  105.  *
  106.  * Revision 1.6  1993/02/13  00:56:04  kevinl
  107.  * long's replaced with bucketId
  108.  * getShot nuked
  109.  *
  110.  * Revision 1.5  1993/02/11  03:53:38  kevinl
  111.  * a few fixes.
  112.  *
  113.  * Revision 1.4  1993/02/10  07:59:03  darrenp
  114.  * Global definitions of things like bool.
  115.  *
  116.  * Revision 1.3  1993/02/08  14:36:00  darrenp
  117.  * sploosh added, some output routines in class bucket
  118.  * enum for leaf and some Borlandc/Unix confrontation added
  119.  *
  120.  * Revision 1.2  1993/02/08  00:13:21  kevinl
  121.  * Kevin adds a constructor for the bucket class...
  122.  *
  123.  * Revision 1.1  1993/02/06  16:33:53  kevinl
  124.  * Initial revision
  125.  *
  126.  **************************************************************************/
  127. #ifndef __DEFS_H__
  128. #define __DEFS_H__
  129.  
  130. #include <dberr.h>
  131.  
  132. #ifdef __CC
  133. extern "C" {
  134. void bcopy(const void*, void*, long);
  135. }
  136. #else
  137. #include <string.h>
  138. #endif // __CC
  139.  
  140. // Some useful types...
  141.  
  142. typedef unsigned short ushort;
  143. typedef unsigned long ulong;
  144. typedef unsigned char uchar;
  145.  
  146. typedef long bucketId;
  147.  
  148. #ifndef BOOL_DEFINED
  149. #define BOOL_DEFINED
  150. #ifdef FALSE
  151. #undef FALSE
  152. #endif
  153. #ifdef TRUE
  154. #undef TRUE
  155. #endif
  156. enum bool {FALSE=0, TRUE=1,false=0,true=1};
  157. #endif
  158.  
  159. enum e_leaf {INNER, LEAF};
  160.  
  161. // Borland and Unix don't share a memory move/copy routine that understands
  162. // overlaps. Also the two have different argument orderings.
  163.  
  164. #if defined(__BORLANDC__) || defined(__EMX__)
  165. #define bcopy(a,b,c) memmove(b,a,c)
  166. #define strcasecmp(a,b) stricmp(a,b)
  167. #define strncasecmp(a,b,c) strnicmp(a,b,c)
  168. #else
  169. extern "C" {
  170. #if !defined(linux) && !defined(__CC)
  171. int strncasecmp(char*, char*, int);
  172. #endif
  173. }
  174. #endif
  175.  
  176. // maximum number of btree queries
  177. #define MAX_QUERY 10
  178.  
  179. class object;
  180. class bucket;
  181.  
  182. // All keys stored in buckets are of this type. Objects should cast these
  183. // as appropriate.
  184.  
  185. typedef char* pKeyType;
  186.  
  187. // Data stored at the head of each bucket's data area.
  188.  
  189. struct headerData
  190. {
  191.     e_leaf        leaf;
  192.     bucketId    parentId;
  193.     bucketId    nextBucket;
  194.     bucketId    prevBucket;
  195.     long        activeKeys;
  196. };
  197.  
  198. // Class definition for the bucket.
  199.  
  200. class bucket
  201. {
  202.     long*        data; // This is long* so that the headerData is aligned right
  203.     bucketId    id;
  204.     long        keyLength;
  205.     long        numKeys;
  206.     long        size;
  207.     long        keyNum;
  208.     long        myTupleSize;
  209.  
  210. public:
  211. //-----
  212.     bucket(bucketId newId, long newKeyLength, long newSize, e_leaf newLeaf, bucketId newParentId, bucketId newNextBucket, bucketId newPrevBucket, long NewkeyNum=0);
  213. //    bucket(long newKeyNum=0 ) {};
  214.     ~bucket();
  215.     inline bucketId getId(void)
  216.         {
  217.             return id;
  218.         }
  219.     void    info(void);
  220.     inline    long headerSize(void)
  221.         {
  222.             return sizeof(headerData);
  223.         }
  224.  
  225.     inline    long tupleSize(void)
  226.         {
  227.             return myTupleSize;
  228.         }
  229.  
  230.     inline    headerData* header(void)
  231.         {
  232.         // Return a correctly typed pointer to the header data.
  233.         // This gives access to private data and should probably be
  234.         // replaced with access methods for all header data individually.
  235.  
  236.             return (headerData*)data;
  237.         }
  238.  
  239.     inline    char* firstTuple(void)
  240.         {
  241.         // The keys and links in the bucket are numbered as follows:
  242.         //    +-+-+-+-+-+-+-+
  243.         //    |L|K|L|K|L|K|L|  L = Link,  K = Key
  244.         //    +-+-+-+-+-+-+-+
  245.         //     0 1 1 2 2 3 3
  246.         //
  247.  
  248.         // Return the address of the
  249.         // start of the Tuple space (i.e.
  250.         // the address of Link 0).
  251.  
  252.             return (char*)data + headerSize();
  253.         }
  254.  
  255.     inline    pKeyType getKeyAddr(long pos)
  256.         {
  257.         // Return a pointer to the pos'th
  258.         // Key in the Tuple space.
  259.  
  260.             pKeyType key = (pKeyType)(firstTuple() + (pos-1) * tupleSize() +sizeof(long));
  261.             return key;
  262.         }
  263.  
  264.     inline    char* getLinkAddr(long pos)
  265.         {
  266.         // Return a pointer to the pos'th link
  267.         // in the Tuple space.
  268.         // char * so no unaligned long accesses with keys not a multiple
  269.         // of 4 long. This problem now removed with keys rounded up to
  270.         // multiples of 4 long
  271.  
  272.             char* link = (firstTuple()+pos*tupleSize());
  273.             return link;
  274.         }
  275.  
  276.     bucketId    getLink(long pos);
  277.     void        setLink(long pos, bucketId link);
  278.     bool    searchForKey(object& theObject, long& ptr);
  279.     void    findIndex(long& ind, bool& found);
  280.     void    insert(object& theObject, long link);
  281.     dbError del(long pos);
  282.     inline    bool    full(void)
  283.         {
  284.             return (bool)(numKeys == header()->activeKeys);
  285.         }
  286.  
  287.     inline    bool    empty(void)
  288.         {
  289.         // Leaf buckets are considered empty when 0 keys are present.
  290.         // Inner buckets still contain a link at this point so we test for -1.
  291.         // A slight adaptation to the delete mechanism, with appropriate care
  292.         // for balancing might not require this strange treatment.
  293.  
  294.             return (bool)(header()->activeKeys == ((header()->leaf==LEAF)?0:-1));
  295.         }
  296.  
  297.     inline    pKeyType allocTmpKey(void)
  298.         {
  299.         // The bucket is in charge of allocating temporary key areas but
  300.         // the deleting is left to the caller.
  301.  
  302.             return (pKeyType)new char[keyLength];
  303.         }
  304.  
  305.     void    sploosh(bucket* newBucket, pKeyType in, pKeyType out, object& theObject, long link);
  306.  
  307.     // A couple of access to functions to keep our data safe...
  308.  
  309.     inline    void setActiveKeys(long keys)
  310.         {
  311.             header()->activeKeys = keys;
  312.         }
  313.  
  314.     inline    bucketId getParent(void)
  315.         {
  316.             return header()->parentId;
  317.         }
  318.  
  319.     inline    void setParent(bucketId parent)
  320.         {
  321.             header()->parentId = parent;
  322.         }
  323.  
  324.     void    dump(void);
  325.     inline    void*    getData(void)
  326.         {
  327.             return (void*)data;
  328.         }
  329.     char* verStr(void);
  330.  
  331. friend class bTree;
  332.  
  333. }; //---- class def for Buckets...
  334.  
  335. #endif
  336.  
  337.