~drizzle-trunk/drizzle/development

1 by brian
clean slate
1
/* Copyright (C) 2003 MySQL AB
2
3
   This program is free software; you can redistribute it and/or modify
4
   it under the terms of the GNU General Public License as published by
5
   the Free Software Foundation; version 2 of the License.
6
7
   This program is distributed in the hope that it will be useful,
8
   but WITHOUT ANY WARRANTY; without even the implied warranty of
9
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
   GNU General Public License for more details.
11
12
   You should have received a copy of the GNU General Public License
13
   along with this program; if not, write to the Free Software
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
15
16
#include <sys/types.h>
17
#include <sys/stat.h>
18
#include "transparent_file.h"
19
20
#define DEFAULT_CHAIN_LENGTH 512
21
/*
22
  Version for file format.
23
  1 - Initial Version. That is, the version when the metafile was introduced.
24
*/
25
26
#define TINA_VERSION 1
27
28
typedef struct st_tina_share {
29
  char *table_name;
30
  char data_file_name[FN_REFLEN];
31
  uint table_name_length, use_count;
32
  /*
33
    Below flag is needed to make log tables work with concurrent insert.
34
    For more details see comment to ha_tina::update_status.
35
  */
36
  my_bool is_log_table;
37
  /*
38
    Here we save the length of the file for readers. This is updated by
39
    inserts, updates and deletes. The var is initialized along with the
40
    share initialization.
41
  */
42
  off_t saved_data_file_length;
43
  pthread_mutex_t mutex;
44
  THR_LOCK lock;
45
  bool update_file_opened;
46
  bool tina_write_opened;
47
  File meta_file;           /* Meta file we use */
48
  File tina_write_filedes;  /* File handler for readers */
49
  bool crashed;             /* Meta file is crashed */
50
  ha_rows rows_recorded;    /* Number of rows in tables */
51
  uint data_file_version;   /* Version of the data file used */
52
} TINA_SHARE;
53
54
struct tina_set {
55
  off_t begin;
56
  off_t end;
57
};
58
59
class ha_tina: public handler
60
{
61
  THR_LOCK_DATA lock;      /* MySQL lock */
62
  TINA_SHARE *share;       /* Shared lock info */
63
  off_t current_position;  /* Current position in the file during a file scan */
64
  off_t next_position;     /* Next position in the file scan */
65
  off_t local_saved_data_file_length; /* save position for reads */
66
  off_t temp_file_length;
67
  uchar byte_buffer[IO_SIZE];
68
  Transparent_file *file_buff;
69
  File data_file;                   /* File handler for readers */
70
  File update_temp_file;
71
  String buffer;
72
  /*
73
    The chain contains "holes" in the file, occured because of
74
    deletes/updates. It is used in rnd_end() to get rid of them
75
    in the end of the query.
76
  */
77
  tina_set chain_buffer[DEFAULT_CHAIN_LENGTH];
78
  tina_set *chain;
79
  tina_set *chain_ptr;
80
  uchar chain_alloced;
205 by Brian Aker
uint32 -> uin32_t
81
  uint32_t chain_size;
1 by brian
clean slate
82
  uint local_data_file_version;  /* Saved version of the data file used */
83
  bool records_is_known;
84
  MEM_ROOT blobroot;
85
86
private:
87
  bool get_write_pos(off_t *end_pos, tina_set *closest_hole);
88
  int open_update_temp_file_if_needed();
89
  int init_tina_writer();
90
  int init_data_file();
91
92
public:
93
  ha_tina(handlerton *hton, TABLE_SHARE *table_arg);
94
  ~ha_tina()
95
  {
96
    if (chain_alloced)
97
      my_free(chain, 0);
98
    if (file_buff)
99
      delete file_buff;
100
  }
77.1.6 by Monty Taylor
CSV is clean.
101
  const char *table_type(void) const { return "CSV"; }
212.1.3 by Monty Taylor
Renamed __attribute__((__unused__)) to __attribute__((unused)).
102
  const char *index_type(uint inx __attribute__((unused)))
77.1.6 by Monty Taylor
CSV is clean.
103
  { return "NONE"; }
1 by brian
clean slate
104
  const char **bas_ext() const;
105
  uint64_t table_flags() const
106
  {
107
    return (HA_NO_TRANSACTIONS | HA_REC_NOT_IN_SEQ | HA_NO_AUTO_INCREMENT |
108
            HA_BINLOG_ROW_CAPABLE | HA_BINLOG_STMT_CAPABLE);
109
  }
212.1.3 by Monty Taylor
Renamed __attribute__((__unused__)) to __attribute__((unused)).
110
  uint32_t index_flags(uint idx __attribute__((unused)),
111
                       uint part __attribute__((unused)),
112
                       bool all_parts __attribute__((unused))) const
1 by brian
clean slate
113
  {
114
    /*
115
      We will never have indexes so this will never be called(AKA we return
116
      zero)
117
    */
118
    return 0;
119
  }
120
  uint max_record_length() const { return HA_MAX_REC_LENGTH; }
121
  uint max_keys()          const { return 0; }
122
  uint max_key_parts()     const { return 0; }
123
  uint max_key_length()    const { return 0; }
124
  /*
125
     Called in test_quick_select to determine if indexes should be used.
126
   */
127
  virtual double scan_time() { return (double) (stats.records+stats.deleted) / 20.0+10; }
128
  /* The next method will never be called */
129
  virtual bool fast_key_read() { return 1;}
130
  /* 
131
    TODO: return actual upper bound of number of records in the table.
132
    (e.g. save number of records seen on full table scan and/or use file size
133
    as upper bound)
134
  */
135
  ha_rows estimate_rows_upper_bound() { return HA_POS_ERROR; }
136
137
  int open(const char *name, int mode, uint open_options);
138
  int close(void);
139
  int write_row(uchar * buf);
140
  int update_row(const uchar * old_data, uchar * new_data);
141
  int delete_row(const uchar * buf);
142
  int rnd_init(bool scan=1);
143
  int rnd_next(uchar *buf);
144
  int rnd_pos(uchar * buf, uchar *pos);
145
  bool check_and_repair(THD *thd);
146
  int check(THD* thd, HA_CHECK_OPT* check_opt);
147
  bool is_crashed() const;
148
  int rnd_end();
149
  int repair(THD* thd, HA_CHECK_OPT* check_opt);
150
  /* This is required for SQL layer to know that we support autorepair */
151
  bool auto_repair() const { return 1; }
152
  void position(const uchar *record);
153
  int info(uint);
154
  int delete_all_rows(void);
155
  int create(const char *name, TABLE *form, HA_CREATE_INFO *create_info);
156
  bool check_if_incompatible_data(HA_CREATE_INFO *info,
157
                                  uint table_changes);
158
159
  THR_LOCK_DATA **store_lock(THD *thd, THR_LOCK_DATA **to,
160
      enum thr_lock_type lock_type);
161
162
  /*
163
    These functions used to get/update status of the handler.
164
    Needed to enable concurrent inserts.
165
  */
166
  void get_status();
167
  void update_status();
168
169
  /* The following methods were added just for TINA */
170
  int encode_quote(uchar *buf);
171
  int find_current_row(uchar *buf);
172
  int chain_append();
173
};
174