ha_partition.cc 153 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/* Copyright (C) 2005 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; either version 2 of the License, or
  (at your option) any later version.

  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 */

/*
patg@krsna.patg.net's avatar
patg@krsna.patg.net committed
18
  This handler was developed by Mikael Ronstrom for version 5.1 of MySQL.
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
  It is an abstraction layer on top of other handlers such as MyISAM,
  InnoDB, Federated, Berkeley DB and so forth. Partitioned tables can also
  be handled by a storage engine. The current example of this is NDB
  Cluster that has internally handled partitioning. This have benefits in
  that many loops needed in the partition handler can be avoided.

  Partitioning has an inherent feature which in some cases is positive and
  in some cases is negative. It splits the data into chunks. This makes
  the data more manageable, queries can easily be parallelised towards the
  parts and indexes are split such that there are less levels in the
  index trees. The inherent disadvantage is that to use a split index
  one has to scan all index parts which is ok for large queries but for
  small queries it can be a disadvantage.

  Partitioning lays the foundation for more manageable databases that are
  extremely large. It does also lay the foundation for more parallelism
  in the execution of queries. This functionality will grow with later
  versions of MySQL.

  You can enable it in your buld by doing the following during your build
  process:
  ./configure --with-partition

  The partition is setup to use table locks. It implements an partition "SHARE"
  that is inserted into a hash by table name. You can use this to store
  information of state that any partition handler object will be able to see
  if it is using the same table.

  Please read the object definition in ha_partition.h before reading the rest
  if this file.
*/

#ifdef __GNUC__
#pragma implementation				// gcc: Class implementation
#endif

kent@mysql.com's avatar
kent@mysql.com committed
55
#include "mysql_priv.h"
56 57 58 59 60 61 62 63 64 65 66 67 68

#include "ha_partition.h"

static const char *ha_par_ext= ".par";
#ifdef NOT_USED
static int free_share(PARTITION_SHARE * share);
static PARTITION_SHARE *get_share(const char *table_name, TABLE * table);
#endif

/****************************************************************************
                MODULE create/delete handler object
****************************************************************************/

69
static handler *partition_create_handler(TABLE_SHARE *share);
70 71
static uint partition_flags();
static uint alter_table_flags(uint flags);
72

73
handlerton partition_hton = {
74
  MYSQL_HANDLERTON_INTERFACE_VERSION,
tulin@dl145b.mysql.com's avatar
merge  
tulin@dl145b.mysql.com committed
75
  "partition",
tomas@poseidon.ndb.mysql.com's avatar
merge  
tomas@poseidon.ndb.mysql.com committed
76
  SHOW_OPTION_YES,
77
  "Partition Storage Engine Helper", /* A comment used by SHOW to describe an engine */
tomas@poseidon.ndb.mysql.com's avatar
merge  
tomas@poseidon.ndb.mysql.com committed
78
  DB_TYPE_PARTITION_DB,
79
  0, /* Method that initializes a storage engine */
tulin@dl145b.mysql.com's avatar
merge  
tulin@dl145b.mysql.com committed
80 81 82 83 84 85 86 87 88 89 90 91
  0, /* slot */
  0, /* savepoint size */
  NULL /*ndbcluster_close_connection*/,
  NULL, /* savepoint_set */
  NULL, /* savepoint_rollback */
  NULL, /* savepoint_release */
  NULL /*ndbcluster_commit*/,
  NULL /*ndbcluster_rollback*/,
  NULL, /* prepare */
  NULL, /* recover */
  NULL, /* commit_by_xid */
  NULL, /* rollback_by_xid */
92 93 94
  NULL,
  NULL,
  NULL,
95 96 97 98 99 100
  partition_create_handler, /* Create a new handler */
  NULL, /* Drop a database */
  NULL, /* Panic call */
  NULL, /* Start Consistent Snapshot */
  NULL, /* Flush logs */
  NULL, /* Show status */
101 102
  partition_flags, /* Partition flags */
  alter_table_flags, /* Partition flags */
103
  NULL, /* Alter Tablespace */
104
  NULL, /* Fill FILES table */
105 106 107
  HTON_NOT_USER_SELECTABLE | HTON_HIDDEN,
  NULL,                         /* binlog_func */
  NULL                          /* binlog_log_query */
tulin@dl145b.mysql.com's avatar
merge  
tulin@dl145b.mysql.com committed
108 109
};

110 111 112 113 114 115 116 117 118 119 120
/*
  Create new partition handler

  SYNOPSIS
    partition_create_handler()
    table                       Table object

  RETURN VALUE
    New partition object
*/

121
static handler *partition_create_handler(TABLE_SHARE *share)
122
{
123
  return new ha_partition(share);
124 125
}

126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
/*
  HA_CAN_PARTITION:
  Used by storage engines that can handle partitioning without this
  partition handler
  (Partition, NDB)

  HA_CAN_UPDATE_PARTITION_KEY:
  Set if the handler can update fields that are part of the partition
  function.

  HA_CAN_PARTITION_UNIQUE:
  Set if the handler can handle unique indexes where the fields of the
  unique key are not part of the fields of the partition function. Thus
  a unique key can be set on all fields.

  HA_USE_AUTO_PARTITION
  Set if the handler sets all tables to be partitioned by default.
*/

static uint partition_flags()
{
  return HA_CAN_PARTITION;
}

static uint alter_table_flags(uint flags __attribute__((unused)))
{
  return (HA_PARTITION_FUNCTION_SUPPORTED |
          HA_FAST_CHANGE_PARTITION);
}

/*
  Constructor method

  SYNOPSIS
    ha_partition()
    table                       Table object

  RETURN VALUE
    NONE
*/
166 167 168

ha_partition::ha_partition(TABLE_SHARE *share)
  :handler(&partition_hton, share), m_part_info(NULL), m_create_handler(FALSE),
169 170 171 172 173 174 175 176
   m_is_sub_partitioned(0)
{
  DBUG_ENTER("ha_partition::ha_partition(table)");
  init_handler_variables();
  DBUG_VOID_RETURN;
}


177 178 179 180 181 182 183 184 185 186 187
/*
  Constructor method

  SYNOPSIS
    ha_partition()
    part_info                       Partition info

  RETURN VALUE
    NONE
*/

188
ha_partition::ha_partition(partition_info *part_info)
189 190
  :handler(&partition_hton, NULL), m_part_info(part_info),
   m_create_handler(TRUE),
191
   m_is_sub_partitioned(m_part_info->is_sub_partitioned())
192 193 194 195 196 197 198 199 200

{
  DBUG_ENTER("ha_partition::ha_partition(part_info)");
  init_handler_variables();
  DBUG_ASSERT(m_part_info);
  DBUG_VOID_RETURN;
}


201 202 203 204 205 206 207 208 209 210
/*
  Initialise handler object

  SYNOPSIS
    init_handler_variables()

  RETURN VALUE
    NONE
*/

211 212 213
void ha_partition::init_handler_variables()
{
  active_index= MAX_KEY;
214 215
  m_mode= 0;
  m_open_test_lock= 0;
216 217 218 219
  m_file_buffer= NULL;
  m_name_buffer_ptr= NULL;
  m_engine_array= NULL;
  m_file= NULL;
220
  m_reorged_file= NULL;
221
  m_new_file= NULL;
222 223
  m_reorged_parts= 0;
  m_added_file= NULL;
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
  m_tot_parts= 0;
  m_has_transactions= 0;
  m_pkey_is_clustered= 0;
  m_lock_type= F_UNLCK;
  m_part_spec.start_part= NO_CURRENT_PART_ID;
  m_scan_value= 2;
  m_ref_length= 0;
  m_part_spec.end_part= NO_CURRENT_PART_ID;
  m_index_scan_type= partition_no_index_scan;
  m_start_key.key= NULL;
  m_start_key.length= 0;
  m_myisam= FALSE;
  m_innodb= FALSE;
  m_extra_cache= FALSE;
  m_extra_cache_size= 0;
  m_table_flags= HA_FILE_BASED | HA_REC_NOT_IN_SEQ;
  m_low_byte_first= 1;
  m_part_field_array= NULL;
  m_ordered_rec_buffer= NULL;
  m_top_entry= NO_CURRENT_PART_ID;
  m_rec_length= 0;
  m_last_part= 0;
  m_rec0= 0;
  m_curr_key_info= 0;
patg@krsna.patg.net's avatar
patg@krsna.patg.net committed
248 249 250 251
  /*
    this allows blackhole to work properly
  */
  m_no_locks= 0;
252 253 254 255 256 257 258 259

#ifdef DONT_HAVE_TO_BE_INITALIZED
  m_start_key.flag= 0;
  m_ordered= TRUE;
#endif
}


260 261 262 263 264 265 266 267 268 269
/*
  Destructor method

  SYNOPSIS
    ~ha_partition()

  RETURN VALUE
    NONE
*/

270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
ha_partition::~ha_partition()
{
  DBUG_ENTER("ha_partition::~ha_partition()");
  if (m_file != NULL)
  {
    uint i;
    for (i= 0; i < m_tot_parts; i++)
      delete m_file[i];
  }
  my_free((char*) m_ordered_rec_buffer, MYF(MY_ALLOW_ZERO_PTR));

  clear_handler_file();
  DBUG_VOID_RETURN;
}


/*
287 288 289 290 291 292 293 294 295 296 297
  Initialise partition handler object

  SYNOPSIS
    ha_initialise()

  RETURN VALUE
    1                         Error
    0                         Success

  DESCRIPTION

298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
  The partition handler is only a layer on top of other engines. Thus it
  can't really perform anything without the underlying handlers. Thus we
  add this method as part of the allocation of a handler object.

  1) Allocation of underlying handlers
     If we have access to the partition info we will allocate one handler
     instance for each partition.
  2) Allocation without partition info
     The cases where we don't have access to this information is when called
     in preparation for delete_table and rename_table and in that case we
     only need to set HA_FILE_BASED. In that case we will use the .par file
     that contains information about the partitions and their engines and
     the names of each partition.
  3) Table flags initialisation
     We need also to set table flags for the partition handler. This is not
     static since it depends on what storage engines are used as underlying
     handlers.
     The table flags is set in this routine to simulate the behaviour of a
     normal storage engine
     The flag HA_FILE_BASED will be set independent of the underlying handlers
  4) Index flags initialisation
     When knowledge exists on the indexes it is also possible to initialise the
     index flags. Again the index flags must be initialised by using the under-
     lying handlers since this is storage engine dependent.
     The flag HA_READ_ORDER will be reset for the time being to indicate no
     ordered output is available from partition handler indexes. Later a merge
     sort will be performed using the underlying handlers.
  5) primary_key_is_clustered, has_transactions and low_byte_first is
     calculated here.
327

328 329 330 331 332
*/

int ha_partition::ha_initialise()
{
  handler **file_array, *file;
333
  DBUG_ENTER("ha_partition::ha_initialise");
334

335
  if (m_create_handler)
336
  {
337
    m_tot_parts= m_part_info->get_tot_partitions();
338
    DBUG_ASSERT(m_tot_parts > 0);
339
    if (new_handlers_from_part_info())
340
      DBUG_RETURN(1);
341 342 343
  }
  else if (!table_share || !table_share->normalized_path.str)
  {
344
    /*
345 346 347
      Called with dummy table share (delete, rename and alter table)
      Don't need to set-up table flags other than
      HA_FILE_BASED here
348
    */
349 350 351 352 353
    m_table_flags|= HA_FILE_BASED | HA_REC_NOT_IN_SEQ;
    DBUG_RETURN(0);
  }
  else if (get_from_handler_file(table_share->normalized_path.str))
  {
354
    mem_alloc_error(2);
355
    DBUG_RETURN(1);
356
  }
357 358 359 360 361 362 363 364 365
  /*
    We create all underlying table handlers here. We do it in this special
    method to be able to report allocation errors.

    Set up table_flags, low_byte_first, primary_key_is_clustered and
    has_transactions since they are called often in all kinds of places,
    other parameters are calculated on demand.
    HA_FILE_BASED is always set for partition handler since we use a
    special file for handling names of partitions, engine types.
366
    HA_CAN_GEOMETRY, HA_CAN_FULLTEXT, HA_CAN_SQL_HANDLER, HA_DUPP_POS,
367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
    HA_CAN_INSERT_DELAYED is disabled until further investigated.
  */
  m_table_flags= m_file[0]->table_flags();
  m_low_byte_first= m_file[0]->low_byte_first();
  m_has_transactions= TRUE;
  m_pkey_is_clustered= TRUE;
  file_array= m_file;
  do
  {
    file= *file_array;
    if (m_low_byte_first != file->low_byte_first())
    {
      // Cannot have handlers with different endian
      my_error(ER_MIX_HANDLER_ERROR, MYF(0));
      DBUG_RETURN(1);
    }
    if (!file->has_transactions())
      m_has_transactions= FALSE;
    if (!file->primary_key_is_clustered())
      m_pkey_is_clustered= FALSE;
    m_table_flags&= file->table_flags();
  } while (*(++file_array));
389 390
  m_table_flags&= ~(HA_CAN_GEOMETRY | HA_CAN_FULLTEXT | HA_DUPP_POS |
                    HA_CAN_SQL_HANDLER | HA_CAN_INSERT_DELAYED);
391 392 393 394 395 396 397
  m_table_flags|= HA_FILE_BASED | HA_REC_NOT_IN_SEQ;
  DBUG_RETURN(0);
}

/****************************************************************************
                MODULE meta data changes
****************************************************************************/
398
/*
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
  Create partition names

  SYNOPSIS
    create_partition_name()
    out:out                   Created partition name string
    in1                       First part
    in2                       Second part
    name_variant              Normal, temporary or renamed partition name

  RETURN VALUE
    NONE

  DESCRIPTION
    This method is used to calculate the partition name, service routine to
    the del_ren_cre_table method.
414 415
*/

416 417 418 419 420 421
#define NORMAL_PART_NAME 0
#define TEMP_PART_NAME 1
#define RENAMED_PART_NAME 2
static void create_partition_name(char *out, const char *in1,
                                  const char *in2, uint name_variant,
                                  bool translate)
422
{
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438
  char transl_part_name[FN_REFLEN];
  const char *transl_part;

  if (translate)
  {
    tablename_to_filename(in2, transl_part_name, FN_REFLEN);
    transl_part= transl_part_name;
  }
  else
    transl_part= in2;
  if (name_variant == NORMAL_PART_NAME)
    strxmov(out, in1, "#P#", transl_part, NullS);
  else if (name_variant == TEMP_PART_NAME)
    strxmov(out, in1, "#P#", transl_part, "#TMP#", NullS);
  else if (name_variant == RENAMED_PART_NAME)
    strxmov(out, in1, "#P#", transl_part, "#REN#", NullS);
439 440 441
}

/*
442 443 444 445 446 447 448 449 450 451 452 453 454 455 456
  Create subpartition name

  SYNOPSIS
    create_subpartition_name()
    out:out                   Created partition name string
    in1                       First part
    in2                       Second part
    in3                       Third part
    name_variant              Normal, temporary or renamed partition name

  RETURN VALUE
    NONE

  DESCRIPTION
  This method is used to calculate the subpartition name, service routine to
457 458 459 460
  the del_ren_cre_table method.
*/

static void create_subpartition_name(char *out, const char *in1,
461 462
                                     const char *in2, const char *in3,
                                     uint name_variant)
463
{
464 465 466 467 468 469 470 471 472 473 474 475 476
  char transl_part_name[FN_REFLEN], transl_subpart_name[FN_REFLEN];

  tablename_to_filename(in2, transl_part_name, FN_REFLEN);
  tablename_to_filename(in3, transl_subpart_name, FN_REFLEN);
  if (name_variant == NORMAL_PART_NAME)
    strxmov(out, in1, "#P#", transl_part_name,
            "#SP#", transl_subpart_name, NullS);
  else if (name_variant == TEMP_PART_NAME)
    strxmov(out, in1, "#P#", transl_part_name,
            "#SP#", transl_subpart_name, "#TMP#", NullS);
  else if (name_variant == RENAMED_PART_NAME)
    strxmov(out, in1, "#P#", transl_part_name,
            "#SP#", transl_subpart_name, "#REN#", NullS);
477 478 479
}


480
/*
481
  Delete a table
482

483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504
  SYNOPSIS
    delete_table()
    name                    Full path of table name

  RETURN VALUE
    >0                        Error
    0                         Success

  DESCRIPTION
    Used to delete a table. By the time delete_table() has been called all
    opened references to this table will have been closed (and your globally
    shared references released. The variable name will just be the name of
    the table. You will need to remove any files you have created at this
    point.

    If you do not implement this, the default delete_table() is called from
    handler.cc and it will delete all files with the file extentions returned
    by bas_ext().

    Called from handler.cc by delete_table and  ha_create_table(). Only used
    during create if the table_flag HA_DROP_BEFORE_CREATE was specified for
    the storage engine.
505 506 507 508 509 510
*/

int ha_partition::delete_table(const char *name)
{
  int error;
  DBUG_ENTER("ha_partition::delete_table");
511

512 513 514 515 516 517 518
  if ((error= del_ren_cre_table(name, NULL, NULL, NULL)))
    DBUG_RETURN(error);
  DBUG_RETURN(handler::delete_table(name));
}


/*
519 520 521 522 523 524 525 526 527 528
  Rename a table

  SYNOPSIS
    rename_table()
    from                      Full path of old table name
    to                        Full path of new table name

  RETURN VALUE
    >0                        Error
    0                         Success
529

530 531
  DESCRIPTION
    Renames a table from one name to another from alter table call.
532

533 534 535 536 537
    If you do not implement this, the default rename_table() is called from
    handler.cc and it will rename all files with the file extentions returned
    by bas_ext().

    Called from sql_table.cc by mysql_rename_table().
538 539 540 541 542 543
*/

int ha_partition::rename_table(const char *from, const char *to)
{
  int error;
  DBUG_ENTER("ha_partition::rename_table");
544

545 546 547 548 549 550 551
  if ((error= del_ren_cre_table(from, to, NULL, NULL)))
    DBUG_RETURN(error);
  DBUG_RETURN(handler::rename_table(from, to));
}


/*
552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567
  Create the handler file (.par-file)

  SYNOPSIS
    create_handler_files()
    name                              Full path of table name

  RETURN VALUE
    >0                        Error
    0                         Success

  DESCRIPTION
    create_handler_files is called to create any handler specific files
    before opening the file with openfrm to later call ::create on the
    file object.
    In the partition handler this is used to store the names of partitions
    and types of engines in the partitions.
568 569 570 571 572
*/

int ha_partition::create_handler_files(const char *name)
{
  DBUG_ENTER("ha_partition::create_handler_files()");
573 574 575 576 577

  /*
    We need to update total number of parts since we might write the handler
    file as part of a partition management command
  */
578 579 580 581 582 583 584 585 586 587
  if (create_handler_file(name))
  {
    my_error(ER_CANT_CREATE_HANDLER_FILE, MYF(0));
    DBUG_RETURN(1);
  }
  DBUG_RETURN(0);
}


/*
588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083
  Create a partitioned table

  SYNOPSIS
    create()
    name                              Full path of table name
    table_arg                         Table object
    create_info                       Create info generated for CREATE TABLE

  RETURN VALUE
    >0                        Error
    0                         Success

  DESCRIPTION
    create() is called to create a table. The variable name will have the name
    of the table. When create() is called you do not need to worry about
    opening the table. Also, the FRM file will have already been created so
    adjusting create_info will not do you any good. You can overwrite the frm
    file at this point if you wish to change the table definition, but there
    are no methods currently provided for doing that.

    Called from handler.cc by ha_create_table().
*/

int ha_partition::create(const char *name, TABLE *table_arg,
			 HA_CREATE_INFO *create_info)
{
  char t_name[FN_REFLEN];
  DBUG_ENTER("ha_partition::create");

  strmov(t_name, name);
  DBUG_ASSERT(*fn_rext((char*)name) == '\0');
  if (del_ren_cre_table(t_name, NULL, table_arg, create_info))
  {
    handler::delete_table(t_name);
    DBUG_RETURN(1);
  }
  DBUG_RETURN(0);
}


/*
  Drop partitions as part of ALTER TABLE of partitions

  SYNOPSIS
    drop_partitions()
    path                        Complete path of db and table name

  RETURN VALUE
    >0                          Failure
    0                           Success

  DESCRIPTION
    Use part_info object on handler object to deduce which partitions to
    drop (each partition has a state attached to it)
*/

int ha_partition::drop_partitions(const char *path)
{
  List_iterator<partition_element> part_it(m_part_info->partitions);
  List_iterator<partition_element> temp_it(m_part_info->temp_partitions);
  char part_name_buff[FN_REFLEN];
  uint no_parts= m_part_info->partitions.elements;
  uint part_count= 0;
  uint no_subparts= m_part_info->no_subparts;
  uint i= 0;
  uint name_variant;
  int  error= 1;
  bool reorged_parts= (m_reorged_parts > 0);
  bool temp_partitions= (m_part_info->temp_partitions.elements > 0);
  DBUG_ENTER("ha_partition::drop_partitions");

  if (temp_partitions)
    no_parts= m_part_info->temp_partitions.elements;
  do
  {
    partition_element *part_elem;
    if (temp_partitions)
    {
      /*
        We need to remove the reorganised partitions that were put in the
        temp_partitions-list.
      */
      part_elem= temp_it++;
      DBUG_ASSERT(part_elem->part_state == PART_TO_BE_DROPPED);
    }
    else
      part_elem= part_it++;
    if (part_elem->part_state == PART_TO_BE_DROPPED ||
        part_elem->part_state == PART_IS_CHANGED)
    {
      handler *file;
      /*
        This part is to be dropped, meaning the part or all its subparts.
      */
      name_variant= NORMAL_PART_NAME;
      if (part_elem->part_state == PART_IS_CHANGED ||
          (part_elem->part_state == PART_TO_BE_DROPPED && temp_partitions))
        name_variant= RENAMED_PART_NAME;
      if (m_is_sub_partitioned)
      {
        List_iterator<partition_element> sub_it(part_elem->subpartitions);
        uint j= 0, part;
        do
        {
          partition_element *sub_elem= sub_it++;
          part= i * no_subparts + j;
          create_subpartition_name(part_name_buff, path,
                                   part_elem->partition_name,
                                   sub_elem->partition_name, name_variant);
          if (reorged_parts)
            file= m_reorged_file[part_count++];
          else
            file= m_file[part];
          DBUG_PRINT("info", ("Drop subpartition %s", part_name_buff));
          error= file->delete_table((const char *) part_name_buff);
        } while (++j < no_subparts);
      }
      else
      {
        create_partition_name(part_name_buff, path,
                              part_elem->partition_name, name_variant,
                              TRUE);
        if (reorged_parts)
          file= m_reorged_file[part_count++];
        else
          file= m_file[i];
        DBUG_PRINT("info", ("Drop partition %s", part_name_buff));
        error= file->delete_table((const char *) part_name_buff);
      }
      if (part_elem->part_state == PART_IS_CHANGED)
        part_elem->part_state= PART_NORMAL;
      else
        part_elem->part_state= PART_IS_DROPPED;
    }
  } while (++i < no_parts);
  DBUG_RETURN(error);
}


