~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/haildb/haildb_engine.cc

  • Committer: Brian Aker
  • Date: 2011-02-17 10:09:00 UTC
  • mfrom: (2173.2.1 clean-include-usuage)
  • Revision ID: brian@tangent.org-20110217100900-4tpuxxzdl1sj00sh
Merge Monty for headers.

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
 
22
22
/*****************************************************************************
23
23
 
24
 
Copyright (c) 2000, 2009, MySQL AB & Innobase Oy. All Rights Reserved.
25
 
Copyright (c) 2008, 2009 Google Inc.
 
24
Copyright (C) 2000, 2009, MySQL AB & Innobase Oy. All Rights Reserved.
 
25
Copyright (C) 2008, 2009 Google Inc.
26
26
 
27
27
Portions of this file contain modifications contributed and copyrighted by
28
28
Google, Inc. Those modifications are gratefully acknowledged and are described
45
45
*****************************************************************************/
46
46
/***********************************************************************
47
47
 
48
 
Copyright (c) 1995, 2009, Innobase Oy. All Rights Reserved.
49
 
Copyright (c) 2009, Percona Inc.
 
48
Copyright (C) 1995, 2009, Innobase Oy. All Rights Reserved.
 
49
Copyright (C) 2009, Percona Inc.
50
50
 
51
51
Portions of this file contain modifications contributed and copyrighted
52
52
by Percona Inc.. Those modifications are
71
71
***********************************************************************/
72
72
 
73
73
 
74
 
#include "config.h"
 
74
#include <config.h>
75
75
#include <drizzled/table.h>
76
76
#include <drizzled/error.h>
77
 
#include "drizzled/internal/my_pthread.h"
 
77
#include <drizzled/internal/my_pthread.h>
78
78
#include <drizzled/plugin/transactional_storage_engine.h>
79
79
#include <drizzled/plugin/error_message.h>
80
80
 
88
88
#include <map>
89
89
#include <fstream>
90
90
#include <drizzled/message/table.pb.h>
91
 
#include "drizzled/internal/m_string.h"
 
91
#include <drizzled/internal/m_string.h>
92
92
 
93
 
#include "drizzled/global_charset_info.h"
 
93
#include <drizzled/global_charset_info.h>
94
94
 
95
95
#include "haildb_datadict_dump_func.h"
96
96
#include "config_table_function.h"
101
101
#include "haildb_engine.h"
102
102
 
103
103
#include <drizzled/field.h>
104
 
#include "drizzled/field/timestamp.h" // needed for UPDATE NOW()
105
 
#include "drizzled/field/blob.h"
106
 
#include "drizzled/field/enum.h"
 
104
#include <drizzled/field/blob.h>
 
105
#include <drizzled/field/enum.h>
107
106
#include <drizzled/session.h>
108
 
#include <boost/program_options.hpp>
109
107
#include <drizzled/module/option_map.h>
 
108
#include <drizzled/charset.h>
 
109
#include <drizzled/current_session.h>
 
110
 
110
111
#include <iostream>
111
 
#include <drizzled/charset.h>
112
112
 
113
113
namespace po= boost::program_options;
 
114
#include <boost/program_options.hpp>
114
115
#include <boost/algorithm/string.hpp>
115
116
 
116
117
using namespace std;
117
118
using namespace google;
118
119
using namespace drizzled;
119
120
 
120
 
int read_row_from_haildb(unsigned char* buf, ib_crsr_t cursor, ib_tpl_t tuple, Table* table, bool has_hidden_primary_key, uint64_t *hidden_pkey, drizzled::memory::Root **blobroot= NULL);
 
121
int read_row_from_haildb(Session *session, unsigned char* buf, ib_crsr_t cursor, ib_tpl_t tuple, Table* table, bool has_hidden_primary_key, uint64_t *hidden_pkey, drizzled::memory::Root **blobroot= NULL);
121
122
static void fill_ib_search_tpl_from_drizzle_key(ib_tpl_t search_tuple,
122
123
                                                const drizzled::KeyInfo *key_info,
123
124
                                                const unsigned char *key_ptr,
166
167
 
167
168
  int doCreateTable(Session&,
168
169
                    Table& table_arg,
169
 
                    const drizzled::TableIdentifier &identifier,
 
170
                    const drizzled::identifier::Table &identifier,
170
171
                    drizzled::message::Table& proto);
171
172
 
172
 
  int doDropTable(Session&, const TableIdentifier &identifier);
 
173
  int doDropTable(Session&, const identifier::Table &identifier);
173
174
 
174
175
  int doRenameTable(drizzled::Session&,
175
 
                    const drizzled::TableIdentifier&,
176
 
                    const drizzled::TableIdentifier&);
 
176
                    const drizzled::identifier::Table&,
 
177
                    const drizzled::identifier::Table&);
177
178
 
178
179
  int doGetTableDefinition(Session& session,
179
 
                           const TableIdentifier &identifier,
 
180
                           const identifier::Table &identifier,
180
181
                           drizzled::message::Table &table_proto);
