~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
    Here we save the length of the file for readers. This is updated by
34
    inserts, updates and deletes. The var is initialized along with the
35
    share initialization.
36
  */
37
  off_t saved_data_file_length;
38
  pthread_mutex_t mutex;
39
  THR_LOCK lock;
40
  bool update_file_opened;
41
  bool tina_write_opened;
42
  File meta_file;           /* Meta file we use */
43
  File tina_write_filedes;  /* File handler for readers */
44
  bool crashed;             /* Meta file is crashed */
45
  ha_rows rows_recorded;    /* Number of rows in tables */
46
  uint data_file_version;   /* Version of the data file used */
47
} TINA_SHARE;
48
49
struct tina_set {
50
  off_t begin;
51
  off_t end;
52
};
53
54
class ha_tina: public handler
55
{
56
  THR_LOCK_DATA lock;      /* MySQL lock */
57
  TINA_SHARE *share;       /* Shared lock info */
58
  off_t current_position;  /* Current position in the file during a file scan */
59
  off_t next_position;     /* Next position in the file scan */
60
  off_t local_saved_data_file_length; /* save position for reads */
61
  off_t temp_file_length;
62
  uchar byte_buffer[IO_SIZE];
63
  Transparent_file *file_buff;
64
  File data_file;                   /* File handler for readers */
65
  File update_temp_file;
66
  String buffer;
67
  /*
68
    The chain contains "holes" in the file, occured because of
69
    deletes/updates. It is used in rnd_end() to get rid of them
70
    in the end of the query.
71
  */
72
  tina_set chain_buffer[DEFAULT_CHAIN_LENGTH];
73
  tina_set *chain;
74
  tina_set *chain_ptr;
75
  uchar chain_alloced;
205 by Brian Aker
uint32 -> uin32_t
76
  uint32_t chain_size;
1 by brian
clean slate
77
  uint local_data_file_version;  /* Saved version of the data file used */
78
  bool records_is_known;
79
  MEM_ROOT blobroot;
80
81
private:
82
  bool get_write_pos(off_t *end_pos, tina_set *closest_hole);
83
  int open_update_temp_file_if_needed();
84
  int init_tina_writer();
85
  int init_data_file();
86
87
public:
88
  ha_tina(handlerton *hton, TABLE_SHARE *table_arg);
89
  ~ha_tina()
90
  {
91
    if (chain_alloced)
92
      my_free(chain, 0);
93
    if (file_buff)
94
      delete file_buff;
95
  }
77.1.6 by Monty Taylor
CSV is clean.
96
  const char *table_type(void) const { return "CSV"; }
212.1.3 by Monty Taylor
Renamed __attribute__((__unused__)) to __attribute__((unused)).
97
  const char *index_type(uint inx __attribute__((unused)))
77.1.6 by Monty Taylor
CSV is clean.
98
  { return "NONE"; }
1 by brian
clean slate
99
  const char **bas_ext() const;
100
  uint64_t table_flags() const
101
  {
102
    return (HA_NO_TRANSACTIONS | HA_REC_NOT_IN_SEQ | HA_NO_AUTO_INCREMENT |
103
            HA_BINLOG_ROW_CAPABLE | HA_BINLOG_STMT_CAPABLE);
104
  }
212.1.3 by Monty Taylor
Renamed __attribute__((__unused__)) to __attribute__((unused)).
105
  uint32_t index_flags(uint idx __attribute__((unused)),
106
                       uint part __attribute__((unused)),
107
                       bool all_parts __attribute__((unused))) const
1 by brian
clean slate
108
  {
109
    /*
110
      We will never have indexes so this will never be called(AKA we return
111
      zero)
112
    */
113
    return 0;
114
  }
115
  uint max_record_length() const { return HA_MAX_REC_LENGTH; }
116
  uint max_keys()          const { return 0; }
117
  uint max_key_parts()     const { return 0; }
118
  uint max_key_length()    const { return 0; }
119
  /*
120
     Called in test_quick_select to determine if indexes should be used.
121
   */
122
  virtual double scan_time() { return (double) (stats.records+stats.deleted) / 20.0+10; }
123
  /* The next method will never be called */
124
  virtual bool fast_key_read() { return 1;}
125
  /* 
126
    TODO: return actual upper bound of number of records in the table.
127
    (e.g. save number of records seen on full table scan and/or use file size
128
    as upper bound)
129
  */
130
  ha_rows estimate_rows_upper_bound() { return HA_POS_ERROR; }
131
132
  int open(const char *name, int mode, uint open_options);
133
  int close(void);
134
  int write_row(uchar * buf);
135
  int update_row(const uchar * old_data, uchar * new_data);
136
  int delete_row(const uchar * buf);
137
  int rnd_init(bool scan=1);
138
  int rnd_next(uchar *buf);
139
  int rnd_pos(uchar * buf, uchar *pos);
140
  bool check_and_repair(THD *thd);
141
  int check(THD* thd, HA_CHECK_OPT* check_opt);
142
  bool is_crashed() const;
143
  int rnd_end();
144
  int repair(THD* thd, HA_CHECK_OPT* check_opt);
145
  /* This is required for SQL layer to know that we support autorepair */
146
  bool auto_repair() const { return 1; }
147
  void position(const uchar *record);
148
  int info(uint);
149
  int delete_all_rows(void);
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
150
  int create(const char *name, Table *form, HA_CREATE_INFO *create_info);
1 by brian
clean slate
151
  bool check_if_incompatible_data(HA_CREATE_INFO *info,
152
                                  uint table_changes);
153
154
  THR_LOCK_DATA **store_lock(THD *thd, THR_LOCK_DATA **to,
155
      enum thr_lock_type lock_type);
156
157
  /*
158
    These functions used to get/update status of the handler.
159
    Needed to enable concurrent inserts.
160
  */
161
  void get_status();
162
  void update_status();
163
164
  /* The following methods were added just for TINA */
165
  int encode_quote(uchar *buf);
166
  int find_current_row(uchar *buf);
167
  int chain_append();
168
};
169