/*
  Rename partitions as part of ALTER TABLE of partitions

  SYNOPSIS
    rename_partitions()
    path                        Complete path of db and table name

  RETURN VALUE
    TRUE                        Failure
    FALSE                       Success

  DESCRIPTION
    When reorganising partitions, adding hash partitions and coalescing
    partitions it can be necessary to rename partitions while holding
    an exclusive lock on the table.
    Which partitions to rename is given by state of partitions found by the
    partition info struct referenced from the handler object
*/

int ha_partition::rename_partitions(const char *path)
{
  List_iterator<partition_element> part_it(m_part_info->partitions);
  List_iterator<partition_element> temp_it(m_part_info->temp_partitions);
  char part_name_buff[FN_REFLEN];
  char norm_name_buff[FN_REFLEN];
  uint no_parts= m_part_info->partitions.elements;
  uint part_count= 0;
  uint no_subparts= m_part_info->no_subparts;
  uint i= 0;
  uint j= 0;
  int error= 1;
  uint temp_partitions= m_part_info->temp_partitions.elements;
  handler *file;
  partition_element *part_elem, *sub_elem;
  DBUG_ENTER("ha_partition::rename_partitions");

  if (temp_partitions)
  {
    do
    {
      part_elem= temp_it++;
      if (m_is_sub_partitioned)
      {
        List_iterator<partition_element> sub_it(part_elem->subpartitions);
        do
        {
          sub_elem= sub_it++;
          file= m_reorged_file[part_count++];
          create_subpartition_name(part_name_buff, path,
                                   part_elem->partition_name,
                                   sub_elem->partition_name,
                                   RENAMED_PART_NAME);
          create_subpartition_name(norm_name_buff, path,
                                   part_elem->partition_name,
                                   sub_elem->partition_name,
                                   NORMAL_PART_NAME);
          DBUG_PRINT("info", ("Rename subpartition from %s to %s",
                     norm_name_buff, part_name_buff));
          error= file->rename_table((const char *) norm_name_buff,
                                    (const char *) part_name_buff);
        } while (++j < no_subparts);
      }
      else
      {
        file= m_reorged_file[part_count++];
        create_partition_name(part_name_buff, path,
                              part_elem->partition_name, RENAMED_PART_NAME,
                              TRUE);
        create_partition_name(norm_name_buff, path,
                              part_elem->partition_name, NORMAL_PART_NAME,
                              TRUE);
        DBUG_PRINT("info", ("Rename partition from %s to %s",
                   norm_name_buff, part_name_buff));
        error= file->rename_table((const char *) norm_name_buff,
                                  (const char *) part_name_buff);
      }
    } while (++i < temp_partitions);
  }
  i= 0;
  do
  {
    part_elem= part_it++;
    if (part_elem->part_state == PART_IS_CHANGED ||
        (part_elem->part_state == PART_IS_ADDED && temp_partitions))
    {
      if (m_is_sub_partitioned)
      {
        List_iterator<partition_element> sub_it(part_elem->subpartitions);
        uint part;

        j= 0;
        do
        {
          sub_elem= sub_it++;
          part= i * no_subparts + j;
          create_subpartition_name(norm_name_buff, path,
                                   part_elem->partition_name,
                                   sub_elem->partition_name,
                                   NORMAL_PART_NAME);
          if (part_elem->part_state == PART_IS_CHANGED)
          {
            file= m_reorged_file[part_count++];
            create_subpartition_name(part_name_buff, path,
                                     part_elem->partition_name,
                                     sub_elem->partition_name,
                                     RENAMED_PART_NAME);
            DBUG_PRINT("info", ("Rename subpartition from %s to %s",
                       norm_name_buff, part_name_buff));
            error= file->rename_table((const char *) norm_name_buff,
                                      (const char *) part_name_buff);
          }
          file= m_new_file[part];
          create_subpartition_name(part_name_buff, path,
                                   part_elem->partition_name,
                                   sub_elem->partition_name,
                                   TEMP_PART_NAME);
          DBUG_PRINT("info", ("Rename subpartition from %s to %s",
                     part_name_buff, norm_name_buff));
          error= file->rename_table((const char *) part_name_buff,
                                    (const char *) norm_name_buff);
        } while (++j < no_subparts);
      }
      else
      {
        create_partition_name(norm_name_buff, path,
                              part_elem->partition_name, NORMAL_PART_NAME,
                              TRUE);
        if (part_elem->part_state == PART_IS_CHANGED)
        {
          file= m_reorged_file[part_count++];
          create_partition_name(part_name_buff, path,
                                part_elem->partition_name, RENAMED_PART_NAME,
                                TRUE);
          DBUG_PRINT("info", ("Rename partition from %s to %s",
                     norm_name_buff, part_name_buff));
          error= file->rename_table((const char *) norm_name_buff,
                                    (const char *) part_name_buff);
        }
        file= m_new_file[i];
        create_partition_name(part_name_buff, path,
                              part_elem->partition_name, TEMP_PART_NAME,
                              TRUE);
        DBUG_PRINT("info", ("Rename partition from %s to %s",
                   part_name_buff, norm_name_buff));
        error= file->rename_table((const char *) part_name_buff,
                                  (const char *) norm_name_buff);
      }
    }
  } while (++i < no_parts);
  DBUG_RETURN(error);
}


#define OPTIMIZE_PARTS 1
#define ANALYZE_PARTS 2
#define CHECK_PARTS   3
#define REPAIR_PARTS 4

/*
  Optimize table

  SYNOPSIS
    optimize()
    thd               Thread object
    check_opt         Check/analyze/repair/optimize options

  RETURN VALUES
    >0                Error
    0                 Success
*/

int ha_partition::optimize(THD *thd, HA_CHECK_OPT *check_opt)
{
  DBUG_ENTER("ha_partition::optimize");

  DBUG_RETURN(handle_opt_partitions(thd, &thd->lex->check_opt, 
                                    OPTIMIZE_PARTS, TRUE));
}


/*
  Analyze table

  SYNOPSIS
    analyze()
    thd               Thread object
    check_opt         Check/analyze/repair/optimize options

  RETURN VALUES
    >0                Error
    0                 Success
*/

int ha_partition::analyze(THD *thd, HA_CHECK_OPT *check_opt)
{
  DBUG_ENTER("ha_partition::analyze");

  DBUG_RETURN(handle_opt_partitions(thd, &thd->lex->check_opt, 
                                    ANALYZE_PARTS, TRUE));
}


/*
  Check table

  SYNOPSIS
    check()
    thd               Thread object
    check_opt         Check/analyze/repair/optimize options

  RETURN VALUES
    >0                Error
    0                 Success
*/

int ha_partition::check(THD *thd, HA_CHECK_OPT *check_opt)
{
  DBUG_ENTER("ha_partition::check");

  DBUG_RETURN(handle_opt_partitions(thd, &thd->lex->check_opt, 
                                    CHECK_PARTS, TRUE));
}


/*
  Repair table

  SYNOPSIS
    repair()
    thd               Thread object
    check_opt         Check/analyze/repair/optimize options

  RETURN VALUES
    >0                Error
    0                 Success
*/

int ha_partition::repair(THD *thd, HA_CHECK_OPT *check_opt)
{
  DBUG_ENTER("ha_partition::repair");

  DBUG_RETURN(handle_opt_partitions(thd, &thd->lex->check_opt, 
                                    REPAIR_PARTS, TRUE));
}

/*
  Optimize partitions

  SYNOPSIS
    optimize_partitions()
    thd                   Thread object
  RETURN VALUE
    >0                        Failure
    0                         Success
  DESCRIPTION
    Call optimize on each partition marked with partition state PART_CHANGED
*/

int ha_partition::optimize_partitions(THD *thd)
{
  DBUG_ENTER("ha_partition::optimize_partitions");

  DBUG_RETURN(handle_opt_partitions(thd, &thd->lex->check_opt, 
                                    OPTIMIZE_PARTS, FALSE));
}

/*
  Analyze partitions

  SYNOPSIS
    analyze_partitions()
    thd                   Thread object
  RETURN VALUE
    >0                        Failure
    0                         Success
  DESCRIPTION
    Call analyze on each partition marked with partition state PART_CHANGED
*/

int ha_partition::analyze_partitions(THD *thd)
{
  DBUG_ENTER("ha_partition::analyze_partitions");

  DBUG_RETURN(handle_opt_partitions(thd, &thd->lex->check_opt, 
                                    ANALYZE_PARTS, FALSE));
}

/*
  Check partitions

  SYNOPSIS
    check_partitions()
    thd                   Thread object
  RETURN VALUE
    >0                        Failure
    0                         Success
  DESCRIPTION
    Call check on each partition marked with partition state PART_CHANGED
*/

int ha_partition::check_partitions(THD *thd)
{
  DBUG_ENTER("ha_partition::check_partitions");

  DBUG_RETURN(handle_opt_partitions(thd, &thd->lex->check_opt, 
                                    CHECK_PARTS, FALSE));
}

/*
  Repair partitions

  SYNOPSIS
    repair_partitions()
    thd                   Thread object
  RETURN VALUE
    >0                        Failure
    0                         Success
  DESCRIPTION
    Call repair on each partition marked with partition state PART_CHANGED
*/

int ha_partition::repair_partitions(THD *thd)
{
  DBUG_ENTER("ha_partition::repair_partitions");

  DBUG_RETURN(handle_opt_partitions(thd, &thd->lex->check_opt, 
                                    REPAIR_PARTS, FALSE));
}


/*
  Handle optimize/analyze/check/repair of one partition

  SYNOPSIS
    handle_opt_part()
    thd                      Thread object
    check_opt                Options
    file                     Handler object of partition
    flag                     Optimize/Analyze/Check/Repair flag

  RETURN VALUE
    >0                        Failure
    0                         Success
*/

static int handle_opt_part(THD *thd, HA_CHECK_OPT *check_opt,
                           handler *file, uint flag)
{
  int error;
  DBUG_ENTER("handle_opt_part");
  DBUG_PRINT("enter", ("flag = %u", flag));

  if (flag == OPTIMIZE_PARTS)
    error= file->optimize(thd, check_opt);
  else if (flag == ANALYZE_PARTS)
    error= file->analyze(thd, check_opt);
  else if (flag == CHECK_PARTS)
1084
    error= file->ha_check(thd, check_opt);
1085
  else if (flag == REPAIR_PARTS)
1086
    error= file->ha_repair(thd, check_opt);
1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204
  else
  {
    DBUG_ASSERT(FALSE);
    error= 1;
  }
  if (error == HA_ADMIN_ALREADY_DONE)
    error= 0;
  DBUG_RETURN(error);
}


/*
  Handle optimize/analyze/check/repair of partitions

  SYNOPSIS
    handle_opt_partitions()
    thd                      Thread object
    check_opt                Options
    flag                     Optimize/Analyze/Check/Repair flag
    all_parts                All partitions or only a subset

  RETURN VALUE
    >0                        Failure
    0                         Success
*/

int ha_partition::handle_opt_partitions(THD *thd, HA_CHECK_OPT *check_opt,
                                        uint flag, bool all_parts)
{
  List_iterator<partition_element> part_it(m_part_info->partitions);
  uint no_parts= m_part_info->no_parts;
  uint no_subparts= m_part_info->no_subparts;
  uint i= 0;
  LEX *lex= thd->lex;
  int error;
  DBUG_ENTER("ha_partition::handle_opt_partitions");
  DBUG_PRINT("enter", ("all_parts %u, flag= %u", all_parts, flag));

  do
  {
    partition_element *part_elem= part_it++;
    if (all_parts || part_elem->part_state == PART_CHANGED)
    {
      handler *file;
      if (m_is_sub_partitioned)
      {
        List_iterator<partition_element> sub_it(part_elem->subpartitions);
        uint j= 0, part;
        do
        {
          partition_element *sub_elem= sub_it++;
          part= i * no_subparts + j;
          DBUG_PRINT("info", ("Optimize subpartition %u",
                     part));
          if ((error= handle_opt_part(thd, check_opt, m_file[part], flag)))
          {
            my_error(ER_GET_ERRNO, MYF(0), error);
            DBUG_RETURN(TRUE);
          }
        } while (++j < no_subparts);
      }
      else
      {
        DBUG_PRINT("info", ("Optimize partition %u", i));
        if ((error= handle_opt_part(thd, check_opt, m_file[i], flag)))
        {
          my_error(ER_GET_ERRNO, MYF(0), error);
          DBUG_RETURN(TRUE);
        }
      }
    }
  } while (++i < no_parts);
  DBUG_RETURN(FALSE);
}

/*
  Prepare by creating a new partition

  SYNOPSIS
    prepare_new_partition()
    table                      Table object
    create_info                Create info from CREATE TABLE
    file                       Handler object of new partition
    part_name                  partition name

  RETURN VALUE
    >0                         Error
    0                          Success
*/

int ha_partition::prepare_new_partition(TABLE *table,
                                        HA_CREATE_INFO *create_info,
                                        handler *file, const char *part_name)
{
  int error;
  bool create_flag= FALSE;
  bool open_flag= FALSE;
  DBUG_ENTER("prepare_new_partition");

  if ((error= file->create(part_name, table, create_info)))
    goto error;
  create_flag= TRUE;
  if ((error= file->ha_open(table, part_name, m_mode, m_open_test_lock)))
    goto error;
  if ((error= file->external_lock(current_thd, m_lock_type)))
    goto error;

  DBUG_RETURN(0);
error:
  if (create_flag)
    VOID(file->delete_table(part_name));
  print_error(error, MYF(0));
  DBUG_RETURN(error);
}


/*
  Cleanup by removing all created partitions after error
1205

1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219
  SYNOPSIS
    cleanup_new_partition()
    part_count             Number of partitions to remove

  RETURN VALUE
    NONE

  DESCRIPTION
  TODO:
    We must ensure that in the case that we get an error during the process
    that we call external_lock with F_UNLCK, close the table and delete the
    table in the case where we have been successful with prepare_handler.
    We solve this by keeping an array of successful calls to prepare_handler
    which can then be used to undo the call.
1220 1221
*/

1222
void ha_partition::cleanup_new_partition(uint part_count)
1223
{
1224 1225
  handler **save_m_file= m_file;
  DBUG_ENTER("ha_partition::cleanup_new_partition");
1226

1227
  if (m_added_file && m_added_file[0])
1228
  {
1229 1230 1231 1232 1233 1234 1235 1236 1237
    m_file= m_added_file;
    m_added_file= NULL;

    external_lock(current_thd, F_UNLCK);
    /* delete_table also needed, a bit more complex */
    close();

    m_added_file= m_file;
    m_file= save_m_file;
1238
  }
1239
  DBUG_VOID_RETURN;
1240 1241
}

1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278
/*
  Implement the partition changes defined by ALTER TABLE of partitions

  SYNOPSIS
    change_partitions()
    create_info                 HA_CREATE_INFO object describing all
                                fields and indexes in table
    path                        Complete path of db and table name
    out: copied                 Output parameter where number of copied
                                records are added
    out: deleted                Output parameter where number of deleted
                                records are added
    pack_frm_data               Reference to packed frm file
    pack_frm_len                Length of packed frm file

  RETURN VALUE
    >0                        Failure
    0                         Success

  DESCRIPTION
    Add and copy if needed a number of partitions, during this operation
    no other operation is ongoing in the server. This is used by
    ADD PARTITION all types as well as by REORGANIZE PARTITION. For
    one-phased implementations it is used also by DROP and COALESCE
    PARTITIONs.
    One-phased implementation needs the new frm file, other handlers will
    get zero length and a NULL reference here.
*/

int ha_partition::change_partitions(HA_CREATE_INFO *create_info,
                                    const char *path,
                                    ulonglong *copied,
                                    ulonglong *deleted,
                                    const void *pack_frm_data
                                    __attribute__((unused)),
                                    uint pack_frm_len
                                    __attribute__((unused)))
1279 1280
{
  List_iterator<partition_element> part_it(m_part_info->partitions);
1281
  List_iterator <partition_element> t_it(m_part_info->temp_partitions);
1282
  char part_name_buff[FN_REFLEN];
1283 1284 1285 1286 1287
  uint no_parts= m_part_info->partitions.elements;
  uint no_subparts= m_part_info->no_subparts;
  uint i= 0;
  uint no_remain_partitions, part_count;
  handler **new_file_array;
1288
  int error= 1;
1289 1290 1291 1292 1293 1294 1295
  bool first;
  bool copy_parts= FALSE;
  uint temp_partitions= m_part_info->temp_partitions.elements;
  THD *thd= current_thd;
  DBUG_ENTER("ha_partition::change_partitions");

  m_reorged_parts= 0;
1296
  if (!m_part_info->is_sub_partitioned())
1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433
    no_subparts= 1;

  /*
    Step 1:
      Calculate number of reorganised partitions and allocate space for
      their handler references.
  */
  if (temp_partitions)
  {
    m_reorged_parts= temp_partitions * no_subparts;
  }
  else
  {
    do
    {
      partition_element *part_elem= part_it++;
      if (part_elem->part_state == PART_CHANGED ||
          part_elem->part_state == PART_REORGED_DROPPED)
      {
        m_reorged_parts+= no_subparts;
      }
    } while (++i < no_parts);
  }
  if (m_reorged_parts &&
      !(m_reorged_file= (handler**)sql_calloc(sizeof(partition_element*)*
                                              (m_reorged_parts + 1))))
  {
    mem_alloc_error(sizeof(partition_element*)*(m_reorged_parts+1));
    DBUG_RETURN(TRUE);
  }

  /*
    Step 2:
      Calculate number of partitions after change and allocate space for
      their handler references.
  */
  no_remain_partitions= 0;
  if (temp_partitions)
  {
    no_remain_partitions= no_parts * no_subparts;
  }
  else
  {
    part_it.rewind();
    i= 0;
    do
    {
      partition_element *part_elem= part_it++;
      if (part_elem->part_state == PART_NORMAL ||
          part_elem->part_state == PART_TO_BE_ADDED ||
          part_elem->part_state == PART_CHANGED)
      {
        no_remain_partitions+= no_subparts;
      }
    } while (++i < no_parts);
  }
  if (!(new_file_array= (handler**)sql_calloc(sizeof(handler*)*
                                              (2*(no_remain_partitions + 1)))))
  {
    mem_alloc_error(sizeof(handler*)*2*(no_remain_partitions+1));
    DBUG_RETURN(TRUE);
  }
  m_added_file= &new_file_array[no_remain_partitions + 1];

  /*
    Step 3:
      Fill m_reorged_file with handler references and NULL at the end
  */
  if (m_reorged_parts)
  {
    i= 0;
    part_count= 0;
    first= TRUE;
    part_it.rewind();
    do
    {
      partition_element *part_elem= part_it++;
      if (part_elem->part_state == PART_CHANGED ||
          part_elem->part_state == PART_REORGED_DROPPED)
      {
        memcpy((void*)&m_reorged_file[part_count],
               (void*)&m_file[i*no_subparts],
               sizeof(handler*)*no_subparts);
        part_count+= no_subparts;
      }
      else if (first && temp_partitions &&
               part_elem->part_state == PART_TO_BE_ADDED)
      {
        /*
          When doing an ALTER TABLE REORGANIZE PARTITION a number of
          partitions is to be reorganised into a set of new partitions.
          The reorganised partitions are in this case in the temp_partitions
          list. We copy all of them in one batch and thus we only do this
          until we find the first partition with state PART_TO_BE_ADDED
          since this is where the new partitions go in and where the old
          ones used to be.
        */
        first= FALSE;
        memcpy((void*)m_reorged_file, &m_file[i*no_subparts],
               sizeof(handler*)*m_reorged_parts*no_subparts);
      }
    } while (++i < no_parts);
  }

  /*
    Step 4:
      Fill new_array_file with handler references. Create the handlers if
      needed.
  */
  i= 0;
  part_count= 0;
  part_it.rewind();
  do
  {
    partition_element *part_elem= part_it++;
    if (part_elem->part_state == PART_NORMAL)
    {
      memcpy((void*)&new_file_array[part_count], (void*)&m_file[i],
             sizeof(handler*)*no_subparts);
      part_count+= no_subparts;
    }
    else if (part_elem->part_state == PART_CHANGED ||
             part_elem->part_state == PART_TO_BE_ADDED)
    {
      uint j= 0;
      do
      {
        if (!(new_file_array[part_count++]= get_new_handler(table->s,
                                            thd->mem_root,
                                            part_elem->engine_type)))
        {
          mem_alloc_error(sizeof(handler));
          DBUG_RETURN(TRUE);
        }
      } while (++j < no_subparts);
    }
  } while (++i < no_parts);
1434

1435 1436 1437 1438 1439 1440 1441 1442 1443
  /*
    Step 5:
      Create the new partitions and also open, lock and call external_lock
      on them to prepare them for copy phase and also for later close
      calls
  */
  i= 0;
  part_count= 0;
  part_it.rewind();
1444 1445 1446
  do
  {
    partition_element *part_elem= part_it++;
1447 1448
    if (part_elem->part_state == PART_TO_BE_ADDED ||
        part_elem->part_state == PART_CHANGED)
1449 1450
    {
      /*
1451 1452 1453
        A new partition needs to be created PART_TO_BE_ADDED means an
        entirely new partition and PART_CHANGED means a changed partition
        that will still exist with either more or less data in it.
1454
      */
1455 1456 1457 1458
      uint name_variant= NORMAL_PART_NAME;
      if (part_elem->part_state == PART_CHANGED ||
          (part_elem->part_state == PART_TO_BE_ADDED && temp_partitions))
        name_variant= TEMP_PART_NAME;
1459
      if (m_part_info->is_sub_partitioned())
1460 1461 1462 1463 1464 1465 1466 1467
      {
        List_iterator<partition_element> sub_it(part_elem->subpartitions);
        uint j= 0, part;
        do
        {
          partition_element *sub_elem= sub_it++;
          create_subpartition_name(part_name_buff, path,
                                   part_elem->partition_name,
1468 1469
                                   sub_elem->partition_name,
                                   name_variant);
1470
          part= i * no_subparts + j;
1471 1472 1473 1474 1475 1476 1477 1478 1479
          DBUG_PRINT("info", ("Add subpartition %s", part_name_buff));
          if ((error= prepare_new_partition(table, create_info,
                                            new_file_array[part],
                                            (const char *)part_name_buff)))
          {
            cleanup_new_partition(part_count);
            DBUG_RETURN(TRUE);
          }
          m_added_file[part_count++]= new_file_array[part];
1480 1481 1482 1483 1484
        } while (++j < no_subparts);
      }
      else
      {
        create_partition_name(part_name_buff, path,
1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495
                              part_elem->partition_name, name_variant,
                              TRUE);
        DBUG_PRINT("info", ("Add partition %s", part_name_buff));
        if ((error= prepare_new_partition(table, create_info,
                                          new_file_array[i],
                                          (const char *)part_name_buff)))
        {
          cleanup_new_partition(part_count);
          DBUG_RETURN(TRUE);
        }
        m_added_file[part_count++]= new_file_array[i];
1496 1497 1498
      }
    }
  } while (++i < no_parts);
1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548

  /*
    Step 6:
      State update to prepare for next write of the frm file.
  */
  i= 0;
  part_it.rewind();
  do
  {
    partition_element *part_elem= part_it++;
    if (part_elem->part_state == PART_TO_BE_ADDED)
      part_elem->part_state= PART_IS_ADDED;
    else if (part_elem->part_state == PART_CHANGED)
      part_elem->part_state= PART_IS_CHANGED;
    else if (part_elem->part_state == PART_REORGED_DROPPED)
      part_elem->part_state= PART_TO_BE_DROPPED;
  } while (++i < no_parts);
  for (i= 0; i < temp_partitions; i++)
  {
    partition_element *part_elem= t_it++;
    DBUG_ASSERT(part_elem->part_state == PART_TO_BE_REORGED);
    part_elem->part_state= PART_TO_BE_DROPPED;
  }
  m_new_file= new_file_array;
  DBUG_RETURN(copy_partitions(copied, deleted));
}


