~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/table_share.h

  • Committer: Brian Aker
  • Date: 2010-06-15 13:37:26 UTC
  • Revision ID: brian@gaz-20100615133726-160z0ql8gxi8s3rg
This is a rollup set of patches for modifications to TableIdentifier to have
a getKey() which is now used for open_cache and table_share_def cache. This
also includes additional unittests on TableIdentifier, and makes use of some
cleanup, though not perfect, to TableShare for its contructors.

Show diffs side-by-side

added added

removed removed

Lines of Context:
28
28
 
29
29
#include <string>
30
30
 
31
 
#include <drizzled/unordered_map.h>
 
31
#include <boost/unordered_map.hpp>
32
32
 
33
33
#include "drizzled/typelib.h"
34
34
#include "drizzled/my_hash.h"
38
38
namespace drizzled
39
39
{
40
40
 
41
 
typedef unordered_map<std::string, TableShare *> TableDefinitionCache;
 
41
typedef boost::unordered_map< TableIdentifier::Key, TableShare *> TableDefinitionCache;
42
42
 
43
43
const static std::string STANDARD_STRING("STANDARD");
44
44
const static std::string TEMPORARY_STRING("TEMPORARY");
56
56
{
57
57
  typedef std::vector<std::string> StringVector;
58
58
public:
59
 
  TableShare(TableIdentifier::Type type_arg= message::Table::STANDARD);
 
59
  TableShare(TableIdentifier::Type type_arg);
 
60
 
 
61
  TableShare(TableIdentifier &identifier, const TableIdentifier::Key &key); // Used by placeholder
 
62
 
 
63
  TableShare(TableIdentifier &identifier); // Just used during createTable()
60
64
 
61
65
  TableShare(TableIdentifier::Type type_arg,
62
 
             const char *key,
63
 
             uint32_t key_length,
64
 
             const char *new_table_name,
65
 
             const char *new_path);
66
 
 
67
 
  TableShare(TableIdentifier &identifier);
68
 
 
69
 
  TableShare(TableIdentifier::Type type_arg, char *key, uint32_t key_length, char *path_arg= NULL, uint32_t path_length_arg= 0);
 
66
             TableIdentifier &identifier,
 
67
             char *path_arg= NULL, uint32_t path_length_arg= 0); // Shares for cache
70
68
 
71
69
  ~TableShare() 
72
70
  {
274
272
    To ensure this one can use set_table_cache() methods.
275
273
  */
276
274
private:
277
 
  LEX_STRING table_cache_key;                        /* Pointer to db */
 
275
  TableIdentifier::Key private_key_for_cache; // This will not exist in the final design.
 
276
  std::vector<char> private_normalized_path; // This will not exist in the final design.
278
277
  LEX_STRING db;                        /* Pointer to db */
279
278
  LEX_STRING table_name;                /* Table name (for open) */
280
279
  LEX_STRING path;      /* Path to table (from datadir) */
291
290
    return path.str;
292
291
  }
293
292
 
294
 
  const char *getCacheKey() const
 
293
  const char *getCacheKey() const // This should never be called when we aren't looking at a cache.
295
294
  {
296
 
    return table_cache_key.str;
 
295
    assert(private_key_for_cache.size());
 
296
    return &private_key_for_cache[0];
297
297
  }
298
298
 
299
299
  size_t getCacheKeySize() const
300
300
  {
301
 
    return table_cache_key.length;
 
301
    return private_key_for_cache.size();
302
302
  }
303
303
 
304
304
  void setPath(char *str_arg, uint32_t size_arg)
323
323
    return table_name.length;
324
324
  }
325
325
 
326
 
  const char *getTableCacheKey() const
327
 
  {
328
 
    return table_cache_key.str;
329
 
  }
330
 
 
331
326
  const std::string &getTableName(std::string &name_arg) const
332
327
  {
333
328
    name_arg.clear();
537
532
   * only supports a single-column primary key. Is there a better way
538
533
   * to ask for the fields which are in a primary key?
539
534
 */
 
535
private:
540
536
  uint32_t primary_key;
 
537
public:
 
538
 
 
539
  uint32_t getPrimaryKey() const
 
540
  {
 
541
    return primary_key;
 
542
  }
 
543
 
 
544
  bool hasPrimaryKey() const
 
545
  {
 
546
    return primary_key != MAX_KEY;
 
547
  }
 
548
 
541
549
  /* Index of auto-updated TIMESTAMP field in field array */
542
550
  uint32_t next_number_index;               /* autoincrement key number */
543
551
  uint32_t next_number_key_offset;          /* autoinc keypart offset in a key */
591
599
  }
592
600
  
593
601
  /*
594
 
    Set share's table cache key and update its db and table name appropriately.
 
602
    Set share's identifier information.
595
603
 
596
604
    SYNOPSIS
597
 
    set_table_cache_key()
598
 
    key_buff    Buffer with already built table cache key to be
599
 
    referenced from share.
600
 
    key_length  Key length.
 
605
    setIdentifier()
601
606
 
602
607
    NOTES
603
 
    Since 'key_buff' buffer will be referenced from share it should has same
604
 
    life-time as share itself.
605
 
    This method automatically ensures that TableShare::table_name/db have
606
 
    appropriate values by using table cache key as their source.
607
608
  */