181
182
 
182
 
  bool doDoesTableExist(Session&, const TableIdentifier &identifier);
 
183
  bool doDoesTableExist(Session&, const identifier::Table &identifier);
183
184
 
184
185
private:
185
 
  void getTableNamesInSchemaFromHailDB(const drizzled::SchemaIdentifier &schema,
 
186
  void getTableNamesInSchemaFromHailDB(const drizzled::identifier::Schema &schema,
186
187
                                       drizzled::plugin::TableNameList *set_of_names,
187
 
                                       drizzled::TableIdentifier::vector *identifiers);
 
188
                                       drizzled::identifier::Table::vector *identifiers);
188
189
 
189
190
public:
190
191
  void doGetTableIdentifiers(drizzled::CachedDirectory &,
191
 
                             const drizzled::SchemaIdentifier &schema,
192
 
                             drizzled::TableIdentifier::vector &identifiers);
 
192
                             const drizzled::identifier::Schema &schema,
 
193
                             drizzled::identifier::Table::vector &identifiers);
193
194
 
194
195
  /* The following defines can be increased if necessary */
195
196
  uint32_t max_supported_keys()          const { return 1000; }
240
241
/* This is a superset of the map from innobase plugin.
241
242
   Unlike innobase plugin we don't act on errors here, we just
242
243
   map error codes. */
243
 
static int ib_err_t_to_drizzle_error(ib_err_t err)
 
244
static int ib_err_t_to_drizzle_error(Session* session, ib_err_t err)
244
245
{
245
246
  switch (err)
246
247
  {
270
271
    return HA_ERR_NO_ACTIVE_RECORD;
271
272
 
272
273
  case DB_DEADLOCK:
 
274
    /* HailDB will roll back a transaction itself due to DB_DEADLOCK.
 
275
       This means we have to tell Drizzle about it */
 
276
    session->markTransactionForRollback(true);
273
277
    return HA_ERR_LOCK_DEADLOCK;
274
278
 
275
279
  case DB_LOCK_WAIT_TIMEOUT:
 
280
    session->markTransactionForRollback(false);
276
281
    return HA_ERR_LOCK_WAIT_TIMEOUT;
277
282
 
278
283
  case DB_NO_REFERENCED_ROW:
351
356
  (void)options;
352
357
 
353
358
  transaction= get_trx(session);
354
 
  isolation_level= tx_isolation_to_ib_trx_level((enum_tx_isolation)session_tx_isolation(session));
 
359
  isolation_level= tx_isolation_to_ib_trx_level(session->getTxIsolation());
355
360
  *transaction= ib_trx_begin(isolation_level);
356
361
 
357
362
  return *transaction == NULL;
388
393
  err= ib_savepoint_rollback(*transaction, savepoint.getName().c_str(),
389
394
                             savepoint.getName().length());
390
395
 
391
 
  return ib_err_t_to_drizzle_error(err);
 
396
  return ib_err_t_to_drizzle_error(session, err);
392
397
}
393
398
 
394
399
int HailDBEngine::doReleaseSavepoint(Session* session,
400
405
  err= ib_savepoint_release(*transaction, savepoint.getName().c_str(),
401
406
                            savepoint.getName().length());
402
407
  if (err != DB_SUCCESS)
403
 
    return ib_err_t_to_drizzle_error(err);
 
408
    return ib_err_t_to_drizzle_error(session, err);
404
409
 
405
410
  return 0;
406
411
}
410
415
  ib_err_t err;
411
416
  ib_trx_t *transaction= get_trx(session);
412
417
 
413
 
  if (all || (!session_test_options(session, OPTION_NOT_AUTOCOMMIT | OPTION_BEGIN)))
 
418
  if (all)
414
419
  {
415
420
    err= ib_trx_commit(*transaction);
416
421
 
417
422
    if (err != DB_SUCCESS)
418
 
      return ib_err_t_to_drizzle_error(err);
 
423
      return ib_err_t_to_drizzle_error(session, err);
419
424
 
420
425
    *transaction= NULL;
421
426
  }
428
433
  ib_err_t err;
429
434
  ib_trx_t *transaction= get_trx(session);
430
435
 
431
 
  if (all || !session_test_options(session, OPTION_NOT_AUTOCOMMIT | OPTION_BEGIN))
 
436
  if (all)
432
437
  {
433
 
    err= ib_trx_rollback(*transaction);
 
438
    if (ib_trx_state(*transaction) == IB_TRX_NOT_STARTED)
 
439
      err= ib_trx_release(*transaction);
 
440
    else
 
441
      err= ib_trx_rollback(*transaction);
434
442
 
435
443
    if (err != DB_SUCCESS)
436
 
      return ib_err_t_to_drizzle_error(err);
 
444
      return ib_err_t_to_drizzle_error(session, err);
437
445
 
438
446
    *transaction= NULL;
439
447
  }
440
448
  else
441
449
  {
 
450
    if (ib_trx_state(*transaction) == IB_TRX_NOT_STARTED)
 
451
      return 0;
 
452
 
442
453
    err= ib_savepoint_rollback(*transaction, statement_savepoint_name.c_str(),
443
454
                               statement_savepoint_name.length());
444
455
    if (err != DB_SUCCESS)
445
 
      return ib_err_t_to_drizzle_error(err);
 
456
      return ib_err_t_to_drizzle_error(session, err);
446
457
  }
447
458
 
448
459
  return 0;
486
497
  else
487
498
  {
488
499
    assert (err == DB_SUCCESS);
489
 
    err= ib_tuple_read_u64(tuple, getTable()->getShare()->fields, &nr);
 
500
    err= ib_tuple_read_u64(tuple, getTable()->getShare()->sizeFields(), &nr);
490
501
    nr++;
491
502
  }
492
503
  ib_tuple_delete(tuple);
527
538
  doEndIndexScan();
528
539
  (void) extra(HA_EXTRA_NO_KEYREAD);
529
540
 
530
 
  if (getTable()->getShare()->getTableProto()->options().auto_increment_value() > nr)
531
 
    nr= getTable()->getShare()->getTableProto()->options().auto_increment_value();
 
541
  if (getTable()->getShare()->getTableMessage()->options().auto_increment_value() > nr)
 
542
    nr= getTable()->getShare()->getTableMessage()->options().auto_increment_value();
532
543
 
533
544
  return nr;
534
545
}
641
652
 