/*
  Copy partitions as part of ALTER TABLE of partitions

  SYNOPSIS
    copy_partitions()
    out:copied                 Number of records copied
    out:deleted                Number of records deleted

  RETURN VALUE
    >0                         Error code
    0                          Success

  DESCRIPTION
    change_partitions has done all the preparations, now it is time to
    actually copy the data from the reorganised partitions to the new
    partitions.
*/

int ha_partition::copy_partitions(ulonglong *copied, ulonglong *deleted)
{
  uint reorg_part= 0;
  int result= 0;
1549
  longlong func_value;
1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574
  DBUG_ENTER("ha_partition::copy_partitions");

  while (reorg_part < m_reorged_parts)
  {
    handler *file= m_reorged_file[reorg_part];
    uint32 new_part;

    late_extra_cache(reorg_part);
    if ((result= file->ha_rnd_init(1)))
      goto error;
    while (TRUE)
    {
      if ((result= file->rnd_next(m_rec0)))
      {
        if (result == HA_ERR_RECORD_DELETED)
          continue;                              //Probably MyISAM
        if (result != HA_ERR_END_OF_FILE)
          goto error;
        /*
          End-of-file reached, break out to continue with next partition or
          end the copy process.
        */
        break;
      }
      /* Found record to insert into new handler */
1575 1576
      if (m_part_info->get_partition_id(m_part_info, &new_part,
                                        &func_value))
1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600
      {
        /*
           This record is in the original table but will not be in the new
           table since it doesn't fit into any partition any longer due to
           changed partitioning ranges or list values.
        */
        deleted++;
      }
      else
      {
        /* Copy record to new handler */
        copied++;
        if ((result= m_new_file[new_part]->write_row(m_rec0)))
          goto error;
      }
    }
    late_extra_no_cache(reorg_part);
    file->rnd_end();
    reorg_part++;
  }
  DBUG_RETURN(FALSE);
error:
  print_error(result, MYF(0));
  DBUG_RETURN(TRUE);
1601
}
1602

1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617

/*
  Update create info as part of ALTER TABLE

  SYNOPSIS
    update_create_info()
    create_info                   Create info from ALTER TABLE

  RETURN VALUE
    NONE

  DESCRIPTION
    Method empty so far
*/

1618 1619 1620 1621 1622 1623
void ha_partition::update_create_info(HA_CREATE_INFO *create_info)
{
  return;
}


1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637
/*
  Change comments specific to handler

  SYNOPSIS
    update_table_comment()
    comment                       Original comment

  RETURN VALUE
    new comment 

  DESCRIPTION
    No comment changes so far
*/

1638 1639
char *ha_partition::update_table_comment(const char *comment)
{
1640
  return (char*) comment;                       /* Nothing to change */
1641 1642 1643 1644 1645
}



/*
1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665
  Handle delete, rename and create table

  SYNOPSIS
    del_ren_cre_table()
    from                    Full path of old table
    to                      Full path of new table
    table_arg               Table object
    create_info             Create info

  RETURN VALUE
    >0                      Error
    0                       Success

  DESCRIPTION
    Common routine to handle delete_table and rename_table.
    The routine uses the partition handler file to get the
    names of the partition instances. Both these routines
    are called after creating the handler without table
    object and thus the file is needed to discover the
    names of the partitions and the underlying storage engines.
1666 1667 1668 1669 1670 1671 1672
*/

uint ha_partition::del_ren_cre_table(const char *from,
				     const char *to,
				     TABLE *table_arg,
				     HA_CREATE_INFO *create_info)
{
1673 1674
  int save_error= 0;
  int error;
1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688
  char from_buff[FN_REFLEN], to_buff[FN_REFLEN];
  char *name_buffer_ptr;
  uint i;
  handler **file;
  DBUG_ENTER("del_ren_cre_table()");

  if (get_from_handler_file(from))
    DBUG_RETURN(TRUE);
  DBUG_ASSERT(m_file_buffer);
  name_buffer_ptr= m_name_buffer_ptr;
  file= m_file;
  i= 0;
  do
  {
1689 1690
    create_partition_name(from_buff, from, name_buffer_ptr, NORMAL_PART_NAME,
                          FALSE);
1691 1692
    if (to != NULL)
    {						// Rename branch
1693 1694
      create_partition_name(to_buff, to, name_buffer_ptr, NORMAL_PART_NAME,
                            FALSE);
1695 1696 1697 1698 1699 1700 1701
      error= (*file)->rename_table((const char*) from_buff,
				   (const char*) to_buff);
    }
    else if (table_arg == NULL)			// delete branch
      error= (*file)->delete_table((const char*) from_buff);
    else
    {
1702
      set_up_table_before_create(table_arg, from_buff, create_info, i);
1703 1704 1705 1706 1707 1708 1709 1710 1711 1712
      error= (*file)->create(from_buff, table_arg, create_info);
    }
    name_buffer_ptr= strend(name_buffer_ptr) + 1;
    if (error)
      save_error= error;
    i++;
  } while (*(++file));
  DBUG_RETURN(save_error);
}

1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723
/*
  Find partition based on partition id

  SYNOPSIS
    find_partition_element()
    part_id                   Partition id of partition looked for

  RETURN VALUE
    >0                        Reference to partition_element
    0                         Partition not found
*/
1724 1725 1726 1727 1728

partition_element *ha_partition::find_partition_element(uint part_id)
{
  uint i;
  uint curr_part_id= 0;
1729
  List_iterator_fast <partition_element> part_it(m_part_info->partitions);
1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754

  for (i= 0; i < m_part_info->no_parts; i++)
  {
    partition_element *part_elem;
    part_elem= part_it++;
    if (m_is_sub_partitioned)
    {
      uint j;
      List_iterator_fast <partition_element> sub_it(part_elem->subpartitions);
      for (j= 0; j < m_part_info->no_subparts; j++)
      {
	part_elem= sub_it++;
	if (part_id == curr_part_id++)
	  return part_elem;
      }
    }
    else if (part_id == curr_part_id++)
      return part_elem;
  }
  DBUG_ASSERT(0);
  current_thd->fatal_error();                   // Abort
  return NULL;
}


1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774
/*
   Set up table share object before calling create on underlying handler

   SYNOPSIS
     set_up_table_before_create()
     table                       Table object
     info                        Create info
     part_id                     Partition id of partition to set-up

   RETURN VALUE
     NONE

   DESCRIPTION
     Set up
     1) Comment on partition
     2) MAX_ROWS, MIN_ROWS on partition
     3) Index file name on partition
     4) Data file name on partition
*/

1775
void ha_partition::set_up_table_before_create(TABLE *table,
1776 1777 1778
                   const char *partition_name_with_path, 
                   HA_CREATE_INFO *info,
                   uint part_id)
1779 1780
{
  partition_element *part_elem= find_partition_element(part_id);
1781

1782 1783 1784 1785
  if (!part_elem)
    return;                                     // Fatal error
  table->s->max_rows= part_elem->part_max_rows;
  table->s->min_rows= part_elem->part_min_rows;
1786
  const char *partition_name= strrchr(partition_name_with_path, FN_LIBCHAR);
1787 1788 1789 1790 1791 1792 1793 1794
  if (part_elem->index_file_name)
    append_file_to_dir(current_thd,
                       (const char**)&part_elem->index_file_name,
                       partition_name+1);
  if (part_elem->data_file_name)
    append_file_to_dir(current_thd,
                       (const char**)&part_elem->data_file_name,
                       partition_name+1);
1795 1796 1797 1798 1799 1800
  info->index_file_name= part_elem->index_file_name;
  info->data_file_name= part_elem->data_file_name;
}


/*
1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817
  Add two names together

  SYNOPSIS
    name_add()
    out:dest                          Destination string
    first_name                        First name
    sec_name                          Second name

  RETURN VALUE
    >0                                Error
    0                                 Success

  DESCRIPTION
    Routine used to add two names with '_' in between then. Service routine
    to create_handler_file
    Include the NULL in the count of characters since it is needed as separator
    between the partition names.
1818 1819 1820 1821
*/

static uint name_add(char *dest, const char *first_name, const char *sec_name)
{
1822
  return (uint) (strxmov(dest, first_name, "#SP#", sec_name, NullS) -dest) + 1;
1823 1824 1825 1826
}


/*
1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839
  Create the special .par file

  SYNOPSIS
    create_handler_file()
    name                      Full path of table name

  RETURN VALUE
    >0                        Error code
    0                         Success

  DESCRIPTION
    Method used to create handler file with names of partitions, their
    engine types and the number of partitions.
1840 1841 1842 1843 1844 1845
*/

bool ha_partition::create_handler_file(const char *name)
{
  partition_element *part_elem, *subpart_elem;
  uint i, j, part_name_len, subpart_name_len;
1846 1847
  uint tot_partition_words, tot_name_len, no_parts;
  uint tot_parts= 0;
1848 1849 1850 1851 1852
  uint tot_len_words, tot_len_byte, chksum, tot_name_words;
  char *name_buffer_ptr;
  uchar *file_buffer, *engine_array;
  bool result= TRUE;
  char file_name[FN_REFLEN];
1853 1854
  char part_name[FN_REFLEN];
  char subpart_name[FN_REFLEN];
1855
  File file;
1856
  List_iterator_fast <partition_element> part_it(m_part_info->partitions);
1857 1858
  DBUG_ENTER("create_handler_file");

1859 1860 1861
  no_parts= m_part_info->partitions.elements;
  DBUG_PRINT("info", ("table name = %s, no_parts = %u", name,
                      no_parts));
1862
  tot_name_len= 0;
1863
  for (i= 0; i < no_parts; i++)
1864 1865
  {
    part_elem= part_it++;
1866 1867 1868 1869 1870 1871 1872
    if (part_elem->part_state != PART_NORMAL &&
        part_elem->part_state != PART_IS_ADDED &&
        part_elem->part_state != PART_IS_CHANGED)
      continue;
    tablename_to_filename(part_elem->partition_name, part_name,
                          FN_REFLEN);
    part_name_len= strlen(part_name);
1873
    if (!m_is_sub_partitioned)
1874
    {
1875
      tot_name_len+= part_name_len + 1;
1876 1877
      tot_parts++;
    }
1878 1879
    else
    {
1880
      List_iterator_fast <partition_element> sub_it(part_elem->subpartitions);
1881 1882 1883
      for (j= 0; j < m_part_info->no_subparts; j++)
      {
	subpart_elem= sub_it++;
1884 1885 1886 1887 1888 1889
        tablename_to_filename(subpart_elem->partition_name,
                              subpart_name,
                              FN_REFLEN);
	subpart_name_len= strlen(subpart_name);
	tot_name_len+= part_name_len + subpart_name_len + 5;
        tot_parts++;
1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905
      }
    }
  }
  /*
     File format:
     Length in words              4 byte
     Checksum                     4 byte
     Total number of partitions   4 byte
     Array of engine types        n * 4 bytes where
     n = (m_tot_parts + 3)/4
     Length of name part in bytes 4 bytes
     Name part                    m * 4 bytes where
     m = ((length_name_part + 3)/4)*4

     All padding bytes are zeroed
  */
1906
  tot_partition_words= (tot_parts + 3) / 4;
1907 1908 1909 1910 1911 1912 1913 1914
  tot_name_words= (tot_name_len + 3) / 4;
  tot_len_words= 4 + tot_partition_words + tot_name_words;
  tot_len_byte= 4 * tot_len_words;
  if (!(file_buffer= (uchar *) my_malloc(tot_len_byte, MYF(MY_ZEROFILL))))
    DBUG_RETURN(TRUE);
  engine_array= (file_buffer + 12);
  name_buffer_ptr= (char*) (file_buffer + ((4 + tot_partition_words) * 4));
  part_it.rewind();
1915
  for (i= 0; i < no_parts; i++)
1916 1917
  {
    part_elem= part_it++;
1918 1919 1920 1921
    if (part_elem->part_state != PART_NORMAL &&
        part_elem->part_state != PART_IS_ADDED &&
        part_elem->part_state != PART_IS_CHANGED)
      continue;
1922 1923
    if (!m_is_sub_partitioned)
    {
1924 1925
      tablename_to_filename(part_elem->partition_name, part_name, FN_REFLEN);
      name_buffer_ptr= strmov(name_buffer_ptr, part_name)+1;
1926
      *engine_array= (uchar) ha_legacy_type(part_elem->engine_type);
1927 1928 1929 1930 1931
      DBUG_PRINT("info", ("engine: %u", *engine_array));
      engine_array++;
    }
    else
    {
1932
      List_iterator_fast <partition_element> sub_it(part_elem->subpartitions);
1933 1934 1935
      for (j= 0; j < m_part_info->no_subparts; j++)
      {
	subpart_elem= sub_it++;
1936 1937 1938 1939
        tablename_to_filename(part_elem->partition_name, part_name,
                              FN_REFLEN);
        tablename_to_filename(subpart_elem->partition_name, subpart_name,
                              FN_REFLEN);
1940
	name_buffer_ptr+= name_add(name_buffer_ptr,
1941 1942
				   part_name,
				   subpart_name);
1943 1944
        *engine_array= (uchar) ha_legacy_type(subpart_elem->engine_type);
        DBUG_PRINT("info", ("engine: %u", *engine_array));
1945 1946 1947 1948 1949 1950
	engine_array++;
      }
    }
  }
  chksum= 0;
  int4store(file_buffer, tot_len_words);
1951
  int4store(file_buffer + 8, tot_parts);
1952 1953 1954 1955 1956 1957 1958 1959 1960
  int4store(file_buffer + 12 + (tot_partition_words * 4), tot_name_len);
  for (i= 0; i < tot_len_words; i++)
    chksum^= uint4korr(file_buffer + 4 * i);
  int4store(file_buffer + 4, chksum);
  /*
    Remove .frm extension and replace with .par
    Create and write and close file
    to be used at open, delete_table and rename_table
  */
1961
  fn_format(file_name, name, "", ha_par_ext, MY_APPEND_EXT);
1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974
  if ((file= my_create(file_name, CREATE_MODE, O_RDWR | O_TRUNC,
		       MYF(MY_WME))) >= 0)
  {
    result= my_write(file, (byte *) file_buffer, tot_len_byte,
			   MYF(MY_WME | MY_NABP));
    VOID(my_close(file, MYF(0)));
  }
  else
    result= TRUE;
  my_free((char*) file_buffer, MYF(0));
  DBUG_RETURN(result);
}

1975 1976 1977 1978 1979 1980 1981 1982 1983
/*
  Clear handler variables and free some memory

  SYNOPSIS
    clear_handler_file()

  RETURN VALUE 
    NONE
*/
1984 1985 1986 1987

void ha_partition::clear_handler_file()
{
  my_free((char*) m_file_buffer, MYF(MY_ALLOW_ZERO_PTR));
1988
  my_free((char*) m_engine_array, MYF(MY_ALLOW_ZERO_PTR));
1989 1990 1991 1992 1993
  m_file_buffer= NULL;
  m_name_buffer_ptr= NULL;
  m_engine_array= NULL;
}

1994 1995 1996 1997 1998 1999 2000 2001 2002 2003
/*
  Create underlying handler objects

  SYNOPSIS
    create_handlers()

  RETURN VALUE
    TRUE                  Error
    FALSE                 Success
*/
2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015

bool ha_partition::create_handlers()
{
  uint i;
  uint alloc_len= (m_tot_parts + 1) * sizeof(handler*);
  DBUG_ENTER("create_handlers");

  if (!(m_file= (handler **) sql_alloc(alloc_len)))
    DBUG_RETURN(TRUE);
  bzero(m_file, alloc_len);
  for (i= 0; i < m_tot_parts; i++)
  {
2016
    if (!(m_file[i]= get_new_handler(table_share, current_thd->mem_root,
2017
                                     m_engine_array[i])))
2018 2019 2020 2021 2022
      DBUG_RETURN(TRUE);
    DBUG_PRINT("info", ("engine_type: %u", m_engine_array[i]));
  }
  m_file[m_tot_parts]= 0;
  /* For the moment we only support partition over the same table engine */
2023
  if (m_engine_array[0] == &myisam_hton)
2024 2025 2026 2027
  {
    DBUG_PRINT("info", ("MyISAM"));
    m_myisam= TRUE;
  }
2028 2029
  /* INNODB may not be compiled in... */
  else if (ha_legacy_type(m_engine_array[0]) == DB_TYPE_INNODB)
2030 2031 2032 2033 2034 2035 2036
  {
    DBUG_PRINT("info", ("InnoDB"));
    m_innodb= TRUE;
  }
  DBUG_RETURN(FALSE);
}

2037 2038 2039 2040 2041 2042 2043 2044 2045 2046
/*
  Create underlying handler objects from partition info

  SYNOPSIS
    new_handlers_from_part_info()

  RETURN VALUE
    TRUE                  Error
    FALSE                 Success
*/
2047 2048 2049

bool ha_partition::new_handlers_from_part_info()
{
2050
  uint i, j, part_count;
2051 2052 2053
  partition_element *part_elem;
  uint alloc_len= (m_tot_parts + 1) * sizeof(handler*);
  List_iterator_fast <partition_element> part_it(m_part_info->partitions);
2054
  THD *thd= current_thd;
2055 2056 2057
  DBUG_ENTER("ha_partition::new_handlers_from_part_info");

  if (!(m_file= (handler **) sql_alloc(alloc_len)))
2058 2059 2060 2061
  {
    mem_alloc_error(alloc_len);
    goto error_end;
  }
2062 2063 2064 2065
  bzero(m_file, alloc_len);
  DBUG_ASSERT(m_part_info->no_parts > 0);

  i= 0;
2066
  part_count= 0;
2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077
  /*
    Don't know the size of the underlying storage engine, invent a number of
    bytes allocated for error message if allocation fails
  */
  do
  {
    part_elem= part_it++;
    if (m_is_sub_partitioned)
    {
      for (j= 0; j < m_part_info->no_subparts; j++)
      {
2078
	if (!(m_file[i]= get_new_handler(table_share, thd->mem_root,
2079
                                         part_elem->engine_type)))
2080
          goto error;
2081 2082
	DBUG_PRINT("info", ("engine_type: %u",
                   (uint) ha_legacy_type(part_elem->engine_type)));
2083 2084
      }
    }
2085 2086 2087 2088 2089 2090 2091 2092
    else
    {
      if (!(m_file[part_count++]= get_new_handler(table_share, thd->mem_root,
                                                  part_elem->engine_type)))
        goto error;
      DBUG_PRINT("info", ("engine_type: %u",
                 (uint) ha_legacy_type(part_elem->engine_type)));
    }
2093
  } while (++i < m_part_info->no_parts);
2094
  if (part_elem->engine_type == &myisam_hton)
2095 2096 2097 2098 2099 2100
  {
    DBUG_PRINT("info", ("MyISAM"));
    m_myisam= TRUE;
  }
  DBUG_RETURN(FALSE);
error:
2101 2102
  mem_alloc_error(sizeof(handler));
error_end:
2103 2104 2105 2106 2107
  DBUG_RETURN(TRUE);
}


