1 /* Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
2 
3    This program is free software; you can redistribute it and/or modify
4    it under the terms of the GNU General Public License, version 2.0,
5    as published by the Free Software Foundation.
6 
7    This program is also distributed with certain software (including
8    but not limited to OpenSSL) that is licensed under separate terms,
9    as designated in a particular file or component or in included license
10    documentation.  The authors of MySQL hereby grant you an additional
11    permission to link the program and your derivative works with the
12    separately licensed software that they have included with MySQL.
13 
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License, version 2.0, for more details.
18 
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA */
22 
23 /* This file is included in all heap-files */
24 
25 #include <my_base.h>			/* This includes global */
26 C_MODE_START
27 #include <my_pthread.h>
28 #include "heap.h"			/* Structs & some defines */
29 #include "my_tree.h"
30 
31 /*
32   When allocating keys /rows in the internal block structure, do it
33   within the following boundaries.
34 
35   The challenge is to find the balance between allocate as few blocks
36   as possible and keep memory consumption down.
37 */
38 
39 #define HP_MIN_RECORDS_IN_BLOCK 16
40 #define HP_MAX_RECORDS_IN_BLOCK 8192
41 
42 /* this chunk has been deleted and can be reused */
43 #define CHUNK_STATUS_DELETED 0
44 /* this chunk represents the first part of a live record */
45 #define CHUNK_STATUS_ACTIVE  1
46 /* this chunk is a continuation from another chunk (part of chunkset) */
47 #define CHUNK_STATUS_LINKED  2
48 
49 	/* Some extern variables */
50 
51 extern LIST *heap_open_list,*heap_share_list;
52 
53 #define test_active(info) \
54 if (!(info->update & HA_STATE_AKTIV))\
55 { my_errno=HA_ERR_NO_ACTIVE_RECORD; DBUG_RETURN(-1); }
56 #define hp_find_hash(A,B) ((HASH_INFO*) hp_find_block((A),(B)))
57 
58 	/* Find pos for record and update it in info->current_ptr */
59 #define hp_find_record(info,pos) \
60   (info)->current_ptr= hp_find_block(&(info)->s->recordspace.block,pos)
61 
62 #define get_chunk_status(info,ptr) (ptr[(info)->offset_status])
63 
64 #define get_chunk_count(info,rec_length) \
65   ((rec_length + (info)->chunk_dataspace_length - 1) / \
66    (info)->chunk_dataspace_length)
67 
68 typedef struct st_hp_hash_info
69 {
70   struct st_hp_hash_info *next_key;
71   uchar *ptr_to_rec;
72 } HASH_INFO;
73 
74 typedef struct {
75   HA_KEYSEG *keyseg;
76   uint key_length;
77   uint search_flag;
78 } heap_rb_param;
79 
80 	/* Prototypes for intern functions */
81 
82 extern HP_SHARE *hp_find_named_heap(const char *name);
83 extern int hp_rectest(HP_INFO *info,const uchar *old);
84 extern uchar *hp_find_block(HP_BLOCK *info,ulong pos);
85 extern int hp_get_new_block(HP_BLOCK *info, size_t* alloc_length);
86 extern void hp_free(HP_SHARE *info);
87 extern uchar *hp_free_level(HP_BLOCK *block,uint level,HP_PTRS *pos,
88 			   uchar *last_pos);
89 extern int hp_write_key(HP_INFO *info, HP_KEYDEF *keyinfo,
90 			const uchar *record, uchar *recpos);
91 extern int hp_rb_write_key(HP_INFO *info, HP_KEYDEF *keyinfo,
92 			   const uchar *record, uchar *recpos);
93 extern int hp_rb_delete_key(HP_INFO *info,HP_KEYDEF *keyinfo,
94 			    const uchar *record,uchar *recpos,int flag);
95 extern int hp_delete_key(HP_INFO *info,HP_KEYDEF *keyinfo,
96 			 const uchar *record,uchar *recpos,int flag);
97 extern HASH_INFO *_heap_find_hash(HP_BLOCK *block,ulong pos);
98 extern uchar *hp_search(HP_INFO *info,HP_KEYDEF *keyinfo,const uchar *key,
99 		       uint nextflag);
100 extern uchar *hp_search_next(HP_INFO *info, HP_KEYDEF *keyinfo,
101 			    const uchar *key, HASH_INFO *pos);
102 extern ulong hp_hashnr(HP_KEYDEF *keyinfo,const uchar *key);
103 extern ulong hp_rec_hashnr(HP_KEYDEF *keyinfo,const uchar *rec);
104 extern ulong hp_mask(ulong hashnr,ulong buffmax,ulong maxlength);
105 extern void hp_movelink(HASH_INFO *pos,HASH_INFO *next_link,
106 			 HASH_INFO *newlink);
107 extern int hp_rec_key_cmp(HP_KEYDEF *keydef,const uchar *rec1,
108 			  const uchar *rec2,
109                           my_bool diff_if_only_endspace_difference);
110 extern int hp_key_cmp(HP_KEYDEF *keydef,const uchar *rec,
111 		      const uchar *key);
112 extern void hp_make_key(HP_KEYDEF *keydef,uchar *key,const uchar *rec);
113 extern uint hp_rb_make_key(HP_KEYDEF *keydef, uchar *key,
114 			   const uchar *rec, uchar *recpos, my_bool packed);
115 extern uint hp_rb_key_length(HP_KEYDEF *keydef, const uchar *key);
116 extern uint hp_rb_null_key_length(HP_KEYDEF *keydef, const uchar *key);
117 extern uint hp_rb_var_key_length(HP_KEYDEF *keydef, const uchar *key);
118 extern my_bool hp_if_null_in_key(HP_KEYDEF *keyinfo, const uchar *record);
119 extern int hp_close(HP_INFO *info);
120 extern void hp_clear(HP_SHARE *info);
121 extern void hp_clear_keys(HP_SHARE *info);
122 extern uint hp_rb_pack_key(HP_KEYDEF *keydef, uchar *key, const uchar *old,
123                            key_part_map keypart_map);
124 extern uint hp_calc_blob_length(uint length, const uchar *pos);
125 
126 /* Chunkset management (alloc/free/encode/decode) functions */
127 extern uchar *hp_allocate_chunkset(HP_DATASPACE *info, uint chunk_count);
128 extern int hp_reallocate_chunkset(HP_DATASPACE *info, uint chunk_count,
129                                   uchar *pos);
130 extern void hp_free_chunks(HP_DATASPACE *info, uchar *pos);
131 extern void hp_clear_dataspace(HP_DATASPACE *info);
132 
133 extern uint hp_get_encoded_data_length(HP_SHARE *info, const uchar *record,
134                                        uint *chunk_count);
135 extern void hp_copy_record_data_to_chunkset(HP_SHARE *info, const uchar *record,
136                                             uchar *pos);
137 extern int hp_extract_record(HP_INFO *info, uchar *record, const uchar *pos);
138 extern uint hp_process_record_data_to_chunkset(HP_SHARE *info,
139                                                const uchar *record, uchar *pos,
140                                                uint is_compare);
141 
142 extern mysql_mutex_t THR_LOCK_heap;
143 
144 #ifdef HAVE_PSI_INTERFACE
145 extern PSI_mutex_key hp_key_mutex_HP_SHARE_intern_lock;
146 void init_heap_psi_keys();
147 #endif /* HAVE_PSI_INTERFACE */
148 
149 C_MODE_END
150