642
653
  /* the below is adapted from ha_innodb.cc */
643
654
 
644
 
  const uint32_t sql_command = session_sql_command(session);
 
655
  const uint32_t sql_command = session->getSqlCommand();
645
656
 
646
657
  if (sql_command == SQLCOM_DROP_TABLE) {
647
658
 
672
683
    unexpected if an obsolete consistent read view would be
673
684
    used. */
674
685
 
675
 
    enum_tx_isolation isolation_level= session_tx_isolation(session);
 
686
    enum_tx_isolation isolation_level= session->getTxIsolation();
676
687
 
677
688
    if (isolation_level != ISO_SERIALIZABLE
678
689
        && (lock_type == TL_READ || lock_type == TL_READ_NO_INSERT)
715
726
 
716
727
    if ((lock_type >= TL_WRITE_CONCURRENT_INSERT
717
728
         && lock_type <= TL_WRITE)
718
 
        && !session_tablespace_op(session)
 
729
        && ! session->doing_tablespace_operation()
719
730
        && sql_command != SQLCOM_TRUNCATE
720
731
        && sql_command != SQLCOM_CREATE_TABLE) {
721
732
 
792
803
  return &name[l];
793
804
}
794
805
 
795
 
static void TableIdentifier_to_haildb_name(const TableIdentifier &identifier, std::string *str)
 
806
static void TableIdentifier_to_haildb_name(const identifier::Table &identifier, std::string *str)
796
807
{
797
808
  str->assign(table_path_to_haildb_name(identifier.getPath().c_str()));
798
809
}
826
837
  ib_id_t idx_id;
827
838
 
828
839
  if (err != DB_SUCCESS)
829
 
    return ib_err_t_to_drizzle_error(err);
 
840
    return ib_err_t_to_drizzle_error(getTable()->getSession(), err);
830
841
 
831
842
  err= ib_cursor_open_table_using_id(table_id, NULL, &cursor);
832
843
  cursor_is_sec_index= false;
833
844
 
834
845
  if (err != DB_SUCCESS)
835
 
    return ib_err_t_to_drizzle_error(err);
 
846
    return ib_err_t_to_drizzle_error(getTable()->getSession(), err);
836
847
 
837
848
  err= ib_index_get_id(haildb_table_name, "HIDDEN_PRIMARY", &idx_id);
838
849
 
863
874
{
864
875
  ib_err_t err= ib_cursor_close(cursor);
865
876
  if (err != DB_SUCCESS)
866
 
    return ib_err_t_to_drizzle_error(err);
 
877
    return ib_err_t_to_drizzle_error(getTable()->getSession(), err);
867
878
 
868
879
  free_share();
869
880
 
896
907
{
897
908
  ib_col_attr_t column_attr= IB_COL_NONE;
898
909
 
899
 
  if (field.has_constraints() && ! field.constraints().is_nullable())
 
910
  if (field.has_constraints() && field.constraints().is_notnull())
900
911
    column_attr= IB_COL_NOT_NULL;
901
912
 
902
913
  switch (field.type())
927
938
    *err= ib_table_schema_add_col(schema, field.name().c_str(), IB_INT,
928
939
                                  column_attr, 0, 4);
929
940
    break;
930
 
  case message::Table::Field::TIMESTAMP:
 
941
  case message::Table::Field::EPOCH:
931
942
    *err= ib_table_schema_add_col(schema, field.name().c_str(), IB_INT,
932
943
                                  column_attr, 0, 8);
933
944
    break;
1027
1038
 
1028
1039
int HailDBEngine::doCreateTable(Session &session,
1029
1040
                                        Table& table_obj,
1030
 
                                        const drizzled::TableIdentifier &identifier,
 
1041
                                        const drizzled::identifier::Table &identifier,
1031
1042
                                        drizzled::message::Table& table_message)
1032
1043
{
1033
1044
  ib_tbl_sch_t haildb_table_schema= NULL;
1071
1082
                        ER_CANT_CREATE_TABLE,
1072
1083
                        _("Cannot create table %s. HailDB Error %d (%s)\n"),
1073
1084
                        haildb_table_name.c_str(), haildb_err, ib_strerror(haildb_err));
1074
 
    return ib_err_t_to_drizzle_error(haildb_err);
 
1085
    return ib_err_t_to_drizzle_error(&session, haildb_err);
1075
1086
  }
1076
1087
 
1077
1088
  for (int colnr= 0; colnr < table_message.field_size() ; colnr++)
1092
1103
                            " HailDB Error %d (%s)\n"),
1093
1104
                          field.name().c_str(), haildb_table_name.c_str(),
1094
1105
                          haildb_err, ib_strerror(haildb_err));
1095
 
      return ib_err_t_to_drizzle_error(haildb_err);
 
1106
      return ib_err_t_to_drizzle_error(&session, haildb_err);
1096
1107
    }