/*
2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120
  Get info about partition engines and their names from the .par file

  SYNOPSIS
    get_from_handler_file()
    name                        Full path of table name

  RETURN VALUE
    TRUE                        Error
    FALSE                       Success

  DESCRIPTION
    Open handler file to get partition names, engine types and number of
    partitions.
2121 2122 2123 2124 2125 2126 2127
*/

bool ha_partition::get_from_handler_file(const char *name)
{
  char buff[FN_REFLEN], *address_tot_name_len;
  File file;
  char *file_buffer, *name_buffer_ptr;
2128
  handlerton **engine_array;
2129 2130 2131 2132 2133 2134
  uint i, len_bytes, len_words, tot_partition_words, tot_name_words, chksum;
  DBUG_ENTER("ha_partition::get_from_handler_file");
  DBUG_PRINT("enter", ("table name: '%s'", name));

  if (m_file_buffer)
    DBUG_RETURN(FALSE);
2135
  fn_format(buff, name, "", ha_par_ext, MY_APPEND_EXT);
2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155

  /* Following could be done with my_stat to read in whole file */
  if ((file= my_open(buff, O_RDONLY | O_SHARE, MYF(0))) < 0)
    DBUG_RETURN(TRUE);
  if (my_read(file, (byte *) & buff[0], 8, MYF(MY_NABP)))
    goto err1;
  len_words= uint4korr(buff);
  len_bytes= 4 * len_words;
  if (!(file_buffer= my_malloc(len_bytes, MYF(0))))
    goto err1;
  VOID(my_seek(file, 0, MY_SEEK_SET, MYF(0)));
  if (my_read(file, (byte *) file_buffer, len_bytes, MYF(MY_NABP)))
    goto err2;

  chksum= 0;
  for (i= 0; i < len_words; i++)
    chksum ^= uint4korr((file_buffer) + 4 * i);
  if (chksum)
    goto err2;
  m_tot_parts= uint4korr((file_buffer) + 8);
2156
  DBUG_PRINT("info", ("No of parts = %u", m_tot_parts));
2157
  tot_partition_words= (m_tot_parts + 3) / 4;
2158 2159 2160
  if (!(engine_array= (handlerton **) my_malloc(m_tot_parts * sizeof(handlerton*),MYF(0))))
    goto err2;
  for (i= 0; i < m_tot_parts; i++)
patg@govinda.patg.net's avatar
patg@govinda.patg.net committed
2161
    engine_array[i]= ha_resolve_by_legacy_type(current_thd,
2162
                (enum legacy_db_type) *(uchar *) ((file_buffer) + 12 + i));
2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185
  address_tot_name_len= file_buffer + 12 + 4 * tot_partition_words;
  tot_name_words= (uint4korr(address_tot_name_len) + 3) / 4;
  if (len_words != (tot_partition_words + tot_name_words + 4))
    goto err2;
  name_buffer_ptr= file_buffer + 16 + 4 * tot_partition_words;
  VOID(my_close(file, MYF(0)));
  m_file_buffer= file_buffer;          // Will be freed in clear_handler_file()
  m_name_buffer_ptr= name_buffer_ptr;
  m_engine_array= engine_array;
  if (!m_file && create_handlers())
  {
    clear_handler_file();
    DBUG_RETURN(TRUE);
  }
  DBUG_RETURN(FALSE);

err2:
  my_free(file_buffer, MYF(0));
err1:
  VOID(my_close(file, MYF(0)));
  DBUG_RETURN(TRUE);
}

2186

2187 2188 2189 2190
/****************************************************************************
                MODULE open/close object
****************************************************************************/
/*
2191
  Open handler object
2192

2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210
  SYNOPSIS
    open()
    name                  Full path of table name
    mode                  Open mode flags
    test_if_locked        ?

  RETURN VALUE
    >0                    Error
    0                     Success

  DESCRIPTION
    Used for opening tables. The name will be the name of the file.
    A table is opened when it needs to be opened. For instance
    when a request comes in for a select on the table (tables are not
    open and closed for each request, they are cached).

    Called from handler.cc by handler::ha_open(). The server opens all tables
    by calling ha_open() which then calls the handler specific open().
2211 2212 2213 2214 2215
*/

int ha_partition::open(const char *name, int mode, uint test_if_locked)
{
  char *name_buffer_ptr= m_name_buffer_ptr;
patg@govinda.patg.net's avatar
patg@govinda.patg.net committed
2216
  int error;
2217
  uint alloc_len;
patg@govinda.patg.net's avatar
patg@govinda.patg.net committed
2218 2219
  handler **file;
  char name_buff[FN_REFLEN];
2220 2221 2222
  DBUG_ENTER("ha_partition::open");

  ref_length= 0;
2223 2224
  m_mode= mode;
  m_open_test_lock= test_if_locked;
2225 2226 2227 2228 2229 2230
  m_part_field_array= m_part_info->full_part_field_array;
  if (get_from_handler_file(name))
    DBUG_RETURN(1);
  m_start_key.length= 0;
  m_rec0= table->record[0];
  m_rec_length= table->s->reclength;
patg@govinda.patg.net's avatar
patg@govinda.patg.net committed
2231
  alloc_len= m_tot_parts * (m_rec_length + PARTITION_BYTES_IN_POS);
2232 2233 2234
  alloc_len+= table->s->max_key_length;
  if (!m_ordered_rec_buffer)
  {
2235
    if (!(m_ordered_rec_buffer= (byte*)my_malloc(alloc_len, MYF(MY_WME))))
2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246
    {
      DBUG_RETURN(1);
    }
    {
      /*
        We set-up one record per partition and each record has 2 bytes in
        front where the partition id is written. This is used by ordered
        index_read.
        We also set-up a reference to the first record for temporary use in
        setting up the scan.
      */
2247
      char *ptr= (char*)m_ordered_rec_buffer;
2248 2249 2250 2251 2252 2253
      uint i= 0;
      do
      {
        int2store(ptr, i);
        ptr+= m_rec_length + PARTITION_BYTES_IN_POS;
      } while (++i < m_tot_parts);
2254
      m_start_key.key= (const byte*)ptr;
2255 2256
    }
  }
patg@govinda.patg.net's avatar
patg@govinda.patg.net committed
2257 2258 2259 2260 2261 2262

  /* Initialise the bitmap we use to determine what partitions are used */
  if (bitmap_init(&(m_part_info->used_partitions), NULL, m_tot_parts, TRUE))
    DBUG_RETURN(1);
  bitmap_set_all(&(m_part_info->used_partitions));

2263 2264 2265
  file= m_file;
  do
  {
2266 2267
    create_partition_name(name_buff, name, name_buffer_ptr, NORMAL_PART_NAME,
                          FALSE);
2268
    if ((error= (*file)->ha_open(table, (const char*) name_buff, mode,
2269 2270
                                 test_if_locked)))
      goto err_handler;
patg@krsna.patg.net's avatar
patg@krsna.patg.net committed
2271
    m_no_locks+= (*file)->lock_count();
2272 2273 2274
    name_buffer_ptr+= strlen(name_buffer_ptr) + 1;
    set_if_bigger(ref_length, ((*file)->ref_length));
  } while (*(++file));
patg@govinda.patg.net's avatar
patg@govinda.patg.net committed
2275

2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289
  /*
    Add 2 bytes for partition id in position ref length.
    ref_length=max_in_all_partitions(ref_length) + PARTITION_BYTES_IN_POS
  */
  ref_length+= PARTITION_BYTES_IN_POS;
  m_ref_length= ref_length;
  /*
    Release buffer read from .par file. It will not be reused again after
    being opened once.
  */
  clear_handler_file();
  /*
    Initialise priority queue, initialised to reading forward.
  */
2290
  if ((error= init_queue(&m_queue, m_tot_parts, (uint) PARTITION_BYTES_IN_POS,
2291 2292
                         0, key_rec_cmp, (void*)this)))
    goto err_handler;
patg@govinda.patg.net's avatar
patg@govinda.patg.net committed
2293

2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305
  /*
    Some handlers update statistics as part of the open call. This will in
    some cases corrupt the statistics of the partition handler and thus
    to ensure we have correct statistics we call info from open after
    calling open on all individual handlers.
  */
  info(HA_STATUS_VARIABLE | HA_STATUS_CONST);
  DBUG_RETURN(0);

err_handler:
  while (file-- != m_file)
    (*file)->close();
patg@govinda.patg.net's avatar
patg@govinda.patg.net committed
2306
err:
2307 2308 2309
  DBUG_RETURN(error);
}

2310

2311
/*
2312
  Close handler object
2313

2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326
  SYNOPSIS
    close()

  RETURN VALUE
    >0                   Error code
    0                    Success

  DESCRIPTION
    Called from sql_base.cc, sql_select.cc, and table.cc.
    In sql_select.cc it is only used to close up temporary tables or during
    the process where a temporary table is converted over to being a
    myisam table.
    For sql_base.cc look at close_data_tables().
2327 2328 2329 2330
*/

int ha_partition::close(void)
{
2331
  bool first= TRUE;
patg@govinda.patg.net's avatar
patg@govinda.patg.net committed
2332
  handler **file;
2333
  DBUG_ENTER("ha_partition::close");
2334

2335
  delete_queue(&m_queue);
patg@govinda.patg.net's avatar
patg@govinda.patg.net committed
2336
  bitmap_free(&(m_part_info->used_partitions));
2337
  file= m_file;
2338 2339

repeat:
2340 2341 2342 2343
  do
  {
    (*file)->close();
  } while (*(++file));
patg@govinda.patg.net's avatar
patg@govinda.patg.net committed
2344

2345 2346 2347 2348 2349 2350
  if (first && m_added_file && m_added_file[0])
  {
    file= m_added_file;
    first= FALSE;
    goto repeat;
  }
patg@govinda.patg.net's avatar
patg@govinda.patg.net committed
2351

2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364
  DBUG_RETURN(0);
}

/****************************************************************************
                MODULE start/end statement
****************************************************************************/
/*
  A number of methods to define various constants for the handler. In
  the case of the partition handler we need to use some max and min
  of the underlying handlers in most cases.
*/

/*
2365
  Set external locks on table
2366

2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392
  SYNOPSIS
    external_lock()
    thd                    Thread object
    lock_type              Type of external lock

  RETURN VALUE
    >0                   Error code
    0                    Success

  DESCRIPTION
    First you should go read the section "locking functions for mysql" in
    lock.cc to understand this.
    This create a lock on the table. If you are implementing a storage engine
    that can handle transactions look at ha_berkeley.cc to see how you will
    want to go about doing this. Otherwise you should consider calling
    flock() here.
    Originally this method was used to set locks on file level to enable
    several MySQL Servers to work on the same data. For transactional
    engines it has been "abused" to also mean start and end of statements
    to enable proper rollback of statements and transactions. When LOCK
    TABLES has been issued the start_stmt method takes over the role of
    indicating start of statement but in this case there is no end of
    statement indicator(?).

    Called from lock.cc by lock_external() and unlock_external(). Also called
    from sql_table.cc by copy_data_between_tables().
2393 2394 2395 2396
*/

int ha_partition::external_lock(THD *thd, int lock_type)
{
patg@govinda.patg.net's avatar
patg@govinda.patg.net committed
2397
  bool first= TRUE;
2398 2399 2400
  uint error;
  handler **file;
  DBUG_ENTER("ha_partition::external_lock");
2401

2402
  file= m_file;
2403 2404 2405
  m_lock_type= lock_type;

repeat:
2406 2407
  do
  {
patg@govinda.patg.net's avatar
patg@govinda.patg.net committed
2408 2409
    DBUG_PRINT("info", ("external_lock(thd, %d) iteration %d",
                        lock_type, (file - m_file)));
2410 2411
    if ((error= (*file)->external_lock(thd, lock_type)))
    {
patg@govinda.patg.net's avatar
patg@govinda.patg.net committed
2412 2413
      if (F_UNLCK != lock_type)
        goto err_handler;
2414 2415
    }
  } while (*(++file));
patg@govinda.patg.net's avatar
patg@govinda.patg.net committed
2416

2417 2418 2419 2420 2421 2422 2423
  if (first && m_added_file && m_added_file[0])
  {
    DBUG_ASSERT(lock_type == F_UNLCK);
    file= m_added_file;
    first= FALSE;
    goto repeat;
  }
2424 2425 2426 2427
  DBUG_RETURN(0);

err_handler:
  while (file-- != m_file)
patg@govinda.patg.net's avatar
patg@govinda.patg.net committed
2428
  {
2429
    (*file)->external_lock(thd, F_UNLCK);
patg@govinda.patg.net's avatar
patg@govinda.patg.net committed
2430
  }
2431 2432 2433 2434 2435
  DBUG_RETURN(error);
}


/*
2436
  Get the lock(s) for the table and perform conversion of locks if needed
2437

2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478
  SYNOPSIS
    store_lock()
    thd                   Thread object
    to                    Lock object array
    lock_type             Table lock type

  RETURN VALUE
    >0                   Error code
    0                    Success

  DESCRIPTION
    The idea with handler::store_lock() is the following:

    The statement decided which locks we should need for the table
    for updates/deletes/inserts we get WRITE locks, for SELECT... we get
    read locks.

    Before adding the lock into the table lock handler (see thr_lock.c)
    mysqld calls store lock with the requested locks.  Store lock can now
    modify a write lock to a read lock (or some other lock), ignore the
    lock (if we don't want to use MySQL table locks at all) or add locks
    for many tables (like we do when we are using a MERGE handler).

    Berkeley DB for partition  changes all WRITE locks to TL_WRITE_ALLOW_WRITE
    (which signals that we are doing WRITES, but we are still allowing other
    reader's and writer's.

    When releasing locks, store_lock() is also called. In this case one
    usually doesn't have to do anything.

    store_lock is called when holding a global mutex to ensure that only
    one thread at a time changes the locking information of tables.

    In some exceptional cases MySQL may send a request for a TL_IGNORE;
    This means that we are requesting the same lock as last time and this
    should also be ignored. (This may happen when someone does a flush
    table when we have opened a part of the tables, in which case mysqld
    closes and reopens the tables and tries to get the same locks as last
    time).  In the future we will probably try to remove this.

    Called from lock.cc by get_lock_data().
2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489
*/

THR_LOCK_DATA **ha_partition::store_lock(THD *thd,
					 THR_LOCK_DATA **to,
					 enum thr_lock_type lock_type)
{
  handler **file;
  DBUG_ENTER("ha_partition::store_lock");
  file= m_file;
  do
  {
patg@govinda.patg.net's avatar
patg@govinda.patg.net committed
2490
    DBUG_PRINT("info", ("store lock %d iteration", (file - m_file)));
2491 2492 2493 2494 2495
    to= (*file)->store_lock(thd, to, lock_type);
  } while (*(++file));
  DBUG_RETURN(to);
}

2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511
/*
  Start a statement when table is locked

  SYNOPSIS
    start_stmt()
    thd                  Thread object
    lock_type            Type of external lock

  RETURN VALUE
    >0                   Error code
    0                    Success

  DESCRIPTION
    This method is called instead of external lock when the table is locked
    before the statement is executed.
*/
2512

tomas@poseidon.ndb.mysql.com's avatar
merge  
tomas@poseidon.ndb.mysql.com committed
2513
int ha_partition::start_stmt(THD *thd, thr_lock_type lock_type)
2514 2515 2516 2517
{
  int error= 0;
  handler **file;
  DBUG_ENTER("ha_partition::start_stmt");
2518

2519 2520 2521
  file= m_file;
  do
  {
tomas@poseidon.ndb.mysql.com's avatar
merge  
tomas@poseidon.ndb.mysql.com committed
2522
    if ((error= (*file)->start_stmt(thd, lock_type)))
2523 2524 2525 2526 2527 2528 2529
      break;
  } while (*(++file));
  DBUG_RETURN(error);
}


/*
2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542
  Get number of lock objects returned in store_lock

  SYNOPSIS
    lock_count()

  RETURN VALUE
    Number of locks returned in call to store_lock

  DESCRIPTION
    Returns the number of store locks needed in call to store lock.
    We return number of partitions since we call store_lock on each
    underlying handler. Assists the above functions in allocating
    sufficient space for lock structures.
2543 2544 2545 2546 2547
*/

uint ha_partition::lock_count() const
{
  DBUG_ENTER("ha_partition::lock_count");
patg@govinda.patg.net's avatar
patg@govinda.patg.net committed
2548
  DBUG_PRINT("info", ("m_no_locks %d", m_no_locks));
patg@krsna.patg.net's avatar
patg@krsna.patg.net committed
2549
  DBUG_RETURN(m_no_locks);
2550 2551 2552 2553
}


/*
2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564
  Unlock last accessed row

  SYNOPSIS
    unlock_row()

  RETURN VALUE
    NONE

  DESCRIPTION
    Record currently processed was not in the result set of the statement
    and is thus unlocked. Used for UPDATE and DELETE queries.
2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578
*/

void ha_partition::unlock_row()
{
  m_file[m_last_part]->unlock_row();
  return;
}


/****************************************************************************
                MODULE change record
****************************************************************************/

/*
2579
  Insert a row to the table
2580

2581 2582 2583
  SYNOPSIS
    write_row()
    buf                        The row in MySQL Row Format
2584

2585 2586 2587 2588 2589 2590 2591
  RETURN VALUE
    >0                         Error code
    0                          Success

  DESCRIPTION
    write_row() inserts a row. buf() is a byte array of data, normally
    record[0].
2592

2593 2594
    You can use the field information to extract the data from the native byte
    array type.
2595

2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607
    Example of this would be:
    for (Field **field=table->field ; *field ; field++)
    {
      ...
    }

    See ha_tina.cc for a variant of extracting all of the data as strings.
    ha_berkeley.cc has a variant of how to store it intact by "packing" it
    for ha_berkeley's own native storage type.

    See the note for update_row() on auto_increments and timestamps. This
    case also applied to write_row().
2608

2609 2610
    Called from item_sum.cc, item_sum.cc, sql_acl.cc, sql_insert.cc,
    sql_insert.cc, sql_select.cc, sql_table.cc, sql_udf.cc, and sql_update.cc.
2611

2612
    ADDITIONAL INFO:
2613

2614
    Most handlers set timestamp when calling write row if any such fields
2615
    exists. Since we are calling an underlying handler we assume the´
2616
    underlying handler will assume this responsibility.
2617

2618 2619 2620 2621
    Underlying handlers will also call update_auto_increment to calculate
    the new auto increment value. We will catch the call to
    get_auto_increment and ensure this increment value is maintained by
    only one of the underlying handlers.
2622 2623 2624 2625 2626 2627
*/

int ha_partition::write_row(byte * buf)
{
  uint32 part_id;
  int error;
2628
  longlong func_value;
2629 2630 2631 2632 2633 2634 2635 2636 2637
#ifdef NOT_NEEDED
  byte *rec0= m_rec0;
#endif
  DBUG_ENTER("ha_partition::write_row");
  DBUG_ASSERT(buf == m_rec0);

#ifdef NOT_NEEDED
  if (likely(buf == rec0))
#endif
2638 2639
    error= m_part_info->get_partition_id(m_part_info, &part_id,
                                         &func_value);
2640 2641 2642 2643
#ifdef NOT_NEEDED
  else
  {
    set_field_ptr(m_part_field_array, buf, rec0);
2644 2645
    error= m_part_info->get_partition_id(m_part_info, &part_id,
                                         &func_value);
2646 2647 2648 2649
    set_field_ptr(m_part_field_array, rec0, buf);
  }
#endif
  if (unlikely(error))
2650
    DBUG_RETURN(error);
2651 2652 2653 2654 2655 2656 2657
  m_last_part= part_id;
  DBUG_PRINT("info", ("Insert in partition %d", part_id));
  DBUG_RETURN(m_file[part_id]->write_row(buf));
}


/*
2658
  Update an existing row
2659

2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685
  SYNOPSIS
    update_row()
    old_data                 Old record in MySQL Row Format
    new_data                 New record in MySQL Row Format

  RETURN VALUE
    >0                         Error code
    0                          Success

  DESCRIPTION
    Yes, update_row() does what you expect, it updates a row. old_data will
    have the previous row record in it, while new_data will have the newest
    data in it.
    Keep in mind that the server can do updates based on ordering if an
    ORDER BY clause was used. Consecutive ordering is not guarenteed.

    Currently new_data will not have an updated auto_increament record, or
    and updated timestamp field. You can do these for partition by doing these:
    if (table->timestamp_field_type & TIMESTAMP_AUTO_SET_ON_UPDATE)
      table->timestamp_field->set_time();
    if (table->next_number_field && record == table->record[0])
      update_auto_increment();

    Called from sql_select.cc, sql_acl.cc, sql_update.cc, and sql_insert.cc.
    new_data is always record[0]
    old_data is normally record[1] but may be anything
2686 2687 2688 2689 2690 2691
*/

