Drizzled Public API Documentation

singular.cc
1 /* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3  *
4  * Copyright (C) 2010 Brian Aker
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #include <config.h>
22 
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <fcntl.h>
26 
27 #include <drizzled/session.h>
28 #include <plugin/myisam/myisam.h>
29 #include <drizzled/plugin/transactional_storage_engine.h>
30 #include <drizzled/statistics_variables.h>
31 #include <drizzled/table.h>
32 #include <drizzled/create_field.h>
33 
34 namespace drizzled {
35 namespace table {
36 
37 Singular::Singular(Session *session, std::list<CreateField>& field_list) :
38  _share(message::Table::INTERNAL),
39  _has_variable_width(false)
40 {
41  uint32_t field_count= field_list.size();
42  uint32_t blob_count= 0;
43  uint32_t record_length= 0;
44  uint32_t null_count= 0; /* number of columns which may be null */
45  uint32_t null_pack_length; /* NULL representation array length */
46 
47  getMutableShare()->setFields(field_count + 1);
48  setFields(getMutableShare()->getFields(true));
49  Field** field_arg= getMutableShare()->getFields(true);
50  getMutableShare()->blob_field.resize(field_count+1);
51  getMutableShare()->setFieldSize(field_count);
52  getMutableShare()->blob_ptr_size= portable_sizeof_char_ptr;
53  setup_tmp_table_column_bitmaps();
54 
55  in_use= session; /* field_arg->reset() may access in_use */
56 
57  /* Create all fields and calculate the total length of record */
58  message::Table::Field null_field;
59  BOOST_FOREACH(CreateField& it, field_list)
60  {
61  *field_arg= getMutableShare()->make_field(null_field,
62  NULL,
63  it.length,
64  (it.flags & NOT_NULL_FLAG) ? false : true,
65  (unsigned char *) ((it.flags & NOT_NULL_FLAG) ? 0 : ""),
66  (it.flags & NOT_NULL_FLAG) ? 0 : 1,
67  it.decimals,
68  it.sql_type,
69  it.charset,
70  it.unireg_check,
71  it.interval,
72  it.field_name,
73  it.flags & UNSIGNED_FLAG ? true : false);
74  (*field_arg)->init(this);
75  record_length+= (*field_arg)->pack_length();
76  if (! ((*field_arg)->flags & NOT_NULL_FLAG))
77  null_count++;
78 
79  if ((*field_arg)->flags & BLOB_FLAG)
80  getMutableShare()->blob_field[blob_count++]= (uint32_t) (field_arg - getFields());
81 
82  field_arg++;
83  }
84  *field_arg= NULL; /* mark the end of the list */
85  getMutableShare()->blob_field[blob_count]= 0; /* mark the end of the list */
86  getMutableShare()->blob_fields= blob_count;
87 
88  null_pack_length= (null_count + 7)/8;
89  getMutableShare()->setRecordLength(record_length + null_pack_length);
90  getMutableShare()->rec_buff_length= ALIGN_SIZE(getMutableShare()->getRecordLength() + 1);
91  record[0]= session->mem.alloc(getMutableShare()->rec_buff_length);
92 
93  if (null_pack_length)
94  {
95  null_flags= getInsertRecord();
96  getMutableShare()->null_fields= null_count;
97  getMutableShare()->null_bytes= null_pack_length;
98  }
99  {
100  /* Set up field pointers */
101  unsigned char *null_pos= getInsertRecord();
102  unsigned char *field_pos= null_pos + getMutableShare()->null_bytes;
103  uint32_t null_bit= 1;
104 
105  for (field_arg= getFields(); *field_arg; ++field_arg)
106  {
107  Field *cur_field= *field_arg;
108  if ((cur_field->flags & NOT_NULL_FLAG))
109  cur_field->move_field(field_pos);
110  else
111  {
112  cur_field->move_field(field_pos, (unsigned char*) null_pos, null_bit);
113  null_bit<<= 1;
114  if (null_bit == (1 << 8))
115  {
116  ++null_pos;
117  null_bit= 1;
118  }
119  }
120  cur_field->reset();
121 
122  field_pos+= cur_field->pack_length();
123  }
124  }
125 }
126 
127 bool Singular::open_tmp_table()
128 {
129  if (int error= cursor->ha_open(getShare()->getTableIdentifier(), O_RDWR, HA_OPEN_TMP_TABLE | HA_OPEN_INTERNAL_TABLE))
130  {
131  print_error(error, MYF(0));
132  db_stat= 0;
133  return true;
134  }
135  (void) cursor->extra(HA_EXTRA_QUICK); /* Faster */
136  return false;
137 }
138 
139 
140 /*
141  Create MyISAM temporary table
142 
143  SYNOPSIS
144  create_myisam_tmp_table()
145  keyinfo Description of the index (there is always one index)
146  start_recinfo MyISAM's column descriptions
147  recinfo INOUT End of MyISAM's column descriptions
148  options Option bits
149 
150  DESCRIPTION
151  Create a MyISAM temporary table according to passed description. The is
152  assumed to have one unique index or constraint.
153 
154  The passed array or MI_COLUMNDEF structures must have this form:
155 
156  1. 1-byte column (afaiu for 'deleted' flag) (note maybe not 1-byte
157  when there are many nullable columns)
158  2. Table columns
159  3. One free MI_COLUMNDEF element (*recinfo points here)
160 
161  This function may use the free element to create hash column for unique
162  constraint.
163 
164  RETURN
165  false - OK
166  true - Error
167 */
168 
169 bool Singular::create_myisam_tmp_table(KeyInfo *keyinfo,
170  MI_COLUMNDEF *start_recinfo,
171  MI_COLUMNDEF **recinfo,
172  uint64_t options)
173 {
174  int error;
175  MI_KEYDEF keydef;
176  MI_UNIQUEDEF uniquedef;
177 
178  if (getShare()->sizeKeys())
179  { // Get keys for ni_create
180  bool using_unique_constraint= false;
181  HA_KEYSEG *seg= new (mem()) HA_KEYSEG[keyinfo->key_parts];
182 
183  memset(seg, 0, sizeof(*seg) * keyinfo->key_parts);
184  if (keyinfo->key_length >= cursor->getEngine()->max_key_length() ||
185  keyinfo->key_parts > cursor->getEngine()->max_key_parts() ||
186  getShare()->uniques)
187  {
188  /* Can't create a key; Make a unique constraint instead of a key */
189  getMutableShare()->keys= 0;
190  getMutableShare()->uniques= 1;
191  using_unique_constraint= true;
192  memset(&uniquedef, 0, sizeof(uniquedef));
193  uniquedef.keysegs=keyinfo->key_parts;
194  uniquedef.seg=seg;
195  uniquedef.null_are_equal=1;
196 
197  /* Create extra column for hash value */
198  memset(*recinfo, 0, sizeof(**recinfo));
199  (*recinfo)->type= FIELD_CHECK;
200  (*recinfo)->length=MI_UNIQUE_HASH_LENGTH;
201  (*recinfo)++;
202  getMutableShare()->setRecordLength(getShare()->getRecordLength() + MI_UNIQUE_HASH_LENGTH);
203  }
204  else
205  {
206  /* Create an unique key */
207  memset(&keydef, 0, sizeof(keydef));
208  keydef.flag=HA_NOSAME | HA_BINARY_PACK_KEY | HA_PACK_KEY;
209  keydef.keysegs= keyinfo->key_parts;
210  keydef.seg= seg;
211  }
212  for (uint32_t i= 0; i < keyinfo->key_parts ; i++,seg++)
213  {
214  Field *key_field=keyinfo->key_part[i].field;
215  seg->flag= 0;
216  seg->language= key_field->charset()->number;
217  seg->length= keyinfo->key_part[i].length;
218  seg->start= keyinfo->key_part[i].offset;
219  if (key_field->flags & BLOB_FLAG)
220  {
221  seg->type= ((keyinfo->key_part[i].key_type & 1 /* binary */) ?
222  HA_KEYTYPE_VARBINARY2 : HA_KEYTYPE_VARTEXT2);
223  seg->bit_start= (uint8_t)(key_field->pack_length() - getShare()->blob_ptr_size);
224  seg->flag= HA_BLOB_PART;
225  seg->length= 0; // Whole blob in unique constraint
226  }
227  else
228  {
229  seg->type= keyinfo->key_part[i].type;
230  }
231  if (!(key_field->flags & NOT_NULL_FLAG))
232  {
233  seg->null_bit= key_field->null_bit;
234  seg->null_pos= (uint32_t) (key_field->null_ptr - getInsertRecord());
235  /*
236  We are using a GROUP BY on something that contains NULL
237  In this case we have to tell MyISAM that two NULL should
238  on INSERT be regarded at the same value
239  */
240  if (! using_unique_constraint)
241  keydef.flag|= HA_NULL_ARE_EQUAL;
242  }
243  }
244  }
245  MI_CREATE_INFO create_info;
246 
247  if ((options & (OPTION_BIG_TABLES | SELECT_SMALL_RESULT)) ==
248  OPTION_BIG_TABLES)
249  create_info.data_file_length= ~(uint64_t) 0;
250 
251  if ((error= mi_create(getShare()->getTableName(), getShare()->sizeKeys(), &keydef,
252  (uint32_t) (*recinfo-start_recinfo),
253  start_recinfo,
254  getShare()->uniques, &uniquedef,
255  &create_info,
256  HA_CREATE_TMP_TABLE)))
257  {
258  print_error(error, MYF(0));
259  db_stat= 0;
260 
261  return true;
262  }
263  in_use->status_var.created_tmp_disk_tables++;
264  getMutableShare()->db_record_offset= 1;
265  return false;
266 }
267 
268 /*
269  Set up column usage bitmaps for a temporary table
270 
271  IMPLEMENTATION
272  For temporary tables, we need one bitmap with all columns set and
273  a tmp_set bitmap to be used by things like filesort.
274 */
275 
276 void Singular::setup_tmp_table_column_bitmaps()
277 {
278  uint32_t field_count= getShare()->sizeFields();
279 
280  def_read_set.resize(field_count);
281  def_write_set.resize(field_count);
282  tmp_set.resize(field_count);
283  getMutableShare()->all_set.resize(field_count);
284  getMutableShare()->all_set.set();
285  def_write_set.set();
286  def_read_set.set();
287  default_column_bitmaps();
288 }
289 
290 Singular::~Singular()
291 {
292  const char* save_proc_info= in_use->get_proc_info();
293  in_use->set_proc_info("removing tmp table");
294 
295  // Release latches since this can take a long time
297 
298  if (cursor)
299  {
300  if (db_stat)
301  cursor->closeMarkForDelete();
302 
303  drizzled::error_t ignored;
304  plugin::StorageEngine::dropTable(*in_use, *getShare()->getEngine(), getShare()->getTableIdentifier(), ignored);
305  delete cursor;
306  }
307 
308  /* free blobs */
309  for (Field **ptr= getFields(); *ptr; ptr++)
310  {
311  (*ptr)->free();
312  }
313  free_io_cache();
314 
315  mem().free_root(MYF(0));
316  in_use->set_proc_info(save_proc_info);
317 }
318 
319 } /* namespace table */
320 } /* namespace drizzled */
TODO: Rename this file - func.h is stupid.