CUBRID Engine  latest
btree_load.h
Go to the documentation of this file.
1 /*
2  * Copyright 2008 Search Solution Corporation
3  * Copyright 2016 CUBRID Corporation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
19 
20 /*
21  * btree_load.h: Contains private information of B+tree Module
22  */
23 
24 #ifndef _BTREE_LOAD_H_
25 #define _BTREE_LOAD_H_
26 
27 #ident "$Id$"
28 
29 #include <assert.h>
30 
31 #include "btree.h"
33 #include "error_manager.h"
34 #include "storage_common.h"
35 #include "oid.h"
36 #include "system_parameter.h"
37 #include "object_domain.h"
38 #include "slotted_page.h"
39 
40 /*
41  * Constants related to b+tree structure
42  */
43 
44 #define PEEK_KEY_VALUE PEEK
45 #define COPY_KEY_VALUE COPY
46 
47 /* The revision level of the the Btree should be incremented whenever there
48  * is a disk representation change for the Btree structure.
49  */
50 #define BTREE_CURRENT_REV_LEVEL 0
51 
52 /* each index page is supposed to be left empty as indicated by the
53  * UNFILL FACTOR during index loading phase.
54  */
55 #define LOAD_FIXED_EMPTY_FOR_LEAF \
56  (DB_PAGESIZE * prm_get_float_value (PRM_ID_BT_UNFILL_FACTOR) \
57  + DISK_VPID_SIZE)
58 #define LOAD_FIXED_EMPTY_FOR_NONLEAF \
59  (DB_PAGESIZE * MAX (prm_get_float_value (PRM_ID_BT_UNFILL_FACTOR), 0.1) \
60  + DISK_VPID_SIZE)
61 
62 /* each page is supposed to have around 30% blank area during merge
63  considerations of a delete operation */
64 
65 /* Maximum Alignment */
66 #define BTREE_MAX_ALIGN INT_ALIGNMENT
67 
68 /* Maximum Leaf Node Entry Size */
69 #define LEAF_ENTRY_MAX_SIZE(n) \
70  (LEAF_RECORD_SIZE \
71  + (2 * OR_OID_SIZE) /* OID + class OID */ \
72  + (2 * OR_MVCCID_SIZE) /* Insert/delete MVCCID */ \
73  + BTREE_MAX_ALIGN /* Alignment */ \
74  + n /* Key disk length. */)
75 
76 /* Maximum size of new fence key. */
77 #define LEAF_FENCE_MAX_SIZE(n) \
78  (LEAF_RECORD_SIZE \
79  + OR_OID_SIZE /* OID marker for fence key */ \
80  + BTREE_MAX_ALIGN /* Alignment */ \
81  + n /* Key disk length. */)
82 
83 /* Maximum Non_Leaf Entry Size */
84 #define NON_LEAF_ENTRY_MAX_SIZE(n) \
85  (NON_LEAF_RECORD_SIZE + BTREE_MAX_ALIGN + n)
86 
87 /* New b-tree entry maximum size. */
88 #define BTREE_NEW_ENTRY_MAX_SIZE(key_disk_size, node_type) \
89  ((node_type) == BTREE_LEAF_NODE ? \
90  LEAF_ENTRY_MAX_SIZE (key_disk_size) : \
91  NON_LEAF_ENTRY_MAX_SIZE (key_disk_size))
92 
93 /* compare two object identifiers */
94 #define OIDCMP(n1, n2) \
95  ((n1).volid == (n2).volid \
96  && (n1).pageid == (n2).pageid \
97  && (n1).slotid == (n2).slotid)
98 
99 /* Header (Oth) record of the page */
100 #define HEADER 0
101 
102 #if !defined(NDEBUG)
103 #define BTREE_INVALID_INDEX_ID(btid) \
104  ((btid)->vfid.fileid == NULL_FILEID || (btid)->vfid.volid == NULL_VOLID \
105  || (btid)->root_pageid == NULL_PAGEID)
106 #endif
107 
108 /* The size of an object in cases it has fixed size (includes all required
109  * info).
110  * In case of unique: OID, class OID, insert and delete MVCCID.
111  * In case of non-unique: OID, insert and delete MVCCID.
112  *
113  * Fixed size is used when:
114  * 1. object is saved in overflow page.
115  * 2. object is non-first in leaf record.
116  * 3. object is first in a leaf record that has overflow pages.
117  */
118 #define BTREE_OBJECT_FIXED_SIZE(btree_info) \
119  (BTREE_IS_UNIQUE ((btree_info)->unique_pk) ? \
120  2 * OR_OID_SIZE + 2 * OR_MVCCID_SIZE : OR_OID_SIZE + 2 * OR_MVCCID_SIZE)
121 /* Maximum possible size for one b-tree object including all its information.
122  */
123 #define BTREE_OBJECT_MAX_SIZE (2 * OR_OID_SIZE + 2 * OR_MVCCID_SIZE)
124 
125 /*
126  * Overflow key related defines
127  */
128 
129 /* We never want to store keys larger than an eighth of the page size
130  * directly on the btree page since this will make the btree too deep.
131  * Large keys are automatically stored on overflow pages. With prefix
132  * keys this shouldn't be much of a problem anyway (when we get them
133  * turned back on).
134  */
135 #define BTREE_MAX_KEYLEN_INPAGE ((int)(DB_PAGESIZE / 8))
136 /* in MVCC BTREE_MAX_OIDLEN_INPAGE include MVCC fields too */
137 #define BTREE_MAX_OIDLEN_INPAGE ((int)(DB_PAGESIZE / 8))
138 
139 /* Maximum number of objects that surely fit given size including all info. */
140 #define BTREE_MAX_OIDCOUNT_IN_SIZE(btid, size) \
141  ((int) (size) / BTREE_OBJECT_FIXED_SIZE (btid))
142 /* Maximum number of objects for a leaf record */
143 #define BTREE_MAX_OIDCOUNT_IN_LEAF_RECORD(btid) \
144  (BTREE_MAX_OIDCOUNT_IN_SIZE (btid, BTREE_MAX_OIDLEN_INPAGE))
145 
146 #define BTREE_MAX_OVERFLOW_RECORD_SIZE \
147  (DB_PAGESIZE - DB_ALIGN (spage_header_size (), BTREE_MAX_ALIGN) \
148  - DB_ALIGN (sizeof (BTREE_OVERFLOW_HEADER), BTREE_MAX_ALIGN))
149 #define BTREE_MAX_OIDCOUNT_IN_OVERFLOW_RECORD(btid) \
150  (BTREE_MAX_OIDCOUNT_IN_SIZE (btid, BTREE_MAX_OVERFLOW_RECORD_SIZE))
151 
152 extern int btree_node_number_of_keys (THREAD_ENTRY * thread_p, PAGE_PTR page_ptr);
153 extern int btree_get_next_overflow_vpid (THREAD_ENTRY * thread_p, PAGE_PTR page_ptr, VPID * vpid);
154 
155 #define BTREE_GET_KEY_LEN_IN_PAGE(key_len) \
156  (((key_len) >= BTREE_MAX_KEYLEN_INPAGE) ? DISK_VPID_SIZE : (key_len))
157 
158 /* for notification log messages */
159 #define BTREE_SET_CREATED_OVERFLOW_KEY_NOTIFICATION(THREAD,KEY,OID,C_OID,BTID,BTNM) \
160  btree_set_error(THREAD, KEY, OID, C_OID, BTID, BTNM, ER_NOTIFICATION_SEVERITY, \
161  ER_BTREE_CREATED_OVERFLOW_KEY, __FILE__, __LINE__)
162 
163 #define BTREE_SET_CREATED_OVERFLOW_PAGE_NOTIFICATION(THREAD,KEY,OID,C_OID,BTID) \
164  btree_set_error(THREAD, KEY, OID, C_OID, BTID, NULL, ER_NOTIFICATION_SEVERITY, \
165  ER_BTREE_CREATED_OVERFLOW_PAGE, __FILE__, __LINE__)
166 
167 #define BTREE_SET_DELETED_OVERFLOW_PAGE_NOTIFICATION(THREAD,KEY,OID,C_OID,BTID) \
168  btree_set_error(THREAD, KEY, OID, C_OID, BTID, NULL, ER_NOTIFICATION_SEVERITY, \
169  ER_BTREE_DELETED_OVERFLOW_PAGE, __FILE__, __LINE__)
170 
171 /* set fixed size for MVCC record header */
172 inline void
174 {
175  assert (p_mvcc_rec_header != NULL);
176  if (!(p_mvcc_rec_header->mvcc_flag & OR_MVCC_FLAG_VALID_INSID))
177  {
178  p_mvcc_rec_header->mvcc_flag |= OR_MVCC_FLAG_VALID_INSID;
179  p_mvcc_rec_header->mvcc_ins_id = MVCCID_ALL_VISIBLE;
180  }
181  if (!(p_mvcc_rec_header->mvcc_flag & OR_MVCC_FLAG_VALID_DELID))
182  {
183  p_mvcc_rec_header->mvcc_flag |= OR_MVCC_FLAG_VALID_DELID;
184  p_mvcc_rec_header->mvcc_del_id = MVCCID_NULL;
185  }
186 }
187 
188 /*
189  * Type definitions related to b+tree structure and operations
190  */
191 
192 /* Node header information */
195 {
196  BTREE_NODE_SPLIT_INFO split_info; /* split point info. of the node */
197  VPID prev_vpid; /* Leaf Page Previous Node Pointer */
198  VPID next_vpid; /* Leaf Page Next Node Pointer */
199  short node_level; /* btree depth; Leaf(= 1), Non_leaf(> 1) */
200  short max_key_len; /* Maximum key length for the subtree */
201 };
202 
203 /* Root header information */
206 {
208  int num_oids; /* Number of OIDs stored in the Btree */
209  int num_nulls; /* Number of NULLs (they aren't stored) */
210  int num_keys; /* Number of unique keys in the Btree */
211  OID topclass_oid; /* topclass oid or NULL OID(non unique index) */
212  int unique_pk; /* unique or non-unique, is primary key */
213  int reverse_reserved; /* reverse or normal *//* not used */
214  int rev_level; /* Btree revision level */
215  VFID ovfid; /* Overflow file */
216  MVCCID creator_mvccid; /* MVCCID of creator transaction. */
217 
218  /* Always leave this field last. */
219  char packed_key_domain[1]; /* The key type for the index */
220 };
221 
222 /* overflow header information */
225 {
227 };
228 
231 {
232  short max_key_len; /* Maximum key length for the subtree */
233  int height; /* The height of the subtree */
234  INT32 tot_key_cnt; /* Total key count in the subtree */
235  int page_cnt; /* Total page count in the subtree */
236  int leafpg_cnt; /* Total leaf page count in the subtree */
237  int nleafpg_cnt; /* Total non_leaf page count */
238  int key_area_len; /* Current max_key area length malloced */
239  DB_VALUE max_key; /* Largest key in the subtreee */
240 }; /* contains statistical data for testing purposes */
241 
242 /*
243  * B+tree load structures
244  */
245 
246 typedef struct btree_node BTREE_NODE;
248 {
249  BTREE_NODE *next; /* Pointer to next node */
250  VPID pageid; /* Identifier of the page */
251 };
252 
253 /* Recovery routines */
254 extern void btree_rv_nodehdr_dump (FILE * fp, int length, void *data);
255 extern void btree_rv_mvcc_save_increments (const BTID * btid, int key_delta, int oid_delta, int null_delta,
256  RECDES * recdes);
257 
258 extern bool btree_clear_key_value (bool * clear_flag, DB_VALUE * key_value);
259 extern void btree_init_temp_key_value (bool * clear_flag, DB_VALUE * key_value);
260 extern int btree_create_overflow_key_file (THREAD_ENTRY * thread_p, BTID_INT * btid);
261 extern int btree_init_overflow_header (THREAD_ENTRY * thread_p, PAGE_PTR page_ptr, BTREE_OVERFLOW_HEADER * ovf_header);
262 extern int btree_init_node_header (THREAD_ENTRY * thread_p, const VFID * vfid, PAGE_PTR page_ptr,
263  BTREE_NODE_HEADER * header, bool redo);
264 extern int btree_init_root_header (THREAD_ENTRY * thread_p, VFID * vfid, PAGE_PTR page_ptr,
265  BTREE_ROOT_HEADER * root_header, TP_DOMAIN * key_type);
266 extern BTREE_NODE_HEADER *btree_get_node_header (THREAD_ENTRY * thread_p, PAGE_PTR page_ptr);
267 extern BTREE_ROOT_HEADER *btree_get_root_header (THREAD_ENTRY * thread_p, PAGE_PTR page_ptr);
269 extern int btree_node_header_undo_log (THREAD_ENTRY * thread_p, VFID * vfid, PAGE_PTR page_ptr);
270 extern int btree_node_header_redo_log (THREAD_ENTRY * thread_p, VFID * vfid, PAGE_PTR page_ptr);
271 extern int btree_change_root_header_delta (THREAD_ENTRY * thread_p, VFID * vfid, PAGE_PTR page_ptr, int null_delta,
272  int oid_delta, int key_delta);
273 
274 extern int btree_get_disk_size_of_key (DB_VALUE *);
276 extern int btree_glean_root_header_info (THREAD_ENTRY * thread_p, BTREE_ROOT_HEADER * root_header, BTID_INT * btid);
277 extern DISK_ISVALID btree_verify_tree (THREAD_ENTRY * thread_p, const OID * class_oid_p, BTID_INT * btid,
278  const char *btname);
279 extern int btree_get_prefix_separator (const DB_VALUE * key1, const DB_VALUE * key2, DB_VALUE * prefix_key,
280  TP_DOMAIN * key_domain);
281 
282 extern int btree_get_asc_desc (THREAD_ENTRY * thread_p, BTID * btid, int col_idx, int *asc_desc);
283 
284 #endif /* _BTREE_LOAD_H_ */
char * PAGE_PTR
INT32 mvcc_flag
Definition: mvcc.h:40
MVCCID mvcc_ins_id
Definition: mvcc.h:43
bool btree_clear_key_value(bool *clear_flag, DB_VALUE *key_value)
Definition: btree.c:1919
int btree_change_root_header_delta(THREAD_ENTRY *thread_p, VFID *vfid, PAGE_PTR page_ptr, int null_delta, int oid_delta, int key_delta)
Definition: btree_load.c:461
DB_VALUE max_key
Definition: btree_load.h:239
#define OR_MVCC_FLAG_VALID_INSID
BTREE_NODE_HEADER * btree_get_node_header(THREAD_ENTRY *thread_p, PAGE_PTR page_ptr)
Definition: btree_load.c:275
#define MVCCID_NULL
#define OR_MVCC_FLAG_VALID_DELID
int btree_glean_root_header_info(THREAD_ENTRY *thread_p, BTREE_ROOT_HEADER *root_header, BTID_INT *btid)
Definition: btree.c:5797
TP_DOMAIN * btree_generate_prefix_domain(BTID_INT *btid)
Definition: btree.c:5749
int btree_get_next_overflow_vpid(THREAD_ENTRY *thread_p, PAGE_PTR page_ptr, VPID *vpid)
Definition: btree_load.c:677
void THREAD_ENTRY
#define MVCCID_ALL_VISIBLE
int btree_get_asc_desc(THREAD_ENTRY *thread_p, BTID *btid, int col_idx, int *asc_desc)
Definition: btree.c:18408
#define assert(x)
BTREE_NODE_HEADER node
Definition: btree_load.h:207
int btree_node_header_redo_log(THREAD_ENTRY *thread_p, VFID *vfid, PAGE_PTR page_ptr)
Definition: btree_load.c:436
void btree_init_temp_key_value(bool *clear_flag, DB_VALUE *key_value)
Definition: btree.c:1938
MVCCID creator_mvccid
Definition: btree_load.h:216
#define NULL
Definition: freelistheap.h:34
UINT64 MVCCID
int btree_node_header_undo_log(THREAD_ENTRY *thread_p, VFID *vfid, PAGE_PTR page_ptr)
Definition: btree_load.c:414
int btree_init_root_header(THREAD_ENTRY *thread_p, VFID *vfid, PAGE_PTR page_ptr, BTREE_ROOT_HEADER *root_header, TP_DOMAIN *key_type)
Definition: btree_load.c:540
BTREE_NODE * next
Definition: btree_load.h:249
int btree_create_overflow_key_file(THREAD_ENTRY *thread_p, BTID_INT *btid)
Definition: btree.c:1953
BTREE_ROOT_HEADER * btree_get_root_header(THREAD_ENTRY *thread_p, PAGE_PTR page_ptr)
Definition: btree_load.c:309
void btree_rv_mvcc_save_increments(const BTID *btid, int key_delta, int oid_delta, int null_delta, RECDES *recdes)
Definition: btree_load.c:645
int btree_init_node_header(THREAD_ENTRY *thread_p, const VFID *vfid, PAGE_PTR page_ptr, BTREE_NODE_HEADER *header, bool redo)
Definition: btree_load.c:373
int btree_node_number_of_keys(THREAD_ENTRY *thread_p, PAGE_PTR page_ptr)
Definition: btree_load.c:3755
void BTREE_MVCC_SET_HEADER_FIXED_SIZE(MVCC_REC_HEADER *p_mvcc_rec_header)
Definition: btree_load.h:173
VPID pageid
Definition: btree_load.h:250
DISK_ISVALID btree_verify_tree(THREAD_ENTRY *thread_p, const OID *class_oid_p, BTID_INT *btid, const char *btname)
Definition: btree.c:7568
void btree_rv_nodehdr_dump(FILE *fp, int length, void *data)
Definition: btree_load.c:3737
int btree_init_overflow_header(THREAD_ENTRY *thread_p, PAGE_PTR page_ptr, BTREE_OVERFLOW_HEADER *ovf_header)
Definition: btree_load.c:579
BTREE_OVERFLOW_HEADER * btree_get_overflow_header(THREAD_ENTRY *thread_p, PAGE_PTR page_ptr)
Definition: btree_load.c:344
MVCCID mvcc_del_id
Definition: mvcc.h:44
BTREE_NODE_SPLIT_INFO split_info
Definition: btree_load.h:196
int btree_get_disk_size_of_key(DB_VALUE *)
Definition: btree.c:4041
DISK_ISVALID
Definition: disk_manager.h:53
int btree_get_prefix_separator(const DB_VALUE *key1, const DB_VALUE *key2, DB_VALUE *prefix_key, TP_DOMAIN *key_domain)
Definition: btree.c:11763