1097
1108
    if (field_err != 0)
1098
1109
      return field_err;
1240
1251
                        _("Cannot create table %s. HailDB Error %d (%s)\n"),
1241
1252
                        haildb_table_name.c_str(),
1242
1253
                        haildb_err, ib_strerror(haildb_err));
1243
 
    return ib_err_t_to_drizzle_error(haildb_err);
 
1254
    return ib_err_t_to_drizzle_error(&session, haildb_err);
1244
1255
  }
1245
1256
 
1246
1257
  return 0;
1283
1294
}
1284
1295
 
1285
1296
int HailDBEngine::doDropTable(Session &session,
1286
 
                                      const TableIdentifier &identifier)
 
1297
                                      const identifier::Table &identifier)
1287
1298
{
1288
1299
  ib_trx_t haildb_schema_transaction;
1289
1300
  ib_err_t haildb_err;
1365
1376
  return 0;
1366
1377
}
1367
1378
 
1368
 
static ib_err_t rename_table_message(ib_trx_t transaction, const TableIdentifier &from_identifier, const TableIdentifier &to_identifier)
 
1379
static ib_err_t rename_table_message(ib_trx_t transaction, const identifier::Table &from_identifier, const identifier::Table &to_identifier)
1369
1380
{
1370
1381
  ib_crsr_t cursor;
1371
1382
  ib_tpl_t search_tuple;
1456
1467
}
1457
1468
 
1458
1469
int HailDBEngine::doRenameTable(drizzled::Session &session,
1459
 
                                        const drizzled::TableIdentifier &from,
1460
 
                                        const drizzled::TableIdentifier &to)
 
1470
                                        const drizzled::identifier::Table &from,
 
1471
                                        const drizzled::identifier::Table &to)
1461
1472
{
1462
1473
  ib_trx_t haildb_schema_transaction;
1463
1474
  ib_err_t err;
1507
1518
  assert(rollback_err == DB_SUCCESS);
1508
1519
  rollback_err= ib_trx_rollback(haildb_schema_transaction);
1509
1520
  assert(rollback_err == DB_SUCCESS);
1510
 
  return ib_err_t_to_drizzle_error(err);
 
1521
  return ib_err_t_to_drizzle_error(&session, err);
1511
1522
}
1512
1523
 
1513
1524
void HailDBEngine::getTableNamesInSchemaFromHailDB(
1514
 
                                 const drizzled::SchemaIdentifier &schema,
 
1525
                                 const drizzled::identifier::Schema &schema,
1515
1526
                                 drizzled::plugin::TableNameList *set_of_names,
1516
 
                                 drizzled::TableIdentifier::vector *identifiers)
 
1527
                                 drizzled::identifier::Table::vector *identifiers)
1517
1528
{
1518
1529
  ib_trx_t   transaction;
1519
1530
  ib_crsr_t  cursor;
1543
1554
    {
1544
1555
      BOOST_FOREACH(std::string table_name, haildb_system_table_names)
1545
1556
      {
1546
 
        identifiers->push_back(TableIdentifier(schema.getSchemaName(),
 
1557
        identifiers->push_back(identifier::Table(schema.getSchemaName(),
1547
1558
                                               table_name));
1548
1559
      }
1549
1560
    }
1586
1597
      if (set_of_names)
1587
1598
        set_of_names->insert(just_table_name);
1588
1599
      if (identifiers)
1589
 
        identifiers->push_back(TableIdentifier(schema.getSchemaName(), just_table_name));
 
1600
        identifiers->push_back(identifier::Table(schema.getSchemaName(), just_table_name));
1590
1601
    }
1591
1602
 
1592
1603
 
1605
1616
}
1606
1617
 
1607
1618
void HailDBEngine::doGetTableIdentifiers(drizzled::CachedDirectory &,
1608
 
                                                 const drizzled::SchemaIdentifier &schema,
1609
 
                                                 drizzled::TableIdentifier::vector &identifiers)
 
1619
                                                 const drizzled::identifier::Schema &schema,
 
1620
                                                 drizzled::identifier::Table::vector &identifiers)
