You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

480 lines
16 KiB

  1. /* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
  2. // vim: ft=cpp:expandtab:ts=8:sw=4:softtabstop=4:
  3. #ident "$Id$"
  4. /*
  5. COPYING CONDITIONS NOTICE:
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of version 2 of the GNU General Public License as
  8. published by the Free Software Foundation, and provided that the
  9. following conditions are met:
  10. * Redistributions of source code must retain this COPYING
  11. CONDITIONS NOTICE, the COPYRIGHT NOTICE (below), the
  12. DISCLAIMER (below), the UNIVERSITY PATENT NOTICE (below), the
  13. PATENT MARKING NOTICE (below), and the PATENT RIGHTS
  14. GRANT (below).
  15. * Redistributions in binary form must reproduce this COPYING
  16. CONDITIONS NOTICE, the COPYRIGHT NOTICE (below), the
  17. DISCLAIMER (below), the UNIVERSITY PATENT NOTICE (below), the
  18. PATENT MARKING NOTICE (below), and the PATENT RIGHTS
  19. GRANT (below) in the documentation and/or other materials
  20. provided with the distribution.
  21. You should have received a copy of the GNU General Public License
  22. along with this program; if not, write to the Free Software
  23. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  24. 02110-1301, USA.
  25. COPYRIGHT NOTICE:
  26. TokuDB, Tokutek Fractal Tree Indexing Library.
  27. Copyright (C) 2007-2013 Tokutek, Inc.
  28. DISCLAIMER:
  29. This program is distributed in the hope that it will be useful, but
  30. WITHOUT ANY WARRANTY; without even the implied warranty of
  31. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  32. General Public License for more details.
  33. UNIVERSITY PATENT NOTICE:
  34. The technology is licensed by the Massachusetts Institute of
  35. Technology, Rutgers State University of New Jersey, and the Research
  36. Foundation of State University of New York at Stony Brook under
  37. United States of America Serial No. 11/760379 and to the patents
  38. and/or patent applications resulting from it.
  39. PATENT MARKING NOTICE:
  40. This software is covered by US Patent No. 8,185,551.
  41. PATENT RIGHTS GRANT:
  42. "THIS IMPLEMENTATION" means the copyrightable works distributed by
  43. Tokutek as part of the Fractal Tree project.
  44. "PATENT CLAIMS" means the claims of patents that are owned or
  45. licensable by Tokutek, both currently or in the future; and that in
  46. the absence of this license would be infringed by THIS
  47. IMPLEMENTATION or by using or running THIS IMPLEMENTATION.
  48. "PATENT CHALLENGE" shall mean a challenge to the validity,
  49. patentability, enforceability and/or non-infringement of any of the
  50. PATENT CLAIMS or otherwise opposing any of the PATENT CLAIMS.
  51. Tokutek hereby grants to you, for the term and geographical scope of
  52. the PATENT CLAIMS, a non-exclusive, no-charge, royalty-free,
  53. irrevocable (except as stated in this section) patent license to
  54. make, have made, use, offer to sell, sell, import, transfer, and
  55. otherwise run, modify, and propagate the contents of THIS
  56. IMPLEMENTATION, where such license applies only to the PATENT
  57. CLAIMS. This grant does not include claims that would be infringed
  58. only as a consequence of further modifications of THIS
  59. IMPLEMENTATION. If you or your agent or licensee institute or order
  60. or agree to the institution of patent litigation against any entity
  61. (including a cross-claim or counterclaim in a lawsuit) alleging that
  62. THIS IMPLEMENTATION constitutes direct or contributory patent
  63. infringement, or inducement of patent infringement, then any rights
  64. granted to you under this License shall terminate as of the date
  65. such litigation is filed. If you or your agent or exclusive
  66. licensee institute or order or agree to the institution of a PATENT
  67. CHALLENGE, then Tokutek may terminate any rights granted to you
  68. under this License.
  69. */
  70. #ident "Copyright (c) 2007-2013 Tokutek Inc. All rights reserved."
  71. #ident "The technology is licensed by the Massachusetts Institute of Technology, Rutgers State University of New Jersey, and the Research Foundation of State University of New York at Stony Brook under United States of America Serial No. 11/760379 and to the patents and/or patent applications resulting from it."
  72. #ifndef _HATOKU_CMP
  73. #define _HATOKU_CMP
  74. #include "stdint.h"
  75. #include <db.h>
  76. //
  77. // A MySQL row is encoded in TokuDB, as follows:
  78. // Keys:
  79. // Keys pack the defined columns in the order that they are declared.
  80. // The primary key contains only the columns listed
  81. // If no primary key is defined, then an eight byte hidden primary key is autogenerated (like an auto increment) and used
  82. // Secondary keys contains the defined key and the primary key.
  83. // Two examples:
  84. // 1) table foo (a int, b int, c int, d int, key(b))
  85. // The key of the main dictionary contains an eight byte autogenerated hidden primary key
  86. // The key of key-b is the column 'b' followed by the hidden primary key
  87. // 2) table foo (a int, b int, c int, d int, primary key(a), key(b))
  88. // The key of the main dictionary contains 'a'
  89. // The key of key-b is the column 'b followed by 'a'
  90. // Vals:
  91. // For secondary keys they are empty.
  92. // For the main dictionary and clustering keys, they contain all columns that do not show up in the dictionary's key
  93. // Two examples:
  94. // 1) table foo (a int, b int, c int, d varchar(100), primary key(a), clustering key d(d), clustering key d2(d(20))
  95. // the val of the main dictionary contains (b,c,d)
  96. // the val of d contains (b,c)
  97. // the val of d2 contains (b,c,d). d is there because the entire row does not show up in the key
  98. // Vals are encoded as follows. They have four components:
  99. // 1) Null bytes: contains a bit field that states what columns are NULL.
  100. // 2) Fixed fields: all fixed fields are then packed together. If a fixed field is NULL, its data is considered junk
  101. // 3) varchars and varbinaries: stored in two pieces, first all the offsets and then all the data. If a var field is NULL, its data is considered junk
  102. // 4) blobs: stored in (length, data) pairs. If a blob is NULL, its data is considered junk
  103. // An example:
  104. // Table: (a int, b varchar(20), c blob, d bigint, e varbinary(10), f largeblob, g varchar(10)) <-- no primary key defined
  105. // Row inserted: (1, "bbb", "cc", 100, "eeeee", "ffff", "g")
  106. // The packed format of the val looks like:
  107. // NULL byte <-- 1 byte to encode nothing is NULL
  108. // 1 <-- four bytes for 'a'
  109. // 100 <-- four bytes for 'd'
  110. // 3,8,9 <--offsets for location of data fields, note offsets point to where data ENDS
  111. // "bbbeeeeeg" <-- data for variable length stuff
  112. // 2,"cc",4,"ffff"<-- data that stores the blobs
  113. // The structures below describe are used for the TokuDB encoding of a row
  114. //
  115. // used for queries
  116. typedef struct st_col_pack_info {
  117. uint32_t col_pack_val; //offset if fixed, pack_index if var
  118. } COL_PACK_INFO;
  119. //
  120. // used to define a couple of characteristics of a packed val for the main dictionary or a clustering dictionary
  121. // fixed_field_size is the size of the fixed fields in the val.
  122. // len_of_offsets is the size of the bytes that make up the offsets of variable size columns
  123. // Some notes:
  124. // If the val has no fixed fields, fixed_field_size is 0
  125. // If the val has no variable fields, len_of_offsets is 0
  126. // The number of null bytes at the beginning of a row is not saved, it is derived from table_share->null_bytes
  127. // The pointer to where the variable data in a val starts is table_share->null_bytes + fixed_field_size + len_of_offsets
  128. // To figure out where the blobs start, find the last offset listed (if offsets exist)
  129. //
  130. typedef struct st_multi_col_pack_info {
  131. uint32_t fixed_field_size; //where the fixed length stuff ends and the offsets for var stuff begins
  132. uint32_t len_of_offsets; //length of the offset bytes in a packed row
  133. } MULTI_COL_PACK_INFO;
  134. typedef struct st_key_and_col_info {
  135. //
  136. // bitmaps for each key. key_filters[i] is associated with the i'th dictionary
  137. // States what columns are not stored in the vals of each key, because
  138. // the column is stored in the key. So, for example, the table (a int, b int, c int, d int, primary key (b,d)) will
  139. // have bit 1 (for 'b') and bit 3 (for 'd') of the primary key's bitmap set for the main dictionary's bitmap,
  140. // because 'b' and 'd' do not show up in the val
  141. //
  142. MY_BITMAP key_filters[MAX_KEY+1];
  143. //
  144. // following three arrays are used to identify the types of rows in the field
  145. // If table->field[i] is a fixed field:
  146. // field_lengths[i] stores the field length, which is fixed
  147. // length_bytes[i] is 0
  148. // 'i' does not show up in the array blob_fields
  149. // If table->field[i] is a varchar or varbinary:
  150. // field_lengths[i] is 0
  151. // length_bytes[i] stores the number of bytes MySQL uses to encode the length of the field in table->record[0]
  152. // 'i' does not show up in the array blob_fields
  153. // If table->field[i] is a blob:
  154. // field_lengths[i] is 0
  155. // length_bytes[i] is 0
  156. // 'i' shows up in blob_fields
  157. //
  158. uint16_t* field_lengths; //stores the field lengths of fixed size fields (1<<16 - 1 max),
  159. uchar* length_bytes; // stores the length of lengths of varchars and varbinaries
  160. uint32_t* blob_fields; // list of indexes of blob fields,
  161. uint32_t num_blobs; // number of blobs in the table
  162. //
  163. // val packing info for all dictionaries. i'th one represents info for i'th dictionary
  164. //
  165. MULTI_COL_PACK_INFO mcp_info[MAX_KEY+1];
  166. COL_PACK_INFO* cp_info[MAX_KEY+1];
  167. //
  168. // number bytes used to represent an offset in a val. Can be 1 or 2.
  169. // The number of var fields in a val for dictionary i can be evaluated by
  170. // mcp_info[i].len_of_offsets/num_offset_bytes.
  171. //
  172. uint32_t num_offset_bytes; //number of bytes needed to encode the offset
  173. } KEY_AND_COL_INFO;
  174. bool field_valid_for_tokudb_table(Field* field);
  175. void get_var_field_info(
  176. uint32_t* field_len,
  177. uint32_t* start_offset,
  178. uint32_t var_field_index,
  179. const uchar* var_field_offset_ptr,
  180. uint32_t num_offset_bytes
  181. );
  182. void get_blob_field_info(
  183. uint32_t* start_offset,
  184. uint32_t len_of_offsets,
  185. const uchar* var_field_data_ptr,
  186. uint32_t num_offset_bytes
  187. );
  188. static inline uint32_t get_blob_field_len(
  189. const uchar* from_tokudb,
  190. uint32_t len_bytes
  191. )
  192. {
  193. uint32_t length = 0;
  194. switch (len_bytes) {
  195. case (1):
  196. length = (uint32_t)(*from_tokudb);
  197. break;
  198. case (2):
  199. length = uint2korr(from_tokudb);
  200. break;
  201. case (3):
  202. length = uint3korr(from_tokudb);
  203. break;
  204. case (4):
  205. length = uint4korr(from_tokudb);
  206. break;
  207. default:
  208. assert(false);
  209. }
  210. return length;
  211. }
  212. static inline const uchar* unpack_toku_field_blob(
  213. uchar *to_mysql,
  214. const uchar* from_tokudb,
  215. uint32_t len_bytes,
  216. bool skip
  217. )
  218. {
  219. uint32_t length = 0;
  220. const uchar* data_ptr = NULL;
  221. if (!skip) {
  222. memcpy(to_mysql, from_tokudb, len_bytes);
  223. }
  224. length = get_blob_field_len(from_tokudb,len_bytes);
  225. data_ptr = from_tokudb + len_bytes;
  226. if (!skip) {
  227. memcpy(to_mysql + len_bytes, (uchar *)(&data_ptr), sizeof(uchar *));
  228. }
  229. return (from_tokudb + len_bytes + length);
  230. }
  231. static inline uint get_null_offset(TABLE* table, Field* field) {
  232. #if 50606 <= MYSQL_VERSION_ID
  233. return field->null_offset(table->record[0]);
  234. #else
  235. return (uint) ((uchar*) field->null_ptr - (uchar*) table->record[0]);
  236. #endif
  237. }
  238. typedef enum {
  239. toku_type_int = 0,
  240. toku_type_double,
  241. toku_type_float,
  242. toku_type_fixbinary,
  243. toku_type_fixstring,
  244. toku_type_varbinary,
  245. toku_type_varstring,
  246. toku_type_blob,
  247. toku_type_hpk, //for hidden primary key
  248. toku_type_unknown
  249. } TOKU_TYPE;
  250. TOKU_TYPE mysql_to_toku_type (Field* field);
  251. uchar* pack_toku_varbinary_from_desc(
  252. uchar* to_tokudb,
  253. const uchar* from_desc,
  254. uint32_t key_part_length, //number of bytes to use to encode the length in to_tokudb
  255. uint32_t field_length //length of field
  256. );
  257. uchar* pack_toku_varstring_from_desc(
  258. uchar* to_tokudb,
  259. const uchar* from_desc,
  260. uint32_t key_part_length, //number of bytes to use to encode the length in to_tokudb
  261. uint32_t field_length,
  262. uint32_t charset_num//length of field
  263. );
  264. uchar* pack_toku_key_field(
  265. uchar* to_tokudb,
  266. uchar* from_mysql,
  267. Field* field,
  268. uint32_t key_part_length //I really hope this is temporary as I phase out the pack_cmp stuff
  269. );
  270. uchar* pack_key_toku_key_field(
  271. uchar* to_tokudb,
  272. uchar* from_mysql,
  273. Field* field,
  274. uint32_t key_part_length //I really hope this is temporary as I phase out the pack_cmp stuff
  275. );
  276. uchar* unpack_toku_key_field(
  277. uchar* to_mysql,
  278. uchar* from_tokudb,
  279. Field* field,
  280. uint32_t key_part_length
  281. );
  282. //
  283. // for storing NULL byte in keys
  284. //
  285. #define NULL_COL_VAL 0
  286. #define NONNULL_COL_VAL 1
  287. //
  288. // for storing if rest of key is +/- infinity
  289. //
  290. #define COL_NEG_INF -1
  291. #define COL_ZERO 0
  292. #define COL_POS_INF 1
  293. //
  294. // information for hidden primary keys
  295. //
  296. #define TOKUDB_HIDDEN_PRIMARY_KEY_LENGTH 8
  297. //
  298. // function to convert a hidden primary key into a byte stream that can be stored in DBT
  299. //
  300. static inline void hpk_num_to_char(uchar* to, ulonglong num) {
  301. int8store(to, num);
  302. }
  303. //
  304. // function that takes a byte stream of a hidden primary key and returns a ulonglong
  305. //
  306. static inline ulonglong hpk_char_to_num(uchar* val) {
  307. return uint8korr(val);
  308. }
  309. int tokudb_compare_two_keys(
  310. const void* new_key_data,
  311. const uint32_t new_key_size,
  312. const void* saved_key_data,
  313. const uint32_t saved_key_size,
  314. const void* row_desc,
  315. const uint32_t row_desc_size,
  316. bool cmp_prefix
  317. );
  318. int tokudb_cmp_dbt_key(DB* db, const DBT *keya, const DBT *keyb);
  319. //TODO: QQQ Only do one direction for prefix.
  320. int tokudb_prefix_cmp_dbt_key(DB *file, const DBT *keya, const DBT *keyb);
  321. static int tokudb_compare_two_key_parts(
  322. const void* new_key_data,
  323. const uint32_t new_key_size,
  324. const void* saved_key_data,
  325. const uint32_t saved_key_size,
  326. const void* row_desc,
  327. const uint32_t row_desc_size,
  328. uint max_parts
  329. );
  330. static int tokudb_cmp_dbt_key_parts(DB *file, const DBT *keya, const DBT *keyb, uint max_parts);
  331. int create_toku_key_descriptor(
  332. uchar* buf,
  333. bool is_first_hpk,
  334. KEY* first_key,
  335. bool is_second_hpk,
  336. KEY* second_key
  337. );
  338. uint32_t create_toku_main_key_pack_descriptor (
  339. uchar* buf
  340. );
  341. uint32_t get_max_clustering_val_pack_desc_size(
  342. TABLE_SHARE* table_share
  343. );
  344. uint32_t create_toku_clustering_val_pack_descriptor (
  345. uchar* buf,
  346. uint pk_index,
  347. TABLE_SHARE* table_share,
  348. KEY_AND_COL_INFO* kc_info,
  349. uint32_t keynr,
  350. bool is_clustering
  351. );
  352. static inline bool is_key_clustering(
  353. void* row_desc,
  354. uint32_t row_desc_size
  355. )
  356. {
  357. return (row_desc_size > 0);
  358. }
  359. uint32_t pack_clustering_val_from_desc(
  360. uchar* buf,
  361. void* row_desc,
  362. uint32_t row_desc_size,
  363. const DBT* pk_val
  364. );
  365. uint32_t get_max_secondary_key_pack_desc_size(
  366. KEY_AND_COL_INFO* kc_info
  367. );
  368. uint32_t create_toku_secondary_key_pack_descriptor (
  369. uchar* buf,
  370. bool has_hpk,
  371. uint pk_index,
  372. TABLE_SHARE* table_share,
  373. TABLE* table,
  374. KEY_AND_COL_INFO* kc_info,
  375. KEY* key_info,
  376. KEY* prim_key
  377. );
  378. static inline bool is_key_pk(
  379. void* row_desc,
  380. uint32_t row_desc_size
  381. )
  382. {
  383. uchar* buf = (uchar *)row_desc;
  384. return buf[0];
  385. }
  386. uint32_t max_key_size_from_desc(
  387. void* row_desc,
  388. uint32_t row_desc_size
  389. );
  390. uint32_t pack_key_from_desc(
  391. uchar* buf,
  392. void* row_desc,
  393. uint32_t row_desc_size,
  394. const DBT* pk_key,
  395. const DBT* pk_val
  396. );
  397. bool fields_have_same_name(
  398. Field* a,
  399. Field* b
  400. );
  401. bool fields_are_same_type(
  402. Field* a,
  403. Field* b
  404. );
  405. bool are_two_fields_same(
  406. Field* a,
  407. Field* b
  408. );
  409. #endif