608
609
 
609
 
  void set_table_cache_key(char *key_buff, uint32_t key_length, uint32_t db_length= 0, uint32_t table_name_length= 0)
610
 
  {
611
 
    table_cache_key.str= key_buff;
612
 
    table_cache_key.length= key_length;
613
 
    /*
614
 
      Let us use the fact that the key is "db/0/table_name/0" + optional
615
 
      part for temporary tables.
616
 
    */
617
 
    db.str=            table_cache_key.str;
618
 
    db.length=         db_length ? db_length : strlen(db.str);
619
 
    table_name.str=    db.str + db.length + 1;
620
 
    table_name.length= table_name_length ? table_name_length :strlen(table_name.str);
621
 
  }
 
610
  void setIdentifier(TableIdentifier &identifier_arg);
622
611
 
623
612
  inline bool honor_global_locks()
624
613
  {
639
628
    path        Path to table (possible in lower case)
640
629
 
641
630
    NOTES
642
 
    This is different from alloc_table_share() because temporary tables
643
 
    don't have to be shared between threads or put into the table def
644
 
    cache, so we can do some things notable simpler and faster
645
 
 
646
 
    If table is not put in session->temporary_tables (happens only when
647
 
    one uses OPEN TEMPORARY) then one can specify 'db' as key and
648
 
    use key_length= 0 as neither table_cache_key or key_length will be used).
 
631
    
649
632
  */
650
633
 
651
634
private:
652
 
  void init()
653
 
  {
654
 
    init("", 0, "", "");
655
 
  }
656
 
 
657
635
  void init(const char *new_table_name,
658
 
            const char *new_path)
659
 
  {
660
 
    init("", 0, new_table_name, new_path);
661
 
  }
662
 
 
663
 
  void init(const char *key,
664
 
            uint32_t key_length, const char *new_table_name,
665
636
            const char *new_path);
666
637
public:
667
638
 
668
639
  void open_table_error(int pass_error, int db_errno, int pass_errarg);
669
640
 
670
 
  /*
671
 
    Create a table cache key
672
 
 
673
 
    SYNOPSIS
674
 
    createKey()
675
 
    key                 Create key here (must be of size MAX_DBKEY_LENGTH)
676
 
    table_list          Table definition
677
 
 
678
 
    IMPLEMENTATION
679
 
    The table cache_key is created from:
680
 
    db_name + \0
681
 
    table_name + \0
682
 
 
683
 
    if the table is a tmp table, we add the following to make each tmp table
684
 
    unique on the slave:
685
 
 
686
 
    4 bytes for master thread id
687
 
    4 bytes pseudo thread id
688
 
 
689
 
    RETURN
690
 
    Length of key
691
 
  */
692
 
  static inline uint32_t createKey(char *key, const char *db_arg, const char *table_name_arg)
693
 
  {
694
 
    uint32_t key_length;
695
 
    char *key_pos= key;
696
 
 
697
 
    key_pos= strcpy(key_pos, db_arg) + strlen(db_arg);
698
 
    key_pos= strcpy(key_pos+1, table_name_arg) +
699
 
      strlen(table_name_arg);
700
 
    key_length= (uint32_t)(key_pos-key)+1;
701
 
 
702
 
    return key_length;
703
 
  }
704
 
 
705
 
  static inline uint32_t createKey(char *key, TableIdentifier &identifier)
706
 
  {
707
 
    uint32_t key_length;
708
 
    char *key_pos= key;
709
 
 
710
 
    key_pos= strcpy(key_pos, identifier.getSchemaName().c_str()) + identifier.getSchemaName().length();
711
 
    key_pos= strcpy(key_pos + 1, identifier.getTableName().c_str()) + identifier.getTableName().length();
712
 
    key_length= (uint32_t)(key_pos-key)+1;
713
 
 
714
 
    return key_length;
715
 
  }
716
 
 
717
641
  static void cacheStart(void);
718
642
  static void cacheStop(void);
719
643
  static void release(TableShare *share);
720
 
  static void release(const char *key, uint32_t key_length);
721
 
  static TableDefinitionCache &getCache();
 
644
  static void release(TableIdentifier &identifier);
 
645
  static const TableDefinitionCache &getCache();
722
646
  static TableShare *getShare(TableIdentifier &identifier);
723
 
  static TableShare *getShare(Session *session, 
724
 
                              char *key, uint32_t key_length, int *error);
 
647
  static TableShare *getShareCreate(Session *session, 
 
648
                                    TableIdentifier &identifier,
 
649
                                    int *error);
725
650
 
726
651
  friend std::ostream& operator<<(std::ostream& output, const TableShare &share)
727
652
  {