1610
1621
{
1611
1622
  getTableNamesInSchemaFromHailDB(schema, NULL, &identifiers);
1612
1623
}
1731
1742
}
1732
1743
 
1733
1744
int HailDBEngine::doGetTableDefinition(Session &session,
1734
 
                                               const TableIdentifier &identifier,
 
1745
                                               const identifier::Table &identifier,
1735
1746
                                               drizzled::message::Table &table)
1736
1747
{
1737
1748
  ib_crsr_t haildb_cursor= NULL;
1760
1771
}
1761
1772
 
1762
1773
bool HailDBEngine::doDoesTableExist(Session &,
1763
 
                                    const TableIdentifier& identifier)
 
1774
                                    const identifier::Table& identifier)
1764
1775
{
1765
1776
  ib_crsr_t haildb_cursor;
1766
1777
  string haildb_table_name;
1902
1913
    err= ib_cursor_open_table_using_id(table_id, transaction, &cursor);
1903
1914
 
1904
1915
    if (err != DB_SUCCESS)
1905
 
      return ib_err_t_to_drizzle_error(err);
 
1916
      return ib_err_t_to_drizzle_error(getTable()->getSession(), err);
1906
1917
 
1907
1918
    cursor_is_sec_index= false;
1908
1919
  }
1972
1983
 
1973
1984
  if (share->has_hidden_primary_key)
1974
1985
  {
1975
 
    err= ib_tuple_write_u64(tuple, getTable()->getShare()->fields, share->hidden_pkey_auto_increment_value.fetch_and_increment());
 
1986
    err= ib_tuple_write_u64(tuple, getTable()->getShare()->sizeFields(),
 
1987
                            share->hidden_pkey_auto_increment_value.fetch_and_increment());
1976
1988
  }
1977
1989
 
1978
1990
  err= ib_cursor_insert_row(cursor, tuple);
2010
2022
      ret= HA_ERR_FOUND_DUPP_KEY;
2011
2023
  }
2012
2024
  else if (err != DB_SUCCESS)
2013
 
    ret= ib_err_t_to_drizzle_error(err);
 
2025
    ret= ib_err_t_to_drizzle_error(getTable()->getSession(), err);
2014
2026
 
2015
2027
  tuple= ib_tuple_clear(tuple);
2016
2028
  ib_tuple_delete(tuple);
2041
2053
      err= ib_cursor_open_table_using_id(table_id, transaction, &cursor);
2042
2054
 
2043
2055
      if (err != DB_SUCCESS)
2044
 
        return ib_err_t_to_drizzle_error(err);
 
2056
        return ib_err_t_to_drizzle_error(getTable()->getSession(), err);
2045
2057
      cursor_is_sec_index= false;
2046
2058
    }
2047
2059
    else
2093
2105
 
2094
2106
  advance_cursor= true;
2095
2107
 
2096
 
  return ib_err_t_to_drizzle_error(err);
 
2108
  return ib_err_t_to_drizzle_error(getTable()->getSession(), err);
2097
2109
}
2098
2110
 
2099
2111
int HailDBCursor::doDeleteRecord(const unsigned char *)
2105
2117
 
2106
2118
  advance_cursor= true;
2107
2119
 
2108
 
  return ib_err_t_to_drizzle_error(err);
 
2120
  return ib_err_t_to_drizzle_error(getTable()->getSession(), err);
2109
2121
}
2110
2122
 
2111
2123
int HailDBCursor::delete_all_rows(void)
2114
2126
     so only support TRUNCATE and not DELETE FROM t;
2115
2127
     (this is what ha_innodb does)
2116
2128
  */
2117
 
  if (session_sql_command(getTable()->in_use) != SQLCOM_TRUNCATE)
 
2129
  if (getTable()->in_use->getSqlCommand() != SQLCOM_TRUNCATE)
2118
2130
    return HA_ERR_WRONG_COMMAND;
2119
2131
 
2120
2132
  ib_id_t id;
2130
2142
    err= ib_cursor_open_table_using_id(table_id, transaction, &cursor);
2131
2143
 
2132
2144
    if (err != DB_SUCCESS)
2133
 
      return ib_err_t_to_drizzle_error(err);
 
2145
      return ib_err_t_to_drizzle_error(getTable()->getSession(), err);
2134
2146
    cursor_is_sec_index= false;
2135
2147
  }
2136
2148
  else
2172
2184
  ib_schema_unlock(transaction);
2173
2185
  ib_err_t rollback_err= ib_trx_rollback(transaction);
2174
2186
  assert(rollback_err == DB_SUCCESS);