int ha_partition::update_row(const byte *old_data, byte *new_data)
{
  uint32 new_part_id, old_part_id;
  int error;
2692
  longlong func_value;
2693 2694 2695
  DBUG_ENTER("ha_partition::update_row");

  if ((error= get_parts_for_update(old_data, new_data, table->record[0],
2696 2697
                                   m_part_info, &old_part_id, &new_part_id,
                                   &func_value)))
2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731
  {
    DBUG_RETURN(error);
  }

  /*
    TODO:
      set_internal_auto_increment=
        max(set_internal_auto_increment, new_data->auto_increment)
  */
  m_last_part= new_part_id;
  if (new_part_id == old_part_id)
  {
    DBUG_PRINT("info", ("Update in partition %d", new_part_id));
    DBUG_RETURN(m_file[new_part_id]->update_row(old_data, new_data));
  }
  else
  {
    DBUG_PRINT("info", ("Update from partition %d to partition %d",
			old_part_id, new_part_id));
    if ((error= m_file[new_part_id]->write_row(new_data)))
      DBUG_RETURN(error);
    if ((error= m_file[old_part_id]->delete_row(old_data)))
    {
#ifdef IN_THE_FUTURE
      (void) m_file[new_part_id]->delete_last_inserted_row(new_data);
#endif
      DBUG_RETURN(error);
    }
  }
  DBUG_RETURN(0);
}


/*
2732
  Remove an existing row
2733

2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756
  SYNOPSIS
    delete_row
    buf                      Deleted row in MySQL Row Format

  RETURN VALUE
    >0                       Error Code
    0                        Success

  DESCRIPTION
    This will delete a row. buf will contain a copy of the row to be deleted.
    The server will call this right after the current row has been read
    (from either a previous rnd_xxx() or index_xxx() call).
    If you keep a pointer to the last row or can access a primary key it will
    make doing the deletion quite a bit easier.
    Keep in mind that the server does no guarentee consecutive deletions.
    ORDER BY clauses can be used.

    Called in sql_acl.cc and sql_udf.cc to manage internal table information.
    Called in sql_delete.cc, sql_insert.cc, and sql_select.cc. In sql_select
    it is used for removing duplicates while in insert it is used for REPLACE
    calls.

    buf is either record[0] or record[1]
2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774
*/

int ha_partition::delete_row(const byte *buf)
{
  uint32 part_id;
  int error;
  DBUG_ENTER("ha_partition::delete_row");

  if ((error= get_part_for_delete(buf, m_rec0, m_part_info, &part_id)))
  {
    DBUG_RETURN(error);
  }
  m_last_part= part_id;
  DBUG_RETURN(m_file[part_id]->delete_row(buf));
}


/*
2775
  Delete all rows in a table
2776

2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793
  SYNOPSIS
    delete_all_rows()

  RETURN VALUE
    >0                       Error Code
    0                        Success

  DESCRIPTION
    Used to delete all rows in a table. Both for cases of truncate and
    for cases where the optimizer realizes that all rows will be
    removed as a result of a SQL statement.

    Called from item_sum.cc by Item_func_group_concat::clear(),
    Item_sum_count_distinct::clear(), and Item_func_group_concat::clear().
    Called from sql_delete.cc by mysql_delete().
    Called from sql_select.cc by JOIN::reinit().
    Called from sql_union.cc by st_select_lex_unit::exec().
2794 2795 2796 2797 2798 2799 2800
*/

int ha_partition::delete_all_rows()
{
  int error;
  handler **file;
  DBUG_ENTER("ha_partition::delete_all_rows");
2801

2802 2803 2804 2805 2806 2807 2808 2809 2810
  file= m_file;
  do
  {
    if ((error= (*file)->delete_all_rows()))
      DBUG_RETURN(error);
  } while (*(++file));
  DBUG_RETURN(0);
}

2811

2812
/*
2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823
  Start a large batch of insert rows

  SYNOPSIS
    start_bulk_insert()
    rows                  Number of rows to insert

  RETURN VALUE
    NONE

  DESCRIPTION
    rows == 0 means we will probably insert many rows
2824 2825 2826 2827 2828 2829
*/

void ha_partition::start_bulk_insert(ha_rows rows)
{
  handler **file;
  DBUG_ENTER("ha_partition::start_bulk_insert");
2830

2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845
  if (!rows)
  {
    /* Avoid allocation big caches in all underlaying handlers */
    DBUG_VOID_RETURN;
  }
  rows= rows/m_tot_parts + 1;
  file= m_file;
  do
  {
    (*file)->start_bulk_insert(rows);
  } while (*(++file));
  DBUG_VOID_RETURN;
}


2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856
/*
  Finish a large batch of insert rows

  SYNOPSIS
    end_bulk_insert()

  RETURN VALUE
    >0                      Error code
    0                       Success
*/

2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872
int ha_partition::end_bulk_insert()
{
  int error= 0;
  handler **file;
  DBUG_ENTER("ha_partition::end_bulk_insert");

  file= m_file;
  do
  {
    int tmp;
    if ((tmp= (*file)->end_bulk_insert()))
      error= tmp;
  } while (*(++file));
  DBUG_RETURN(error);
}

2873

2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884
/****************************************************************************
                MODULE full table scan
****************************************************************************/
/*
  Initialize engine for random reads

  SYNOPSIS
    ha_partition::rnd_init()
    scan	0  Initialize for random reads through rnd_pos()
		1  Initialize for random scan through rnd_next()

2885 2886 2887
  RETURN VALUE
    >0          Error code
    0           Success
2888

2889 2890 2891
  DESCRIPTION 
    rnd_init() is called when the server wants the storage engine to do a
    table scan or when the server wants to access data through rnd_pos.
2892

2893 2894 2895 2896 2897 2898 2899 2900
    When scan is used we will scan one handler partition at a time.
    When preparing for rnd_pos we will init all handler partitions.
    No extra cache handling is needed when scannning is not performed.

    Before initialising we will call rnd_end to ensure that we clean up from
    any previous incarnation of a table scan.
    Called from filesort.cc, records.cc, sql_handler.cc, sql_select.cc,
    sql_table.cc, and sql_update.cc.
2901 2902 2903 2904 2905
*/

int ha_partition::rnd_init(bool scan)
{
  int error;
patg@govinda.patg.net's avatar
patg@govinda.patg.net committed
2906 2907
  uint i= 0;
  uint32 part_id;
2908 2909 2910 2911
  handler **file;
  DBUG_ENTER("ha_partition::rnd_init");

  include_partition_fields_in_used_fields();
patg@govinda.patg.net's avatar
patg@govinda.patg.net committed
2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926
  
  /* Now we see what the index of our first important partition is */
  DBUG_PRINT("info", ("m_part_info->used_partitions 0x%x",
             m_part_info->used_partitions.bitmap));
  part_id= bitmap_get_first_set(&(m_part_info->used_partitions));
  DBUG_PRINT("info", ("m_part_spec.start_part %d", part_id));

  if (MY_BIT_NONE == part_id)
    goto err1;

  /*
    We have a partition and we are scanning with rnd_next
    so we bump our cache
  */
  DBUG_PRINT("info", ("rnd_init on partition %d", part_id));
2927 2928 2929 2930 2931 2932 2933
  if (scan)
  {
    /*
      rnd_end() is needed for partitioning to reset internal data if scan
      is already in use
    */
    rnd_end();
patg@govinda.patg.net's avatar
patg@govinda.patg.net committed
2934 2935 2936 2937 2938 2939 2940
    late_extra_cache(part_id);
    if ((error= m_file[part_id]->ha_rnd_init(scan)))
      goto err;
  }
  else
  {
    for (i= part_id; i < m_tot_parts; i++)
2941
    {
patg@govinda.patg.net's avatar
patg@govinda.patg.net committed
2942 2943 2944 2945 2946
      if (bitmap_is_set(&(m_part_info->used_partitions), i))
      {
        if ((error= m_file[i]->ha_rnd_init(scan)))
          goto err;
      }
2947 2948
    }
  }
patg@govinda.patg.net's avatar
patg@govinda.patg.net committed
2949 2950 2951 2952
  m_scan_value= scan;
  m_part_spec.start_part= part_id;
  m_part_spec.end_part= m_tot_parts - 1;
  DBUG_PRINT("info", ("m_scan_value=%d", m_scan_value));
2953 2954 2955
  DBUG_RETURN(0);

err:
patg@govinda.patg.net's avatar
patg@govinda.patg.net committed
2956 2957 2958 2959 2960 2961 2962 2963
  while ((int)--i >= (int)part_id)
  {
    if (bitmap_is_set(&(m_part_info->used_partitions), i))
	  m_file[i]->ha_rnd_end();
  }
err1:
  m_scan_value= 2;
  m_part_spec.start_part= NO_CURRENT_PART_ID;
2964 2965 2966 2967
  DBUG_RETURN(error);
}


2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978
/*
  End of a table scan

  SYNOPSIS
    rnd_end()

  RETURN VALUE
    >0          Error code
    0           Success
*/

2979 2980 2981 2982 2983 2984 2985
int ha_partition::rnd_end()
{
  handler **file;
  DBUG_ENTER("ha_partition::rnd_end");
  switch (m_scan_value) {
  case 2:                                       // Error
    break;
patg@govinda.patg.net's avatar
patg@govinda.patg.net committed
2986 2987
  case 1:
    if (NO_CURRENT_PART_ID != m_part_spec.start_part)         // Table scan
2988 2989 2990 2991 2992 2993 2994 2995 2996
    {
      late_extra_no_cache(m_part_spec.start_part);
      m_file[m_part_spec.start_part]->ha_rnd_end();
    }
    break;
  case 0:
    file= m_file;
    do
    {
patg@govinda.patg.net's avatar
patg@govinda.patg.net committed
2997 2998
      if (bitmap_is_set(&(m_part_info->used_partitions), (file - m_file)))
        (*file)->ha_rnd_end();
2999 3000 3001 3002
    } while (*(++file));
    break;
  }
  m_scan_value= 2;
patg@govinda.patg.net's avatar
patg@govinda.patg.net committed
3003
  m_part_spec.start_part= NO_CURRENT_PART_ID;
3004 3005 3006 3007 3008 3009 3010 3011 3012 3013
  DBUG_RETURN(0);
}

/*
  read next row during full table scan (scan in random row order)

  SYNOPSIS
    rnd_next()
    buf		buffer that should be filled with data

3014 3015 3016
  RETURN VALUE
    >0          Error code
    0           Success
3017

3018 3019 3020 3021 3022 3023 3024 3025
  DESCRIPTION
    This is called for each row of the table scan. When you run out of records
    you should return HA_ERR_END_OF_FILE.
    The Field structure for the table is the key to getting data into buf
    in a manner that will allow the server to understand it.

    Called from filesort.cc, records.cc, sql_handler.cc, sql_select.cc,
    sql_table.cc, and sql_update.cc.
3026 3027 3028 3029
*/

int ha_partition::rnd_next(byte *buf)
{
patg@govinda.patg.net's avatar
patg@govinda.patg.net committed
3030
  handler *file;
3031
  int result= HA_ERR_END_OF_FILE;
patg@govinda.patg.net's avatar
patg@govinda.patg.net committed
3032
  uint part_id= m_part_spec.start_part;
3033 3034
  DBUG_ENTER("ha_partition::rnd_next");

patg@govinda.patg.net's avatar
patg@govinda.patg.net committed
3035
  if (NO_CURRENT_PART_ID == part_id)
3036 3037 3038 3039 3040 3041 3042
  {
    /*
      The original set of partitions to scan was empty and thus we report
      the result here.
    */
    goto end;
  }
patg@govinda.patg.net's avatar
patg@govinda.patg.net committed
3043 3044 3045 3046
  
  DBUG_ASSERT(m_scan_value == 1);
  file= m_file[part_id];
  
3047 3048
  while (TRUE)
  {
patg@govinda.patg.net's avatar
patg@govinda.patg.net committed
3049 3050
    int result= file->rnd_next(buf);
    if (!result)
3051 3052
    {
      m_last_part= part_id;
patg@govinda.patg.net's avatar
patg@govinda.patg.net committed
3053
      m_part_spec.start_part= part_id;
3054 3055 3056
      table->status= 0;
      DBUG_RETURN(0);
    }
patg@govinda.patg.net's avatar
patg@govinda.patg.net committed
3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086

    /*
      if we get here, then the current partition rnd_next returned failure
    */
    if (result == HA_ERR_RECORD_DELETED)
      continue;                               // Probably MyISAM

    if (result != HA_ERR_END_OF_FILE)
      break;                                  // Return error

    /* End current partition */
    late_extra_no_cache(part_id);
    DBUG_PRINT("info", ("rnd_end on partition %d", part_id));
    if ((result= file->ha_rnd_end()))
      break;
    
    /* Shift to next partition */
    while (++part_id < m_tot_parts &&
           !bitmap_is_set(&(m_part_info->used_partitions), part_id))
      ;
    if (part_id >= m_tot_parts)
    {
      result= HA_ERR_END_OF_FILE;
      break;
    }
    file= m_file[part_id];
    DBUG_PRINT("info", ("rnd_init on partition %d", part_id));
    if ((result= file->ha_rnd_init(1)))
      break;
    late_extra_cache(part_id);
3087 3088 3089 3090 3091 3092 3093 3094 3095
  }

end:
  m_part_spec.start_part= NO_CURRENT_PART_ID;
  table->status= STATUS_NOT_FOUND;
  DBUG_RETURN(result);
}


3096 3097
/*
  Save position of current row
3098

3099 3100 3101
  SYNOPSIS
    position()
    record             Current record in MySQL Row Format
3102

3103 3104
  RETURN VALUE
    NONE
3105

3106 3107 3108 3109 3110
  DESCRIPTION
    position() is called after each call to rnd_next() if the data needs
    to be ordered. You can do something like the following to store
    the position:
    ha_store_ptr(ref, ref_length, current_position);
3111

3112 3113 3114 3115 3116 3117 3118
    The server uses ref to store data. ref_length in the above case is
    the size needed to store current_position. ref is just a byte array
    that the server will maintain. If you are using offsets to mark rows, then
    current_position should be the offset. If it is a primary key like in
    BDB, then it needs to be a primary key.

    Called from filesort.cc, sql_select.cc, sql_delete.cc and sql_update.cc.
3119 3120 3121 3122 3123 3124
*/

void ha_partition::position(const byte *record)
{
  handler *file= m_file[m_last_part];
  DBUG_ENTER("ha_partition::position");
3125

3126
  file->position(record);
3127
  int2store(ref, m_last_part);
3128 3129 3130 3131 3132
  memcpy((ref + PARTITION_BYTES_IN_POS), file->ref,
	 (ref_length - PARTITION_BYTES_IN_POS));

#ifdef SUPPORTING_PARTITION_OVER_DIFFERENT_ENGINES
#ifdef HAVE_purify
patg@govinda.patg.net's avatar
patg@govinda.patg.net committed
3133 3134
  bzero(ref + PARTITION_BYTES_IN_POS + ref_length,
        max_ref_length-ref_length);
3135 3136 3137 3138 3139 3140
#endif /* HAVE_purify */
#endif
  DBUG_VOID_RETURN;
}

/*
3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158
  Read row using position

  SYNOPSIS
    rnd_pos()
    out:buf                     Row read in MySQL Row Format
    position                    Position of read row

  RETURN VALUE
    >0                          Error code
    0                           Success

  DESCRIPTION
    This is like rnd_next, but you are given a position to use
    to determine the row. The position will be of the type that you stored in
    ref. You can use ha_get_ptr(pos,ref_length) to retrieve whatever key
    or position you saved when position() was called.
    Called from filesort.cc records.cc sql_insert.cc sql_select.cc
    sql_update.cc.
3159 3160 3161 3162 3163 3164 3165 3166
*/

int ha_partition::rnd_pos(byte * buf, byte *pos)
{
  uint part_id;
  handler *file;
  DBUG_ENTER("ha_partition::rnd_pos");

3167
  part_id= uint2korr((const byte *) pos);
3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194
  DBUG_ASSERT(part_id < m_tot_parts);
  file= m_file[part_id];
  m_last_part= part_id;
  DBUG_RETURN(file->rnd_pos(buf, (pos + PARTITION_BYTES_IN_POS)));
}


/****************************************************************************
                MODULE index scan
****************************************************************************/
/*
  Positions an index cursor to the index specified in the handle. Fetches the
  row if available. If the key value is null, begin at the first key of the
  index.

  There are loads of optimisations possible here for the partition handler.
  The same optimisations can also be checked for full table scan although
  only through conditions and not from index ranges.
  Phase one optimisations:
    Check if the fields of the partition function are bound. If so only use
    the single partition it becomes bound to.
  Phase two optimisations:
    If it can be deducted through range or list partitioning that only a
    subset of the partitions are used, then only use those partitions.
*/

/*
3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208
  Initialise handler before start of index scan

  SYNOPSIS
    index_init()
    inx                Index number
    sorted             Is rows to be returned in sorted order

  RETURN VALUE
    >0                 Error code
    0                  Success

  DESCRIPTION
    index_init is always called before starting index scans (except when
    starting through index_read_idx and using read_range variants).
3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226
*/

int ha_partition::index_init(uint inx, bool sorted)
{
  int error= 0;
  handler **file;
  DBUG_ENTER("ha_partition::index_init");

  active_index= inx;
  m_part_spec.start_part= NO_CURRENT_PART_ID;
  m_start_key.length= 0;
  m_ordered= sorted;
  m_curr_key_info= table->key_info+inx;
  include_partition_fields_in_used_fields();
  file= m_file;
  do
  {
    /* TODO RONM: Change to index_init() when code is stable */
patg@govinda.patg.net's avatar
patg@govinda.patg.net committed
3227 3228 3229 3230 3231 3232
    if (bitmap_is_set(&(m_part_info->used_partitions), (file - m_file)))
      if ((error= (*file)->ha_index_init(inx, sorted)))
      {
        DBUG_ASSERT(0);                           // Should never happen
        break;
      }
3233 3234 3235 3236 3237 3238
  } while (*(++file));
  DBUG_RETURN(error);
}


/*
3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250
  End of index scan

  SYNOPSIS
    index_end()

  RETURN VALUE
    >0                 Error code
    0                  Success

  DESCRIPTION
    index_end is called at the end of an index scan to clean up any
    things needed to clean up.
3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265
*/

int ha_partition::index_end()
{
  int error= 0;
  handler **file;
  DBUG_ENTER("ha_partition::index_end");

  active_index= MAX_KEY;
  m_part_spec.start_part= NO_CURRENT_PART_ID;
  file= m_file;
  do
  {
    int tmp;
    /* TODO RONM: Change to index_end() when code is stable */
patg@govinda.patg.net's avatar
patg@govinda.patg.net committed
3266 3267 3268
    if (bitmap_is_set(&(m_part_info->used_partitions), (file - m_file)))
      if ((tmp= (*file)->ha_index_end()))
        error= tmp;
3269 3270 3271 3272 3273 3274
  } while (*(++file));
  DBUG_RETURN(error);
}


/*
3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296
  Read one record in an index scan and start an index scan

  SYNOPSIS
    index_read()
    buf                    Read row in MySQL Row Format
    key                    Key parts in consecutive order
    key_len                Total length of key parts
    find_flag              What type of key condition is used

  RETURN VALUE
    >0                 Error code
    0                  Success

  DESCRIPTION
    index_read starts a new index scan using a start key. The MySQL Server
    will check the end key on its own. Thus to function properly the
    partitioned handler need to ensure that it delivers records in the sort
    order of the MySQL Server.
    index_read can be restarted without calling index_end on the previous
    index scan and without calling index_init. In this case the index_read
    is on the same index as the previous index_scan. This is particularly
    used in conjuntion with multi read ranges.
3297 3298 3299 3300 3301 3302
*/

int ha_partition::index_read(byte * buf, const byte * key,
			     uint key_len, enum ha_rkey_function find_flag)
{
  DBUG_ENTER("ha_partition::index_read");
3303

3304 3305 3306 3307 3308
  end_range= 0;
  DBUG_RETURN(common_index_read(buf, key, key_len, find_flag));
}


3309 3310 3311 3312 3313 3314 3315 3316 3317
/*
  Common routine for a number of index_read variants

  SYNOPSIS
    common_index_read
  
  see index_read for rest
*/

3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365
int ha_partition::common_index_read(byte *buf, const byte *key, uint key_len,
				    enum ha_rkey_function find_flag)
{
  int error;
  DBUG_ENTER("ha_partition::common_index_read");

  memcpy((void*)m_start_key.key, key, key_len);
  m_start_key.length= key_len;
  m_start_key.flag= find_flag;
  m_index_scan_type= partition_index_read;

  if ((error= partition_scan_set_up(buf, TRUE)))
  {
    DBUG_RETURN(error);
  }

  if (!m_ordered_scan_ongoing ||
      (find_flag == HA_READ_KEY_EXACT &&
       (key_len >= m_curr_key_info->key_length ||
	key_len == 0)))
  {
    /*
      We use unordered index scan either when read_range is used and flag
      is set to not use ordered or when an exact key is used and in this
      case all records will be sorted equal and thus the sort order of the
      resulting records doesn't matter.
      We also use an unordered index scan when the number of partitions to
      scan is only one.
      The unordered index scan will use the partition set created.
      Need to set unordered scan ongoing since we can come here even when
      it isn't set.
    */
    m_ordered_scan_ongoing= FALSE;
    error= handle_unordered_scan_next_partition(buf);
  }
  else
  {
    /*
      In all other cases we will use the ordered index scan. This will use
      the partition set created by the get_partition_set method.
    */
    error= handle_ordered_index_scan(buf);
  }
  DBUG_RETURN(error);
}


/*
3366 3367 3368 3369 3370
  Start an index scan from leftmost record and return first record

  SYNOPSIS
    index_first()
    buf                 Read row in MySQL Row Format
3371

3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383
  RETURN VALUE
    >0                  Error code
    0                   Success

  DESCRIPTION
    index_first() asks for the first key in the index.
    This is similar to index_read except that there is no start key since
    the scan starts from the leftmost entry and proceeds forward with
    index_next.

    Called from opt_range.cc, opt_sum.cc, sql_handler.cc,
    and sql_select.cc.
3384 3385 3386 3387 3388
*/

