home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / cprog / btree.zip / DBFILES.HPP < prev    next >
C/C++ Source or Header  |  1992-02-16  |  4KB  |  172 lines

  1. //    C++ B+Tree
  2. //    (c) 1990,1991 Larry A. Walker
  3. //    This source is proprietary information which cannot be distributed 
  4. //    without written permission of Larry A. Walker
  5.  
  6. #ifndef DBFILES_H
  7. #define DBFILES_H
  8.             // Combined Block File Handler Header and
  9.             // B+Tree Header
  10. void fail_que(int);
  11. void display_que();
  12.  
  13.             // Define one of these three:
  14. /*
  15. #define UNIX
  16. #define XENIX
  17. */
  18. #define MSDOS
  19.  
  20. #include <stdio.h>
  21. #include <string.h>
  22. #include <fcntl.h>
  23. #include <stdlib.h>
  24. #include <time.h>
  25. #ifdef UNIX
  26. #include <sys/types.h>
  27. #include <sys/stat.h>
  28. #include <unistd.h>
  29. #include <osfcn.h>
  30. #else
  31. #include <sys\stat.h>
  32. #include <io.h>
  33. #endif
  34. #ifdef ZORTECH
  35. #define main void main
  36. #endif
  37.  
  38. #define    SUCCESS            1
  39. #define FAIL            0
  40. #define NEAR_MATCH        2
  41. #define NODE_EOF        -1
  42. #define CREATE            1
  43. #define CLEAN            0
  44. #define DIRTY            1
  45. #define EXACT            1
  46. #define INSERT            2
  47. #define UNLINK            1
  48.  
  49. #define UNDEF_KEY        0
  50. #define VAR_LNG_KEY        1
  51. #define FIX_LNG_KEY        2
  52. #define LONG_KEY        3
  53. #define DOUBLE_KEY        4
  54.  
  55. #define SEARCH_LEVELS        1
  56. #define DUPLICATES        2
  57. #define TYPE_OF_KEY        3
  58. #define KEY_LENGTH        4
  59. #define ACTIVE_LEVEL        5
  60. #define NUMBER_OF_KEYS        6
  61. #define LAST_ACCESS         7
  62.  
  63. #define MAXVARSTR        64
  64.  
  65. #define EQ            0
  66. #define LT            -1
  67. #define LE            -2
  68. #define GT            1
  69. #define GE            2
  70. #define ANY            3
  71.  
  72. #define FirstKeyOffset        ((2*sizeof(int)) + sizeof(long))
  73.  
  74.         // The definition TreeBlockSize is physical size of the block
  75.         // as it lays on the disk.  There is a one byte overhead used
  76.         // by the bfh class.  The archecture of he machine may ultim-
  77.         // ately decide the value used when creating an index.  Use
  78.         // only to open a bfh file. To get the block size of an already
  79.         // created bfh file use GetBFHSize()
  80. #define TreeBlockSize        (1024-1)
  81. #define KeySpace        (TreeBlockSize - FirstKeyOffset)
  82.  
  83. #define MaxKeySize        255    
  84. #define MaxHierarchyPointers     10  // same as the number of allowable levels
  85. #define AllowableLevels        MaxHierarchyPointers
  86.  
  87. int create_index_string(char *,char *);
  88. int create_c_string(char *,char *);
  89.  
  90. // The block file handler data block header 
  91. // bfh_data and the following data structure combined size cannot exceed
  92. // BUFSIZ  
  93.  
  94. struct bfh_data {
  95.     long     magic;        // magic id insures only properly marked
  96.     time_t    date_opened;    // files are used
  97.     time_t    date_updated;
  98.     int    block_size;
  99.     long    first_free_block_addr;
  100.     long    last_free_block_addr; // last free block has its own addr
  101.     long    add_block_addr;
  102. };
  103.  
  104. // This identifier describes a free block
  105.  
  106. struct free_block_id {
  107.                 // bit 0x1 = Full Bit Set
  108.     char    switches;    // the switch always resides outside the
  109.                 // user block area
  110.     long     previous_block;
  111.     long     next_block;
  112. };
  113.  
  114. // The trunk is the tree information storage block 
  115. // It follows in the block file handler directly behind bfh data
  116.  
  117. struct Trunk {
  118.     long     magic;
  119.     int    search_levels;
  120.     int      duplicates;
  121.     int    type_of_key;
  122.     int    fix_lng_key_lng;
  123.     long    rootnode;
  124.     long    number_of_keys;
  125.     time_t     time_of_last_use;
  126. };
  127.  
  128.     // if the total_node_keys = -1 then this is an empty node
  129.     // if the next free block addr is -1, this is the last
  130.     // free block address
  131. struct Node {
  132.     int     total_node_keys;
  133.     int    next_free_position;
  134.     union {
  135.         long    left_node;
  136.         long    next_free_block_addr;
  137.     };
  138.     char    key_space[KeySpace];
  139. };
  140.  
  141. union Key{
  142.     long    long_key;
  143.     double    double_key;
  144.     char    key[MaxKeySize]; // null terminated string
  145. };
  146.  
  147. struct KeyGroup {
  148.     long    right_node;
  149.     long    address;
  150.     union Key k ;
  151. };
  152.  
  153. // The position status is returned from the position in node function
  154.  
  155. struct posn_status {
  156.     struct KeyGroup * prev_key;
  157.     int term_status;
  158.     int compare_status;
  159. };
  160.  
  161. // This hierarchy differs from a cache in that its purpose is not to store nodes
  162. // for more efficient rapid access, but to hold the linear parent list for
  163. // more efficient movement through the tree
  164.  
  165. struct HierarchyBlock {
  166.     long    block_id;
  167.     int    state;     // CLEAN or DIRTY
  168.     struct     Node hierarchy_node;
  169. };
  170.  
  171. #endif DBFILES_H
  172.