Drizzled Public API Documentation

heap.h
00001 /* 
00002   Copyright (C) Brian Aker
00003     Copyright (C) 2000,2004 MySQL AB
00004 
00005    This program is free software; you can redistribute it and/or modify
00006    it under the terms of the GNU General Public License as published by
00007    the Free Software Foundation; version 2 of the License.
00008 
00009    This program is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012    GNU General Public License for more details.
00013 
00014    You should have received a copy of the GNU General Public License
00015    along with this program; if not, write to the Free Software
00016    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
00017 
00018 /* This file should be included when using heap_database_functions */
00019 /* Author: Michael Widenius */
00020 
00021 #pragma once
00022 
00023 #include <drizzled/base.h>
00024 #include <drizzled/common.h>
00025 #include <drizzled/internal/my_pthread.h>
00026 #include <drizzled/thr_lock.h>
00027 
00028 #include <plugin/myisam/my_handler.h>
00029 #include <drizzled/tree.h>
00030 
00031 #include <vector>
00032 
00033   /* defines used by heap-funktions */
00034 
00035 #define HP_MAX_LEVELS 4   /* 128^5 records is enough */
00036 #define HP_PTRS_IN_NOD  128
00037 
00038   /* struct used with heap_funktions */
00039 
00040 typedef struct st_heapinfo    /* Struct from heap_info */
00041 {
00042   uint32_t records;     /* Records in database */
00043   uint32_t deleted;     /* Deleted records in database */
00044   uint32_t max_records;
00045   uint64_t data_length;
00046   uint64_t index_length;
00047   uint32_t reclength;     /* Length of one record */
00048   int errkey;
00049   uint64_t auto_increment;
00050 
00051   st_heapinfo():
00052     records(0),
00053     deleted(0),
00054     max_records(),
00055     data_length(0),
00056     index_length(0),
00057     reclength(0),
00058     errkey(0),
00059     auto_increment(0)
00060   { }
00061 
00062 } HEAPINFO;
00063 
00064 
00065   /* Structs used by heap-database-handler */
00066 
00067 typedef struct st_heap_ptrs
00068 {
00069   unsigned char *blocks[HP_PTRS_IN_NOD];    /* pointers to HP_PTRS or records */
00070 } HP_PTRS;
00071 
00072 struct st_level_info
00073 {
00074   /* Number of unused slots in *last_blocks HP_PTRS block (0 for 0th level) */
00075   uint32_t free_ptrs_in_block;
00076 
00077   /*
00078     Maximum number of records that can be 'contained' inside of each element
00079     of last_blocks array. For level 0 - 1, for level 1 - HP_PTRS_IN_NOD, for
00080     level 2 - HP_PTRS_IN_NOD^2 and so forth.
00081   */
00082   uint32_t records_under_level;
00083 
00084   /*
00085     Ptr to last allocated HP_PTRS (or records buffer for level 0) on this
00086     level.
00087   */
00088   HP_PTRS *last_blocks;
00089 
00090   st_level_info():
00091     free_ptrs_in_block(0),
00092     records_under_level(0),
00093     last_blocks(0)
00094   { }
00095 };
00096 
00097 
00098 /*
00099   Heap table records and hash index entries are stored in HP_BLOCKs.
00100   HP_BLOCK is used as a 'growable array' of fixed-size records. Size of record
00101   is recbuffer bytes.
00102   The internal representation is as follows:
00103   HP_BLOCK is a hierarchical structure of 'blocks'.
00104   A block at level 0 is an array records_in_block records.
00105   A block at higher level is an HP_PTRS structure with pointers to blocks at
00106   lower levels.
00107   At the highest level there is one top block. It is stored in HP_BLOCK::root.
00108 
00109   See hp_find_block for a description of how record pointer is obtained from
00110   its index.
00111   See hp_get_new_block
00112 */
00113 
00114 typedef struct st_heap_block
00115 {
00116   HP_PTRS *root;                        /* Top-level block */
00117   struct st_level_info level_info[HP_MAX_LEVELS+1];
00118   uint32_t levels;                          /* number of used levels */
00119   uint32_t records_in_block;    /* Records in one heap-block */
00120   uint32_t recbuffer;     /* Length of one saved record */
00121   uint32_t last_allocated; /* number of records there is allocated space for */
00122 
00123   st_heap_block() :
00124     root(NULL),
00125     levels(0),
00126     records_in_block(0),
00127     recbuffer(0),
00128     last_allocated(0)
00129   {
00130   }
00131 } HP_BLOCK;
00132 
00133 struct st_heap_info;      /* For referense */
00134 
00135 typedef struct st_hp_keydef   /* Key definition with open */
00136 {
00137   uint32_t flag;        /* HA_NOSAME | HA_NULL_PART_KEY */
00138   uint32_t keysegs;       /* Number of key-segment */
00139   uint32_t length;        /* Length of key (automatic) */
00140   HA_KEYSEG *seg;
00141   HP_BLOCK block;     /* Where keys are saved */
00142   /*
00143     Number of buckets used in hash table. Used only to provide
00144     #records estimates for heap key scans.
00145   */
00146   drizzled::ha_rows hash_buckets;
00147 } HP_KEYDEF;
00148 
00149 typedef struct st_heap_dataspace   /* control data for data space */
00150 {
00151   HP_BLOCK block;
00152   uint32_t chunk_count;             /* Total chunks ever allocated in this dataspace */
00153   uint32_t del_chunk_count;         /* Deleted chunks count */
00154   unsigned char *del_link;               /* Link to last deleted chunk */
00155   uint32_t chunk_length;            /* Total length of one chunk */
00156   uint32_t chunk_dataspace_length;  /* Length of payload that will be placed into one chunk */
00157   uint32_t offset_status;           /* Offset of the status flag relative to the chunk start */
00158   uint32_t offset_link;             /* Offset of the linking pointer relative to the chunk start */
00159   uint64_t total_data_length;  /* Total size allocated within this data space */
00160 
00161   st_heap_dataspace() :
00162     chunk_count(0),
00163     del_chunk_count(0),
00164     del_link(0),
00165     chunk_length(0),
00166     chunk_dataspace_length(0),
00167     offset_status(0),
00168     offset_link(0),
00169     total_data_length(0)
00170   { }
00171 
00172 } HP_DATASPACE;
00173 
00174 
00175 typedef struct st_heap_share
00176 {
00177   HP_KEYDEF  *keydef;
00178   HP_DATASPACE recordspace;  /* Describes "block", which contains actual records */
00179 
00180   uint32_t min_records,max_records; /* Params to open */
00181   uint64_t index_length,max_table_size;
00182   uint32_t key_stat_version;                /* version to indicate insert/delete */
00183   uint32_t records;             /* Actual record (row) count */
00184   uint32_t blength;                                     /* used_chunk_count rounded up to 2^n */
00185   uint32_t fixed_data_length;     /* Length of record's fixed part, which contains keys and always fits into the first chunk */
00186   uint32_t fixed_column_count;  /* Number of columns stored in fixed_data_length */
00187   uint32_t changed;
00188   uint32_t keys,max_key_length;
00189   uint32_t column_count;
00190   uint32_t currently_disabled_keys;    /* saved value from "keys" when disabled */
00191   uint32_t open_count;
00192 
00193 
00194   std::string name;     /* Name of "memory-file" */
00195   bool delete_on_close;
00196   uint32_t auto_key;
00197   uint32_t auto_key_type;     /* real type of the auto key segment */
00198   uint64_t auto_increment;
00199 
00200   st_heap_share() :
00201     keydef(0),
00202     min_records(0),
00203     max_records(0),
00204     index_length(0),
00205     max_table_size(0),
00206     key_stat_version(0),
00207     records(0),
00208     blength(0),
00209     fixed_data_length(0),
00210     fixed_column_count(0),
00211     changed(0),
00212     keys(0),
00213     max_key_length(0),
00214     column_count(0),
00215     currently_disabled_keys(0),
00216     open_count(0),
00217     delete_on_close(0),
00218     auto_key(0),
00219     auto_key_type(0),
00220     auto_increment(0)
00221   { }
00222 
00223 } HP_SHARE;
00224 
00225 struct st_hp_hash_info;
00226 
00227 typedef struct st_heap_info
00228 {
00229 private:
00230   HP_SHARE *s;
00231 public:
00232 
00233   HP_SHARE *getShare()
00234   {
00235     return s;
00236   }
00237 
00238   void setShare(HP_SHARE *s_arg)
00239   {
00240     s= s_arg;
00241   }
00242 
00243   unsigned char *current_ptr;
00244   struct st_hp_hash_info *current_hash_ptr;
00245   uint32_t current_record,next_block;
00246   int lastinx,errkey;
00247   int  mode;        /* Mode of file (READONLY..) */
00248   uint32_t opt_flag,update;
00249   std::vector <unsigned char> lastkey;      /* Last used key with rkey */
00250   enum drizzled::ha_rkey_function last_find_flag;
00251   uint32_t lastkey_len;
00252   drizzled::THR_LOCK_DATA lock;
00253 } HP_INFO;
00254 
00255 
00256 typedef struct st_heap_create_info
00257 {
00258   uint32_t auto_key;                        /* keynr [1 - maxkey] for auto key */
00259   uint32_t auto_key_type;
00260   uint32_t max_chunk_size;
00261   uint64_t max_table_size;
00262   uint64_t auto_increment;
00263   bool with_auto_increment;
00264   bool internal_table;
00265 } HP_CREATE_INFO;
00266 
00267   /* Prototypes for heap-functions */
00268 
00269 extern HP_INFO *heap_open(const char *name, int mode);
00270 extern HP_INFO *heap_open_from_share(HP_SHARE *share, int mode);
00271 extern HP_INFO *heap_open_from_share_and_register(HP_SHARE *share, int mode);
00272 extern int heap_close(HP_INFO *info);
00273 extern int heap_write(HP_INFO *info,const unsigned char *record);
00274 extern int heap_update(HP_INFO *info,const unsigned char *old_record,const unsigned char *new_record);
00275 extern int heap_rrnd(HP_INFO *info,unsigned char *buf,unsigned char *pos);
00276 extern int heap_scan_init(HP_INFO *info);
00277 extern int heap_scan(register HP_INFO *info, unsigned char *record);
00278 extern int heap_delete(HP_INFO *info,const unsigned char *buff);
00279 extern int heap_info(HP_INFO *info,HEAPINFO *x,int flag);
00280 extern int heap_create(const char *name, uint32_t keys, HP_KEYDEF *keydef,
00281            uint32_t columns, 
00282            uint32_t key_part_size,
00283            uint32_t reclength, uint32_t keys_memory_size,
00284            uint32_t max_records, uint32_t min_records,
00285            HP_CREATE_INFO *create_info, HP_SHARE **share);
00286 
00287 extern int heap_delete_table(const char *name);
00288 extern int heap_extra(HP_INFO *info,enum drizzled::ha_extra_function function);
00289 extern int heap_reset(HP_INFO *info);
00290 extern int heap_rename(const char *old_name,const char *new_name);
00291 extern int heap_panic(enum drizzled::ha_panic_function flag);
00292 extern int heap_rsame(HP_INFO *info,unsigned char *record,int inx);
00293 extern int heap_rnext(HP_INFO *info,unsigned char *record);
00294 extern int heap_rprev(HP_INFO *info,unsigned char *record);
00295 extern int heap_rfirst(HP_INFO *info,unsigned char *record,int inx);
00296 extern int heap_rlast(HP_INFO *info,unsigned char *record,int inx);
00297 extern void heap_clear(HP_INFO *info);
00298 extern int heap_disable_indexes(HP_INFO *info);
00299 extern int heap_enable_indexes(HP_INFO *info);
00300 extern int heap_indexes_are_disabled(HP_INFO *info);
00301 extern void heap_update_auto_increment(HP_INFO *info, const unsigned char *record);
00302 int hp_panic(enum drizzled::ha_panic_function flag);
00303 int heap_rkey(HP_INFO *info, unsigned char *record, int inx, const unsigned char *key,
00304               drizzled::key_part_map keypart_map,
00305               enum drizzled::ha_rkey_function find_flag);
00306 extern unsigned char * heap_find(HP_INFO *info,int inx,const unsigned char *key);
00307 extern unsigned char *heap_position(HP_INFO *info);
00308 
00309 /* The following is for programs that uses the old HEAP interface where
00310    pointer to rows where a long instead of a (unsigned char*).
00311 */
00312 
00313 typedef unsigned char *HEAP_PTR;
00314