int ha_partition::index_first(byte * buf)
{
  DBUG_ENTER("ha_partition::index_first");
3389

3390 3391 3392 3393 3394 3395 3396
  end_range= 0;
  m_index_scan_type= partition_index_first;
  DBUG_RETURN(common_first_last(buf));
}


/*
3397 3398 3399 3400 3401
  Start an index scan from rightmost record and return first record
  
  SYNOPSIS
    index_last()
    buf                 Read row in MySQL Row Format
3402

3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414
  RETURN VALUE
    >0                  Error code
    0                   Success

  DESCRIPTION
    index_last() asks for the last key in the index.
    This is similar to index_read except that there is no start key since
    the scan starts from the rightmost entry and proceeds forward with
    index_prev.

    Called from opt_range.cc, opt_sum.cc, sql_handler.cc,
    and sql_select.cc.
3415 3416 3417 3418 3419
*/

int ha_partition::index_last(byte * buf)
{
  DBUG_ENTER("ha_partition::index_last");
3420

3421 3422 3423 3424
  m_index_scan_type= partition_index_last;
  DBUG_RETURN(common_first_last(buf));
}

3425 3426 3427 3428 3429 3430 3431 3432 3433
/*
  Common routine for index_first/index_last

  SYNOPSIS
    common_index_first_last
  
  see index_first for rest
*/

3434 3435 3436
int ha_partition::common_first_last(byte *buf)
{
  int error;
3437

3438 3439 3440 3441 3442 3443 3444
  if ((error= partition_scan_set_up(buf, FALSE)))
    return error;
  if (!m_ordered_scan_ongoing)
    return handle_unordered_scan_next_partition(buf);
  return handle_ordered_index_scan(buf);
}

3445

3446
/*
3447 3448 3449 3450 3451 3452 3453 3454 3455 3456
  Perform index read using index where always only one row is returned

  SYNOPSIS
    index_read_idx()
    see index_read for rest of parameters and return values

  DESCRIPTION
    Positions an index cursor to the index specified in key. Fetches the
    row if any.  This is only used to read whole keys.
    TODO: Optimise this code to avoid index_init and index_end
3457 3458 3459 3460 3461 3462 3463 3464
*/

int ha_partition::index_read_idx(byte * buf, uint index, const byte * key,
				 uint key_len,
                                 enum ha_rkey_function find_flag)
{
  int res;
  DBUG_ENTER("ha_partition::index_read_idx");
3465

3466 3467 3468 3469 3470 3471
  index_init(index, 0);
  res= index_read(buf, key, key_len, find_flag);
  index_end();
  DBUG_RETURN(res);
}

3472

3473
/*
3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488
  Read last using key

  SYNOPSIS
    index_read_last()
    buf                   Read row in MySQL Row Format
    key                   Key
    keylen                Length of key

  RETURN VALUE
    >0                    Error code
    0                     Success

  DESCRIPTION
    This is used in join_read_last_key to optimise away an ORDER BY.
    Can only be used on indexes supporting HA_READ_ORDER
3489 3490 3491 3492 3493
*/

int ha_partition::index_read_last(byte *buf, const byte *key, uint keylen)
{
  DBUG_ENTER("ha_partition::index_read_last");
3494

3495 3496 3497 3498 3499 3500
  m_ordered= TRUE;				// Safety measure
  DBUG_RETURN(index_read(buf, key, keylen, HA_READ_PREFIX_LAST));
}


/*
3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512
  Read next record in a forward index scan

  SYNOPSIS
    index_next()
    buf                   Read row in MySQL Row Format

  RETURN VALUE
    >0                    Error code
    0                     Success

  DESCRIPTION
    Used to read forward through the index.
3513 3514 3515 3516 3517
*/

int ha_partition::index_next(byte * buf)
{
  DBUG_ENTER("ha_partition::index_next");
3518

3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533
  /*
    TODO(low priority):
    If we want partition to work with the HANDLER commands, we
    must be able to do index_last() -> index_prev() -> index_next()
  */
  DBUG_ASSERT(m_index_scan_type != partition_index_last);
  if (!m_ordered_scan_ongoing)
  {
    DBUG_RETURN(handle_unordered_next(buf, FALSE));
  }
  DBUG_RETURN(handle_ordered_next(buf, FALSE));
}


/*
3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548
  Read next record special

  SYNOPSIS
    index_next_same()
    buf                   Read row in MySQL Row Format
    key                   Key
    keylen                Length of key

  RETURN VALUE
    >0                    Error code
    0                     Success

  DESCRIPTION
    This routine is used to read the next but only if the key is the same
    as supplied in the call.
3549 3550 3551 3552 3553
*/

int ha_partition::index_next_same(byte *buf, const byte *key, uint keylen)
{
  DBUG_ENTER("ha_partition::index_next_same");
3554

3555 3556 3557 3558 3559 3560 3561
  DBUG_ASSERT(keylen == m_start_key.length);
  DBUG_ASSERT(m_index_scan_type != partition_index_last);
  if (!m_ordered_scan_ongoing)
    DBUG_RETURN(handle_unordered_next(buf, TRUE));
  DBUG_RETURN(handle_ordered_next(buf, TRUE));
}

3562

3563
/*
3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575
  Read next record when performing index scan backwards

  SYNOPSIS
    index_prev()
    buf                   Read row in MySQL Row Format

  RETURN VALUE
    >0                    Error code
    0                     Success

  DESCRIPTION
    Used to read backwards through the index.
3576 3577 3578 3579 3580
*/

int ha_partition::index_prev(byte * buf)
{
  DBUG_ENTER("ha_partition::index_prev");
3581

3582 3583 3584 3585 3586 3587 3588
  /* TODO: read comment in index_next */
  DBUG_ASSERT(m_index_scan_type != partition_index_first);
  DBUG_RETURN(handle_ordered_prev(buf));
}


/*
3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606
  Start a read of one range with start and end key

  SYNOPSIS
    read_range_first()
    start_key           Specification of start key
    end_key             Specification of end key
    eq_range_arg        Is it equal range
    sorted              Should records be returned in sorted order

  RETURN VALUE
    >0                    Error code
    0                     Success

  DESCRIPTION
    We reimplement read_range_first since we don't want the compare_key
    check at the end. This is already performed in the partition handler.
    read_range_next is very much different due to that we need to scan
    all underlying handlers.
3607 3608 3609 3610 3611 3612 3613 3614
*/

int ha_partition::read_range_first(const key_range *start_key,
				   const key_range *end_key,
				   bool eq_range_arg, bool sorted)
{
  int error;
  DBUG_ENTER("ha_partition::read_range_first");
3615

3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643
  m_ordered= sorted;
  eq_range= eq_range_arg;
  end_range= 0;
  if (end_key)
  {
    end_range= &save_end_range;
    save_end_range= *end_key;
    key_compare_result_on_equal=
      ((end_key->flag == HA_READ_BEFORE_KEY) ? 1 :
       (end_key->flag == HA_READ_AFTER_KEY) ? -1 : 0);
  }
  range_key_part= m_curr_key_info->key_part;

  if (!start_key)				// Read first record
  {
    m_index_scan_type= partition_index_first;
    error= common_first_last(m_rec0);
  }
  else
  {
    error= common_index_read(m_rec0,
			     start_key->key,
			     start_key->length, start_key->flag);
  }
  DBUG_RETURN(error);
}


3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654
/*
  Read next record in read of a range with start and end key

  SYNOPSIS
    read_range_next()

  RETURN VALUE
    >0                    Error code
    0                     Success
*/

3655 3656 3657
int ha_partition::read_range_next()
{
  DBUG_ENTER("ha_partition::read_range_next");
3658

3659 3660 3661 3662 3663 3664 3665 3666
  if (m_ordered)
  {
    DBUG_RETURN(handler::read_range_next());
  }
  DBUG_RETURN(handle_unordered_next(m_rec0, eq_range));
}


3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682
/*
  Common routine to set up scans

  SYNOPSIS
    buf                  Buffer to later return record in
    idx_read_flag        Is it index scan

  RETURN VALUE
    >0                    Error code
    0                     Success

  DESCRIPTION
    This is where we check which partitions to actually scan if not all
    of them
*/

3683 3684 3685 3686 3687 3688 3689
int ha_partition::partition_scan_set_up(byte * buf, bool idx_read_flag)
{
  DBUG_ENTER("ha_partition::partition_scan_set_up");

  if (idx_read_flag)
    get_partition_set(table,buf,active_index,&m_start_key,&m_part_spec);
  else
patg@govinda.patg.net's avatar
patg@govinda.patg.net committed
3690 3691 3692 3693
  {
    m_part_spec.start_part= 0;
    m_part_spec.end_part= m_tot_parts - 1;
  }
3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716
  if (m_part_spec.start_part > m_part_spec.end_part)
  {
    /*
      We discovered a partition set but the set was empty so we report
      key not found.
    */
    DBUG_PRINT("info", ("scan with no partition to scan"));
    DBUG_RETURN(HA_ERR_END_OF_FILE);
  }
  if (m_part_spec.start_part == m_part_spec.end_part)
  {
    /*
      We discovered a single partition to scan, this never needs to be
      performed using the ordered index scan.
    */
    DBUG_PRINT("info", ("index scan using the single partition %d",
			m_part_spec.start_part));
    m_ordered_scan_ongoing= FALSE;
  }
  else
  {
    /*
      Set m_ordered_scan_ongoing according how the scan should be done
patg@govinda.patg.net's avatar
patg@govinda.patg.net committed
3717 3718 3719
      Only exact partitions are discovered atm by get_partition_set.
      Verify this, also bitmap must have at least one bit set otherwise
      the result from this table is the empty set.
3720
    */
patg@govinda.patg.net's avatar
patg@govinda.patg.net committed
3721 3722 3723 3724 3725 3726 3727 3728 3729
    uint start_part= bitmap_get_first_set(&(m_part_info->used_partitions));
    if (start_part == MY_BIT_NONE)
    {
      DBUG_PRINT("info", ("scan with no partition to scan"));
      DBUG_RETURN(HA_ERR_END_OF_FILE);
    }
    if (start_part > m_part_spec.start_part)
      m_part_spec.start_part= start_part;
    DBUG_ASSERT(m_part_spec.start_part < m_tot_parts);
3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741
    m_ordered_scan_ongoing= m_ordered;
  }
  DBUG_ASSERT(m_part_spec.start_part < m_tot_parts &&
              m_part_spec.end_part < m_tot_parts);
  DBUG_RETURN(0);
}


/****************************************************************************
  Unordered Index Scan Routines
****************************************************************************/
/*
3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761
  Common routine to handle index_next with unordered results

  SYNOPSIS
    handle_unordered_next()
    out:buf                       Read row in MySQL Row Format
    next_same                     Called from index_next_same

  RETURN VALUE
    HA_ERR_END_OF_FILE            End of scan
    0                             Success
    other                         Error code

  DESCRIPTION
    These routines are used to scan partitions without considering order.
    This is performed in two situations.
    1) In read_multi_range this is the normal case
    2) When performing any type of index_read, index_first, index_last where
    all fields in the partition function is bound. In this case the index
    scan is performed on only one partition and thus it isn't necessary to
    perform any sort.
3762 3763
*/

3764
int ha_partition::handle_unordered_next(byte *buf, bool is_next_same)
3765 3766 3767 3768 3769 3770 3771 3772 3773
{
  handler *file= file= m_file[m_part_spec.start_part];
  int error;
  DBUG_ENTER("ha_partition::handle_unordered_next");

  /*
    We should consider if this should be split into two functions as
    next_same is alwas a local constant
  */
3774
  if (is_next_same)
3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802
  {
    if (!(error= file->index_next_same(buf, m_start_key.key,
                                       m_start_key.length)))
    {
      m_last_part= m_part_spec.start_part;
      DBUG_RETURN(0);
    }
  }
  else if (!(error= file->index_next(buf)))
  {
    if (compare_key(end_range) <= 0)
    {
      m_last_part= m_part_spec.start_part;
      DBUG_RETURN(0);                           // Row was in range
    }
    error= HA_ERR_END_OF_FILE;
  }

  if (error == HA_ERR_END_OF_FILE)
  {
    m_part_spec.start_part++;                    // Start using next part
    error= handle_unordered_scan_next_partition(buf);
  }
  DBUG_RETURN(error);
}


/*
3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816
  Handle index_next when changing to new partition

  SYNOPSIS
    handle_unordered_scan_next_partition()
    buf                       Read row in MySQL Row Format

  RETURN VALUE
    HA_ERR_END_OF_FILE            End of scan
    0                             Success
    other                         Error code

  DESCRIPTION
    This routine is used to start the index scan on the next partition.
    Both initial start and after completing scan on one partition.
3817 3818 3819 3820 3821 3822 3823 3824 3825 3826
*/

int ha_partition::handle_unordered_scan_next_partition(byte * buf)
{
  uint i;
  DBUG_ENTER("ha_partition::handle_unordered_scan_next_partition");

  for (i= m_part_spec.start_part; i <= m_part_spec.end_part; i++)
  {
    int error;
patg@govinda.patg.net's avatar
patg@govinda.patg.net committed
3827
    handler *file;
3828

patg@govinda.patg.net's avatar
patg@govinda.patg.net committed
3829 3830 3831
    if (!(bitmap_is_set(&(m_part_info->used_partitions), i)))
      continue;
    file= m_file[i];
3832 3833 3834 3835 3836
    m_part_spec.start_part= i;
    switch (m_index_scan_type) {
    case partition_index_read:
      DBUG_PRINT("info", ("index_read on partition %d", i));
      error= file->index_read(buf, m_start_key.key,
patg@govinda.patg.net's avatar
patg@govinda.patg.net committed
3837 3838
                              m_start_key.length,
                              m_start_key.flag);
3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852
      break;
    case partition_index_first:
      DBUG_PRINT("info", ("index_first on partition %d", i));
      error= file->index_first(buf);
      break;
    default:
      DBUG_ASSERT(FALSE);
      DBUG_RETURN(1);
    }
    if (!error)
    {
      if (compare_key(end_range) <= 0)
      {
        m_last_part= i;
patg@govinda.patg.net's avatar
patg@govinda.patg.net committed
3853
        DBUG_RETURN(0);
3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866
      }
      error= HA_ERR_END_OF_FILE;
    }
    if ((error != HA_ERR_END_OF_FILE) && (error != HA_ERR_KEY_NOT_FOUND))
      DBUG_RETURN(error);
    DBUG_PRINT("info", ("HA_ERR_END_OF_FILE on partition %d", i));
  }
  m_part_spec.start_part= NO_CURRENT_PART_ID;
  DBUG_RETURN(HA_ERR_END_OF_FILE);
}


/*
3867
  Common routine to start index scan with ordered results
3868

3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891
  SYNOPSIS
    handle_ordered_index_scan()
    out:buf                       Read row in MySQL Row Format

  RETURN VALUE
    HA_ERR_END_OF_FILE            End of scan
    0                             Success
    other                         Error code

  DESCRIPTION
    This part contains the logic to handle index scans that require ordered
    output. This includes all except those started by read_range_first with
    the flag ordered set to FALSE. Thus most direct index_read and all
    index_first and index_last.

    We implement ordering by keeping one record plus a key buffer for each
    partition. Every time a new entry is requested we will fetch a new
    entry from the partition that is currently not filled with an entry.
    Then the entry is put into its proper sort position.

    Returning a record is done by getting the top record, copying the
    record to the request buffer and setting the partition as empty on
    entries.
3892 3893 3894 3895
*/

int ha_partition::handle_ordered_index_scan(byte *buf)
{
3896 3897
  uint i;
  uint j= 0;
3898 3899 3900 3901 3902
  bool found= FALSE;
  bool reverse_order= FALSE;
  DBUG_ENTER("ha_partition::handle_ordered_index_scan");

  m_top_entry= NO_CURRENT_PART_ID;
3903
  queue_remove_all(&m_queue);
patg@govinda.patg.net's avatar
patg@govinda.patg.net committed
3904 3905

  DBUG_PRINT("info", ("m_part_spec.start_part %d", m_part_spec.start_part));
3906 3907
  for (i= m_part_spec.start_part; i <= m_part_spec.end_part; i++)
  {
patg@govinda.patg.net's avatar
patg@govinda.patg.net committed
3908 3909
    if (!(bitmap_is_set(&(m_part_info->used_partitions), i)))
      continue;
3910
    byte *rec_buf_ptr= rec_buf(i);
patg@govinda.patg.net's avatar
patg@govinda.patg.net committed
3911
    int error;
3912 3913 3914 3915 3916
    handler *file= m_file[i];

    switch (m_index_scan_type) {
    case partition_index_read:
      error= file->index_read(rec_buf_ptr,
patg@govinda.patg.net's avatar
patg@govinda.patg.net committed
3917 3918 3919
                              m_start_key.key,
                              m_start_key.length,
                              m_start_key.flag);
3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939
      reverse_order= FALSE;
      break;
    case partition_index_first:
      error= file->index_first(rec_buf_ptr);
      reverse_order= FALSE;
      break;
    case partition_index_last:
      error= file->index_last(rec_buf_ptr);
      reverse_order= TRUE;
      break;
    default:
      DBUG_ASSERT(FALSE);
      DBUG_RETURN(HA_ERR_END_OF_FILE);
    }
    if (!error)
    {
      found= TRUE;
      /*
        Initialise queue without order first, simply insert
      */
3940
      queue_element(&m_queue, j++)= (byte*)queue_buf(i);
3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952
    }
    else if (error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE)
    {
      DBUG_RETURN(error);
    }
  }
  if (found)
  {
    /*
      We found at least one partition with data, now sort all entries and
      after that read the first entry and copy it to the buffer to return in.
    */
3953 3954 3955 3956
    queue_set_max_at_top(&m_queue, reverse_order);
    queue_set_cmp_arg(&m_queue, (void*)m_curr_key_info);
    m_queue.elements= j;
    queue_fix(&m_queue);
3957 3958 3959 3960 3961 3962 3963 3964
    return_top_record(buf);
    DBUG_PRINT("info", ("Record returned from partition %d", m_top_entry));
    DBUG_RETURN(0);
  }
  DBUG_RETURN(HA_ERR_END_OF_FILE);
}


3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975
/*
  Return the top record in sort order

  SYNOPSIS
    return_top_record()
    out:buf                  Row returned in MySQL Row Format

  RETURN VALUE
    NONE
*/

3976 3977 3978
void ha_partition::return_top_record(byte *buf)
{
  uint part_id;
3979
  byte *key_buffer= queue_top(&m_queue);
3980
  byte *rec_buffer= key_buffer + PARTITION_BYTES_IN_POS;
3981

3982 3983 3984 3985 3986 3987 3988
  part_id= uint2korr(key_buffer);
  memcpy(buf, rec_buffer, m_rec_length);
  m_last_part= part_id;
  m_top_entry= part_id;
}


3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003
/*
  Common routine to handle index_next with ordered results

  SYNOPSIS
    handle_ordered_next()
    out:buf                       Read row in MySQL Row Format
    next_same                     Called from index_next_same

  RETURN VALUE
    HA_ERR_END_OF_FILE            End of scan
    0                             Success
    other                         Error code
*/

int ha_partition::handle_ordered_next(byte *buf, bool is_next_same)
4004 4005 4006 4007 4008 4009
{
  int error;
  uint part_id= m_top_entry;
  handler *file= m_file[part_id];
  DBUG_ENTER("ha_partition::handle_ordered_next");

4010
  if (!is_next_same)
4011 4012 4013 4014 4015 4016 4017 4018 4019
    error= file->index_next(rec_buf(part_id));
  else
    error= file->index_next_same(rec_buf(part_id), m_start_key.key,
				 m_start_key.length);
  if (error)
  {
    if (error == HA_ERR_END_OF_FILE)
    {
      /* Return next buffered row */
4020 4021
      queue_remove(&m_queue, (uint) 0);
      if (m_queue.elements)
4022 4023 4024 4025 4026 4027 4028 4029 4030
      {
         DBUG_PRINT("info", ("Record returned from partition %u (2)",
                     m_top_entry));
         return_top_record(buf);
         error= 0;
      }
    }
    DBUG_RETURN(error);
  }
4031
  queue_replaced(&m_queue);
4032 4033 4034 4035 4036 4037
  return_top_record(buf);
  DBUG_PRINT("info", ("Record returned from partition %u", m_top_entry));
  DBUG_RETURN(0);
}


4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050
/*
  Common routine to handle index_prev with ordered results

  SYNOPSIS
    handle_ordered_prev()
    out:buf                       Read row in MySQL Row Format

  RETURN VALUE
    HA_ERR_END_OF_FILE            End of scan
    0                             Success
    other                         Error code
*/

4051 4052 4053 4054 4055 4056
int ha_partition::handle_ordered_prev(byte *buf)
{
  int error;
  uint part_id= m_top_entry;
  handler *file= m_file[part_id];
  DBUG_ENTER("ha_partition::handle_ordered_prev");
4057

4058 4059 4060 4061
  if ((error= file->index_prev(rec_buf(part_id))))
  {
    if (error == HA_ERR_END_OF_FILE)
    {
4062 4063
      queue_remove(&m_queue, (uint) 0);
      if (m_queue.elements)
4064 4065 4066 4067 4068 4069 4070 4071 4072
      {
	return_top_record(buf);
	DBUG_PRINT("info", ("Record returned from partition %d (2)",
			    m_top_entry));
        error= 0;
      }
    }
    DBUG_RETURN(error);
  }
4073
  queue_replaced(&m_queue);
4074 4075 4076 4077 4078 4079
  return_top_record(buf);
  DBUG_PRINT("info", ("Record returned from partition %d", m_top_entry));
  DBUG_RETURN(0);
}