2175
 
  return ib_err_t_to_drizzle_error(err);
 
2187
  return ib_err_t_to_drizzle_error(getTable()->getSession(), err);
2176
2188
}
2177
2189
 
2178
2190
int HailDBCursor::doStartTableScan(bool)
2202
2214
  }
2203
2215
 
2204
2216
  if (err != DB_SUCCESS)
2205
 
    return ib_err_t_to_drizzle_error(err);
 
2217
    return ib_err_t_to_drizzle_error(getTable()->getSession(), err);
2206
2218
 
2207
2219
  err= ib_cursor_set_lock_mode(cursor, ib_lock_mode);
2208
2220
  assert(err == DB_SUCCESS); // FIXME
2212
2224
  err= ib_cursor_first(cursor);
2213
2225
  if (err != DB_SUCCESS && err != DB_END_OF_INDEX)
2214
2226
  {
2215
 
    previous_error= ib_err_t_to_drizzle_error(err);
2216
 
    err= ib_cursor_reset(cursor);
2217
 
    return previous_error;
 
2227
    int reset_err= ib_cursor_reset(cursor);
 
2228
    assert(reset_err == DB_SUCCESS);
 
2229
    return ib_err_t_to_drizzle_error(getTable()->getSession(), err);
2218
2230
  }
2219
2231
 
2220
2232
  advance_cursor= false;
2221
2233
 
2222
 
  previous_error= 0;
2223
2234
  return(0);
2224
2235
}
2225
2236
 
2226
 
int read_row_from_haildb(unsigned char* buf, ib_crsr_t cursor, ib_tpl_t tuple, Table* table, bool has_hidden_primary_key, uint64_t *hidden_pkey, drizzled::memory::Root **blobroot)
 
2237
int read_row_from_haildb(Session *session, unsigned char* buf, ib_crsr_t cursor, ib_tpl_t tuple, Table* table, bool has_hidden_primary_key, uint64_t *hidden_pkey, drizzled::memory::Root **blobroot)
2227
2238
{
2228
2239
  ib_err_t err;
2229
2240
  ptrdiff_t row_offset= buf - table->getInsertRecord();
2233
2244
  if (err == DB_RECORD_NOT_FOUND)
2234
2245
    return HA_ERR_END_OF_FILE;
2235
2246
  if (err != DB_SUCCESS)
2236
 
    return ib_err_t_to_drizzle_error(err);
 
2247
    return ib_err_t_to_drizzle_error(session, err);
2237
2248
 
2238
2249
  int colnr= 0;
2239
2250
 
2299
2310
    (**field).move_field_offset(-row_offset);
2300
2311
 
2301
2312
    if (err != DB_SUCCESS)
2302
 
      return ib_err_t_to_drizzle_error(err);
 
2313
      return ib_err_t_to_drizzle_error(session, err);
2303
2314
  }
2304
2315
 
2305
2316
  if (has_hidden_primary_key)
2307
2318
    err= ib_tuple_read_u64(tuple, colnr, hidden_pkey);
2308
2319
  }
2309
2320
 
2310
 
  return ib_err_t_to_drizzle_error(err);
 
2321
  return ib_err_t_to_drizzle_error(session, err);
2311
2322
}
2312
2323
 
2313
2324
int HailDBCursor::rnd_next(unsigned char *buf)
2315
2326
  ib_err_t err;
2316
2327
  int ret;
2317
2328
 
2318
 
  if (previous_error)
2319
 
    return previous_error;
2320
 
 
2321
2329
  if (advance_cursor)
2322
2330
  {
2323
2331
    err= ib_cursor_next(cursor);
2324
2332
    if (err != DB_SUCCESS)
2325
 
      return ib_err_t_to_drizzle_error(err);
 
2333
      return ib_err_t_to_drizzle_error(getTable()->getSession(), err);
2326
2334
  }
2327
2335
 
2328
2336
  tuple= ib_tuple_clear(tuple);
2329
 
  ret= read_row_from_haildb(buf, cursor, tuple, getTable(),
 
2337
  ret= read_row_from_haildb(getTable()->getSession(), buf, cursor, tuple,
 
2338
                            getTable(),
2330
2339
                            share->has_hidden_primary_key,
2331
2340
                            &hidden_autoinc_pkey_position);
2332
2341
 
2343
2352
  err= ib_cursor_reset(cursor);
2344
2353
  assert(err == DB_SUCCESS);
2345
2354
  in_table_scan= false;
2346
 
  previous_error= 0;
2347
 
  return ib_err_t_to_drizzle_error(err);
 
2355
  return ib_err_t_to_drizzle_error(getTable()->getSession(), err);
2348
2356
}
2349
2357
 
2350
2358
int HailDBCursor::rnd_pos(unsigned char *buf, unsigned char *pos)
2359
2367
    err= ib_col_set_value(search_tuple, 0,
2360
2368
                          ((uint64_t*)(pos)), sizeof(uint64_t));
2361
2369
    if (err != DB_SUCCESS)
