heap.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /* Copyright (c) 2000, 2011, Oracle and/or its affiliates.
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation; version 2 of the License.
  5. This program is distributed in the hope that it will be useful,
  6. but WITHOUT ANY WARRANTY; without even the implied warranty of
  7. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  8. GNU General Public License for more details.
  9. You should have received a copy of the GNU General Public License
  10. along with this program; if not, write to the Free Software
  11. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  12. */
  13. /* This file should be included when using heap_database_functions */
  14. /* Author: Michael Widenius */
  15. #ifndef _heap_h
  16. #define _heap_h
  17. #ifdef __cplusplus
  18. extern "C" {
  19. #endif
  20. #ifndef _my_base_h
  21. #include <my_base.h>
  22. #endif
  23. #include <my_pthread.h>
  24. #include <thr_lock.h>
  25. #include "my_compare.h"
  26. #include "my_tree.h"
  27. /* defines used by heap-funktions */
  28. #define HP_MAX_LEVELS 4 /* 128^5 records is enough */
  29. #define HP_PTRS_IN_NOD 128
  30. /* struct used with heap_funktions */
  31. typedef struct st_heapinfo /* Struct from heap_info */
  32. {
  33. ulong records; /* Records in database */
  34. ulong deleted; /* Deleted records in database */
  35. ulong max_records;
  36. ulonglong data_length;
  37. ulonglong index_length;
  38. uint reclength; /* Length of one record */
  39. int errkey;
  40. ulonglong auto_increment;
  41. time_t create_time;
  42. } HEAPINFO;
  43. /* Structs used by heap-database-handler */
  44. typedef struct st_heap_ptrs
  45. {
  46. uchar *blocks[HP_PTRS_IN_NOD]; /* pointers to HP_PTRS or records */
  47. } HP_PTRS;
  48. struct st_level_info
  49. {
  50. /* Number of unused slots in *last_blocks HP_PTRS block (0 for 0th level) */
  51. uint free_ptrs_in_block;
  52. /*
  53. Maximum number of records that can be 'contained' inside of each element
  54. of last_blocks array. For level 0 - 1, for level 1 - HP_PTRS_IN_NOD, for
  55. level 2 - HP_PTRS_IN_NOD^2 and so forth.
  56. */
  57. ulong records_under_level;
  58. /*
  59. Ptr to last allocated HP_PTRS (or records buffer for level 0) on this
  60. level.
  61. */
  62. HP_PTRS *last_blocks;
  63. };
  64. /*
  65. Heap table records and hash index entries are stored in HP_BLOCKs.
  66. HP_BLOCK is used as a 'growable array' of fixed-size records. Size of record
  67. is recbuffer bytes.
  68. The internal representation is as follows:
  69. HP_BLOCK is a hierarchical structure of 'blocks'.
  70. A block at level 0 is an array records_in_block records.
  71. A block at higher level is an HP_PTRS structure with pointers to blocks at
  72. lower levels.
  73. At the highest level there is one top block. It is stored in HP_BLOCK::root.
  74. See hp_find_block for a description of how record pointer is obtained from
  75. its index.
  76. See hp_get_new_block
  77. */
  78. typedef struct st_heap_block
  79. {
  80. HP_PTRS *root; /* Top-level block */
  81. struct st_level_info level_info[HP_MAX_LEVELS+1];
  82. uint levels; /* number of used levels */
  83. uint recbuffer; /* Length of one saved record */
  84. ulong records_in_block; /* Records in one heap-block */
  85. ulong last_allocated; /* number of records there is allocated space for */
  86. } HP_BLOCK;
  87. struct st_heap_info; /* For referense */
  88. typedef struct st_hp_keydef /* Key definition with open */
  89. {
  90. uint flag; /* HA_NOSAME | HA_NULL_PART_KEY */
  91. uint keysegs; /* Number of key-segment */
  92. uint length; /* Length of key (automatic) */
  93. uint8 algorithm; /* HASH / BTREE */
  94. HA_KEYSEG *seg;
  95. HP_BLOCK block; /* Where keys are saved */
  96. /*
  97. Number of buckets used in hash table. Used only to provide
  98. #records estimates for heap key scans.
  99. */
  100. ha_rows hash_buckets;
  101. TREE rb_tree;
  102. int (*write_key)(struct st_heap_info *info, struct st_hp_keydef *keyinfo,
  103. const uchar *record, uchar *recpos);
  104. int (*delete_key)(struct st_heap_info *info, struct st_hp_keydef *keyinfo,
  105. const uchar *record, uchar *recpos, int flag);
  106. uint (*get_key_length)(struct st_hp_keydef *keydef, const uchar *key);
  107. } HP_KEYDEF;
  108. typedef struct st_heap_share
  109. {
  110. HP_BLOCK block;
  111. HP_KEYDEF *keydef;
  112. ulonglong data_length,index_length,max_table_size;
  113. ulonglong auto_increment;
  114. ulong min_records,max_records; /* Params to open */
  115. ulong records; /* records */
  116. ulong blength; /* records rounded up to 2^n */
  117. ulong deleted; /* Deleted records in database */
  118. uint key_stat_version; /* version to indicate insert/delete */
  119. uint key_version; /* Updated on key change */
  120. uint file_version; /* Update on clear */
  121. uint reclength; /* Length of one record */
  122. uint changed;
  123. uint keys,max_key_length;
  124. uint currently_disabled_keys; /* saved value from "keys" when disabled */
  125. uint open_count;
  126. uchar *del_link; /* Link to next block with del. rec */
  127. char * name; /* Name of "memory-file" */
  128. time_t create_time;
  129. THR_LOCK lock;
  130. mysql_mutex_t intern_lock; /* Locking for use with _locking */
  131. my_bool delete_on_close;
  132. my_bool internal; /* Internal temporary table */
  133. LIST open_list;
  134. uint auto_key;
  135. uint auto_key_type; /* real type of the auto key segment */
  136. } HP_SHARE;
  137. struct st_hp_hash_info;
  138. typedef struct st_heap_info
  139. {
  140. HP_SHARE *s;
  141. uchar *current_ptr;
  142. struct st_hp_hash_info *current_hash_ptr;
  143. ulong current_record,next_block;
  144. int lastinx,errkey;
  145. int mode; /* Mode of file (READONLY..) */
  146. uint opt_flag,update;
  147. uchar *lastkey; /* Last used key with rkey */
  148. uchar *recbuf; /* Record buffer for rb-tree keys */
  149. enum ha_rkey_function last_find_flag;
  150. TREE_ELEMENT *parents[MAX_TREE_HEIGHT+1];
  151. TREE_ELEMENT **last_pos;
  152. uint key_version; /* Version at last read */
  153. uint file_version; /* Version at scan */
  154. uint lastkey_len;
  155. my_bool implicit_emptied;
  156. THR_LOCK_DATA lock;
  157. LIST open_list;
  158. } HP_INFO;
  159. typedef struct st_heap_create_info
  160. {
  161. HP_KEYDEF *keydef;
  162. uint auto_key; /* keynr [1 - maxkey] for auto key */
  163. uint auto_key_type;
  164. uint keys;
  165. uint reclength;
  166. ulong max_records;
  167. ulong min_records;
  168. ulonglong max_table_size;
  169. ulonglong auto_increment;
  170. my_bool with_auto_increment;
  171. my_bool internal_table;
  172. /*
  173. TRUE if heap_create should 'pin' the created share by setting
  174. open_count to 1. Is only looked at if not internal_table.
  175. */
  176. my_bool pin_share;
  177. } HP_CREATE_INFO;
  178. /* Prototypes for heap-functions */
  179. extern HP_INFO *heap_open(const char *name, int mode);
  180. extern HP_INFO *heap_open_from_share(HP_SHARE *share, int mode);
  181. extern HP_INFO *heap_open_from_share_and_register(HP_SHARE *share, int mode);
  182. extern void heap_release_share(HP_SHARE *share, my_bool internal_table);
  183. extern int heap_close(HP_INFO *info);
  184. extern int heap_write(HP_INFO *info,const uchar *buff);
  185. extern int heap_update(HP_INFO *info,const uchar *old,const uchar *newdata);
  186. extern int heap_rrnd(HP_INFO *info,uchar *buf,uchar *pos);
  187. extern int heap_scan_init(HP_INFO *info);
  188. extern int heap_scan(register HP_INFO *info, uchar *record);
  189. extern int heap_delete(HP_INFO *info,const uchar *buff);
  190. extern int heap_info(HP_INFO *info,HEAPINFO *x,int flag);
  191. extern int heap_create(const char *name,
  192. HP_CREATE_INFO *create_info, HP_SHARE **share,
  193. my_bool *created_new_share);
  194. extern int heap_delete_table(const char *name);
  195. extern void heap_drop_table(HP_INFO *info);
  196. extern int heap_extra(HP_INFO *info,enum ha_extra_function function);
  197. extern int heap_reset(HP_INFO *info);
  198. extern int heap_rename(const char *old_name,const char *new_name);
  199. extern int heap_panic(enum ha_panic_function flag);
  200. extern int heap_rsame(HP_INFO *info,uchar *record,int inx);
  201. extern int heap_rnext(HP_INFO *info,uchar *record);
  202. extern int heap_rprev(HP_INFO *info,uchar *record);
  203. extern int heap_rfirst(HP_INFO *info,uchar *record,int inx);
  204. extern int heap_rlast(HP_INFO *info,uchar *record,int inx);
  205. extern void heap_clear(HP_INFO *info);
  206. extern void heap_clear_keys(HP_INFO *info);
  207. extern int heap_disable_indexes(HP_INFO *info);
  208. extern int heap_enable_indexes(HP_INFO *info);
  209. extern int heap_indexes_are_disabled(HP_INFO *info);
  210. extern void heap_update_auto_increment(HP_INFO *info, const uchar *record);
  211. ha_rows hp_rb_records_in_range(HP_INFO *info, int inx, key_range *min_key,
  212. key_range *max_key);
  213. int hp_panic(enum ha_panic_function flag);
  214. int heap_rkey(HP_INFO *info, uchar *record, int inx, const uchar *key,
  215. key_part_map keypart_map, enum ha_rkey_function find_flag);
  216. extern uchar * heap_find(HP_INFO *info,int inx,const uchar *key);
  217. extern int heap_check_heap(HP_INFO *info, my_bool print_status);
  218. extern uchar *heap_position(HP_INFO *info);
  219. /* The following is for programs that uses the old HEAP interface where
  220. pointer to rows where a long instead of a (uchar*).
  221. */
  222. #if defined(WANT_OLD_HEAP_VERSION) || defined(OLD_HEAP_VERSION)
  223. extern int heap_rrnd_old(HP_INFO *info,uchar *buf,ulong pos);
  224. extern ulong heap_position_old(HP_INFO *info);
  225. #endif
  226. #ifdef OLD_HEAP_VERSION
  227. typedef ulong HEAP_PTR;
  228. #define heap_position(A) heap_position_old(A)
  229. #define heap_rrnd(A,B,C) heap_rrnd_old(A,B,C)
  230. #else
  231. typedef uchar *HEAP_PTR;
  232. #endif
  233. #ifdef __cplusplus
  234. }
  235. #endif
  236. #endif