4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095
/*
  Set fields in partition functions in read set for underlying handlers

  SYNOPSIS
    include_partition_fields_in_used_fields()

  RETURN VALUE
    NONE

  DESCRIPTION
    Some handlers only read fields as specified by the bitmap for the
    read set. For partitioned handlers we always require that the
    fields of the partition functions are read such that we can
    calculate the partition id to place updated and deleted records.
*/

4096 4097 4098
void ha_partition::include_partition_fields_in_used_fields()
{
  Field **ptr= m_part_field_array;
4099 4100
  DBUG_ENTER("ha_partition::include_partition_fields_in_used_fields");

4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118
  do
  {
    ha_set_bit_in_read_set((*ptr)->fieldnr);
  } while (*(++ptr));
  DBUG_VOID_RETURN;
}


/****************************************************************************
                MODULE information calls
****************************************************************************/

/*
  These are all first approximations of the extra, info, scan_time
  and read_time calls
*/

/*
4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180
  General method to gather info from handler

  SYNOPSIS
    info()
    flag              Specifies what info is requested

  RETURN VALUE
    NONE

  DESCRIPTION
    ::info() is used to return information to the optimizer.
    Currently this table handler doesn't implement most of the fields
    really needed. SHOW also makes use of this data
    Another note, if your handler doesn't proved exact record count,
    you will probably want to have the following in your code:
    if (records < 2)
      records = 2;
    The reason is that the server will optimize for cases of only a single
    record. If in a table scan you don't know the number of records
    it will probably be better to set records to two so you can return
    as many records as you need.

    Along with records a few more variables you may wish to set are:
      records
      deleted
      data_file_length
      index_file_length
      delete_length
      check_time
    Take a look at the public variables in handler.h for more information.

    Called in:
      filesort.cc
      ha_heap.cc
      item_sum.cc
      opt_sum.cc
      sql_delete.cc
     sql_delete.cc
     sql_derived.cc
      sql_select.cc
      sql_select.cc
      sql_select.cc
      sql_select.cc
      sql_select.cc
      sql_show.cc
      sql_show.cc
      sql_show.cc
      sql_show.cc
      sql_table.cc
      sql_union.cc
      sql_update.cc

    Some flags that are not implemented
      HA_STATUS_POS:
        This parameter is never used from the MySQL Server. It is checked in a
        place in MyISAM so could potentially be used by MyISAM specific
        programs.
      HA_STATUS_NO_LOCK:
      This is declared and often used. It's only used by MyISAM.
      It means that MySQL doesn't need the absolute latest statistics
      information. This may save the handler from doing internal locks while
      retrieving statistics data.
4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224
*/

void ha_partition::info(uint flag)
{
  handler *file, **file_array;
  DBUG_ENTER("ha_partition:info");

  if (flag & HA_STATUS_AUTO)
  {
    DBUG_PRINT("info", ("HA_STATUS_AUTO"));
    /*
      The auto increment value is only maintained by the first handler
      so we will only call this.
    */
    m_file[0]->info(HA_STATUS_AUTO);
  }
  if (flag & HA_STATUS_VARIABLE)
  {
    DBUG_PRINT("info", ("HA_STATUS_VARIABLE"));
    /*
      Calculates statistical variables
      records:           Estimate of number records in table
      We report sum (always at least 2)
      deleted:           Estimate of number holes in the table due to
      deletes
      We report sum
      data_file_length:  Length of data file, in principle bytes in table
      We report sum
      index_file_length: Length of index file, in principle bytes in
      indexes in the table
      We report sum
      mean_record_length:Mean record length in the table
      We calculate this
      check_time:        Time of last check (only applicable to MyISAM)
      We report last time of all underlying handlers
    */
    records= 0;
    deleted= 0;
    data_file_length= 0;
    index_file_length= 0;
    check_time= 0;
    file_array= m_file;
    do
    {
patg@govinda.patg.net's avatar
patg@govinda.patg.net committed
4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235
      if (bitmap_is_set(&(m_part_info->used_partitions), (file_array - m_file)))
      {
        file= *file_array;
        file->info(HA_STATUS_VARIABLE);
        records+= file->records;
        deleted+= file->deleted;
        data_file_length+= file->data_file_length;
        index_file_length+= file->index_file_length;
        if (file->check_time > check_time)
          check_time= file->check_time;
      }
4236
    } while (*(++file_array));
4237 4238
    if (records < 2 &&
        m_table_flags & HA_NOT_EXACT_COUNT)
4239
      records= 2;
4240 4241 4242 4243
    if (records > 0)
      mean_rec_length= (ulong) (data_file_length / records);
    else
      mean_rec_length= 1; //? What should we set here 
4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337
  }
  if (flag & HA_STATUS_CONST)
  {
    DBUG_PRINT("info", ("HA_STATUS_CONST"));
    /*
      Recalculate loads of constant variables. MyISAM also sets things
      directly on the table share object.

      Check whether this should be fixed since handlers should not
      change things directly on the table object.

      Monty comment: This should NOT be changed!  It's the handlers
      responsibility to correct table->s->keys_xxxx information if keys
      have been disabled.

      The most important parameters set here is records per key on
      all indexes. block_size and primar key ref_length.

      For each index there is an array of rec_per_key.
      As an example if we have an index with three attributes a,b and c
      we will have an array of 3 rec_per_key.
      rec_per_key[0] is an estimate of number of records divided by
      number of unique values of the field a.
      rec_per_key[1] is an estimate of the number of records divided
      by the number of unique combinations of the fields a and b.
      rec_per_key[2] is an estimate of the number of records divided
      by the number of unique combinations of the fields a,b and c.

      Many handlers only set the value of rec_per_key when all fields
      are bound (rec_per_key[2] in the example above).

      If the handler doesn't support statistics, it should set all of the
      above to 0.

      We will allow the first handler to set the rec_per_key and use
      this as an estimate on the total table.

      max_data_file_length:     Maximum data file length
      We ignore it, is only used in
      SHOW TABLE STATUS
      max_index_file_length:    Maximum index file length
      We ignore it since it is never used
      block_size:               Block size used
      We set it to the value of the first handler
      sortkey:                  Never used at any place so ignored
      ref_length:               We set this to the value calculated
      and stored in local object
      create_time:              Creation time of table
      Set by first handler

      So we calculate these constants by using the variables on the first
      handler.
    */

    file= m_file[0];
    file->info(HA_STATUS_CONST);
    create_time= file->create_time;
    ref_length= m_ref_length;
  }
  if (flag & HA_STATUS_ERRKEY)
  {
    handler *file= m_file[m_last_part];
    DBUG_PRINT("info", ("info: HA_STATUS_ERRKEY"));
    /*
      This flag is used to get index number of the unique index that
      reported duplicate key
      We will report the errkey on the last handler used and ignore the rest
    */
    file->info(HA_STATUS_ERRKEY);
    if (file->errkey != (uint) -1)
      errkey= file->errkey;
  }
  if (flag & HA_STATUS_TIME)
  {
    DBUG_PRINT("info", ("info: HA_STATUS_TIME"));
    /*
      This flag is used to set the latest update time of the table.
      Used by SHOW commands
      We will report the maximum of these times
    */
    update_time= 0;
    file_array= m_file;
    do
    {
      file= *file_array;
      file->info(HA_STATUS_TIME);
      if (file->update_time > update_time)
	update_time= file->update_time;
    } while (*(++file_array));
  }
  DBUG_VOID_RETURN;
}


4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360
void ha_partition::get_dynamic_partition_info(PARTITION_INFO *stat_info,
                                              uint part_id)
{
  handler *file= m_file[part_id];
  file->info(HA_STATUS_CONST | HA_STATUS_TIME | HA_STATUS_VARIABLE |
             HA_STATUS_NO_LOCK);

  stat_info->records= file->records;
  stat_info->mean_rec_length= file->mean_rec_length;
  stat_info->data_file_length= file->data_file_length;
  stat_info->max_data_file_length= file->max_data_file_length;
  stat_info->index_file_length= file->index_file_length;
  stat_info->delete_length= file->delete_length;
  stat_info->create_time= file->create_time;
  stat_info->update_time= file->update_time;
  stat_info->check_time= file->check_time;
  stat_info->check_sum= 0;
  if (file->table_flags() & (ulong) HA_HAS_CHECKSUM)
    stat_info->check_sum= file->checksum();
  return;
}


4361
/*
4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372
  General function to prepare handler for certain behavior

  SYNOPSIS
    extra()
    operation              Operation type for extra call

  RETURN VALUE
    >0                     Error code
    0                      Success

  DESCRIPTION
4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683
  extra() is called whenever the server wishes to send a hint to
  the storage engine. The MyISAM engine implements the most hints.

  We divide the parameters into the following categories:
  1) Parameters used by most handlers
  2) Parameters used by some non-MyISAM handlers
  3) Parameters used only by MyISAM
  4) Parameters only used by temporary tables for query processing
  5) Parameters only used by MyISAM internally
  6) Parameters not used at all

  The partition handler need to handle category 1), 2) and 3).

  1) Parameters used by most handlers
  -----------------------------------
  HA_EXTRA_RESET:
    This option is used by most handlers and it resets the handler state
    to the same state as after an open call. This includes releasing
    any READ CACHE or WRITE CACHE or other internal buffer used.

    It is called from the reset method in the handler interface. There are
    three instances where this is called.
    1) After completing a INSERT ... SELECT ... query the handler for the
       table inserted into is reset
    2) It is called from close_thread_table which in turn is called from
       close_thread_tables except in the case where the tables are locked
       in which case ha_commit_stmt is called instead.
       It is only called from here if flush_version hasn't changed and the
       table is not an old table when calling close_thread_table.
       close_thread_tables is called from many places as a general clean up
       function after completing a query.
    3) It is called when deleting the QUICK_RANGE_SELECT object if the
       QUICK_RANGE_SELECT object had its own handler object. It is called
       immediatley before close of this local handler object.
  HA_EXTRA_KEYREAD:
  HA_EXTRA_NO_KEYREAD:
    These parameters are used to provide an optimisation hint to the handler.
    If HA_EXTRA_KEYREAD is set it is enough to read the index fields, for
    many handlers this means that the index-only scans can be used and it
    is not necessary to use the real records to satisfy this part of the
    query. Index-only scans is a very important optimisation for disk-based
    indexes. For main-memory indexes most indexes contain a reference to the
    record and thus KEYREAD only says that it is enough to read key fields.
    HA_EXTRA_NO_KEYREAD disables this for the handler, also HA_EXTRA_RESET
    will disable this option.
    The handler will set HA_KEYREAD_ONLY in its table flags to indicate this
    feature is supported.
  HA_EXTRA_FLUSH:
    Indication to flush tables to disk, called at close_thread_table to
    ensure disk based tables are flushed at end of query execution.

  2) Parameters used by some non-MyISAM handlers
  ----------------------------------------------
  HA_EXTRA_RETRIEVE_ALL_COLS:
    Many handlers have implemented optimisations to avoid fetching all
    fields when retrieving data. In certain situations all fields need
    to be retrieved even though the query_id is not set on all field
    objects.

    It is called from copy_data_between_tables where all fields are
    copied without setting query_id before calling the handlers.
    It is called from UPDATE statements when the fields of the index
    used is updated or ORDER BY is used with UPDATE.
    And finally when calculating checksum of a table using the CHECKSUM
    command.
  HA_EXTRA_RETRIEVE_PRIMARY_KEY:
    In some situations it is mandatory to retrieve primary key fields
    independent of the query id's. This extra flag specifies that fetch
    of primary key fields is mandatory.
  HA_EXTRA_KEYREAD_PRESERVE_FIELDS:
    This is a strictly InnoDB feature that is more or less undocumented.
    When it is activated InnoDB copies field by field from its fetch
    cache instead of all fields in one memcpy. Have no idea what the
    purpose of this is.
    Cut from include/my_base.h:
    When using HA_EXTRA_KEYREAD, overwrite only key member fields and keep
    other fields intact. When this is off (by default) InnoDB will use memcpy
    to overwrite entire row.
  HA_EXTRA_IGNORE_DUP_KEY:
  HA_EXTRA_NO_IGNORE_DUP_KEY:
    Informs the handler to we will not stop the transaction if we get an
    duplicate key errors during insert/upate.
    Always called in pair, triggered by INSERT IGNORE and other similar
    SQL constructs.
    Not used by MyISAM.

  3) Parameters used only by MyISAM
  ---------------------------------
  HA_EXTRA_NORMAL:
    Only used in MyISAM to reset quick mode, not implemented by any other
    handler. Quick mode is also reset in MyISAM by HA_EXTRA_RESET.

    It is called after completing a successful DELETE query if the QUICK
    option is set.

  HA_EXTRA_QUICK:
    When the user does DELETE QUICK FROM table where-clause; this extra
    option is called before the delete query is performed and
    HA_EXTRA_NORMAL is called after the delete query is completed.
    Temporary tables used internally in MySQL always set this option

    The meaning of quick mode is that when deleting in a B-tree no merging
    of leafs is performed. This is a common method and many large DBMS's
    actually only support this quick mode since it is very difficult to
    merge leaves in a tree used by many threads concurrently.

  HA_EXTRA_CACHE:
    This flag is usually set with extra_opt along with a cache size.
    The size of this buffer is set by the user variable
    record_buffer_size. The value of this cache size is the amount of
    data read from disk in each fetch when performing a table scan.
    This means that before scanning a table it is normal to call
    extra with HA_EXTRA_CACHE and when the scan is completed to call
    HA_EXTRA_NO_CACHE to release the cache memory.

    Some special care is taken when using this extra parameter since there
    could be a write ongoing on the table in the same statement. In this
    one has to take special care since there might be a WRITE CACHE as
    well. HA_EXTRA_CACHE specifies using a READ CACHE and using
    READ CACHE and WRITE CACHE at the same time is not possible.

    Only MyISAM currently use this option.

    It is set when doing full table scans using rr_sequential and
    reset when completing such a scan with end_read_record
    (resetting means calling extra with HA_EXTRA_NO_CACHE).

    It is set in filesort.cc for MyISAM internal tables and it is set in
    a multi-update where HA_EXTRA_CACHE is called on a temporary result
    table and after that ha_rnd_init(0) on table to be updated
    and immediately after that HA_EXTRA_NO_CACHE on table to be updated.

    Apart from that it is always used from init_read_record but not when
    used from UPDATE statements. It is not used from DELETE statements
    with ORDER BY and LIMIT but it is used in normal scan loop in DELETE
    statements. The reason here is that DELETE's in MyISAM doesn't move
    existings data rows.

    It is also set in copy_data_between_tables when scanning the old table
    to copy over to the new table.
    And it is set in join_init_read_record where quick objects are used
    to perform a scan on the table. In this case the full table scan can
    even be performed multiple times as part of the nested loop join.

    For purposes of the partition handler it is obviously necessary to have
    special treatment of this extra call. If we would simply pass this
    extra call down to each handler we would allocate
    cache size * no of partitions amount of memory and this is not
    necessary since we will only scan one partition at a time when doing
    full table scans.

    Thus we treat it by first checking whether we have MyISAM handlers in
    the table, if not we simply ignore the call and if we have we will
    record the call but will not call any underlying handler yet. Then
    when performing the sequential scan we will check this recorded value
    and call extra_opt whenever we start scanning a new partition.

    monty: Neads to be fixed so that it's passed to all handlers when we
    move to another partition during table scan.

  HA_EXTRA_NO_CACHE:
    When performing a UNION SELECT HA_EXTRA_NO_CACHE is called from the
    flush method in the select_union class.
    It is used to some extent when insert delayed inserts.
    See HA_EXTRA_RESET_STATE for use in conjunction with delete_all_rows().

    It should be ok to call HA_EXTRA_NO_CACHE on all underlying handlers
    if they are MyISAM handlers. Other handlers we can ignore the call
    for. If no cache is in use they will quickly return after finding
    this out. And we also ensure that all caches are disabled and no one
    is left by mistake.
    In the future this call will probably be deleted an we will instead call
    ::reset();

  HA_EXTRA_WRITE_CACHE:
    See above, called from various places. It is mostly used when we
    do INSERT ... SELECT
    No special handling to save cache space is developed currently.

  HA_EXTRA_PREPARE_FOR_UPDATE:
    This is called as part of a multi-table update. When the table to be
    updated is also scanned then this informs MyISAM handler to drop any
    caches if dynamic records are used (fixed size records do not care
    about this call). We pass this along to all underlying MyISAM handlers
    and ignore it for the rest.

  HA_EXTRA_PREPARE_FOR_DELETE:
    Only used by MyISAM, called in preparation for a DROP TABLE.
    It's used mostly by Windows that cannot handle dropping an open file.
    On other platforms it has the same effect as HA_EXTRA_FORCE_REOPEN.

  HA_EXTRA_READCHECK:
  HA_EXTRA_NO_READCHECK:
    Only one call to HA_EXTRA_NO_READCHECK from ha_open where it says that
    this is not needed in SQL. The reason for this call is that MyISAM sets
    the READ_CHECK_USED in the open call so the call is needed for MyISAM
    to reset this feature.
    The idea with this parameter was to inform of doing/not doing a read
    check before applying an update. Since SQL always performs a read before
    applying the update No Read Check is needed in MyISAM as well.

    This is a cut from Docs/myisam.txt
     Sometimes you might want to force an update without checking whether
     another user has changed the record since you last read it. This is
     somewhat dangerous, so it should ideally not be used. That can be
     accomplished by wrapping the mi_update() call in two calls to mi_extra(),
     using these functions:
     HA_EXTRA_NO_READCHECK=5                 No readcheck on update
     HA_EXTRA_READCHECK=6                    Use readcheck (def)

  HA_EXTRA_FORCE_REOPEN:
    Only used by MyISAM, called when altering table, closing tables to
    enforce a reopen of the table files.

  4) Parameters only used by temporary tables for query processing
  ----------------------------------------------------------------
  HA_EXTRA_RESET_STATE:
    Same as HA_EXTRA_RESET except that buffers are not released. If there is
    a READ CACHE it is reinit'ed. A cache is reinit'ed to restart reading
    or to change type of cache between READ CACHE and WRITE CACHE.

    This extra function is always called immediately before calling
    delete_all_rows on the handler for temporary tables.
    There are cases however when HA_EXTRA_RESET_STATE isn't called in
    a similar case for a temporary table in sql_union.cc and in two other
    cases HA_EXTRA_NO_CACHE is called before and HA_EXTRA_WRITE_CACHE
    called afterwards.
    The case with HA_EXTRA_NO_CACHE and HA_EXTRA_WRITE_CACHE means
    disable caching, delete all rows and enable WRITE CACHE. This is
    used for temporary tables containing distinct sums and a
    functional group.

    The only case that delete_all_rows is called on non-temporary tables
    is in sql_delete.cc when DELETE FROM table; is called by a user.
    In this case no special extra calls are performed before or after this
    call.

    The partition handler should not need to bother about this one. It
    should never be called.

  HA_EXTRA_NO_ROWS:
    Don't insert rows indication to HEAP and MyISAM, only used by temporary
    tables used in query processing.
    Not handled by partition handler.

  5) Parameters only used by MyISAM internally
  --------------------------------------------
  HA_EXTRA_REINIT_CACHE:
    This call reinitialises the READ CACHE described above if there is one
    and otherwise the call is ignored.

    We can thus safely call it on all underlying handlers if they are
    MyISAM handlers. It is however never called so we don't handle it at all.
  HA_EXTRA_FLUSH_CACHE:
    Flush WRITE CACHE in MyISAM. It is only from one place in the code.
    This is in sql_insert.cc where it is called if the table_flags doesn't
    contain HA_DUPP_POS. The only handler having the HA_DUPP_POS set is the
    MyISAM handler and so the only handler not receiving this call is MyISAM.
    Thus in effect this call is called but never used. Could be removed
    from sql_insert.cc
  HA_EXTRA_NO_USER_CHANGE:
    Only used by MyISAM, never called.
    Simulates lock_type as locked.
  HA_EXTRA_WAIT_LOCK:
  HA_EXTRA_WAIT_NOLOCK:
    Only used by MyISAM, called from MyISAM handler but never from server
    code on top of the handler.
    Sets lock_wait on/off
  HA_EXTRA_NO_KEYS:
    Only used MyISAM, only used internally in MyISAM handler, never called
    from server level.
  HA_EXTRA_KEYREAD_CHANGE_POS:
  HA_EXTRA_REMEMBER_POS:
  HA_EXTRA_RESTORE_POS:
  HA_EXTRA_PRELOAD_BUFFER_SIZE:
  HA_EXTRA_CHANGE_KEY_TO_DUP:
  HA_EXTRA_CHANGE_KEY_TO_UNIQUE:
    Only used by MyISAM, never called.

  6) Parameters not used at all
  -----------------------------
  HA_EXTRA_KEY_CACHE:
  HA_EXTRA_NO_KEY_CACHE:
    This parameters are no longer used and could be removed.
*/

int ha_partition::extra(enum ha_extra_function operation)
{
  DBUG_ENTER("ha_partition:extra");
  DBUG_PRINT("info", ("operation: %d", (int) operation));

  switch (operation) {
    /* Category 1), used by most handlers */
  case HA_EXTRA_KEYREAD:
  case HA_EXTRA_NO_KEYREAD:
  case HA_EXTRA_FLUSH:
    DBUG_RETURN(loop_extra(operation));

    /* Category 2), used by non-MyISAM handlers */
  case HA_EXTRA_IGNORE_DUP_KEY:
  case HA_EXTRA_NO_IGNORE_DUP_KEY:
  case HA_EXTRA_RETRIEVE_ALL_COLS:
  case HA_EXTRA_RETRIEVE_PRIMARY_KEY:
  case HA_EXTRA_KEYREAD_PRESERVE_FIELDS:
  {
    if (!m_myisam)
      DBUG_RETURN(loop_extra(operation));
    break;
  }

  /* Category 3), used by MyISAM handlers */
4684 4685 4686
  case HA_EXTRA_PREPARE_FOR_DELETE:
    DBUG_RETURN(prepare_for_delete());
    break;
4687 4688 4689 4690 4691
  case HA_EXTRA_NORMAL:
  case HA_EXTRA_QUICK:
  case HA_EXTRA_NO_READCHECK:
  case HA_EXTRA_PREPARE_FOR_UPDATE:
  case HA_EXTRA_FORCE_REOPEN:
4692
  case HA_EXTRA_FLUSH_CACHE:
4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720
  {
    if (m_myisam)
      DBUG_RETURN(loop_extra(operation));
    break;
  }
  case HA_EXTRA_CACHE:
  {
    prepare_extra_cache(0);
    break;
  }
  case HA_EXTRA_NO_CACHE:
  {
    m_extra_cache= FALSE;
    m_extra_cache_size= 0;
    DBUG_RETURN(loop_extra(operation));
  }
  default:
  {
    /* Temporary crash to discover what is wrong */
    DBUG_ASSERT(0);
    break;
  }
  }
  DBUG_RETURN(0);
}