2362
 
      return ib_err_t_to_drizzle_error(err);
 
2370
      return ib_err_t_to_drizzle_error(getTable()->getSession(), err);
2363
2371
  }
2364
2372
  else
2365
2373
  {
2376
2384
 
2377
2385
  err= ib_cursor_moveto(cursor, search_tuple, IB_CUR_GE, &res);
2378
2386
  if (err != DB_SUCCESS)
2379
 
    return ib_err_t_to_drizzle_error(err);
 
2387
    return ib_err_t_to_drizzle_error(getTable()->getSession(), err);
2380
2388
 
2381
2389
  assert(res==0);
2382
2390
  if (res != 0)
2387
2395
  tuple= ib_tuple_clear(tuple);
2388
2396
 
2389
2397
  if (ret == 0)
2390
 
    ret= read_row_from_haildb(buf, cursor, tuple, getTable(),
 
2398
    ret= read_row_from_haildb(getTable()->getSession(), buf, cursor, tuple,
 
2399
                              getTable(),
2391
2400
                              share->has_hidden_primary_key,
2392
2401
                              &hidden_autoinc_pkey_position);
2393
2402
 
2513
2522
 
2514
2523
    err= ib_get_duplicate_key(transaction, &err_table_name, &err_index_name);
2515
2524
 
2516
 
    errkey= -1;
 
2525
    errkey= UINT32_MAX;
2517
2526
 
2518
2527
    for (unsigned int i = 0; i < getTable()->getShare()->keys; i++)
2519
2528
    {
2526
2535
 
2527
2536
  }
2528
2537
 
 
2538
  if (flag & HA_STATUS_CONST)
 
2539
  {
 
2540
    for (unsigned int i = 0; i < getTable()->getShare()->sizeKeys(); i++)
 
2541
    {
 
2542
      const char* index_name= getTable()->key_info[i].name;
 
2543
      uint64_t ncols;
 
2544
      int64_t *n_diff;
 
2545
      ha_rows rec_per_key;
 
2546
 
 
2547
      err= ib_get_index_stat_n_diff_key_vals(cursor, index_name,
 
2548
                                             &ncols, &n_diff);
 
2549
 
 
2550
      if (err != DB_SUCCESS)
 
2551
        return ib_err_t_to_drizzle_error(getTable()->getSession(), err);
 
2552
 
 
2553
      for (unsigned int j=0; j < getTable()->key_info[i].key_parts; j++)
 
2554
      {
 
2555
        if (n_diff[j+1] == 0)
 
2556
          rec_per_key= stats.records;
 
2557
        else
 
2558
          rec_per_key= stats.records / n_diff[j+1];
 
2559
 
 
2560
        /* We import this heuristic from ha_innodb, which says
 
2561
           that MySQL favours table scans too much over index searches,
 
2562
           so we pretend our index selectivity is 2 times better. */
 
2563
 
 
2564
        rec_per_key= rec_per_key / 2;
 
2565
 
 
2566
        if (rec_per_key == 0)
 
2567
          rec_per_key= 1;
 
2568
 
 
2569
        getTable()->key_info[i].rec_per_key[j]=
 
2570
          rec_per_key >= ~(ulong) 0 ? ~(ulong) 0 :
 
2571
          (ulong) rec_per_key;
 
2572
      }
 
2573
 
 
2574
      free(n_diff);
 
2575
    }
 
2576
  }
 
2577
 
2529
2578
  return(0);
2530
2579
}
2531
2580
 
2546
2595
      err= ib_cursor_open_table_using_id(table_id, transaction, &cursor);
2547
2596
 
2548
2597
      if (err != DB_SUCCESS)
2549
 
        return ib_err_t_to_drizzle_error(err);
 
2598
        return ib_err_t_to_drizzle_error(getTable()->getSession(), err);
2550
2599
 
2551
2600
    }
2552
2601
    else
2564
2613
                         getShare()->getKeyInfo(keynr).name,
2565
2614
                         &index_id);
2566
2615
    if (err != DB_SUCCESS)
2567
 
      return ib_err_t_to_drizzle_error(err);
 
2616
      return ib_err_t_to_drizzle_error(getTable()->getSession(), err);
2568
2617
 
2569
2618
    err= ib_cursor_close(cursor);
2570
2619
    assert(err == DB_SUCCESS);
2572
2621
    err= ib_cursor_open_index_using_id(index_id, transaction, &cursor);
2573
2622
 
2574
2623
    if (err != DB_SUCCESS)
2575
 
      return ib_err_t_to_drizzle_error(err);
 
2624
      return ib_err_t_to_drizzle_error(getTable()->getSession(), err);
2576
2625
 
2577
2626
    cursor_is_sec_index= true;
2578
2627
 
2725
2774
 
2726
2775
  if (err != DB_SUCCESS)
2727
2776
  {
2728
 
    return ib_err_t_to_drizzle_error(err);
 
2777
    return ib_err_t_to_drizzle_error(getTable()->getSession(), err);
2729
2778
  }
