mirror of
https://github.com/MariaDB/server.git
synced 2025-01-24 15:54:37 +01:00
f252f9248a
The following type conversions was done: - Changed byte to uchar - Changed gptr to uchar* - Change my_string to char * - Change my_size_t to size_t - Change size_s to size_t Removed declaration of byte, gptr, my_string, my_size_t and size_s. Following function parameter changes was done: - All string functions in mysys/strings was changed to use size_t instead of uint for string lengths. - All read()/write() functions changed to use size_t (including vio). - All protocoll functions changed to use size_t instead of uint - Functions that used a pointer to a string length was changed to use size_t* - Changed malloc(), free() and related functions from using gptr to use void * as this requires fewer casts in the code and is more in line with how the standard functions work. - Added extra length argument to dirname_part() to return the length of the created string. - Changed (at least) following functions to take uchar* as argument: - db_dump() - my_net_write() - net_write_command() - net_store_data() - DBUG_DUMP() - decimal2bin() & bin2decimal() - Changed my_compress() and my_uncompress() to use size_t. Changed one argument to my_uncompress() from a pointer to a value as we only return one value (makes function easier to use). - Changed type of 'pack_data' argument to packfrm() to avoid casts. - Changed in readfrm() and writefrom(), ha_discover and handler::discover() the type for argument 'frmdata' to uchar** to avoid casts. - Changed most Field functions to use uchar* instead of char* (reduced a lot of casts). - Changed field->val_xxx(xxx, new_ptr) to take const pointers. Other changes: - Removed a lot of not needed casts - Added a few new cast required by other changes - Added some cast to my_multi_malloc() arguments for safety (as string lengths needs to be uint, not size_t). - Fixed all calls to hash-get-key functions to use size_t*. (Needed to be done explicitely as this conflict was often hided by casting the function to hash_get_key). - Changed some buffers to memory regions to uchar* to avoid casts. - Changed some string lengths from uint to size_t. - Changed field->ptr to be uchar* instead of char*. This allowed us to get rid of a lot of casts. - Some changes from true -> TRUE, false -> FALSE, unsigned char -> uchar - Include zlib.h in some files as we needed declaration of crc32() - Changed MY_FILE_ERROR to be (size_t) -1. - Changed many variables to hold the result of my_read() / my_write() to be size_t. This was needed to properly detect errors (which are returned as (size_t) -1). - Removed some very old VMS code - Changed packfrm()/unpackfrm() to not be depending on uint size (portability fix) - Removed windows specific code to restore cursor position as this causes slowdown on windows and we should not mix read() and pread() calls anyway as this is not thread safe. Updated function comment to reflect this. Changed function that depended on original behavior of my_pwrite() to itself restore the cursor position (one such case). - Added some missing checking of return value of malloc(). - Changed definition of MOD_PAD_CHAR_TO_FULL_LENGTH to avoid 'long' overflow. - Changed type of table_def::m_size from my_size_t to ulong to reflect that m_size is the number of elements in the array, not a string/memory length. - Moved THD::max_row_length() to table.cc (as it's not depending on THD). Inlined max_row_length_blob() into this function. - More function comments - Fixed some compiler warnings when compiled without partitions. - Removed setting of LEX_STRING() arguments in declaration (portability fix). - Some trivial indentation/variable name changes. - Some trivial code simplifications: - Replaced some calls to alloc_root + memcpy to use strmake_root()/strdup_root(). - Changed some calls from memdup() to strmake() (Safety fix) - Simpler loops in client-simple.c BitKeeper/etc/ignore: added libmysqld/ha_ndbcluster_cond.cc --- added debian/defs.mk debian/control client/completion_hash.cc: Remove not needed casts client/my_readline.h: Remove some old types client/mysql.cc: Simplify types client/mysql_upgrade.c: Remove some old types Update call to dirname_part client/mysqladmin.cc: Remove some old types client/mysqlbinlog.cc: Remove some old types Change some buffers to be uchar to avoid casts client/mysqlcheck.c: Remove some old types client/mysqldump.c: Remove some old types Remove some not needed casts Change some string lengths to size_t client/mysqlimport.c: Remove some old types client/mysqlshow.c: Remove some old types client/mysqlslap.c: Remove some old types Remove some not needed casts client/mysqltest.c: Removed some old types Removed some not needed casts Updated hash-get-key function arguments Updated parameters to dirname_part() client/readline.cc: Removed some old types Removed some not needed casts Changed some string lengths to use size_t client/sql_string.cc: Removed some old types dbug/dbug.c: Removed some old types Changed some string lengths to use size_t Changed some prototypes to avoid casts extra/comp_err.c: Removed some old types extra/innochecksum.c: Removed some old types extra/my_print_defaults.c: Removed some old types extra/mysql_waitpid.c: Removed some old types extra/perror.c: Removed some old types extra/replace.c: Removed some old types Updated parameters to dirname_part() extra/resolve_stack_dump.c: Removed some old types extra/resolveip.c: Removed some old types include/config-win.h: Removed some old types include/decimal.h: Changed binary strings to be uchar* instead of char* include/ft_global.h: Removed some old types include/hash.h: Removed some old types include/heap.h: Removed some old types Changed records_under_level to be 'ulong' instead of 'uint' to clarify usage of variable include/keycache.h: Removed some old types include/m_ctype.h: Removed some old types Changed some string lengths to use size_t Changed character length functions to return uint unsigned char -> uchar include/m_string.h: Removed some old types Changed some string lengths to use size_t include/my_alloc.h: Changed some string lengths to use size_t include/my_base.h: Removed some old types include/my_dbug.h: Removed some old types Changed some string lengths to use size_t Changed db_dump() to take uchar * as argument for memory to reduce number of casts in usage include/my_getopt.h: Removed some old types include/my_global.h: Removed old types: my_size_t -> size_t byte -> uchar gptr -> uchar * include/my_list.h: Removed some old types include/my_nosys.h: Removed some old types include/my_pthread.h: Removed some old types include/my_sys.h: Removed some old types Changed MY_FILE_ERROR to be in line with new definitions of my_write()/my_read() Changed some string lengths to use size_t my_malloc() / my_free() now uses void * Updated parameters to dirname_part() & my_uncompress() include/my_tree.h: Removed some old types include/my_trie.h: Removed some old types include/my_user.h: Changed some string lengths to use size_t include/my_vle.h: Removed some old types include/my_xml.h: Removed some old types Changed some string lengths to use size_t include/myisam.h: Removed some old types include/myisammrg.h: Removed some old types include/mysql.h: Removed some old types Changed byte streams to use uchar* instead of char* include/mysql_com.h: Removed some old types Changed some string lengths to use size_t Changed some buffers to be uchar* to avoid casts include/queues.h: Removed some old types include/sql_common.h: Removed some old types include/sslopt-longopts.h: Removed some old types include/violite.h: Removed some old types Changed some string lengths to use size_t libmysql/client_settings.h: Removed some old types libmysql/libmysql.c: Removed some old types libmysql/manager.c: Removed some old types libmysqld/emb_qcache.cc: Removed some old types libmysqld/emb_qcache.h: Removed some old types libmysqld/lib_sql.cc: Removed some old types Removed some not needed casts Changed some buffers to be uchar* to avoid casts true -> TRUE, false -> FALSE mysys/array.c: Removed some old types mysys/charset.c: Changed some string lengths to use size_t mysys/checksum.c: Include zlib to get definition for crc32 Removed some old types mysys/default.c: Removed some old types Changed some string lengths to use size_t mysys/default_modify.c: Changed some string lengths to use size_t Removed some not needed casts mysys/hash.c: Removed some old types Changed some string lengths to use size_t Note: Prototype of hash_key() has changed which may cause problems if client uses hash_init() with a cast for the hash-get-key function. hash_element now takes 'ulong' as the index type (cleanup) mysys/list.c: Removed some old types mysys/mf_cache.c: Changed some string lengths to use size_t mysys/mf_dirname.c: Removed some old types Changed some string lengths to use size_t Added argument to dirname_part() to avoid calculation of length for 'to' mysys/mf_fn_ext.c: Removed some old types Updated parameters to dirname_part() mysys/mf_format.c: Removed some old types Changed some string lengths to use size_t mysys/mf_getdate.c: Removed some old types mysys/mf_iocache.c: Removed some old types Changed some string lengths to use size_t Changed calculation of 'max_length' to be done the same way in all functions mysys/mf_iocache2.c: Removed some old types Changed some string lengths to use size_t Clean up comments Removed not needed indentation mysys/mf_keycache.c: Removed some old types mysys/mf_keycaches.c: Removed some old types mysys/mf_loadpath.c: Removed some old types mysys/mf_pack.c: Removed some old types Changed some string lengths to use size_t Removed some not needed casts Removed very old VMS code Updated parameters to dirname_part() Use result of dirnam_part() to remove call to strcat() mysys/mf_path.c: Removed some old types mysys/mf_radix.c: Removed some old types mysys/mf_same.c: Removed some old types mysys/mf_sort.c: Removed some old types mysys/mf_soundex.c: Removed some old types mysys/mf_strip.c: Removed some old types mysys/mf_tempdir.c: Removed some old types mysys/mf_unixpath.c: Removed some old types mysys/mf_wfile.c: Removed some old types mysys/mulalloc.c: Removed some old types mysys/my_alloc.c: Removed some old types Changed some string lengths to use size_t Use void* as type for allocated memory area Removed some not needed casts Changed argument 'Size' to 'length' according coding guidelines mysys/my_chsize.c: Changed some buffers to be uchar* to avoid casts mysys/my_compress.c: More comments Removed some old types Changed string lengths to use size_t Changed arguments to my_uncompress() to make them easier to understand Changed packfrm()/unpackfrm() to not be depending on uint size (portability fix) Changed type of 'pack_data' argument to packfrm() to avoid casts. mysys/my_conio.c: Changed some string lengths to use size_t mysys/my_create.c: Removed some old types mysys/my_div.c: Removed some old types mysys/my_error.c: Removed some old types mysys/my_fopen.c: Removed some old types mysys/my_fstream.c: Removed some old types Changed some string lengths to use size_t writen -> written mysys/my_getopt.c: Removed some old types mysys/my_getwd.c: Removed some old types More comments mysys/my_init.c: Removed some old types mysys/my_largepage.c: Removed some old types Changed some string lengths to use size_t mysys/my_lib.c: Removed some old types mysys/my_lockmem.c: Removed some old types mysys/my_malloc.c: Removed some old types Changed malloc(), free() and related functions to use void * Changed all functions to use size_t mysys/my_memmem.c: Indentation cleanup mysys/my_once.c: Removed some old types Changed malloc(), free() and related functions to use void * mysys/my_open.c: Removed some old types mysys/my_pread.c: Removed some old types Changed all functions to use size_t Added comment for how my_pread() / my_pwrite() are supposed to work. Removed windows specific code to restore cursor position as this causes slowdown on windows and we should not mix read() and pread() calls anyway as this is not thread safe. (If we ever would really need this, it should be enabled only with a flag argument) mysys/my_quick.c: Removed some old types Changed all functions to use size_t mysys/my_read.c: Removed some old types Changed all functions to use size_t mysys/my_realloc.c: Removed some old types Use void* as type for allocated memory area Changed all functions to use size_t mysys/my_static.c: Removed some old types mysys/my_static.h: Removed some old types mysys/my_vle.c: Removed some old types mysys/my_wincond.c: Removed some old types mysys/my_windac.c: Removed some old types mysys/my_write.c: Removed some old types Changed all functions to use size_t mysys/ptr_cmp.c: Removed some old types Changed all functions to use size_t mysys/queues.c: Removed some old types mysys/safemalloc.c: Removed some old types Changed malloc(), free() and related functions to use void * Changed all functions to use size_t mysys/string.c: Removed some old types Changed all functions to use size_t mysys/testhash.c: Removed some old types mysys/thr_alarm.c: Removed some old types mysys/thr_lock.c: Removed some old types mysys/tree.c: Removed some old types mysys/trie.c: Removed some old types mysys/typelib.c: Removed some old types plugin/daemon_example/daemon_example.cc: Removed some old types regex/reginit.c: Removed some old types server-tools/instance-manager/buffer.cc: Changed some string lengths to use size_t Changed buffer to be of type uchar* server-tools/instance-manager/buffer.h: Changed some string lengths to use size_t Changed buffer to be of type uchar* server-tools/instance-manager/commands.cc: Removed some old types Changed some string lengths to use size_t Changed buffer to be of type uchar* server-tools/instance-manager/instance_map.cc: Removed some old types Changed some string lengths to use size_t Changed buffer to be of type uchar* server-tools/instance-manager/instance_options.cc: Changed buffer to be of type uchar* Replaced alloc_root + strcpy() with strdup_root() server-tools/instance-manager/mysql_connection.cc: Changed buffer to be of type uchar* server-tools/instance-manager/options.cc: Removed some old types server-tools/instance-manager/parse.cc: Changed some string lengths to use size_t server-tools/instance-manager/parse.h: Removed some old types Changed some string lengths to use size_t server-tools/instance-manager/protocol.cc: Changed some buffers to be uchar* to avoid casts Changed some string lengths to use size_t server-tools/instance-manager/protocol.h: Changed some string lengths to use size_t server-tools/instance-manager/user_map.cc: Removed some old types Changed some string lengths to use size_t sql/derror.cc: Removed some old types Changed some buffers to be uchar* to avoid casts Changed some string lengths to use size_t sql/discover.cc: Changed in readfrm() and writefrom() the type for argument 'frmdata' to uchar** to avoid casts Changed some string lengths to use size_t Changed some buffers to be uchar* to avoid casts sql/event_data_objects.cc: Removed some old types Added missing casts for alloc() and sprintf() sql/event_db_repository.cc: Changed some buffers to be uchar* to avoid casts Added missing casts for sprintf() sql/event_queue.cc: Removed some old types sql/field.cc: Removed some old types Changed memory buffers to be uchar* Changed some string lengths to use size_t Removed a lot of casts Safety fix in Field_blob::val_decimal() to not access zero pointer sql/field.h: Removed some old types Changed memory buffers to be uchar* (except of store() as this would have caused too many other changes). Changed some string lengths to use size_t Removed some not needed casts Changed val_xxx(xxx, new_ptr) to take const pointers sql/field_conv.cc: Removed some old types Added casts required because memory area pointers are now uchar* sql/filesort.cc: Initalize variable that was used unitialized in error conditions sql/gen_lex_hash.cc: Removed some old types Changed memory buffers to be uchar* Changed some string lengths to use size_t Removed a lot of casts Safety fix in Field_blob::val_decimal() to not access zero pointer sql/gstream.h: Added required cast sql/ha_ndbcluster.cc: Removed some old types Updated hash-get-key function arguments Changed some buffers to be uchar* to avoid casts Added required casts Removed some not needed casts sql/ha_ndbcluster.h: Removed some old types sql/ha_ndbcluster_binlog.cc: Removed some old types Changed some buffers to be uchar* to avoid casts Replaced sql_alloc() + memcpy() + set end 0 with sql_strmake() Changed some string lengths to use size_t Added missing casts for alloc() and sprintf() sql/ha_ndbcluster_binlog.h: Removed some old types sql/ha_ndbcluster_cond.cc: Removed some old types Removed some not needed casts sql/ha_ndbcluster_cond.h: Removed some old types sql/ha_partition.cc: Removed some old types Changed prototype for change_partition() to avoid casts sql/ha_partition.h: Removed some old types sql/handler.cc: Removed some old types Changed some string lengths to use size_t sql/handler.h: Removed some old types Changed some string lengths to use size_t Changed type for 'frmblob' parameter for discover() and ha_discover() to get fewer casts sql/hash_filo.h: Removed some old types Changed all functions to use size_t sql/hostname.cc: Removed some old types sql/item.cc: Removed some old types Changed some string lengths to use size_t Use strmake() instead of memdup() to create a null terminated string. Updated calls to new Field() sql/item.h: Removed some old types Changed malloc(), free() and related functions to use void * Changed some buffers to be uchar* to avoid casts sql/item_cmpfunc.cc: Removed some old types Changed some buffers to be uchar* to avoid casts sql/item_cmpfunc.h: Removed some old types sql/item_create.cc: Removed some old types sql/item_func.cc: Removed some old types Changed some buffers to be uchar* to avoid casts Removed some not needed casts Added test for failing alloc() in init_result_field() Remove old confusing comment Fixed compiler warning sql/item_func.h: Removed some old types sql/item_row.cc: Removed some old types sql/item_row.h: Removed some old types sql/item_strfunc.cc: Include zlib (needed becasue we call crc32) Removed some old types sql/item_strfunc.h: Removed some old types Changed some types to match new function prototypes sql/item_subselect.cc: Removed some old types sql/item_subselect.h: Removed some old types sql/item_sum.cc: Removed some old types Changed some buffers to be uchar* to avoid casts Removed some not needed casts sql/item_sum.h: Removed some old types sql/item_timefunc.cc: Removed some old types Changed some string lengths to use size_t sql/item_timefunc.h: Removed some old types sql/item_xmlfunc.cc: Changed some string lengths to use size_t sql/item_xmlfunc.h: Removed some old types sql/key.cc: Removed some old types Removed some not needed casts sql/lock.cc: Removed some old types Added some cast to my_multi_malloc() arguments for safety sql/log.cc: Removed some old types Changed some string lengths to use size_t Changed some buffers to be uchar* to avoid casts Changed usage of pwrite() to not assume it holds the cursor position for the file Made usage of my_read() safer sql/log_event.cc: Removed some old types Added checking of return value of malloc() in pack_info() Changed some buffers to be uchar* to avoid casts Removed some 'const' to avoid casts Added missing casts for alloc() and sprintf() Added required casts Removed some not needed casts Added some cast to my_multi_malloc() arguments for safety sql/log_event.h: Removed some old types Changed some buffers to be uchar* to avoid casts sql/log_event_old.cc: Changed some buffers to be uchar* to avoid casts Removed some not needed casts sql/log_event_old.h: Changed some buffers to be uchar* to avoid casts sql/mf_iocache.cc: Removed some old types sql/my_decimal.cc: Changed memory area to use uchar* sql/my_decimal.h: Changed memory area to use uchar* sql/mysql_priv.h: Removed some old types Changed malloc(), free() and related functions to use void * Changed some string lengths to use size_t Changed definition of MOD_PAD_CHAR_TO_FULL_LENGTH to avoid long overflow Changed some buffers to be uchar* to avoid casts sql/mysqld.cc: Removed some old types sql/net_serv.cc: Removed some old types Changed some string lengths to use size_t Changed some buffers to be uchar* to avoid casts Ensure that vio_read()/vio_write() return values are stored in a size_t variable Removed some not needed casts sql/opt_range.cc: Removed some old types Changed some buffers to be uchar* to avoid casts Removed some not needed casts sql/opt_range.h: Removed some old types Changed some buffers to be uchar* to avoid casts sql/opt_sum.cc: Removed some old types Removed some not needed casts sql/parse_file.cc: Removed some old types Changed some string lengths to use size_t Changed alloc_root + memcpy + set end 0 -> strmake_root() sql/parse_file.h: Removed some old types sql/partition_info.cc: Removed some old types Added missing casts for alloc() Changed some buffers to be uchar* to avoid casts sql/partition_info.h: Changed some buffers to be uchar* to avoid casts sql/protocol.cc: Removed some old types Changed some buffers to be uchar* to avoid casts Removed some not needed casts sql/protocol.h: Removed some old types Changed some buffers to be uchar* to avoid casts Changed some string lengths to use size_t sql/records.cc: Removed some old types sql/repl_failsafe.cc: Removed some old types Changed some string lengths to use size_t Added required casts sql/rpl_filter.cc: Removed some old types Updated hash-get-key function arguments Changed some string lengths to use size_t sql/rpl_filter.h: Changed some string lengths to use size_t sql/rpl_injector.h: Removed some old types sql/rpl_record.cc: Removed some old types Removed some not needed casts Changed some buffers to be uchar* to avoid casts sql/rpl_record.h: Removed some old types Changed some buffers to be uchar* to avoid casts sql/rpl_record_old.cc: Removed some old types Changed some buffers to be uchar* to avoid casts Removed some not needed casts sql/rpl_record_old.h: Removed some old types Changed some buffers to be uchar* to avoid cast sql/rpl_rli.cc: Removed some old types sql/rpl_tblmap.cc: Removed some old types sql/rpl_tblmap.h: Removed some old types sql/rpl_utility.cc: Removed some old types sql/rpl_utility.h: Removed some old types Changed type of m_size from my_size_t to ulong to reflect that m_size is the number of elements in the array, not a string/memory length sql/set_var.cc: Removed some old types Updated parameters to dirname_part() sql/set_var.h: Removed some old types sql/slave.cc: Removed some old types Changed some string lengths to use size_t sql/slave.h: Removed some old types sql/sp.cc: Removed some old types Added missing casts for printf() sql/sp.h: Removed some old types Updated hash-get-key function arguments sql/sp_cache.cc: Removed some old types Added missing casts for printf() Updated hash-get-key function arguments sql/sp_head.cc: Removed some old types Added missing casts for alloc() and printf() Added required casts Updated hash-get-key function arguments sql/sp_head.h: Removed some old types sql/sp_pcontext.cc: Removed some old types sql/sp_pcontext.h: Removed some old types sql/sql_acl.cc: Removed some old types Changed some string lengths to use size_t Changed some buffers to be uchar* to avoid casts Removed some not needed casts Added required casts sql/sql_analyse.cc: Changed some buffers to be uchar* to avoid casts sql/sql_analyse.h: Changed some buffers to be uchar* to avoid casts sql/sql_array.h: Removed some old types sql/sql_base.cc: Removed some old types Updated hash-get-key function arguments sql/sql_binlog.cc: Removed some old types Added missing casts for printf() sql/sql_cache.cc: Removed some old types Updated hash-get-key function arguments Removed some not needed casts Changed some string lengths to use size_t sql/sql_cache.h: Removed some old types Removed reference to not existing function cache_key() Updated hash-get-key function arguments sql/sql_class.cc: Removed some old types Updated hash-get-key function arguments Added missing casts for alloc() Updated hash-get-key function arguments Moved THD::max_row_length() to table.cc (as it's not depending on THD) Removed some not needed casts sql/sql_class.h: Removed some old types Changed malloc(), free() and related functions to use void * Removed some not needed casts Changed some string lengths to use size_t Moved max_row_length and max_row_length_blob() to table.cc, as they are not depending on THD sql/sql_connect.cc: Removed some old types Added required casts sql/sql_db.cc: Removed some old types Removed some not needed casts Added some cast to my_multi_malloc() arguments for safety Added missing casts for alloc() sql/sql_delete.cc: Removed some old types sql/sql_handler.cc: Removed some old types Updated hash-get-key function arguments Added some cast to my_multi_malloc() arguments for safety sql/sql_help.cc: Removed some old types Changed some buffers to be uchar* to avoid casts Removed some not needed casts sql/sql_insert.cc: Removed some old types Added missing casts for alloc() and printf() sql/sql_lex.cc: Removed some old types sql/sql_lex.h: Removed some old types Removed some not needed casts sql/sql_list.h: Removed some old types Removed some not needed casts sql/sql_load.cc: Removed some old types Removed compiler warning sql/sql_manager.cc: Removed some old types sql/sql_map.cc: Removed some old types sql/sql_map.h: Removed some old types sql/sql_olap.cc: Removed some old types sql/sql_parse.cc: Removed some old types Trivial move of code lines to make things more readable Changed some string lengths to use size_t Added missing casts for alloc() sql/sql_partition.cc: Removed some old types Removed compiler warnings about not used functions Changed some buffers to be uchar* to avoid casts Removed some not needed casts sql/sql_partition.h: Removed some old types Changed some buffers to be uchar* to avoid casts sql/sql_plugin.cc: Removed some old types Added missing casts for alloc() Updated hash-get-key function arguments sql/sql_prepare.cc: Removed some old types Changed some buffers to be uchar* to avoid casts Added missing casts for alloc() and printf() sql-common/client.c: Removed some old types Changed some memory areas to use uchar* sql-common/my_user.c: Changed some string lengths to use size_t sql-common/pack.c: Changed some buffers to be uchar* to avoid casts sql/sql_repl.cc: Added required casts Changed some buffers to be uchar* to avoid casts Changed some string lengths to use size_t sql/sql_select.cc: Removed some old types Changed some buffers to be uchar* to avoid casts Removed some old types sql/sql_select.h: Removed some old types Changed some buffers to be uchar* to avoid casts sql/sql_servers.cc: Removed some old types Updated hash-get-key function arguments sql/sql_show.cc: Removed some old types Added missing casts for alloc() Removed some not needed casts sql/sql_string.cc: Removed some old types Added required casts sql/sql_table.cc: Removed some old types Removed compiler warning about not used variable Changed some buffers to be uchar* to avoid casts Removed some not needed casts sql/sql_test.cc: Removed some old types sql/sql_trigger.cc: Removed some old types Added missing casts for alloc() sql/sql_udf.cc: Removed some old types Updated hash-get-key function arguments sql/sql_union.cc: Removed some old types sql/sql_update.cc: Removed some old types Removed some not needed casts sql/sql_view.cc: Removed some old types sql/sql_yacc.yy: Removed some old types Changed some string lengths to use size_t Added missing casts for alloc() sql/stacktrace.c: Removed some old types sql/stacktrace.h: Removed some old types sql/structs.h: Removed some old types sql/table.cc: Removed some old types Updated hash-get-key function arguments Changed some buffers to be uchar* to avoid casts Removed setting of LEX_STRING() arguments in declaration Added required casts More function comments Moved max_row_length() here from sql_class.cc/sql_class.h sql/table.h: Removed some old types Changed some string lengths to use size_t sql/thr_malloc.cc: Use void* as type for allocated memory area Changed all functions to use size_t sql/tzfile.h: Changed some buffers to be uchar* to avoid casts sql/tztime.cc: Changed some buffers to be uchar* to avoid casts Updated hash-get-key function arguments Added missing casts for alloc() Removed some not needed casts sql/uniques.cc: Removed some old types Removed some not needed casts sql/unireg.cc: Removed some old types Changed some buffers to be uchar* to avoid casts Removed some not needed casts Added missing casts for alloc() storage/archive/archive_reader.c: Removed some old types storage/archive/azio.c: Removed some old types Removed some not needed casts storage/archive/ha_archive.cc: Removed some old types Changed type for 'frmblob' in archive_discover() to match handler Updated hash-get-key function arguments Removed some not needed casts storage/archive/ha_archive.h: Removed some old types storage/blackhole/ha_blackhole.cc: Removed some old types storage/blackhole/ha_blackhole.h: Removed some old types storage/csv/ha_tina.cc: Removed some old types Updated hash-get-key function arguments Changed some buffers to be uchar* to avoid casts storage/csv/ha_tina.h: Removed some old types Removed some not needed casts storage/csv/transparent_file.cc: Removed some old types Changed type of 'bytes_read' to be able to detect read errors Fixed indentation storage/csv/transparent_file.h: Removed some old types storage/example/ha_example.cc: Removed some old types Updated hash-get-key function arguments storage/example/ha_example.h: Removed some old types storage/federated/ha_federated.cc: Removed some old types Updated hash-get-key function arguments Removed some not needed casts storage/federated/ha_federated.h: Removed some old types storage/heap/_check.c: Changed some buffers to be uchar* to avoid casts storage/heap/_rectest.c: Removed some old types storage/heap/ha_heap.cc: Removed some old types storage/heap/ha_heap.h: Removed some old types storage/heap/heapdef.h: Removed some old types storage/heap/hp_block.c: Removed some old types Changed some string lengths to use size_t storage/heap/hp_clear.c: Removed some old types storage/heap/hp_close.c: Removed some old types storage/heap/hp_create.c: Removed some old types storage/heap/hp_delete.c: Removed some old types storage/heap/hp_hash.c: Removed some old types storage/heap/hp_info.c: Removed some old types storage/heap/hp_open.c: Removed some old types storage/heap/hp_rfirst.c: Removed some old types storage/heap/hp_rkey.c: Removed some old types storage/heap/hp_rlast.c: Removed some old types storage/heap/hp_rnext.c: Removed some old types storage/heap/hp_rprev.c: Removed some old types storage/heap/hp_rrnd.c: Removed some old types storage/heap/hp_rsame.c: Removed some old types storage/heap/hp_scan.c: Removed some old types storage/heap/hp_test1.c: Removed some old types storage/heap/hp_test2.c: Removed some old types storage/heap/hp_update.c: Removed some old types storage/heap/hp_write.c: Removed some old types Changed some string lengths to use size_t storage/innobase/handler/ha_innodb.cc: Removed some old types Updated hash-get-key function arguments Added missing casts for alloc() and printf() Removed some not needed casts storage/innobase/handler/ha_innodb.h: Removed some old types storage/myisam/ft_boolean_search.c: Removed some old types storage/myisam/ft_nlq_search.c: Removed some old types storage/myisam/ft_parser.c: Removed some old types Changed some buffers to be uchar* to avoid casts storage/myisam/ft_static.c: Removed some old types storage/myisam/ft_stopwords.c: Removed some old types storage/myisam/ft_update.c: Removed some old types Changed some buffers to be uchar* to avoid casts storage/myisam/ftdefs.h: Removed some old types Changed some buffers to be uchar* to avoid casts storage/myisam/fulltext.h: Removed some old types storage/myisam/ha_myisam.cc: Removed some old types storage/myisam/ha_myisam.h: Removed some old types storage/myisam/mi_cache.c: Removed some old types Changed some buffers to be uchar* to avoid casts storage/myisam/mi_check.c: Removed some old types storage/myisam/mi_checksum.c: Removed some old types storage/myisam/mi_close.c: Removed some old types storage/myisam/mi_create.c: Removed some old types storage/myisam/mi_delete.c: Removed some old types storage/myisam/mi_delete_all.c: Removed some old types storage/myisam/mi_dynrec.c: Removed some old types storage/myisam/mi_extra.c: Removed some old types storage/myisam/mi_key.c: Removed some old types storage/myisam/mi_locking.c: Removed some old types storage/myisam/mi_log.c: Removed some old types storage/myisam/mi_open.c: Removed some old types Removed some not needed casts Check argument of my_write()/my_pwrite() in functions returning int Added casting of string lengths to size_t storage/myisam/mi_packrec.c: Removed some old types Changed some buffers to be uchar* to avoid casts storage/myisam/mi_page.c: Removed some old types storage/myisam/mi_preload.c: Removed some old types storage/myisam/mi_range.c: Removed some old types storage/myisam/mi_rfirst.c: Removed some old types storage/myisam/mi_rkey.c: Removed some old types storage/myisam/mi_rlast.c: Removed some old types storage/myisam/mi_rnext.c: Removed some old types storage/myisam/mi_rnext_same.c: Removed some old types storage/myisam/mi_rprev.c: Removed some old types storage/myisam/mi_rrnd.c: Removed some old types storage/myisam/mi_rsame.c: Removed some old types storage/myisam/mi_rsamepos.c: Removed some old types storage/myisam/mi_scan.c: Removed some old types storage/myisam/mi_search.c: Removed some old types storage/myisam/mi_static.c: Removed some old types storage/myisam/mi_statrec.c: Removed some old types storage/myisam/mi_test1.c: Removed some old types storage/myisam/mi_test2.c: Removed some old types storage/myisam/mi_test3.c: Removed some old types storage/myisam/mi_unique.c: Removed some old types storage/myisam/mi_update.c: Removed some old types storage/myisam/mi_write.c: Removed some old types storage/myisam/myisam_ftdump.c: Removed some old types storage/myisam/myisamchk.c: Removed some old types storage/myisam/myisamdef.h: Removed some old types storage/myisam/myisamlog.c: Removed some old types Indentation fix storage/myisam/myisampack.c: Removed some old types storage/myisam/rt_index.c: Removed some old types storage/myisam/rt_split.c: Removed some old types storage/myisam/sort.c: Removed some old types storage/myisam/sp_defs.h: Removed some old types storage/myisam/sp_key.c: Removed some old types storage/myisammrg/ha_myisammrg.cc: Removed some old types storage/myisammrg/ha_myisammrg.h: Removed some old types storage/myisammrg/myrg_close.c: Removed some old types storage/myisammrg/myrg_def.h: Removed some old types storage/myisammrg/myrg_delete.c: Removed some old types storage/myisammrg/myrg_open.c: Removed some old types Updated parameters to dirname_part() storage/myisammrg/myrg_queue.c: Removed some old types storage/myisammrg/myrg_rfirst.c: Removed some old types storage/myisammrg/myrg_rkey.c: Removed some old types storage/myisammrg/myrg_rlast.c: Removed some old types storage/myisammrg/myrg_rnext.c: Removed some old types storage/myisammrg/myrg_rnext_same.c: Removed some old types storage/myisammrg/myrg_rprev.c: Removed some old types storage/myisammrg/myrg_rrnd.c: Removed some old types storage/myisammrg/myrg_rsame.c: Removed some old types storage/myisammrg/myrg_update.c: Removed some old types storage/myisammrg/myrg_write.c: Removed some old types storage/ndb/include/util/ndb_opts.h: Removed some old types storage/ndb/src/cw/cpcd/main.cpp: Removed some old types storage/ndb/src/kernel/vm/Configuration.cpp: Removed some old types storage/ndb/src/mgmclient/main.cpp: Removed some old types storage/ndb/src/mgmsrv/InitConfigFileParser.cpp: Removed some old types Removed old disabled code storage/ndb/src/mgmsrv/main.cpp: Removed some old types storage/ndb/src/ndbapi/NdbBlob.cpp: Removed some old types storage/ndb/src/ndbapi/NdbOperationDefine.cpp: Removed not used variable storage/ndb/src/ndbapi/NdbOperationInt.cpp: Added required casts storage/ndb/src/ndbapi/NdbScanOperation.cpp: Added required casts storage/ndb/tools/delete_all.cpp: Removed some old types storage/ndb/tools/desc.cpp: Removed some old types storage/ndb/tools/drop_index.cpp: Removed some old types storage/ndb/tools/drop_tab.cpp: Removed some old types storage/ndb/tools/listTables.cpp: Removed some old types storage/ndb/tools/ndb_config.cpp: Removed some old types storage/ndb/tools/restore/consumer_restore.cpp: Changed some buffers to be uchar* to avoid casts with new defintion of packfrm() storage/ndb/tools/restore/restore_main.cpp: Removed some old types storage/ndb/tools/select_all.cpp: Removed some old types storage/ndb/tools/select_count.cpp: Removed some old types storage/ndb/tools/waiter.cpp: Removed some old types strings/bchange.c: Changed function to use uchar * and size_t strings/bcmp.c: Changed function to use uchar * and size_t strings/bmove512.c: Changed function to use uchar * and size_t strings/bmove_upp.c: Changed function to use uchar * and size_t strings/ctype-big5.c: Changed functions to use size_t Changed character length functions to return uint strings/ctype-bin.c: Changed functions to use size_t strings/ctype-cp932.c: Changed functions to use size_t Changed character length functions to return uint strings/ctype-czech.c: Fixed indentation Changed functions to use size_t strings/ctype-euc_kr.c: Changed functions to use size_t Changed character length functions to return uint strings/ctype-eucjpms.c: Changed functions to use size_t Changed character length functions to return uint unsigned char -> uchar strings/ctype-gb2312.c: Changed functions to use size_t Changed character length functions to return uint strings/ctype-gbk.c: Changed functions to use size_t Changed character length functions to return uint strings/ctype-latin1.c: Changed functions to use size_t Changed character length functions to return uint unsigned char -> uchar strings/ctype-mb.c: Changed functions to use size_t Changed character length functions to return uint strings/ctype-simple.c: Changed functions to use size_t Simpler loops for caseup/casedown unsigned int -> uint unsigned char -> uchar strings/ctype-sjis.c: Changed functions to use size_t Changed character length functions to return uint strings/ctype-tis620.c: Changed functions to use size_t Changed character length functions to return uint unsigned char -> uchar strings/ctype-uca.c: Changed functions to use size_t unsigned char -> uchar strings/ctype-ucs2.c: Moved inclusion of stdarg.h to other includes usigned char -> uchar Changed functions to use size_t Changed character length functions to return uint strings/ctype-ujis.c: Changed functions to use size_t Changed character length functions to return uint unsigned char -> uchar strings/ctype-utf8.c: Changed functions to use size_t unsigned char -> uchar Indentation fixes strings/ctype-win1250ch.c: Indentation fixes Changed functions to use size_t strings/ctype.c: Changed functions to use size_t strings/decimal.c: Changed type for memory argument to uchar * strings/do_ctype.c: Indentation fixes strings/my_strtoll10.c: unsigned char -> uchar strings/my_vsnprintf.c: Changed functions to use size_t strings/r_strinstr.c: Removed some old types Changed functions to use size_t strings/str_test.c: Removed some old types strings/strappend.c: Changed functions to use size_t strings/strcont.c: Removed some old types strings/strfill.c: Removed some old types strings/strinstr.c: Changed functions to use size_t strings/strlen.c: Changed functions to use size_t strings/strmake.c: Changed functions to use size_t strings/strnlen.c: Changed functions to use size_t strings/strnmov.c: Changed functions to use size_t strings/strto.c: unsigned char -> uchar strings/strtod.c: Changed functions to use size_t strings/strxnmov.c: Changed functions to use size_t strings/xml.c: Changed functions to use size_t Indentation fixes tests/mysql_client_test.c: Removed some old types tests/thread_test.c: Removed some old types vio/test-ssl.c: Removed some old types vio/test-sslclient.c: Removed some old types vio/test-sslserver.c: Removed some old types vio/vio.c: Removed some old types vio/vio_priv.h: Removed some old types Changed vio_read()/vio_write() to work with size_t vio/viosocket.c: Changed vio_read()/vio_write() to work with size_t Indentation fixes vio/viossl.c: Changed vio_read()/vio_write() to work with size_t Indentation fixes vio/viosslfactories.c: Removed some old types vio/viotest-ssl.c: Removed some old types win/README: More explanations
1921 lines
58 KiB
C
1921 lines
58 KiB
C
/* Copyright (C) 2000-2006 MySQL AB
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
it under the terms of the GNU General Public License as published by
|
|
the Free Software Foundation; version 2 of the License.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
along with this program; if not, write to the Free Software
|
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
|
|
|
|
/* key handling functions */
|
|
|
|
#include "fulltext.h"
|
|
#include "m_ctype.h"
|
|
|
|
static my_bool _mi_get_prev_key(MI_INFO *info, MI_KEYDEF *keyinfo, uchar *page,
|
|
uchar *key, uchar *keypos,
|
|
uint *return_key_length);
|
|
|
|
/* Check index */
|
|
|
|
int _mi_check_index(MI_INFO *info, int inx)
|
|
{
|
|
if (inx == -1) /* Use last index */
|
|
inx=info->lastinx;
|
|
if (inx < 0 || ! mi_is_key_active(info->s->state.key_map, inx))
|
|
{
|
|
my_errno=HA_ERR_WRONG_INDEX;
|
|
return -1;
|
|
}
|
|
if (info->lastinx != inx) /* Index changed */
|
|
{
|
|
info->lastinx = inx;
|
|
info->page_changed=1;
|
|
info->update= ((info->update & (HA_STATE_CHANGED | HA_STATE_ROW_CHANGED)) |
|
|
HA_STATE_NEXT_FOUND | HA_STATE_PREV_FOUND);
|
|
}
|
|
if (info->opt_flag & WRITE_CACHE_USED && flush_io_cache(&info->rec_cache))
|
|
return(-1);
|
|
return(inx);
|
|
} /* mi_check_index */
|
|
|
|
|
|
/*
|
|
** Search after row by a key
|
|
** Position to row is stored in info->lastpos
|
|
** Return: -1 if not found
|
|
** 1 if one should continue search on higher level
|
|
*/
|
|
|
|
int _mi_search(register MI_INFO *info, register MI_KEYDEF *keyinfo,
|
|
uchar *key, uint key_len, uint nextflag, register my_off_t pos)
|
|
{
|
|
my_bool last_key;
|
|
int error,flag;
|
|
uint nod_flag;
|
|
uchar *keypos,*maxpos;
|
|
uchar lastkey[MI_MAX_KEY_BUFF],*buff;
|
|
DBUG_ENTER("_mi_search");
|
|
DBUG_PRINT("enter",("pos: %lu nextflag: %u lastpos: %lu",
|
|
(ulong) pos, nextflag, (ulong) info->lastpos));
|
|
DBUG_EXECUTE("key",_mi_print_key(DBUG_FILE,keyinfo->seg,key,key_len););
|
|
|
|
if (pos == HA_OFFSET_ERROR)
|
|
{
|
|
my_errno=HA_ERR_KEY_NOT_FOUND; /* Didn't find key */
|
|
info->lastpos= HA_OFFSET_ERROR;
|
|
if (!(nextflag & (SEARCH_SMALLER | SEARCH_BIGGER | SEARCH_LAST)))
|
|
DBUG_RETURN(-1); /* Not found ; return error */
|
|
DBUG_RETURN(1); /* Search at upper levels */
|
|
}
|
|
|
|
if (!(buff=_mi_fetch_keypage(info,keyinfo,pos,DFLT_INIT_HITS,info->buff,
|
|
test(!(nextflag & SEARCH_SAVE_BUFF)))))
|
|
goto err;
|
|
DBUG_DUMP("page",(uchar*) buff,mi_getint(buff));
|
|
|
|
flag=(*keyinfo->bin_search)(info,keyinfo,buff,key,key_len,nextflag,
|
|
&keypos,lastkey, &last_key);
|
|
if (flag == MI_FOUND_WRONG_KEY)
|
|
DBUG_RETURN(-1);
|
|
nod_flag=mi_test_if_nod(buff);
|
|
maxpos=buff+mi_getint(buff)-1;
|
|
|
|
if (flag)
|
|
{
|
|
if ((error=_mi_search(info,keyinfo,key,key_len,nextflag,
|
|
_mi_kpos(nod_flag,keypos))) <= 0)
|
|
DBUG_RETURN(error);
|
|
|
|
if (flag >0)
|
|
{
|
|
if (nextflag & (SEARCH_SMALLER | SEARCH_LAST) &&
|
|
keypos == buff+2+nod_flag)
|
|
DBUG_RETURN(1); /* Bigger than key */
|
|
}
|
|
else if (nextflag & SEARCH_BIGGER && keypos >= maxpos)
|
|
DBUG_RETURN(1); /* Smaller than key */
|
|
}
|
|
else
|
|
{
|
|
if ((nextflag & SEARCH_FIND) && nod_flag &&
|
|
((keyinfo->flag & (HA_NOSAME | HA_NULL_PART)) != HA_NOSAME ||
|
|
key_len != USE_WHOLE_KEY))
|
|
{
|
|
if ((error=_mi_search(info,keyinfo,key,key_len,SEARCH_FIND,
|
|
_mi_kpos(nod_flag,keypos))) >= 0 ||
|
|
my_errno != HA_ERR_KEY_NOT_FOUND)
|
|
DBUG_RETURN(error);
|
|
info->last_keypage= HA_OFFSET_ERROR; /* Buffer not in mem */
|
|
}
|
|
}
|
|
if (pos != info->last_keypage)
|
|
{
|
|
uchar *old_buff=buff;
|
|
if (!(buff=_mi_fetch_keypage(info,keyinfo,pos,DFLT_INIT_HITS,info->buff,
|
|
test(!(nextflag & SEARCH_SAVE_BUFF)))))
|
|
goto err;
|
|
keypos=buff+(keypos-old_buff);
|
|
maxpos=buff+(maxpos-old_buff);
|
|
}
|
|
|
|
if ((nextflag & (SEARCH_SMALLER | SEARCH_LAST)) && flag != 0)
|
|
{
|
|
uint not_used[2];
|
|
if (_mi_get_prev_key(info,keyinfo, buff, info->lastkey, keypos,
|
|
&info->lastkey_length))
|
|
goto err;
|
|
if (!(nextflag & SEARCH_SMALLER) &&
|
|
ha_key_cmp(keyinfo->seg, info->lastkey, key, key_len, SEARCH_FIND,
|
|
not_used))
|
|
{
|
|
my_errno=HA_ERR_KEY_NOT_FOUND; /* Didn't find key */
|
|
goto err;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
info->lastkey_length=(*keyinfo->get_key)(keyinfo,nod_flag,&keypos,lastkey);
|
|
if (!info->lastkey_length)
|
|
goto err;
|
|
memcpy(info->lastkey,lastkey,info->lastkey_length);
|
|
}
|
|
info->lastpos=_mi_dpos(info,0,info->lastkey+info->lastkey_length);
|
|
/* Save position for a possible read next / previous */
|
|
info->int_keypos=info->buff+ (keypos-buff);
|
|
info->int_maxpos=info->buff+ (maxpos-buff);
|
|
info->int_nod_flag=nod_flag;
|
|
info->int_keytree_version=keyinfo->version;
|
|
info->last_search_keypage=info->last_keypage;
|
|
info->page_changed=0;
|
|
info->buff_used= (info->buff != buff); /* If we have to reread buff */
|
|
|
|
DBUG_PRINT("exit",("found key at %lu",(ulong) info->lastpos));
|
|
DBUG_RETURN(0);
|
|
|
|
err:
|
|
DBUG_PRINT("exit",("Error: %d",my_errno));
|
|
info->lastpos= HA_OFFSET_ERROR;
|
|
info->page_changed=1;
|
|
DBUG_RETURN (-1);
|
|
} /* _mi_search */
|
|
|
|
|
|
/* Search after key in page-block */
|
|
/* If packed key puts smaller or identical key in buff */
|
|
/* ret_pos point to where find or bigger key starts */
|
|
/* ARGSUSED */
|
|
|
|
int _mi_bin_search(MI_INFO *info, register MI_KEYDEF *keyinfo, uchar *page,
|
|
uchar *key, uint key_len, uint comp_flag, uchar **ret_pos,
|
|
uchar *buff __attribute__((unused)), my_bool *last_key)
|
|
{
|
|
reg4 int start,mid,end,save_end;
|
|
int flag;
|
|
uint totlength,nod_flag,not_used[2];
|
|
DBUG_ENTER("_mi_bin_search");
|
|
|
|
LINT_INIT(flag);
|
|
totlength=keyinfo->keylength+(nod_flag=mi_test_if_nod(page));
|
|
start=0; mid=1;
|
|
save_end=end=(int) ((mi_getint(page)-2-nod_flag)/totlength-1);
|
|
DBUG_PRINT("test",("mi_getint: %d end: %d",mi_getint(page),end));
|
|
page+=2+nod_flag;
|
|
|
|
while (start != end)
|
|
{
|
|
mid= (start+end)/2;
|
|
if ((flag=ha_key_cmp(keyinfo->seg,page+(uint) mid*totlength,key,key_len,
|
|
comp_flag, not_used))
|
|
>= 0)
|
|
end=mid;
|
|
else
|
|
start=mid+1;
|
|
}
|
|
if (mid != start)
|
|
flag=ha_key_cmp(keyinfo->seg,page+(uint) start*totlength,key,key_len,
|
|
comp_flag, not_used);
|
|
if (flag < 0)
|
|
start++; /* point at next, bigger key */
|
|
*ret_pos=page+(uint) start*totlength;
|
|
*last_key= end == save_end;
|
|
DBUG_PRINT("exit",("flag: %d keypos: %d",flag,start));
|
|
DBUG_RETURN(flag);
|
|
} /* _mi_bin_search */
|
|
|
|
|
|
/*
|
|
Locate a packed key in a key page.
|
|
|
|
SYNOPSIS
|
|
_mi_seq_search()
|
|
info Open table information.
|
|
keyinfo Key definition information.
|
|
page Key page (beginning).
|
|
key Search key.
|
|
key_len Length to use from search key or USE_WHOLE_KEY
|
|
comp_flag Search flags like SEARCH_SAME etc.
|
|
ret_pos RETURN Position in key page behind this key.
|
|
buff RETURN Copy of previous or identical unpacked key.
|
|
last_key RETURN If key is last in page.
|
|
|
|
DESCRIPTION
|
|
Used instead of _mi_bin_search() when key is packed.
|
|
Puts smaller or identical key in buff.
|
|
Key is searched sequentially.
|
|
|
|
RETURN
|
|
> 0 Key in 'buff' is smaller than search key.
|
|
0 Key in 'buff' is identical to search key.
|
|
< 0 Not found.
|
|
*/
|
|
|
|
int _mi_seq_search(MI_INFO *info, register MI_KEYDEF *keyinfo, uchar *page,
|
|
uchar *key, uint key_len, uint comp_flag, uchar **ret_pos,
|
|
uchar *buff, my_bool *last_key)
|
|
{
|
|
int flag;
|
|
uint nod_flag,length,not_used[2];
|
|
uchar t_buff[MI_MAX_KEY_BUFF],*end;
|
|
DBUG_ENTER("_mi_seq_search");
|
|
|
|
LINT_INIT(flag); LINT_INIT(length);
|
|
end= page+mi_getint(page);
|
|
nod_flag=mi_test_if_nod(page);
|
|
page+=2+nod_flag;
|
|
*ret_pos=page;
|
|
t_buff[0]=0; /* Avoid bugs */
|
|
while (page < end)
|
|
{
|
|
length=(*keyinfo->get_key)(keyinfo,nod_flag,&page,t_buff);
|
|
if (length == 0 || page > end)
|
|
{
|
|
mi_print_error(info->s, HA_ERR_CRASHED);
|
|
my_errno=HA_ERR_CRASHED;
|
|
DBUG_PRINT("error",
|
|
("Found wrong key: length: %u page: 0x%lx end: 0x%lx",
|
|
length, (long) page, (long) end));
|
|
DBUG_RETURN(MI_FOUND_WRONG_KEY);
|
|
}
|
|
if ((flag=ha_key_cmp(keyinfo->seg,t_buff,key,key_len,comp_flag,
|
|
not_used)) >= 0)
|
|
break;
|
|
#ifdef EXTRA_DEBUG
|
|
DBUG_PRINT("loop",("page: 0x%lx key: '%s' flag: %d", (long) page, t_buff,
|
|
flag));
|
|
#endif
|
|
memcpy(buff,t_buff,length);
|
|
*ret_pos=page;
|
|
}
|
|
if (flag == 0)
|
|
memcpy(buff,t_buff,length); /* Result is first key */
|
|
*last_key= page == end;
|
|
DBUG_PRINT("exit",("flag: %d ret_pos: 0x%lx", flag, (long) *ret_pos));
|
|
DBUG_RETURN(flag);
|
|
} /* _mi_seq_search */
|
|
|
|
|
|
int _mi_prefix_search(MI_INFO *info, register MI_KEYDEF *keyinfo, uchar *page,
|
|
uchar *key, uint key_len, uint nextflag, uchar **ret_pos,
|
|
uchar *buff, my_bool *last_key)
|
|
{
|
|
/*
|
|
my_flag is raw comparison result to be changed according to
|
|
SEARCH_NO_FIND,SEARCH_LAST and HA_REVERSE_SORT flags.
|
|
flag is the value returned by ha_key_cmp and as treated as final
|
|
*/
|
|
int flag=0, my_flag=-1;
|
|
uint nod_flag, length, len, matched, cmplen, kseg_len;
|
|
uint prefix_len,suffix_len;
|
|
int key_len_skip, seg_len_pack, key_len_left;
|
|
uchar *end, *kseg, *vseg;
|
|
uchar *sort_order=keyinfo->seg->charset->sort_order;
|
|
uchar tt_buff[MI_MAX_KEY_BUFF+2], *t_buff=tt_buff+2;
|
|
uchar *saved_from, *saved_to, *saved_vseg;
|
|
uint saved_length=0, saved_prefix_len=0;
|
|
uint length_pack;
|
|
DBUG_ENTER("_mi_prefix_search");
|
|
|
|
LINT_INIT(length);
|
|
LINT_INIT(prefix_len);
|
|
LINT_INIT(seg_len_pack);
|
|
LINT_INIT(saved_from);
|
|
LINT_INIT(saved_to);
|
|
LINT_INIT(saved_vseg);
|
|
|
|
t_buff[0]=0; /* Avoid bugs */
|
|
end= page+mi_getint(page);
|
|
nod_flag=mi_test_if_nod(page);
|
|
page+=2+nod_flag;
|
|
*ret_pos=page;
|
|
kseg=key;
|
|
|
|
get_key_pack_length(kseg_len,length_pack,kseg);
|
|
key_len_skip=length_pack+kseg_len;
|
|
key_len_left=(int) key_len- (int) key_len_skip;
|
|
/* If key_len is 0, then lenght_pack is 1, then key_len_left is -1. */
|
|
cmplen=(key_len_left>=0) ? kseg_len : key_len-length_pack;
|
|
DBUG_PRINT("info",("key: '%.*s'",kseg_len,kseg));
|
|
|
|
/*
|
|
Keys are compressed the following way:
|
|
|
|
If the max length of first key segment <= 127 bytes the prefix is
|
|
1 byte else it's 2 byte
|
|
|
|
(prefix) length The high bit is set if this is a prefix for the prev key.
|
|
[suffix length] Packed length of suffix if the previous was a prefix.
|
|
(suffix) data Key data bytes (past the common prefix or whole segment).
|
|
[next-key-seg] Next key segments (([packed length], data), ...)
|
|
pointer Reference to the data file (last_keyseg->length).
|
|
*/
|
|
|
|
matched=0; /* how many char's from prefix were alredy matched */
|
|
len=0; /* length of previous key unpacked */
|
|
|
|
while (page < end)
|
|
{
|
|
uint packed= *page & 128;
|
|
|
|
vseg=page;
|
|
if (keyinfo->seg->length >= 127)
|
|
{
|
|
suffix_len=mi_uint2korr(vseg) & 32767;
|
|
vseg+=2;
|
|
}
|
|
else
|
|
suffix_len= *vseg++ & 127;
|
|
|
|
if (packed)
|
|
{
|
|
if (suffix_len == 0)
|
|
{
|
|
/* == 0x80 or 0x8000, same key, prefix length == old key length. */
|
|
prefix_len=len;
|
|
}
|
|
else
|
|
{
|
|
/* > 0x80 or 0x8000, this is prefix lgt, packed suffix lgt follows. */
|
|
prefix_len=suffix_len;
|
|
get_key_length(suffix_len,vseg);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
/* Not packed. No prefix used from last key. */
|
|
prefix_len=0;
|
|
}
|
|
|
|
len=prefix_len+suffix_len;
|
|
seg_len_pack=get_pack_length(len);
|
|
t_buff=tt_buff+3-seg_len_pack;
|
|
store_key_length(t_buff,len);
|
|
|
|
if (prefix_len > saved_prefix_len)
|
|
memcpy(t_buff+seg_len_pack+saved_prefix_len,saved_vseg,
|
|
prefix_len-saved_prefix_len);
|
|
saved_vseg=vseg;
|
|
saved_prefix_len=prefix_len;
|
|
|
|
DBUG_PRINT("loop",("page: '%.*s%.*s'",prefix_len,t_buff+seg_len_pack,
|
|
suffix_len,vseg));
|
|
{
|
|
uchar *from=vseg+suffix_len;
|
|
HA_KEYSEG *keyseg;
|
|
uint l;
|
|
|
|
for (keyseg=keyinfo->seg+1 ; keyseg->type ; keyseg++ )
|
|
{
|
|
|
|
if (keyseg->flag & HA_NULL_PART)
|
|
{
|
|
if (!(*from++))
|
|
continue;
|
|
}
|
|
if (keyseg->flag & (HA_VAR_LENGTH_PART | HA_BLOB_PART | HA_SPACE_PACK))
|
|
{
|
|
get_key_length(l,from);
|
|
}
|
|
else
|
|
l=keyseg->length;
|
|
|
|
from+=l;
|
|
}
|
|
from+=keyseg->length;
|
|
page=from+nod_flag;
|
|
length=from-vseg;
|
|
}
|
|
|
|
if (page > end)
|
|
{
|
|
mi_print_error(info->s, HA_ERR_CRASHED);
|
|
my_errno=HA_ERR_CRASHED;
|
|
DBUG_PRINT("error",
|
|
("Found wrong key: length: %u page: 0x%lx end: %lx",
|
|
length, (long) page, (long) end));
|
|
DBUG_RETURN(MI_FOUND_WRONG_KEY);
|
|
}
|
|
|
|
if (matched >= prefix_len)
|
|
{
|
|
/* We have to compare. But we can still skip part of the key */
|
|
uint left;
|
|
uchar *k=kseg+prefix_len;
|
|
|
|
/*
|
|
If prefix_len > cmplen then we are in the end-space comparison
|
|
phase. Do not try to acces the key any more ==> left= 0.
|
|
*/
|
|
left= ((len <= cmplen) ? suffix_len :
|
|
((prefix_len < cmplen) ? cmplen - prefix_len : 0));
|
|
|
|
matched=prefix_len+left;
|
|
|
|
if (sort_order)
|
|
{
|
|
for (my_flag=0;left;left--)
|
|
if ((my_flag= (int) sort_order[*vseg++] - (int) sort_order[*k++]))
|
|
break;
|
|
}
|
|
else
|
|
{
|
|
for (my_flag=0;left;left--)
|
|
if ((my_flag= (int) *vseg++ - (int) *k++))
|
|
break;
|
|
}
|
|
|
|
if (my_flag>0) /* mismatch */
|
|
break;
|
|
if (my_flag==0) /* match */
|
|
{
|
|
/*
|
|
** len cmplen seg_left_len more_segs
|
|
** < matched=len; continue search
|
|
** > = prefix ? found : (matched=len; continue search)
|
|
** > < - ok, found
|
|
** = < - ok, found
|
|
** = = - ok, found
|
|
** = = + next seg
|
|
*/
|
|
if (len < cmplen)
|
|
{
|
|
if ((keyinfo->seg->type != HA_KEYTYPE_TEXT &&
|
|
keyinfo->seg->type != HA_KEYTYPE_VARTEXT1 &&
|
|
keyinfo->seg->type != HA_KEYTYPE_VARTEXT2))
|
|
my_flag= -1;
|
|
else
|
|
{
|
|
/* We have to compare k and vseg as if they were space extended */
|
|
uchar *k_end= k+ (cmplen - len);
|
|
for ( ; k < k_end && *k == ' '; k++) ;
|
|
if (k == k_end)
|
|
goto cmp_rest; /* should never happen */
|
|
if (*k < (uchar) ' ')
|
|
{
|
|
my_flag= 1; /* Compared string is smaller */
|
|
break;
|
|
}
|
|
my_flag= -1; /* Continue searching */
|
|
}
|
|
}
|
|
else if (len > cmplen)
|
|
{
|
|
uchar *vseg_end;
|
|
if ((nextflag & SEARCH_PREFIX) && key_len_left == 0)
|
|
goto fix_flag;
|
|
|
|
/* We have to compare k and vseg as if they were space extended */
|
|
for (vseg_end= vseg + (len-cmplen) ;
|
|
vseg < vseg_end && *vseg == (uchar) ' ';
|
|
vseg++, matched++) ;
|
|
DBUG_ASSERT(vseg < vseg_end);
|
|
|
|
if (*vseg > (uchar) ' ')
|
|
{
|
|
my_flag= 1; /* Compared string is smaller */
|
|
break;
|
|
}
|
|
my_flag= -1; /* Continue searching */
|
|
}
|
|
else
|
|
{
|
|
cmp_rest:
|
|
if (key_len_left>0)
|
|
{
|
|
uint not_used[2];
|
|
if ((flag = ha_key_cmp(keyinfo->seg+1,vseg,
|
|
k, key_len_left, nextflag, not_used)) >= 0)
|
|
break;
|
|
}
|
|
else
|
|
{
|
|
/*
|
|
at this line flag==-1 if the following lines were already
|
|
visited and 0 otherwise, i.e. flag <=0 here always !!!
|
|
*/
|
|
fix_flag:
|
|
DBUG_ASSERT(flag <= 0);
|
|
if (nextflag & (SEARCH_NO_FIND | SEARCH_LAST))
|
|
flag=(nextflag & (SEARCH_BIGGER | SEARCH_LAST)) ? -1 : 1;
|
|
if (flag>=0)
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
matched-=left;
|
|
}
|
|
/* else (matched < prefix_len) ---> do nothing. */
|
|
|
|
memcpy(buff,t_buff,saved_length=seg_len_pack+prefix_len);
|
|
saved_to=buff+saved_length;
|
|
saved_from=saved_vseg;
|
|
saved_length=length;
|
|
*ret_pos=page;
|
|
}
|
|
if (my_flag)
|
|
flag=(keyinfo->seg->flag & HA_REVERSE_SORT) ? -my_flag : my_flag;
|
|
if (flag == 0)
|
|
{
|
|
memcpy(buff,t_buff,saved_length=seg_len_pack+prefix_len);
|
|
saved_to=buff+saved_length;
|
|
saved_from=saved_vseg;
|
|
saved_length=length;
|
|
}
|
|
if (saved_length)
|
|
memcpy(saved_to,saved_from,saved_length);
|
|
|
|
*last_key= page == end;
|
|
|
|
DBUG_PRINT("exit",("flag: %d ret_pos: 0x%lx", flag, (long) *ret_pos));
|
|
DBUG_RETURN(flag);
|
|
} /* _mi_prefix_search */
|
|
|
|
|
|
/* Get pos to a key_block */
|
|
|
|
my_off_t _mi_kpos(uint nod_flag, uchar *after_key)
|
|
{
|
|
after_key-=nod_flag;
|
|
switch (nod_flag) {
|
|
#if SIZEOF_OFF_T > 4
|
|
case 7:
|
|
return mi_uint7korr(after_key)*MI_MIN_KEY_BLOCK_LENGTH;
|
|
case 6:
|
|
return mi_uint6korr(after_key)*MI_MIN_KEY_BLOCK_LENGTH;
|
|
case 5:
|
|
return mi_uint5korr(after_key)*MI_MIN_KEY_BLOCK_LENGTH;
|
|
#else
|
|
case 7:
|
|
after_key++;
|
|
case 6:
|
|
after_key++;
|
|
case 5:
|
|
after_key++;
|
|
#endif
|
|
case 4:
|
|
return ((my_off_t) mi_uint4korr(after_key))*MI_MIN_KEY_BLOCK_LENGTH;
|
|
case 3:
|
|
return ((my_off_t) mi_uint3korr(after_key))*MI_MIN_KEY_BLOCK_LENGTH;
|
|
case 2:
|
|
return (my_off_t) (mi_uint2korr(after_key)*MI_MIN_KEY_BLOCK_LENGTH);
|
|
case 1:
|
|
return (uint) (*after_key)*MI_MIN_KEY_BLOCK_LENGTH;
|
|
case 0: /* At leaf page */
|
|
default: /* Impossible */
|
|
return(HA_OFFSET_ERROR);
|
|
}
|
|
} /* _kpos */
|
|
|
|
|
|
/* Save pos to a key_block */
|
|
|
|
void _mi_kpointer(register MI_INFO *info, register uchar *buff, my_off_t pos)
|
|
{
|
|
pos/=MI_MIN_KEY_BLOCK_LENGTH;
|
|
switch (info->s->base.key_reflength) {
|
|
#if SIZEOF_OFF_T > 4
|
|
case 7: mi_int7store(buff,pos); break;
|
|
case 6: mi_int6store(buff,pos); break;
|
|
case 5: mi_int5store(buff,pos); break;
|
|
#else
|
|
case 7: *buff++=0;
|
|
/* fall trough */
|
|
case 6: *buff++=0;
|
|
/* fall trough */
|
|
case 5: *buff++=0;
|
|
/* fall trough */
|
|
#endif
|
|
case 4: mi_int4store(buff,pos); break;
|
|
case 3: mi_int3store(buff,pos); break;
|
|
case 2: mi_int2store(buff,(uint) pos); break;
|
|
case 1: buff[0]= (uchar) pos; break;
|
|
default: abort(); /* impossible */
|
|
}
|
|
} /* _mi_kpointer */
|
|
|
|
|
|
/* Calc pos to a data-record from a key */
|
|
|
|
|
|
my_off_t _mi_dpos(MI_INFO *info, uint nod_flag, uchar *after_key)
|
|
{
|
|
my_off_t pos;
|
|
after_key-=(nod_flag + info->s->rec_reflength);
|
|
switch (info->s->rec_reflength) {
|
|
#if SIZEOF_OFF_T > 4
|
|
case 8: pos= (my_off_t) mi_uint8korr(after_key); break;
|
|
case 7: pos= (my_off_t) mi_uint7korr(after_key); break;
|
|
case 6: pos= (my_off_t) mi_uint6korr(after_key); break;
|
|
case 5: pos= (my_off_t) mi_uint5korr(after_key); break;
|
|
#else
|
|
case 8: pos= (my_off_t) mi_uint4korr(after_key+4); break;
|
|
case 7: pos= (my_off_t) mi_uint4korr(after_key+3); break;
|
|
case 6: pos= (my_off_t) mi_uint4korr(after_key+2); break;
|
|
case 5: pos= (my_off_t) mi_uint4korr(after_key+1); break;
|
|
#endif
|
|
case 4: pos= (my_off_t) mi_uint4korr(after_key); break;
|
|
case 3: pos= (my_off_t) mi_uint3korr(after_key); break;
|
|
case 2: pos= (my_off_t) mi_uint2korr(after_key); break;
|
|
default:
|
|
pos=0L; /* Shut compiler up */
|
|
}
|
|
return (info->s->options &
|
|
(HA_OPTION_PACK_RECORD | HA_OPTION_COMPRESS_RECORD)) ? pos :
|
|
pos*info->s->base.pack_reclength;
|
|
}
|
|
|
|
|
|
/* Calc position from a record pointer ( in delete link chain ) */
|
|
|
|
my_off_t _mi_rec_pos(MYISAM_SHARE *s, uchar *ptr)
|
|
{
|
|
my_off_t pos;
|
|
switch (s->rec_reflength) {
|
|
#if SIZEOF_OFF_T > 4
|
|
case 8:
|
|
pos= (my_off_t) mi_uint8korr(ptr);
|
|
if (pos == HA_OFFSET_ERROR)
|
|
return HA_OFFSET_ERROR; /* end of list */
|
|
break;
|
|
case 7:
|
|
pos= (my_off_t) mi_uint7korr(ptr);
|
|
if (pos == (((my_off_t) 1) << 56) -1)
|
|
return HA_OFFSET_ERROR; /* end of list */
|
|
break;
|
|
case 6:
|
|
pos= (my_off_t) mi_uint6korr(ptr);
|
|
if (pos == (((my_off_t) 1) << 48) -1)
|
|
return HA_OFFSET_ERROR; /* end of list */
|
|
break;
|
|
case 5:
|
|
pos= (my_off_t) mi_uint5korr(ptr);
|
|
if (pos == (((my_off_t) 1) << 40) -1)
|
|
return HA_OFFSET_ERROR; /* end of list */
|
|
break;
|
|
#else
|
|
case 8:
|
|
case 7:
|
|
case 6:
|
|
case 5:
|
|
ptr+= (s->rec_reflength-4);
|
|
/* fall through */
|
|
#endif
|
|
case 4:
|
|
pos= (my_off_t) mi_uint4korr(ptr);
|
|
if (pos == (my_off_t) (uint32) ~0L)
|
|
return HA_OFFSET_ERROR;
|
|
break;
|
|
case 3:
|
|
pos= (my_off_t) mi_uint3korr(ptr);
|
|
if (pos == (my_off_t) (1 << 24) -1)
|
|
return HA_OFFSET_ERROR;
|
|
break;
|
|
case 2:
|
|
pos= (my_off_t) mi_uint2korr(ptr);
|
|
if (pos == (my_off_t) (1 << 16) -1)
|
|
return HA_OFFSET_ERROR;
|
|
break;
|
|
default: abort(); /* Impossible */
|
|
}
|
|
return ((s->options &
|
|
(HA_OPTION_PACK_RECORD | HA_OPTION_COMPRESS_RECORD)) ? pos :
|
|
pos*s->base.pack_reclength);
|
|
}
|
|
|
|
|
|
/* save position to record */
|
|
|
|
void _mi_dpointer(MI_INFO *info, uchar *buff, my_off_t pos)
|
|
{
|
|
if (!(info->s->options &
|
|
(HA_OPTION_PACK_RECORD | HA_OPTION_COMPRESS_RECORD)) &&
|
|
pos != HA_OFFSET_ERROR)
|
|
pos/=info->s->base.pack_reclength;
|
|
|
|
switch (info->s->rec_reflength) {
|
|
#if SIZEOF_OFF_T > 4
|
|
case 8: mi_int8store(buff,pos); break;
|
|
case 7: mi_int7store(buff,pos); break;
|
|
case 6: mi_int6store(buff,pos); break;
|
|
case 5: mi_int5store(buff,pos); break;
|
|
#else
|
|
case 8: *buff++=0;
|
|
/* fall trough */
|
|
case 7: *buff++=0;
|
|
/* fall trough */
|
|
case 6: *buff++=0;
|
|
/* fall trough */
|
|
case 5: *buff++=0;
|
|
/* fall trough */
|
|
#endif
|
|
case 4: mi_int4store(buff,pos); break;
|
|
case 3: mi_int3store(buff,pos); break;
|
|
case 2: mi_int2store(buff,(uint) pos); break;
|
|
default: abort(); /* Impossible */
|
|
}
|
|
} /* _mi_dpointer */
|
|
|
|
|
|
/* Get key from key-block */
|
|
/* page points at previous key; its advanced to point at next key */
|
|
/* key should contain previous key */
|
|
/* Returns length of found key + pointers */
|
|
/* nod_flag is a flag if we are on nod */
|
|
|
|
/* same as _mi_get_key but used with fixed length keys */
|
|
|
|
uint _mi_get_static_key(register MI_KEYDEF *keyinfo, uint nod_flag,
|
|
register uchar **page, register uchar *key)
|
|
{
|
|
memcpy((uchar*) key,(uchar*) *page,
|
|
(size_t) (keyinfo->keylength+nod_flag));
|
|
*page+=keyinfo->keylength+nod_flag;
|
|
return(keyinfo->keylength);
|
|
} /* _mi_get_static_key */
|
|
|
|
|
|
/*
|
|
get key witch is packed against previous key or key with a NULL column.
|
|
|
|
SYNOPSIS
|
|
_mi_get_pack_key()
|
|
keyinfo key definition information.
|
|
nod_flag If nod: Length of node pointer, else zero.
|
|
page_pos RETURN position in key page behind this key.
|
|
key IN/OUT in: prev key, out: unpacked key.
|
|
|
|
RETURN
|
|
key_length + length of data pointer
|
|
*/
|
|
|
|
uint _mi_get_pack_key(register MI_KEYDEF *keyinfo, uint nod_flag,
|
|
register uchar **page_pos, register uchar *key)
|
|
{
|
|
reg1 HA_KEYSEG *keyseg;
|
|
uchar *start_key,*page=*page_pos;
|
|
uint length;
|
|
|
|
start_key=key;
|
|
for (keyseg=keyinfo->seg ; keyseg->type ;keyseg++)
|
|
{
|
|
if (keyseg->flag & HA_PACK_KEY)
|
|
{
|
|
/* key with length, packed to previous key */
|
|
uchar *start=key;
|
|
uint packed= *page & 128,tot_length,rest_length;
|
|
if (keyseg->length >= 127)
|
|
{
|
|
length=mi_uint2korr(page) & 32767;
|
|
page+=2;
|
|
}
|
|
else
|
|
length= *page++ & 127;
|
|
|
|
if (packed)
|
|
{
|
|
if (length > (uint) keyseg->length)
|
|
{
|
|
mi_print_error(keyinfo->share, HA_ERR_CRASHED);
|
|
my_errno=HA_ERR_CRASHED;
|
|
return 0; /* Error */
|
|
}
|
|
if (length == 0) /* Same key */
|
|
{
|
|
if (keyseg->flag & HA_NULL_PART)
|
|
*key++=1; /* Can't be NULL */
|
|
get_key_length(length,key);
|
|
key+= length; /* Same diff_key as prev */
|
|
if (length > keyseg->length)
|
|
{
|
|
DBUG_PRINT("error",
|
|
("Found too long null packed key: %u of %u at 0x%lx",
|
|
length, keyseg->length, (long) *page_pos));
|
|
DBUG_DUMP("key",(char*) *page_pos,16);
|
|
mi_print_error(keyinfo->share, HA_ERR_CRASHED);
|
|
my_errno=HA_ERR_CRASHED;
|
|
return 0;
|
|
}
|
|
continue;
|
|
}
|
|
if (keyseg->flag & HA_NULL_PART)
|
|
{
|
|
key++; /* Skip null marker*/
|
|
start++;
|
|
}
|
|
|
|
get_key_length(rest_length,page);
|
|
tot_length=rest_length+length;
|
|
|
|
/* If the stored length has changed, we must move the key */
|
|
if (tot_length >= 255 && *start != 255)
|
|
{
|
|
/* length prefix changed from a length of one to a length of 3 */
|
|
bmove_upp((char*) key+length+3,(char*) key+length+1,length);
|
|
*key=255;
|
|
mi_int2store(key+1,tot_length);
|
|
key+=3+length;
|
|
}
|
|
else if (tot_length < 255 && *start == 255)
|
|
{
|
|
bmove(key+1,key+3,length);
|
|
*key=tot_length;
|
|
key+=1+length;
|
|
}
|
|
else
|
|
{
|
|
store_key_length_inc(key,tot_length);
|
|
key+=length;
|
|
}
|
|
memcpy(key,page,rest_length);
|
|
page+=rest_length;
|
|
key+=rest_length;
|
|
continue;
|
|
}
|
|
else
|
|
{
|
|
if (keyseg->flag & HA_NULL_PART)
|
|
{
|
|
if (!length--) /* Null part */
|
|
{
|
|
*key++=0;
|
|
continue;
|
|
}
|
|
*key++=1; /* Not null */
|
|
}
|
|
}
|
|
if (length > (uint) keyseg->length)
|
|
{
|
|
DBUG_PRINT("error",("Found too long packed key: %u of %u at 0x%lx",
|
|
length, keyseg->length, (long) *page_pos));
|
|
DBUG_DUMP("key",(char*) *page_pos,16);
|
|
mi_print_error(keyinfo->share, HA_ERR_CRASHED);
|
|
my_errno=HA_ERR_CRASHED;
|
|
return 0; /* Error */
|
|
}
|
|
store_key_length_inc(key,length);
|
|
}
|
|
else
|
|
{
|
|
if (keyseg->flag & HA_NULL_PART)
|
|
{
|
|
if (!(*key++ = *page++))
|
|
continue;
|
|
}
|
|
if (keyseg->flag &
|
|
(HA_VAR_LENGTH_PART | HA_BLOB_PART | HA_SPACE_PACK))
|
|
{
|
|
uchar *tmp=page;
|
|
get_key_length(length,tmp);
|
|
length+=(uint) (tmp-page);
|
|
}
|
|
else
|
|
length=keyseg->length;
|
|
}
|
|
memcpy((uchar*) key,(uchar*) page,(size_t) length);
|
|
key+=length;
|
|
page+=length;
|
|
}
|
|
length=keyseg->length+nod_flag;
|
|
bmove((uchar*) key,(uchar*) page,length);
|
|
*page_pos= page+length;
|
|
return ((uint) (key-start_key)+keyseg->length);
|
|
} /* _mi_get_pack_key */
|
|
|
|
|
|
|
|
/* key that is packed relatively to previous */
|
|
|
|
uint _mi_get_binary_pack_key(register MI_KEYDEF *keyinfo, uint nod_flag,
|
|
register uchar **page_pos, register uchar *key)
|
|
{
|
|
reg1 HA_KEYSEG *keyseg;
|
|
uchar *start_key,*page,*page_end,*from,*from_end;
|
|
uint length,tmp;
|
|
DBUG_ENTER("_mi_get_binary_pack_key");
|
|
|
|
page= *page_pos;
|
|
page_end=page+MI_MAX_KEY_BUFF+1;
|
|
start_key=key;
|
|
|
|
/*
|
|
Keys are compressed the following way:
|
|
|
|
prefix length Packed length of prefix common with prev key (1 or 3 bytes)
|
|
for each key segment:
|
|
[is null] Null indicator if can be null (1 byte, zero means null)
|
|
[length] Packed length if varlength (1 or 3 bytes)
|
|
key segment 'length' bytes of key segment value
|
|
pointer Reference to the data file (last_keyseg->length).
|
|
|
|
get_key_length() is a macro. It gets the prefix length from 'page'
|
|
and puts it into 'length'. It increments 'page' by 1 or 3, depending
|
|
on the packed length of the prefix length.
|
|
*/
|
|
get_key_length(length,page);
|
|
if (length)
|
|
{
|
|
if (length > keyinfo->maxlength)
|
|
{
|
|
DBUG_PRINT("error",
|
|
("Found too long binary packed key: %u of %u at 0x%lx",
|
|
length, keyinfo->maxlength, (long) *page_pos));
|
|
DBUG_DUMP("key",(char*) *page_pos,16);
|
|
mi_print_error(keyinfo->share, HA_ERR_CRASHED);
|
|
my_errno=HA_ERR_CRASHED;
|
|
DBUG_RETURN(0); /* Wrong key */
|
|
}
|
|
/* Key is packed against prev key, take prefix from prev key. */
|
|
from= key;
|
|
from_end= key + length;
|
|
}
|
|
else
|
|
{
|
|
/* Key is not packed against prev key, take all from page buffer. */
|
|
from= page;
|
|
from_end= page_end;
|
|
}
|
|
|
|
/*
|
|
The trouble is that key can be split in two parts:
|
|
The first part (prefix) is in from .. from_end - 1.
|
|
The second part starts at page.
|
|
The split can be at every byte position. So we need to check for
|
|
the end of the first part before using every byte.
|
|
*/
|
|
for (keyseg=keyinfo->seg ; keyseg->type ;keyseg++)
|
|
{
|
|
if (keyseg->flag & HA_NULL_PART)
|
|
{
|
|
/* If prefix is used up, switch to rest. */
|
|
if (from == from_end) { from=page; from_end=page_end; }
|
|
if (!(*key++ = *from++))
|
|
continue; /* Null part */
|
|
}
|
|
if (keyseg->flag & (HA_VAR_LENGTH_PART | HA_BLOB_PART | HA_SPACE_PACK))
|
|
{
|
|
/* If prefix is used up, switch to rest. */
|
|
if (from == from_end) { from=page; from_end=page_end; }
|
|
/* Get length of dynamic length key part */
|
|
if ((length= (*key++ = *from++)) == 255)
|
|
{
|
|
/* If prefix is used up, switch to rest. */
|
|
if (from == from_end) { from=page; from_end=page_end; }
|
|
length= (uint) ((*key++ = *from++)) << 8;
|
|
/* If prefix is used up, switch to rest. */
|
|
if (from == from_end) { from=page; from_end=page_end; }
|
|
length+= (uint) ((*key++ = *from++));
|
|
}
|
|
}
|
|
else
|
|
length=keyseg->length;
|
|
|
|
if ((tmp=(uint) (from_end-from)) <= length)
|
|
{
|
|
key+=tmp; /* Use old key */
|
|
length-=tmp;
|
|
from=page; from_end=page_end;
|
|
}
|
|
DBUG_PRINT("info",("key: 0x%lx from: 0x%lx length: %u",
|
|
(long) key, (long) from, length));
|
|
memmove((uchar*) key, (uchar*) from, (size_t) length);
|
|
key+=length;
|
|
from+=length;
|
|
}
|
|
/*
|
|
Last segment (type == 0) contains length of data pointer.
|
|
If we have mixed key blocks with data pointer and key block pointer,
|
|
we have to copy both.
|
|
*/
|
|
length=keyseg->length+nod_flag;
|
|
if ((tmp=(uint) (from_end-from)) <= length)
|
|
{
|
|
/* Remaining length is less or equal max possible length. */
|
|
memcpy(key+tmp,page,length-tmp); /* Get last part of key */
|
|
*page_pos= page+length-tmp;
|
|
}
|
|
else
|
|
{
|
|
/*
|
|
Remaining length is greater than max possible length.
|
|
This can happen only if we switched to the new key bytes already.
|
|
'page_end' is calculated with MI_MAX_KEY_BUFF. So it can be far
|
|
behind the real end of the key.
|
|
*/
|
|
if (from_end != page_end)
|
|
{
|
|
DBUG_PRINT("error",("Error when unpacking key"));
|
|
mi_print_error(keyinfo->share, HA_ERR_CRASHED);
|
|
my_errno=HA_ERR_CRASHED;
|
|
DBUG_RETURN(0); /* Error */
|
|
}
|
|
/* Copy data pointer and, if appropriate, key block pointer. */
|
|
memcpy((uchar*) key,(uchar*) from,(size_t) length);
|
|
*page_pos= from+length;
|
|
}
|
|
DBUG_RETURN((uint) (key-start_key)+keyseg->length);
|
|
}
|
|
|
|
|
|
/* Get key at position without knowledge of previous key */
|
|
/* Returns pointer to next key */
|
|
|
|
uchar *_mi_get_key(MI_INFO *info, MI_KEYDEF *keyinfo, uchar *page,
|
|
uchar *key, uchar *keypos, uint *return_key_length)
|
|
{
|
|
uint nod_flag;
|
|
DBUG_ENTER("_mi_get_key");
|
|
|
|
nod_flag=mi_test_if_nod(page);
|
|
if (! (keyinfo->flag & (HA_VAR_LENGTH_KEY | HA_BINARY_PACK_KEY)))
|
|
{
|
|
bmove((uchar*) key,(uchar*) keypos,keyinfo->keylength+nod_flag);
|
|
DBUG_RETURN(keypos+keyinfo->keylength+nod_flag);
|
|
}
|
|
else
|
|
{
|
|
page+=2+nod_flag;
|
|
key[0]=0; /* safety */
|
|
while (page <= keypos)
|
|
{
|
|
*return_key_length=(*keyinfo->get_key)(keyinfo,nod_flag,&page,key);
|
|
if (*return_key_length == 0)
|
|
{
|
|
mi_print_error(info->s, HA_ERR_CRASHED);
|
|
my_errno=HA_ERR_CRASHED;
|
|
DBUG_RETURN(0);
|
|
}
|
|
}
|
|
}
|
|
DBUG_PRINT("exit",("page: 0x%lx length: %u", (long) page,
|
|
*return_key_length));
|
|
DBUG_RETURN(page);
|
|
} /* _mi_get_key */
|
|
|
|
|
|
/* Get key at position without knowledge of previous key */
|
|
/* Returns 0 if ok */
|
|
|
|
static my_bool _mi_get_prev_key(MI_INFO *info, MI_KEYDEF *keyinfo, uchar *page,
|
|
uchar *key, uchar *keypos,
|
|
uint *return_key_length)
|
|
{
|
|
uint nod_flag;
|
|
DBUG_ENTER("_mi_get_prev_key");
|
|
|
|
nod_flag=mi_test_if_nod(page);
|
|
if (! (keyinfo->flag & (HA_VAR_LENGTH_KEY | HA_BINARY_PACK_KEY)))
|
|
{
|
|
*return_key_length=keyinfo->keylength;
|
|
bmove((uchar*) key,(uchar*) keypos- *return_key_length-nod_flag,
|
|
*return_key_length);
|
|
DBUG_RETURN(0);
|
|
}
|
|
else
|
|
{
|
|
page+=2+nod_flag;
|
|
key[0]=0; /* safety */
|
|
while (page < keypos)
|
|
{
|
|
*return_key_length=(*keyinfo->get_key)(keyinfo,nod_flag,&page,key);
|
|
if (*return_key_length == 0)
|
|
{
|
|
mi_print_error(info->s, HA_ERR_CRASHED);
|
|
my_errno=HA_ERR_CRASHED;
|
|
DBUG_RETURN(1);
|
|
}
|
|
}
|
|
}
|
|
DBUG_RETURN(0);
|
|
} /* _mi_get_key */
|
|
|
|
|
|
|
|
/* Get last key from key-page */
|
|
/* Return pointer to where key starts */
|
|
|
|
uchar *_mi_get_last_key(MI_INFO *info, MI_KEYDEF *keyinfo, uchar *page,
|
|
uchar *lastkey, uchar *endpos, uint *return_key_length)
|
|
{
|
|
uint nod_flag;
|
|
uchar *lastpos;
|
|
DBUG_ENTER("_mi_get_last_key");
|
|
DBUG_PRINT("enter",("page: 0x%lx endpos: 0x%lx", (long) page,
|
|
(long) endpos));
|
|
|
|
nod_flag=mi_test_if_nod(page);
|
|
if (! (keyinfo->flag & (HA_VAR_LENGTH_KEY | HA_BINARY_PACK_KEY)))
|
|
{
|
|
lastpos=endpos-keyinfo->keylength-nod_flag;
|
|
*return_key_length=keyinfo->keylength;
|
|
if (lastpos > page)
|
|
bmove((uchar*) lastkey,(uchar*) lastpos,keyinfo->keylength+nod_flag);
|
|
}
|
|
else
|
|
{
|
|
lastpos=(page+=2+nod_flag);
|
|
lastkey[0]=0;
|
|
while (page < endpos)
|
|
{
|
|
lastpos=page;
|
|
*return_key_length=(*keyinfo->get_key)(keyinfo,nod_flag,&page,lastkey);
|
|
if (*return_key_length == 0)
|
|
{
|
|
DBUG_PRINT("error",("Couldn't find last key: page: 0x%lx",
|
|
(long) page));
|
|
mi_print_error(info->s, HA_ERR_CRASHED);
|
|
my_errno=HA_ERR_CRASHED;
|
|
DBUG_RETURN(0);
|
|
}
|
|
}
|
|
}
|
|
DBUG_PRINT("exit",("lastpos: 0x%lx length: %u", (long) lastpos,
|
|
*return_key_length));
|
|
DBUG_RETURN(lastpos);
|
|
} /* _mi_get_last_key */
|
|
|
|
|
|
/* Calculate length of key */
|
|
|
|
uint _mi_keylength(MI_KEYDEF *keyinfo, register uchar *key)
|
|
{
|
|
reg1 HA_KEYSEG *keyseg;
|
|
uchar *start;
|
|
|
|
if (! (keyinfo->flag & (HA_VAR_LENGTH_KEY | HA_BINARY_PACK_KEY)))
|
|
return (keyinfo->keylength);
|
|
|
|
start=key;
|
|
for (keyseg=keyinfo->seg ; keyseg->type ; keyseg++)
|
|
{
|
|
if (keyseg->flag & HA_NULL_PART)
|
|
if (!*key++)
|
|
continue;
|
|
if (keyseg->flag & (HA_SPACE_PACK | HA_BLOB_PART | HA_VAR_LENGTH_PART))
|
|
{
|
|
uint length;
|
|
get_key_length(length,key);
|
|
key+=length;
|
|
}
|
|
else
|
|
key+= keyseg->length;
|
|
}
|
|
return((uint) (key-start)+keyseg->length);
|
|
} /* _mi_keylength */
|
|
|
|
|
|
/*
|
|
Calculate length of part key.
|
|
|
|
Used in mi_rkey() to find the key found for the key-part that was used.
|
|
This is needed in case of multi-byte character sets where we may search
|
|
after '0xDF' but find 'ss'
|
|
*/
|
|
|
|
uint _mi_keylength_part(MI_KEYDEF *keyinfo, register uchar *key,
|
|
HA_KEYSEG *end)
|
|
{
|
|
reg1 HA_KEYSEG *keyseg;
|
|
uchar *start= key;
|
|
|
|
for (keyseg=keyinfo->seg ; keyseg != end ; keyseg++)
|
|
{
|
|
if (keyseg->flag & HA_NULL_PART)
|
|
if (!*key++)
|
|
continue;
|
|
if (keyseg->flag & (HA_SPACE_PACK | HA_BLOB_PART | HA_VAR_LENGTH_PART))
|
|
{
|
|
uint length;
|
|
get_key_length(length,key);
|
|
key+=length;
|
|
}
|
|
else
|
|
key+= keyseg->length;
|
|
}
|
|
return (uint) (key-start);
|
|
}
|
|
|
|
/* Move a key */
|
|
|
|
uchar *_mi_move_key(MI_KEYDEF *keyinfo, uchar *to, uchar *from)
|
|
{
|
|
reg1 uint length;
|
|
memcpy((uchar*) to, (uchar*) from,
|
|
(size_t) (length=_mi_keylength(keyinfo,from)));
|
|
return to+length;
|
|
}
|
|
|
|
/* Find next/previous record with same key */
|
|
/* This can't be used when database is touched after last read */
|
|
|
|
int _mi_search_next(register MI_INFO *info, register MI_KEYDEF *keyinfo,
|
|
uchar *key, uint key_length, uint nextflag, my_off_t pos)
|
|
{
|
|
int error;
|
|
uint nod_flag;
|
|
uchar lastkey[MI_MAX_KEY_BUFF];
|
|
DBUG_ENTER("_mi_search_next");
|
|
DBUG_PRINT("enter",("nextflag: %u lastpos: %lu int_keypos: %lu",
|
|
nextflag, (ulong) info->lastpos,
|
|
(ulong) info->int_keypos));
|
|
DBUG_EXECUTE("key",_mi_print_key(DBUG_FILE,keyinfo->seg,key,key_length););
|
|
|
|
/* Force full read if we are at last key or if we are not on a leaf
|
|
and the key tree has changed since we used it last time
|
|
Note that even if the key tree has changed since last read, we can use
|
|
the last read data from the leaf if we haven't used the buffer for
|
|
something else.
|
|
*/
|
|
|
|
if (((nextflag & SEARCH_BIGGER) && info->int_keypos >= info->int_maxpos) ||
|
|
info->page_changed ||
|
|
(info->int_keytree_version != keyinfo->version &&
|
|
(info->int_nod_flag || info->buff_used)))
|
|
DBUG_RETURN(_mi_search(info,keyinfo,key, USE_WHOLE_KEY,
|
|
nextflag | SEARCH_SAVE_BUFF, pos));
|
|
|
|
if (info->buff_used)
|
|
{
|
|
if (!_mi_fetch_keypage(info,keyinfo,info->last_search_keypage,
|
|
DFLT_INIT_HITS,info->buff,0))
|
|
DBUG_RETURN(-1);
|
|
info->buff_used=0;
|
|
}
|
|
|
|
/* Last used buffer is in info->buff */
|
|
nod_flag=mi_test_if_nod(info->buff);
|
|
|
|
if (nextflag & SEARCH_BIGGER) /* Next key */
|
|
{
|
|
my_off_t tmp_pos=_mi_kpos(nod_flag,info->int_keypos);
|
|
if (tmp_pos != HA_OFFSET_ERROR)
|
|
{
|
|
if ((error=_mi_search(info,keyinfo,key, USE_WHOLE_KEY,
|
|
nextflag | SEARCH_SAVE_BUFF, tmp_pos)) <=0)
|
|
DBUG_RETURN(error);
|
|
}
|
|
memcpy(lastkey,key,key_length);
|
|
if (!(info->lastkey_length=(*keyinfo->get_key)(keyinfo,nod_flag,
|
|
&info->int_keypos,lastkey)))
|
|
DBUG_RETURN(-1);
|
|
}
|
|
else /* Previous key */
|
|
{
|
|
uint length;
|
|
/* Find start of previous key */
|
|
info->int_keypos=_mi_get_last_key(info,keyinfo,info->buff,lastkey,
|
|
info->int_keypos, &length);
|
|
if (!info->int_keypos)
|
|
DBUG_RETURN(-1);
|
|
if (info->int_keypos == info->buff+2)
|
|
DBUG_RETURN(_mi_search(info,keyinfo,key, USE_WHOLE_KEY,
|
|
nextflag | SEARCH_SAVE_BUFF, pos));
|
|
if ((error=_mi_search(info,keyinfo,key, USE_WHOLE_KEY,
|
|
nextflag | SEARCH_SAVE_BUFF,
|
|
_mi_kpos(nod_flag,info->int_keypos))) <= 0)
|
|
DBUG_RETURN(error);
|
|
|
|
/* QQ: We should be able to optimize away the following call */
|
|
if (! _mi_get_last_key(info,keyinfo,info->buff,lastkey,
|
|
info->int_keypos,&info->lastkey_length))
|
|
DBUG_RETURN(-1);
|
|
}
|
|
memcpy(info->lastkey,lastkey,info->lastkey_length);
|
|
info->lastpos=_mi_dpos(info,0,info->lastkey+info->lastkey_length);
|
|
DBUG_PRINT("exit",("found key at %lu",(ulong) info->lastpos));
|
|
DBUG_RETURN(0);
|
|
} /* _mi_search_next */
|
|
|
|
|
|
/* Search after position for the first row in an index */
|
|
/* This is stored in info->lastpos */
|
|
|
|
int _mi_search_first(register MI_INFO *info, register MI_KEYDEF *keyinfo,
|
|
register my_off_t pos)
|
|
{
|
|
uint nod_flag;
|
|
uchar *page;
|
|
DBUG_ENTER("_mi_search_first");
|
|
|
|
if (pos == HA_OFFSET_ERROR)
|
|
{
|
|
my_errno=HA_ERR_KEY_NOT_FOUND;
|
|
info->lastpos= HA_OFFSET_ERROR;
|
|
DBUG_RETURN(-1);
|
|
}
|
|
|
|
do
|
|
{
|
|
if (!_mi_fetch_keypage(info,keyinfo,pos,DFLT_INIT_HITS,info->buff,0))
|
|
{
|
|
info->lastpos= HA_OFFSET_ERROR;
|
|
DBUG_RETURN(-1);
|
|
}
|
|
nod_flag=mi_test_if_nod(info->buff);
|
|
page=info->buff+2+nod_flag;
|
|
} while ((pos=_mi_kpos(nod_flag,page)) != HA_OFFSET_ERROR);
|
|
|
|
if (!(info->lastkey_length=(*keyinfo->get_key)(keyinfo,nod_flag,&page,
|
|
info->lastkey)))
|
|
DBUG_RETURN(-1); /* Crashed */
|
|
|
|
info->int_keypos=page; info->int_maxpos=info->buff+mi_getint(info->buff)-1;
|
|
info->int_nod_flag=nod_flag;
|
|
info->int_keytree_version=keyinfo->version;
|
|
info->last_search_keypage=info->last_keypage;
|
|
info->page_changed=info->buff_used=0;
|
|
info->lastpos=_mi_dpos(info,0,info->lastkey+info->lastkey_length);
|
|
|
|
DBUG_PRINT("exit",("found key at %lu", (ulong) info->lastpos));
|
|
DBUG_RETURN(0);
|
|
} /* _mi_search_first */
|
|
|
|
|
|
/* Search after position for the last row in an index */
|
|
/* This is stored in info->lastpos */
|
|
|
|
int _mi_search_last(register MI_INFO *info, register MI_KEYDEF *keyinfo,
|
|
register my_off_t pos)
|
|
{
|
|
uint nod_flag;
|
|
uchar *buff,*page;
|
|
DBUG_ENTER("_mi_search_last");
|
|
|
|
if (pos == HA_OFFSET_ERROR)
|
|
{
|
|
my_errno=HA_ERR_KEY_NOT_FOUND; /* Didn't find key */
|
|
info->lastpos= HA_OFFSET_ERROR;
|
|
DBUG_RETURN(-1);
|
|
}
|
|
|
|
buff=info->buff;
|
|
do
|
|
{
|
|
if (!_mi_fetch_keypage(info,keyinfo,pos,DFLT_INIT_HITS,buff,0))
|
|
{
|
|
info->lastpos= HA_OFFSET_ERROR;
|
|
DBUG_RETURN(-1);
|
|
}
|
|
page= buff+mi_getint(buff);
|
|
nod_flag=mi_test_if_nod(buff);
|
|
} while ((pos=_mi_kpos(nod_flag,page)) != HA_OFFSET_ERROR);
|
|
|
|
if (!_mi_get_last_key(info,keyinfo,buff,info->lastkey,page,
|
|
&info->lastkey_length))
|
|
DBUG_RETURN(-1);
|
|
info->lastpos=_mi_dpos(info,0,info->lastkey+info->lastkey_length);
|
|
info->int_keypos=info->int_maxpos=page;
|
|
info->int_nod_flag=nod_flag;
|
|
info->int_keytree_version=keyinfo->version;
|
|
info->last_search_keypage=info->last_keypage;
|
|
info->page_changed=info->buff_used=0;
|
|
|
|
DBUG_PRINT("exit",("found key at %lu",(ulong) info->lastpos));
|
|
DBUG_RETURN(0);
|
|
} /* _mi_search_last */
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
**
|
|
** Functions to store and pack a key in a page
|
|
**
|
|
** mi_calc_xx_key_length takes the following arguments:
|
|
** nod_flag If nod: Length of nod-pointer
|
|
** next_key Position to pos after the new key in buffer
|
|
** org_key Key that was before the next key in buffer
|
|
** prev_key Last key before current key
|
|
** key Key that will be stored
|
|
** s_temp Information how next key will be packed
|
|
****************************************************************************/
|
|
|
|
/* Static length key */
|
|
|
|
int
|
|
_mi_calc_static_key_length(MI_KEYDEF *keyinfo,uint nod_flag,
|
|
uchar *next_pos __attribute__((unused)),
|
|
uchar *org_key __attribute__((unused)),
|
|
uchar *prev_key __attribute__((unused)),
|
|
uchar *key, MI_KEY_PARAM *s_temp)
|
|
{
|
|
s_temp->key=key;
|
|
return (int) (s_temp->totlength=keyinfo->keylength+nod_flag);
|
|
}
|
|
|
|
/* Variable length key */
|
|
|
|
int
|
|
_mi_calc_var_key_length(MI_KEYDEF *keyinfo,uint nod_flag,
|
|
uchar *next_pos __attribute__((unused)),
|
|
uchar *org_key __attribute__((unused)),
|
|
uchar *prev_key __attribute__((unused)),
|
|
uchar *key, MI_KEY_PARAM *s_temp)
|
|
{
|
|
s_temp->key=key;
|
|
return (int) (s_temp->totlength=_mi_keylength(keyinfo,key)+nod_flag);
|
|
}
|
|
|
|
/*
|
|
length of key with a variable length first segment which is prefix
|
|
compressed (myisamchk reports 'packed + stripped')
|
|
|
|
Keys are compressed the following way:
|
|
|
|
If the max length of first key segment <= 127 bytes the prefix is
|
|
1 byte else it's 2 byte
|
|
|
|
prefix byte(s) The high bit is set if this is a prefix for the prev key
|
|
length Packed length if the previous was a prefix byte
|
|
[length] data bytes ('length' bytes)
|
|
next-key-seg Next key segments
|
|
|
|
If the first segment can have NULL:
|
|
The length is 0 for NULLS and 1+length for not null columns.
|
|
|
|
*/
|
|
|
|
int
|
|
_mi_calc_var_pack_key_length(MI_KEYDEF *keyinfo,uint nod_flag,uchar *next_key,
|
|
uchar *org_key, uchar *prev_key, uchar *key,
|
|
MI_KEY_PARAM *s_temp)
|
|
{
|
|
reg1 HA_KEYSEG *keyseg;
|
|
int length;
|
|
uint key_length,ref_length,org_key_length=0,
|
|
length_pack,new_key_length,diff_flag,pack_marker;
|
|
uchar *start,*end,*key_end,*sort_order;
|
|
bool same_length;
|
|
|
|
length_pack=s_temp->ref_length=s_temp->n_ref_length=s_temp->n_length=0;
|
|
same_length=0; keyseg=keyinfo->seg;
|
|
key_length=_mi_keylength(keyinfo,key)+nod_flag;
|
|
|
|
sort_order=0;
|
|
if ((keyinfo->flag & HA_FULLTEXT) &&
|
|
((keyseg->type == HA_KEYTYPE_TEXT) ||
|
|
(keyseg->type == HA_KEYTYPE_VARTEXT1) ||
|
|
(keyseg->type == HA_KEYTYPE_VARTEXT2)) &&
|
|
!use_strnxfrm(keyseg->charset))
|
|
sort_order=keyseg->charset->sort_order;
|
|
|
|
/* diff flag contains how many bytes is needed to pack key */
|
|
if (keyseg->length >= 127)
|
|
{
|
|
diff_flag=2;
|
|
pack_marker=32768;
|
|
}
|
|
else
|
|
{
|
|
diff_flag= 1;
|
|
pack_marker=128;
|
|
}
|
|
s_temp->pack_marker=pack_marker;
|
|
|
|
/* Handle the case that the first part have NULL values */
|
|
if (keyseg->flag & HA_NULL_PART)
|
|
{
|
|
if (!*key++)
|
|
{
|
|
s_temp->key=key;
|
|
s_temp->key_length= 0;
|
|
s_temp->totlength=key_length-1+diff_flag;
|
|
s_temp->next_key_pos=0; /* No next key */
|
|
return (s_temp->totlength);
|
|
}
|
|
s_temp->store_not_null=1;
|
|
key_length--; /* We don't store NULL */
|
|
if (prev_key && !*prev_key++)
|
|
org_key=prev_key=0; /* Can't pack against prev */
|
|
else if (org_key)
|
|
org_key++; /* Skip NULL */
|
|
}
|
|
else
|
|
s_temp->store_not_null=0;
|
|
s_temp->prev_key=org_key;
|
|
|
|
/* The key part will start with a packed length */
|
|
|
|
get_key_pack_length(new_key_length,length_pack,key);
|
|
end=key_end= key+ new_key_length;
|
|
start=key;
|
|
|
|
/* Calc how many characters are identical between this and the prev. key */
|
|
if (prev_key)
|
|
{
|
|
get_key_length(org_key_length,prev_key);
|
|
s_temp->prev_key=prev_key; /* Pointer at data */
|
|
/* Don't use key-pack if length == 0 */
|
|
if (new_key_length && new_key_length == org_key_length)
|
|
same_length=1;
|
|
else if (new_key_length > org_key_length)
|
|
end=key + org_key_length;
|
|
|
|
if (sort_order) /* SerG */
|
|
{
|
|
while (key < end && sort_order[*key] == sort_order[*prev_key])
|
|
{
|
|
key++; prev_key++;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
while (key < end && *key == *prev_key)
|
|
{
|
|
key++; prev_key++;
|
|
}
|
|
}
|
|
}
|
|
|
|
s_temp->key=key;
|
|
s_temp->key_length= (uint) (key_end-key);
|
|
|
|
if (same_length && key == key_end)
|
|
{
|
|
/* identical variable length key */
|
|
s_temp->ref_length= pack_marker;
|
|
length=(int) key_length-(int) (key_end-start)-length_pack;
|
|
length+= diff_flag;
|
|
if (next_key)
|
|
{ /* Can't combine with next */
|
|
s_temp->n_length= *next_key; /* Needed by _mi_store_key */
|
|
next_key=0;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (start != key)
|
|
{ /* Starts as prev key */
|
|
ref_length= (uint) (key-start);
|
|
s_temp->ref_length= ref_length + pack_marker;
|
|
length= (int) (key_length - ref_length);
|
|
|
|
length-= length_pack;
|
|
length+= diff_flag;
|
|
length+= ((new_key_length-ref_length) >= 255) ? 3 : 1;/* Rest_of_key */
|
|
}
|
|
else
|
|
{
|
|
s_temp->key_length+=s_temp->store_not_null; /* If null */
|
|
length= key_length - length_pack+ diff_flag;
|
|
}
|
|
}
|
|
s_temp->totlength=(uint) length;
|
|
s_temp->prev_length=0;
|
|
DBUG_PRINT("test",("tot_length: %u length: %d uniq_key_length: %u",
|
|
key_length, length, s_temp->key_length));
|
|
|
|
/* If something after that hasn't length=0, test if we can combine */
|
|
if ((s_temp->next_key_pos=next_key))
|
|
{
|
|
uint packed,n_length;
|
|
|
|
packed = *next_key & 128;
|
|
if (diff_flag == 2)
|
|
{
|
|
n_length= mi_uint2korr(next_key) & 32767; /* Length of next key */
|
|
next_key+=2;
|
|
}
|
|
else
|
|
n_length= *next_key++ & 127;
|
|
if (!packed)
|
|
n_length-= s_temp->store_not_null;
|
|
|
|
if (n_length || packed) /* Don't pack 0 length keys */
|
|
{
|
|
uint next_length_pack, new_ref_length=s_temp->ref_length;
|
|
|
|
if (packed)
|
|
{
|
|
/* If first key and next key is packed (only on delete) */
|
|
if (!prev_key && org_key)
|
|
{
|
|
get_key_length(org_key_length,org_key);
|
|
key=start;
|
|
if (sort_order) /* SerG */
|
|
{
|
|
while (key < end && sort_order[*key] == sort_order[*org_key])
|
|
{
|
|
key++; org_key++;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
while (key < end && *key == *org_key)
|
|
{
|
|
key++; org_key++;
|
|
}
|
|
}
|
|
if ((new_ref_length= (uint) (key - start)))
|
|
new_ref_length+=pack_marker;
|
|
}
|
|
|
|
if (!n_length)
|
|
{
|
|
/*
|
|
We put a different key between two identical variable length keys
|
|
Extend next key to have same prefix as this key
|
|
*/
|
|
if (new_ref_length) /* prefix of previus key */
|
|
{ /* make next key longer */
|
|
s_temp->part_of_prev_key= new_ref_length;
|
|
s_temp->prev_length= org_key_length -
|
|
(new_ref_length-pack_marker);
|
|
s_temp->n_ref_length= s_temp->part_of_prev_key;
|
|
s_temp->n_length= s_temp->prev_length;
|
|
n_length= get_pack_length(s_temp->prev_length);
|
|
s_temp->prev_key+= (new_ref_length - pack_marker);
|
|
length+= s_temp->prev_length + n_length;
|
|
}
|
|
else
|
|
{ /* Can't use prev key */
|
|
s_temp->part_of_prev_key=0;
|
|
s_temp->prev_length= org_key_length;
|
|
s_temp->n_ref_length=s_temp->n_length= org_key_length;
|
|
length+= org_key_length;
|
|
}
|
|
return (int) length;
|
|
}
|
|
|
|
ref_length=n_length;
|
|
/* Get information about not packed key suffix */
|
|
get_key_pack_length(n_length,next_length_pack,next_key);
|
|
|
|
/* Test if new keys has fewer characters that match the previous key */
|
|
if (!new_ref_length)
|
|
{ /* Can't use prev key */
|
|
s_temp->part_of_prev_key= 0;
|
|
s_temp->prev_length= ref_length;
|
|
s_temp->n_ref_length= s_temp->n_length= n_length+ref_length;
|
|
return (int) length+ref_length-next_length_pack;
|
|
}
|
|
if (ref_length+pack_marker > new_ref_length)
|
|
{
|
|
uint new_pack_length=new_ref_length-pack_marker;
|
|
/* We must copy characters from the original key to the next key */
|
|
s_temp->part_of_prev_key= new_ref_length;
|
|
s_temp->prev_length= ref_length - new_pack_length;
|
|
s_temp->n_ref_length=s_temp->n_length=n_length + s_temp->prev_length;
|
|
s_temp->prev_key+= new_pack_length;
|
|
length-= (next_length_pack - get_pack_length(s_temp->n_length));
|
|
return (int) length + s_temp->prev_length;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
/* Next key wasn't a prefix of previous key */
|
|
ref_length=0;
|
|
next_length_pack=0;
|
|
}
|
|
DBUG_PRINT("test",("length: %d next_key: 0x%lx", length,
|
|
(long) next_key));
|
|
|
|
{
|
|
uint tmp_length;
|
|
key=(start+=ref_length);
|
|
if (key+n_length < key_end) /* Normalize length based */
|
|
key_end=key+n_length;
|
|
if (sort_order) /* SerG */
|
|
{
|
|
while (key < key_end && sort_order[*key] ==
|
|
sort_order[*next_key])
|
|
{
|
|
key++; next_key++;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
while (key < key_end && *key == *next_key)
|
|
{
|
|
key++; next_key++;
|
|
}
|
|
}
|
|
if (!(tmp_length=(uint) (key-start)))
|
|
{ /* Key can't be re-packed */
|
|
s_temp->next_key_pos=0;
|
|
return length;
|
|
}
|
|
ref_length+=tmp_length;
|
|
n_length-=tmp_length;
|
|
length-=tmp_length+next_length_pack; /* We gained these chars */
|
|
}
|
|
if (n_length == 0 && ref_length == new_key_length)
|
|
{
|
|
s_temp->n_ref_length=pack_marker; /* Same as prev key */
|
|
}
|
|
else
|
|
{
|
|
s_temp->n_ref_length=ref_length | pack_marker;
|
|
length+= get_pack_length(n_length);
|
|
s_temp->n_length=n_length;
|
|
}
|
|
}
|
|
}
|
|
return length;
|
|
}
|
|
|
|
|
|
/* Length of key which is prefix compressed */
|
|
|
|
int
|
|
_mi_calc_bin_pack_key_length(MI_KEYDEF *keyinfo,uint nod_flag,uchar *next_key,
|
|
uchar *org_key, uchar *prev_key, uchar *key,
|
|
MI_KEY_PARAM *s_temp)
|
|
{
|
|
uint length,key_length,ref_length;
|
|
|
|
s_temp->totlength=key_length=_mi_keylength(keyinfo,key)+nod_flag;
|
|
#ifdef HAVE_purify
|
|
s_temp->n_length= s_temp->n_ref_length=0; /* For valgrind */
|
|
#endif
|
|
s_temp->key=key;
|
|
s_temp->prev_key=org_key;
|
|
if (prev_key) /* If not first key in block */
|
|
{
|
|
/* pack key against previous key */
|
|
/*
|
|
As keys may be identical when running a sort in myisamchk, we
|
|
have to guard against the case where keys may be identical
|
|
*/
|
|
uchar *end;
|
|
end=key+key_length;
|
|
for ( ; *key == *prev_key && key < end; key++,prev_key++) ;
|
|
s_temp->ref_length= ref_length=(uint) (key-s_temp->key);
|
|
length=key_length - ref_length + get_pack_length(ref_length);
|
|
}
|
|
else
|
|
{
|
|
/* No previous key */
|
|
s_temp->ref_length=ref_length=0;
|
|
length=key_length+1;
|
|
}
|
|
if ((s_temp->next_key_pos=next_key)) /* If another key after */
|
|
{
|
|
/* pack key against next key */
|
|
uint next_length,next_length_pack;
|
|
get_key_pack_length(next_length,next_length_pack,next_key);
|
|
|
|
/* If first key and next key is packed (only on delete) */
|
|
if (!prev_key && org_key && next_length)
|
|
{
|
|
uchar *end;
|
|
for (key= s_temp->key, end=key+next_length ;
|
|
*key == *org_key && key < end;
|
|
key++,org_key++) ;
|
|
ref_length= (uint) (key - s_temp->key);
|
|
}
|
|
|
|
if (next_length > ref_length)
|
|
{
|
|
/* We put a key with different case between two keys with the same prefix
|
|
Extend next key to have same prefix as
|
|
this key */
|
|
s_temp->n_ref_length= ref_length;
|
|
s_temp->prev_length= next_length-ref_length;
|
|
s_temp->prev_key+= ref_length;
|
|
return (int) (length+ s_temp->prev_length - next_length_pack +
|
|
get_pack_length(ref_length));
|
|
}
|
|
/* Check how many characters are identical to next key */
|
|
key= s_temp->key+next_length;
|
|
while (*key++ == *next_key++) ;
|
|
if ((ref_length= (uint) (key - s_temp->key)-1) == next_length)
|
|
{
|
|
s_temp->next_key_pos=0;
|
|
return length; /* can't pack next key */
|
|
}
|
|
s_temp->prev_length=0;
|
|
s_temp->n_ref_length=ref_length;
|
|
return (int) (length-(ref_length - next_length) - next_length_pack +
|
|
get_pack_length(ref_length));
|
|
}
|
|
return (int) length;
|
|
}
|
|
|
|
|
|
/*
|
|
** store a key packed with _mi_calc_xxx_key_length in page-buffert
|
|
*/
|
|
|
|
/* store key without compression */
|
|
|
|
void _mi_store_static_key(MI_KEYDEF *keyinfo __attribute__((unused)),
|
|
register uchar *key_pos,
|
|
register MI_KEY_PARAM *s_temp)
|
|
{
|
|
memcpy((uchar*) key_pos,(uchar*) s_temp->key,(size_t) s_temp->totlength);
|
|
}
|
|
|
|
|
|
/* store variable length key with prefix compression */
|
|
|
|
#define store_pack_length(test,pos,length) { \
|
|
if (test) { *((pos)++) = (uchar) (length); } else \
|
|
{ *((pos)++) = (uchar) ((length) >> 8); *((pos)++) = (uchar) (length); } }
|
|
|
|
|
|
void _mi_store_var_pack_key(MI_KEYDEF *keyinfo __attribute__((unused)),
|
|
register uchar *key_pos,
|
|
register MI_KEY_PARAM *s_temp)
|
|
{
|
|
uint length;
|
|
uchar *start;
|
|
|
|
start=key_pos;
|
|
|
|
if (s_temp->ref_length)
|
|
{
|
|
/* Packed against previous key */
|
|
store_pack_length(s_temp->pack_marker == 128,key_pos,s_temp->ref_length);
|
|
/* If not same key after */
|
|
if (s_temp->ref_length != s_temp->pack_marker)
|
|
store_key_length_inc(key_pos,s_temp->key_length);
|
|
}
|
|
else
|
|
{
|
|
/* Not packed against previous key */
|
|
store_pack_length(s_temp->pack_marker == 128,key_pos,s_temp->key_length);
|
|
}
|
|
bmove((uchar*) key_pos,(uchar*) s_temp->key,
|
|
(length=s_temp->totlength-(uint) (key_pos-start)));
|
|
|
|
if (!s_temp->next_key_pos) /* No following key */
|
|
return;
|
|
key_pos+=length;
|
|
|
|
if (s_temp->prev_length)
|
|
{
|
|
/* Extend next key because new key didn't have same prefix as prev key */
|
|
if (s_temp->part_of_prev_key)
|
|
{
|
|
store_pack_length(s_temp->pack_marker == 128,key_pos,
|
|
s_temp->part_of_prev_key);
|
|
store_key_length_inc(key_pos,s_temp->n_length);
|
|
}
|
|
else
|
|
{
|
|
s_temp->n_length+= s_temp->store_not_null;
|
|
store_pack_length(s_temp->pack_marker == 128,key_pos,
|
|
s_temp->n_length);
|
|
}
|
|
memcpy(key_pos, s_temp->prev_key, s_temp->prev_length);
|
|
}
|
|
else if (s_temp->n_ref_length)
|
|
{
|
|
store_pack_length(s_temp->pack_marker == 128,key_pos,s_temp->n_ref_length);
|
|
if (s_temp->n_ref_length == s_temp->pack_marker)
|
|
return; /* Identical key */
|
|
store_key_length(key_pos,s_temp->n_length);
|
|
}
|
|
else
|
|
{
|
|
s_temp->n_length+= s_temp->store_not_null;
|
|
store_pack_length(s_temp->pack_marker == 128,key_pos,s_temp->n_length);
|
|
}
|
|
}
|
|
|
|
|
|
/* variable length key with prefix compression */
|
|
|
|
void _mi_store_bin_pack_key(MI_KEYDEF *keyinfo __attribute__((unused)),
|
|
register uchar *key_pos,
|
|
register MI_KEY_PARAM *s_temp)
|
|
{
|
|
store_key_length_inc(key_pos,s_temp->ref_length);
|
|
memcpy((char*) key_pos,(char*) s_temp->key+s_temp->ref_length,
|
|
(size_t) s_temp->totlength-s_temp->ref_length);
|
|
|
|
if (s_temp->next_key_pos)
|
|
{
|
|
key_pos+=(uint) (s_temp->totlength-s_temp->ref_length);
|
|
store_key_length_inc(key_pos,s_temp->n_ref_length);
|
|
if (s_temp->prev_length) /* If we must extend key */
|
|
{
|
|
memcpy(key_pos,s_temp->prev_key,s_temp->prev_length);
|
|
}
|
|
}
|
|
}
|