/*
4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732
  Special extra call to reset extra parameters

  SYNOPSIS
    reset()

  RETURN VALUE
    >0                   Error code
    0                    Success

  DESCRIPTION
    This will in the future be called instead of extra(HA_EXTRA_RESET) as this
    is such a common call
4733 4734 4735 4736 4737 4738 4739
*/

int ha_partition::reset(void)
{
  int result= 0, tmp;
  handler **file;
  DBUG_ENTER("ha_partition::reset");
4740
  if (m_part_info)
patg@govinda.patg.net's avatar
patg@govinda.patg.net committed
4741 4742
    bitmap_set_all(&m_part_info->used_partitions);
  file= m_file;
4743 4744 4745 4746 4747 4748 4749 4750
  do
  {
    if ((tmp= (*file)->reset()))
      result= tmp;
  } while (*(++file));
  DBUG_RETURN(result);
}

4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763
/*
  Special extra method for HA_EXTRA_CACHE with cachesize as extra parameter

  SYNOPSIS
    extra_opt()
    operation                      Must be HA_EXTRA_CACHE
    cachesize                      Size of cache in full table scan

  RETURN VALUE
    >0                   Error code
    0                    Success
*/

4764 4765 4766
int ha_partition::extra_opt(enum ha_extra_function operation, ulong cachesize)
{
  DBUG_ENTER("ha_partition::extra_opt()");
4767

4768 4769 4770 4771 4772 4773
  DBUG_ASSERT(HA_EXTRA_CACHE == operation);
  prepare_extra_cache(cachesize);
  DBUG_RETURN(0);
}


4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784
/*
  Call extra on handler with HA_EXTRA_CACHE and cachesize

  SYNOPSIS
    prepare_extra_cache()
    cachesize                Size of cache for full table scan

  RETURN VALUE
    NONE
*/

4785 4786 4787 4788 4789 4790 4791 4792
void ha_partition::prepare_extra_cache(uint cachesize)
{
  DBUG_ENTER("ha_partition::prepare_extra_cache()");

  m_extra_cache= TRUE;
  m_extra_cache_size= cachesize;
  if (m_part_spec.start_part != NO_CURRENT_PART_ID)
  {
patg@govinda.patg.net's avatar
patg@govinda.patg.net committed
4793
    late_extra_cache(m_part_spec.start_part);
4794 4795 4796 4797 4798
  }
  DBUG_VOID_RETURN;
}


4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822
/*
  Prepares our new and reorged handlers for rename or delete

  SYNOPSIS
    prepare_for_delete()

  RETURN VALUE
    >0                    Error code
    0                     Success
*/

int ha_partition::prepare_for_delete()
{
  int result= 0, tmp;
  handler **file;
  DBUG_ENTER("ha_partition::prepare_for_delete()");
  
  if (m_new_file != NULL)
  {
    for (file= m_new_file; *file; file++)
      if ((tmp= (*file)->extra(HA_EXTRA_PREPARE_FOR_DELETE)))
        result= tmp;      
    for (file= m_reorged_file; *file; file++)
      if ((tmp= (*file)->extra(HA_EXTRA_PREPARE_FOR_DELETE)))
reggie@big_geek's avatar
reggie@big_geek committed
4823 4824
        result= tmp;   
    DBUG_RETURN(result);   
4825
  }
reggie@big_geek's avatar
reggie@big_geek committed
4826 4827
  
  DBUG_RETURN(loop_extra(HA_EXTRA_PREPARE_FOR_DELETE));
4828 4829
}

4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841
/*
  Call extra on all partitions

  SYNOPSIS
    loop_extra()
    operation             extra operation type

  RETURN VALUE
    >0                    Error code
    0                     Success
*/

4842 4843 4844 4845 4846
int ha_partition::loop_extra(enum ha_extra_function operation)
{
  int result= 0, tmp;
  handler **file;
  DBUG_ENTER("ha_partition::loop_extra()");
4847
  
patg@govinda.patg.net's avatar
patg@govinda.patg.net committed
4848 4849 4850 4851
  /* 
    TODO, 5.2: this is where you could possibly add optimisations to add the bitmap
    _if_ a SELECT.
  */
4852 4853 4854 4855 4856 4857 4858 4859 4860
  for (file= m_file; *file; file++)
  {
    if ((tmp= (*file)->extra(operation)))
      result= tmp;
  }
  DBUG_RETURN(result);
}


4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871
/*
  Call extra(HA_EXTRA_CACHE) on next partition_id

  SYNOPSIS
    late_extra_cache()
    partition_id               Partition id to call extra on

  RETURN VALUE
    NONE
*/

4872 4873 4874 4875
void ha_partition::late_extra_cache(uint partition_id)
{
  handler *file;
  DBUG_ENTER("ha_partition::late_extra_cache");
4876

4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887
  if (!m_extra_cache)
    DBUG_VOID_RETURN;
  file= m_file[partition_id];
  if (m_extra_cache_size == 0)
    VOID(file->extra(HA_EXTRA_CACHE));
  else
    VOID(file->extra_opt(HA_EXTRA_CACHE, m_extra_cache_size));
  DBUG_VOID_RETURN;
}


4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898
/*
  Call extra(HA_EXTRA_NO_CACHE) on next partition_id

  SYNOPSIS
    late_extra_no_cache()
    partition_id               Partition id to call extra on

  RETURN VALUE
    NONE
*/

4899 4900 4901 4902
void ha_partition::late_extra_no_cache(uint partition_id)
{
  handler *file;
  DBUG_ENTER("ha_partition::late_extra_no_cache");
4903

4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915
  if (!m_extra_cache)
    DBUG_VOID_RETURN;
  file= m_file[partition_id];
  VOID(file->extra(HA_EXTRA_NO_CACHE));
  DBUG_VOID_RETURN;
}


/****************************************************************************
                MODULE optimiser support
****************************************************************************/

4916 4917 4918 4919 4920 4921 4922 4923 4924 4925
/*
  Get keys to use for scanning

  SYNOPSIS
    keys_to_use_for_scanning()

  RETURN VALUE
    key_map of keys usable for scanning
*/

4926 4927 4928
const key_map *ha_partition::keys_to_use_for_scanning()
{
  DBUG_ENTER("ha_partition::keys_to_use_for_scanning");
4929

4930 4931 4932
  DBUG_RETURN(m_file[0]->keys_to_use_for_scanning());
}

4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943

/*
  Return time for a scan of the table

  SYNOPSIS
    scan_time()

  RETURN VALUE
    time for scan
*/

4944 4945 4946 4947 4948 4949 4950
double ha_partition::scan_time()
{
  double scan_time= 0;
  handler **file;
  DBUG_ENTER("ha_partition::scan_time");

  for (file= m_file; *file; file++)
patg@govinda.patg.net's avatar
patg@govinda.patg.net committed
4951 4952
    if (bitmap_is_set(&(m_part_info->used_partitions), (file - m_file)))
      scan_time+= (*file)->scan_time();
4953 4954 4955 4956 4957
  DBUG_RETURN(scan_time);
}


/*
4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973
  Get time to read

  SYNOPSIS
    read_time()
    index                Index number used
    ranges               Number of ranges
    rows                 Number of rows

  RETURN VALUE
    time for read

  DESCRIPTION
    This will be optimised later to include whether or not the index can
    be used with partitioning. To achieve we need to add another parameter
    that specifies how many of the index fields that are bound in the ranges.
    Possibly added as a new call to handlers.
4974 4975 4976 4977 4978
*/

double ha_partition::read_time(uint index, uint ranges, ha_rows rows)
{
  DBUG_ENTER("ha_partition::read_time");
4979

4980 4981 4982 4983
  DBUG_RETURN(m_file[0]->read_time(index, ranges, rows));
}

/*
4984 4985 4986 4987 4988 4989 4990 4991 4992 4993
  Find number of records in a range

  SYNOPSIS
    records_in_range()
    inx                  Index number
    min_key              Start of range
    max_key              End of range

  RETURN VALUE
    Number of rows in range
4994

4995 4996 4997 4998
  DESCRIPTION
    Given a starting key, and an ending key estimate the number of rows that
    will exist between the two. end_key may be empty which in case determine
    if start_key matches any rows.
4999

5000 5001 5002 5003 5004
    Called from opt_range.cc by check_quick_keys().

    monty: MUST be called for each range and added.
          Note that MySQL will assume that if this returns 0 there is no
          matching rows for the range!
5005 5006 5007 5008 5009 5010
*/

ha_rows ha_partition::records_in_range(uint inx, key_range *min_key,
				       key_range *max_key)
{
  handler **file;
patg@govinda.patg.net's avatar
patg@govinda.patg.net committed
5011
  ha_rows in_range= 0;
5012 5013 5014 5015 5016
  DBUG_ENTER("ha_partition::records_in_range");

  file= m_file;
  do
  {
patg@govinda.patg.net's avatar
patg@govinda.patg.net committed
5017 5018 5019 5020 5021 5022 5023
    if (bitmap_is_set(&(m_part_info->used_partitions), (file - m_file)))
    {
      ha_rows tmp_in_range= (*file)->records_in_range(inx, min_key, max_key);
      if (tmp_in_range == HA_POS_ERROR)
        DBUG_RETURN(tmp_in_range);
      in_range+= tmp_in_range;
    }
5024 5025 5026 5027 5028
  } while (*(++file));
  DBUG_RETURN(in_range);
}


5029 5030 5031 5032 5033 5034 5035 5036 5037 5038
/*
  Estimate upper bound of number of rows

  SYNOPSIS
    estimate_rows_upper_bound()

  RETURN VALUE
    Number of rows
*/

5039 5040 5041 5042 5043 5044 5045 5046 5047
ha_rows ha_partition::estimate_rows_upper_bound()
{
  ha_rows rows, tot_rows= 0;
  handler **file;
  DBUG_ENTER("ha_partition::estimate_rows_upper_bound");

  file= m_file;
  do
  {
patg@govinda.patg.net's avatar
patg@govinda.patg.net committed
5048 5049 5050 5051 5052 5053 5054
    if (bitmap_is_set(&(m_part_info->used_partitions), (file - m_file)))
    {
      rows= (*file)->estimate_rows_upper_bound();
      if (rows == HA_POS_ERROR)
        DBUG_RETURN(HA_POS_ERROR);
      tot_rows+= rows;
    }
5055 5056 5057 5058 5059
  } while (*(++file));
  DBUG_RETURN(tot_rows);
}


5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097
/*
  Is it ok to switch to a new engine for this table

  SYNOPSIS
    can_switch_engine()

  RETURN VALUE
    TRUE                  Ok
    FALSE                 Not ok

  DESCRIPTION
    Used to ensure that tables with foreign key constraints are not moved
    to engines without foreign key support.
*/

bool ha_partition::can_switch_engines()
{
  handler **file;
  DBUG_ENTER("ha_partition::can_switch_engines");
 
  file= m_file;
  do
  {
    if (!(*file)->can_switch_engines())
      DBUG_RETURN(FALSE);
  } while (*(++file));
  DBUG_RETURN(TRUE);
}


/*
  Is table cache supported

  SYNOPSIS
    table_cache_type()

*/

5098 5099 5100
uint8 ha_partition::table_cache_type()
{
  DBUG_ENTER("ha_partition::table_cache_type");
5101

5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112
  DBUG_RETURN(m_file[0]->table_cache_type());
}


/****************************************************************************
                MODULE print messages
****************************************************************************/

const char *ha_partition::index_type(uint inx)
{
  DBUG_ENTER("ha_partition::index_type");
5113

5114 5115 5116 5117
  DBUG_RETURN(m_file[0]->index_type(inx));
}


5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133
enum row_type ha_partition::get_row_type() const
{
  handler **file;
  enum row_type type= (*m_file)->get_row_type();

  for (file= m_file, file++; *file; file++)
  {
    enum row_type part_type= (*file)->get_row_type();
    if (part_type != type)
      return ROW_TYPE_NOT_USED;
  }

  return type;
}


5134 5135 5136
void ha_partition::print_error(int error, myf errflag)
{
  DBUG_ENTER("ha_partition::print_error");
5137

5138 5139
  /* Should probably look for my own errors first */
  /* monty: needs to be called for the last used partition ! */
5140 5141
  DBUG_PRINT("enter", ("error = %d", error));

5142
  if (error == HA_ERR_NO_PARTITION_FOUND)
5143 5144
  {
    char buf[100];
5145
    my_error(ER_NO_PARTITION_FOR_GIVEN_VALUE, MYF(0),
5146
             m_part_info->part_expr->null_value ? "NULL" :
5147 5148
             llstr(m_part_info->part_expr->val_int(), buf));
  }
5149 5150
  else
    m_file[0]->print_error(error, errflag);
5151 5152 5153 5154 5155 5156 5157
  DBUG_VOID_RETURN;
}


bool ha_partition::get_error_message(int error, String *buf)
{
  DBUG_ENTER("ha_partition::get_error_message");
5158

5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182
  /* Should probably look for my own errors first */
  /* monty: needs to be called for the last used partition ! */
  DBUG_RETURN(m_file[0]->get_error_message(error, buf));
}


/****************************************************************************
                MODULE handler characteristics
****************************************************************************/
/*
  If frm_error() is called then we will use this to to find out what file
  extensions exist for the storage engine. This is also used by the default
  rename_table and delete_table method in handler.cc.
*/

static const char *ha_partition_ext[]=
{
  ha_par_ext, NullS
};

const char **ha_partition::bas_ext() const
{ return ha_partition_ext; }


5183 5184
uint ha_partition::min_of_the_max_uint(
                       uint (handler::*operator_func)(void) const) const
5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231
{
  handler **file;
  uint min_of_the_max= ((*m_file)->*operator_func)();

  for (file= m_file+1; *file; file++)
  {
    uint tmp= ((*file)->*operator_func)();
    set_if_smaller(min_of_the_max, tmp);
  }
  return min_of_the_max;
}


uint ha_partition::max_supported_key_parts() const
{
  return min_of_the_max_uint(&handler::max_supported_key_parts);
}


uint ha_partition::max_supported_key_length() const
{
  return min_of_the_max_uint(&handler::max_supported_key_length);
}


uint ha_partition::max_supported_key_part_length() const
{
  return min_of_the_max_uint(&handler::max_supported_key_part_length);
}


uint ha_partition::max_supported_record_length() const
{
  return min_of_the_max_uint(&handler::max_supported_record_length);
}


uint ha_partition::max_supported_keys() const
{
  return min_of_the_max_uint(&handler::max_supported_keys);
}


uint ha_partition::extra_rec_buf_length() const
{
  handler **file;
  uint max= (*m_file)->extra_rec_buf_length();
5232

5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243
  for (file= m_file, file++; *file; file++)
    if (max < (*file)->extra_rec_buf_length())
      max= (*file)->extra_rec_buf_length();
  return max;
}


uint ha_partition::min_record_length(uint options) const
{
  handler **file;
  uint max= (*m_file)->min_record_length(options);
5244

5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255
  for (file= m_file, file++; *file; file++)
    if (max < (*file)->min_record_length(options))
      max= (*file)->min_record_length(options);
  return max;
}


/****************************************************************************
                MODULE compare records
****************************************************************************/
/*
5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272
  Compare two positions

  SYNOPSIS
    cmp_ref()
    ref1                   First position
    ref2                   Second position

  RETURN VALUE
    <0                     ref1 < ref2
    0                      Equal
    >0                     ref1 > ref2

  DESCRIPTION
    We get two references and need to check if those records are the same.
    If they belong to different partitions we decide that they are not
    the same record. Otherwise we use the particular handler to decide if
    they are the same. Sort in partition id order if not equal.
5273 5274 5275 5276 5277 5278 5279 5280
*/

int ha_partition::cmp_ref(const byte *ref1, const byte *ref2)
{
  uint part_id;
  my_ptrdiff_t diff1, diff2;
  handler *file;
  DBUG_ENTER("ha_partition::cmp_ref");
5281

5282 5283
  if ((ref1[0] == ref2[0]) && (ref1[1] == ref2[1]))
  {
5284
    part_id= uint2korr(ref1);
5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314
    file= m_file[part_id];
    DBUG_ASSERT(part_id < m_tot_parts);
    DBUG_RETURN(file->cmp_ref((ref1 + PARTITION_BYTES_IN_POS),
			      (ref2 + PARTITION_BYTES_IN_POS)));
  }
  diff1= ref2[1] - ref1[1];
  diff2= ref2[0] - ref1[0];
  if (diff1 > 0)
  {
    DBUG_RETURN(-1);
  }
  if (diff1 < 0)
  {
    DBUG_RETURN(+1);
  }
  if (diff2 > 0)
  {
    DBUG_RETURN(-1);
  }
  DBUG_RETURN(+1);
}


/****************************************************************************
                MODULE auto increment
****************************************************************************/

void ha_partition::restore_auto_increment()
{
  DBUG_ENTER("ha_partition::restore_auto_increment");
5315

5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329
  DBUG_VOID_RETURN;
}


/*
  This method is called by update_auto_increment which in turn is called
  by the individual handlers as part of write_row. We will always let
  the first handler keep track of the auto increment value for all
  partitions.
*/

ulonglong ha_partition::get_auto_increment()
{
  DBUG_ENTER("ha_partition::get_auto_increment");
5330

5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365
  DBUG_RETURN(m_file[0]->get_auto_increment());
}


/****************************************************************************
                MODULE initialise handler for HANDLER call
****************************************************************************/

void ha_partition::init_table_handle_for_HANDLER()
{
  return;
}


/****************************************************************************
                MODULE Partition Share
****************************************************************************/
/*
  Service routines for ... methods.
-------------------------------------------------------------------------
  Variables for partition share methods. A hash used to track open tables.
  A mutex for the hash table and an init variable to check if hash table
  is initialised.
  There is also a constant ending of the partition handler file name.
*/

#ifdef NOT_USED
static HASH partition_open_tables;
static pthread_mutex_t partition_mutex;
static int partition_init= 0;


/*
  Function we use in the creation of our hash to get key.
*/
5366

5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464
static byte *partition_get_key(PARTITION_SHARE *share, uint *length,
			       my_bool not_used __attribute__ ((unused)))
{
  *length= share->table_name_length;
  return (byte *) share->table_name;
}

/*
  Example of simple lock controls. The "share" it creates is structure we
  will pass to each partition handler. Do you have to have one of these?
  Well, you have pieces that are used for locking, and they are needed to
  function.
*/

static PARTITION_SHARE *get_share(const char *table_name, TABLE *table)
{
  PARTITION_SHARE *share;
  uint length;
  char *tmp_name;

  /*
    So why does this exist? There is no way currently to init a storage
    engine.
    Innodb and BDB both have modifications to the server to allow them to
    do this. Since you will not want to do this, this is probably the next
    best method.
  */
  if (!partition_init)
  {
    /* Hijack a mutex for init'ing the storage engine */
    pthread_mutex_lock(&LOCK_mysql_create_db);
    if (!partition_init)
    {
      partition_init++;
      VOID(pthread_mutex_init(&partition_mutex, MY_MUTEX_INIT_FAST));
      (void) hash_init(&partition_open_tables, system_charset_info, 32, 0, 0,
		       (hash_get_key) partition_get_key, 0, 0);
    }
    pthread_mutex_unlock(&LOCK_mysql_create_db);
  }
  pthread_mutex_lock(&partition_mutex);
  length= (uint) strlen(table_name);

  if (!(share= (PARTITION_SHARE *) hash_search(&partition_open_tables,
					       (byte *) table_name, length)))
  {
    if (!(share= (PARTITION_SHARE *)
	  my_multi_malloc(MYF(MY_WME | MY_ZEROFILL),
			  &share, sizeof(*share),
			  &tmp_name, length + 1, NullS)))
    {
      pthread_mutex_unlock(&partition_mutex);
      return NULL;
    }

    share->use_count= 0;
    share->table_name_length= length;
    share->table_name= tmp_name;
    strmov(share->table_name, table_name);
    if (my_hash_insert(&partition_open_tables, (byte *) share))
      goto error;
    thr_lock_init(&share->lock);
    pthread_mutex_init(&share->mutex, MY_MUTEX_INIT_FAST);
  }
  share->use_count++;
  pthread_mutex_unlock(&partition_mutex);

  return share;

error:
  pthread_mutex_unlock(&partition_mutex);
  my_free((gptr) share, MYF(0));

  return NULL;
}


/*
  Free lock controls. We call this whenever we close a table. If the table
  had the last reference to the share then we free memory associated with
  it.
*/

static int free_share(PARTITION_SHARE *share)
{
  pthread_mutex_lock(&partition_mutex);
  if (!--share->use_count)
  {
    hash_delete(&partition_open_tables, (byte *) share);
    thr_lock_delete(&share->lock);
    pthread_mutex_destroy(&share->mutex);
    my_free((gptr) share, MYF(0));
  }
  pthread_mutex_unlock(&partition_mutex);

  return 0;
}
#endif /* NOT_USED */