2730
2779
 
2731
2780
  tuple= ib_tuple_clear(tuple);
2732
 
  ret= read_row_from_haildb(buf, cursor, tuple, getTable(),
 
2781
  ret= read_row_from_haildb(getTable()->getSession(), buf, cursor, tuple,
 
2782
                            getTable(),
2733
2783
                            share->has_hidden_primary_key,
2734
2784
                            &hidden_autoinc_pkey_position,
2735
2785
                            (allocate_blobs)? &blobroot : NULL);
2806
2856
  return 0;
2807
2857
}
2808
2858
 
 
2859
int HailDBCursor::analyze(Session*)
 
2860
{
 
2861
  ib_err_t err;
 
2862
 
 
2863
  err= ib_update_table_statistics(cursor);
 
2864
 
 
2865
  return ib_err_t_to_drizzle_error(getTable()->getSession(), err);
 
2866
}
 
2867
 
2809
2868
int HailDBCursor::index_next(unsigned char *buf)
2810
2869
{
2811
2870
  int ret= HA_ERR_END_OF_FILE;
2818
2877
  }
2819
2878
 
2820
2879
  tuple= ib_tuple_clear(tuple);
2821
 
  ret= read_row_from_haildb(buf, cursor, tuple, getTable(),
 
2880
  ret= read_row_from_haildb(getTable()->getSession(), buf, cursor, tuple,
 
2881
                            getTable(),
2822
2882
                            share->has_hidden_primary_key,
2823
2883
                            &hidden_autoinc_pkey_position);
2824
2884
 
2846
2906
      if (err == DB_END_OF_INDEX)
2847
2907
        return HA_ERR_END_OF_FILE;
2848
2908
      else
2849
 
        return ib_err_t_to_drizzle_error(err);
 
2909
        return ib_err_t_to_drizzle_error(getTable()->getSession(), err);
2850
2910
    }
2851
2911
  }
2852
2912
 
2853
2913
  tuple= ib_tuple_clear(tuple);
2854
 
  ret= read_row_from_haildb(buf, cursor, tuple, getTable(),
 
2914
  ret= read_row_from_haildb(getTable()->getSession(), buf, cursor, tuple,
 
2915
                            getTable(),
2855
2916
                            share->has_hidden_primary_key,
2856
2917
                            &hidden_autoinc_pkey_position);
2857
2918
 
2868
2929
 
2869
2930
  err= ib_cursor_first(cursor);
2870
2931
  if (err != DB_SUCCESS)
2871
 
    return ib_err_t_to_drizzle_error(err);
 
2932
    return ib_err_t_to_drizzle_error(getTable()->getSession(), err);
2872
2933
 
2873
2934
  tuple= ib_tuple_clear(tuple);
2874
 
  ret= read_row_from_haildb(buf, cursor, tuple, getTable(),
 
2935
  ret= read_row_from_haildb(getTable()->getSession(), buf, cursor, tuple,
 
2936
                            getTable(),
2875
2937
                            share->has_hidden_primary_key,
2876
2938
                            &hidden_autoinc_pkey_position);
2877
2939
 
2888
2950
 
2889
2951
  err= ib_cursor_last(cursor);
2890
2952
  if (err != DB_SUCCESS)
2891
 
    return ib_err_t_to_drizzle_error(err);
 
2953
    return ib_err_t_to_drizzle_error(getTable()->getSession(), err);
2892
2954
 
2893
2955
  tuple= ib_tuple_clear(tuple);
2894
 
  ret= read_row_from_haildb(buf, cursor, tuple, getTable(),
 
2956
  ret= read_row_from_haildb(getTable()->getSession(), buf, cursor, tuple,
 
2957
                            getTable(),
2895
2958
                            share->has_hidden_primary_key,
2896
2959
                            &hidden_autoinc_pkey_position);
2897
2960
  advance_cursor= true;
3084
3147
  bool r= false;
3085
3148
  va_list args;
3086
3149
  va_start(args, fmt);
3087
 
  if (! shutdown_in_progress)
3088
 
    r= plugin::ErrorMessage::vprintf(NULL, ERRMSG_LVL_WARN, fmt, args);
 
3150
  if (not shutdown_in_progress)
 
3151
  {
 
3152
    r= plugin::ErrorMessage::vprintf(error::WARN, fmt, args);
 
3153
  }
3089
3154
  else
 
3155
  {
3090
3156
    vfprintf(stderr, fmt, args);
 
3157
  }
3091
3158
  va_end(args);
3092
3159
 
3093
3160
  return (! r==true);
3472
3539
  "Transactional Storage Engine using the HailDB Library",
3473
3540
  PLUGIN_LICENSE_GPL,
3474
3541
  haildb_init,     /* Plugin Init */
3475
 
  NULL, /* system variables */
 
3542
  NULL, /* depends */
3476
3543
  init_options                /* config options   */
3477
3544
}
3478
3545
DRIZZLE_DECLARE_PLUGIN_END;