~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sj_tmp_table.cc

  • Committer: Stewart Smith
  • Date: 2008-09-25 10:04:06 UTC
  • mto: This revision was merged to the branch mainline in revision 408.
  • Revision ID: stewart@flamingspork.com-20080925100406-hld92f4dr4nuar3a
Move compression functions (compress, uncompress and compressed_length) out into modules and fix test

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* - mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
 
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 
 *
4
 
 *  Copyright (C) 2008 Sun Microsystems, Inc.
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; version 2 of the License.
9
 
 *
10
 
 *  This program is distributed in the hope that it will be useful,
11
 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 
 *  GNU General Public License for more details.
14
 
 *
15
 
 *  You should have received a copy of the GNU General Public License
16
 
 *  along with this program; if not, write to the Free Software
17
 
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
 
 */
19
 
 
20
1
#include "sj_tmp_table.h"
21
2
 
22
3
/*
24
5
 
25
6
  SYNOPSIS
26
7
 
27
 
  create_duplicate_weedout_tmp_table()
28
 
  thd
29
 
  uniq_tuple_length_arg
30
 
  SJ_TMP_TABLE
 
8
    create_duplicate_weedout_tmp_table()
 
9
      thd
 
10
      uniq_tuple_length_arg
 
11
      SJ_TMP_TABLE 
31
12
 
32
13
  DESCRIPTION
33
 
  Create a temporary table to weed out duplicate rowid combinations. The
34
 
  table has a single column that is a concatenation of all rowids in the
35
 
  combination.
36
 
 
37
 
  Depending on the needed length, there are two cases:
38
 
 
39
 
  1. When the length of the column < max_key_length:
40
 
 
41
 
  CREATE TABLE tmp (col VARBINARY(n) NOT NULL, UNIQUE KEY(col));
42
 
 
43
 
  2. Otherwise (not a valid SQL syntax but internally supported):
44
 
 
45
 
  CREATE TABLE tmp (col VARBINARY NOT NULL, UNIQUE CONSTRAINT(col));
46
 
 
47
 
  The code in this function was produced by extraction of relevant parts
48
 
  from create_tmp_table().
 
14
    Create a temporary table to weed out duplicate rowid combinations. The
 
15
    table has a single column that is a concatenation of all rowids in the
 
16
    combination. 
 
17
 
 
18
    Depending on the needed length, there are two cases:
 
19
 
 
20
    1. When the length of the column < max_key_length:
 
21
 
 
22
      CREATE TABLE tmp (col VARBINARY(n) NOT NULL, UNIQUE KEY(col));
 
23
 
 
24
    2. Otherwise (not a valid SQL syntax but internally supported):
 
25
 
 
26
      CREATE TABLE tmp (col VARBINARY NOT NULL, UNIQUE CONSTRAINT(col));
 
27
 
 
28
    The code in this function was produced by extraction of relevant parts
 
29
    from create_tmp_table().
49
30
 
50
31
  RETURN
51
 
  created table
52
 
  NULL on error
 
32
    created table
 
33
    NULL on error
53
34
*/
54
35
 
55
 
Table *create_duplicate_weedout_tmp_table(THD *thd,
56
 
                                          uint32_t uniq_tuple_length_arg,
57
 
                                          SJ_TMP_TABLE *sjtbl)
 
36
Table *create_duplicate_weedout_tmp_table(THD *thd, 
 
37
                                          uint uniq_tuple_length_arg,
 
38
                                          SJ_TMP_TABLE *sjtbl)
58
39
{
59
40
  MEM_ROOT *mem_root_save, own_root;
60
41
  Table *table;
61
42
  TABLE_SHARE *share;
62
 
  uint32_t  temp_pool_slot=MY_BIT_NONE;
63
 
  char  *tmpname,path[FN_REFLEN];
 
43
  uint  temp_pool_slot=MY_BIT_NONE;
 
44
  char  *tmpname,path[FN_REFLEN];
64
45
  Field **reg_field;
65
46
  KEY_PART_INFO *key_part_info;
66
47
  KEY *keyinfo;
67
 
  unsigned char *group_buff;
68
 
  unsigned char *bitmaps;
69
 
  uint32_t *blob_field;
 
48
  uchar *group_buff;
 
49
  uchar *bitmaps;
 
50
  uint *blob_field;
70
51
  MI_COLUMNDEF *recinfo, *start_recinfo;
71
52
  bool using_unique_constraint=false;
72
53
  Field *field, *key_field;
73
 
  uint32_t blob_count, null_pack_length, null_count;
74
 
  unsigned char *null_flags;
75
 
  unsigned char *pos;
 
54
  uint blob_count, null_pack_length, null_count;
 
55
  uchar *null_flags;
 
56
  uchar *pos;
76
57
  
77
58
  /*
78
59
    STEP 1: Get temporary table name
102
83
                        &table, sizeof(*table),
103
84
                        &share, sizeof(*share),
104
85
                        &reg_field, sizeof(Field*) * (1+1),
105
 
                        &blob_field, sizeof(uint32_t)*2,
 
86
                        &blob_field, sizeof(uint)*2,
106
87
                        &keyinfo, sizeof(*keyinfo),
107
88
                        &key_part_info, sizeof(*key_part_info) * 2,
108
89
                        &start_recinfo,
109
90
                        sizeof(*recinfo)*(1*2+4),
110
 
                        &tmpname, (uint32_t) strlen(path)+1,
 
91
                        &tmpname, (uint) strlen(path)+1,
111
92
                        &group_buff, (!using_unique_constraint ?
112
93
                                      uniq_tuple_length_arg : 0),
113
94
                        &bitmaps, bitmap_buffer_size(1)*2,
114
 
                        NULL))
 
95
                        NullS))
115
96
  {
116
97
    if (temp_pool_slot != MY_BIT_NONE)
117
98
      bitmap_lock_clear_bit(&temp_pool, temp_pool_slot);
118
99
    return(NULL);
119
100
  }
120
 
  my_stpcpy(tmpname,path);
 
101
  stpcpy(tmpname,path);
 
102
  
121
103
 
122
104
  /* STEP 4: Create Table description */
123
105
  memset(table, 0, sizeof(*table));
129
111
 
130
112
  table->field=reg_field;
131
113
  table->alias= "weedout-tmp";
132
 
  table->reginfo.lock_type=TL_WRITE;  /* Will be updated */
 
114
  table->reginfo.lock_type=TL_WRITE;    /* Will be updated */
133
115
  table->db_stat=HA_OPEN_KEYFILE+HA_OPEN_RNDFILE;
134
116
  table->map=1;
135
117
  table->temp_pool_slot = temp_pool_slot;
169
151
    field->reset_fields();
170
152
    field->init(table);
171
153
    field->orig_table= NULL;
172
 
 
 
154
     
173
155
    field->field_index= 0;
174
 
 
 
156
    
175
157
    *(reg_field++)= field;
176
158
    *blob_field= 0;
177
159
    *reg_field= 0;
180
162
    share->blob_fields= 0;
181
163
  }
182
164
 
183
 
  uint32_t reclength= field->pack_length();
 
165
  uint reclength= field->pack_length();
184
166
  if (using_unique_constraint)
185
 
  {
 
167
  { 
186
168
    share->db_plugin= ha_lock_engine(0, myisam_hton);
187
169
    table->file= get_new_handler(share, &table->mem_root,
188
170
                                 share->db_type());
198
180
    goto err;
199
181
 
200
182
  null_count=1;
201
 
 
 
183
  
202
184
  null_pack_length= 1;
203
185
  reclength += null_pack_length;
204
186
 
205
187
  share->reclength= reclength;
206
188
  {
207
 
    uint32_t alloc_length=ALIGN_SIZE(share->reclength + MI_UNIQUE_HASH_LENGTH+1);
 
189
    uint alloc_length=ALIGN_SIZE(share->reclength + MI_UNIQUE_HASH_LENGTH+1);
208
190
    share->rec_buff_length= alloc_length;
209
 
    if (!(table->record[0]= (unsigned char*)
210
 
          alloc_root(&table->mem_root, alloc_length*3)))
 
191
    if (!(table->record[0]= (uchar*)
 
192
                            alloc_root(&table->mem_root, alloc_length*3)))
211
193
      goto err;
212
194
    table->record[1]= table->record[0]+alloc_length;
213
195
    share->default_values= table->record[1]+alloc_length;
215
197
  table->setup_tmp_table_column_bitmaps(bitmaps);
216
198
 
217
199
  recinfo= start_recinfo;
218
 
  null_flags=(unsigned char*) table->record[0];
 
200
  null_flags=(uchar*) table->record[0];
219
201
  pos=table->record[0]+ null_pack_length;
220
202
  if (null_pack_length)
221
203
  {
223
205
    recinfo->type=FIELD_NORMAL;
224
206
    recinfo->length=null_pack_length;
225
207
    recinfo++;
226
 
    memset(null_flags, 255, null_pack_length);  // Set null fields
 
208
    memset(null_flags, 255, null_pack_length);  // Set null fields
227
209
 
228
 
    table->null_flags= (unsigned char*) table->record[0];
 
210
    table->null_flags= (uchar*) table->record[0];
229
211
    share->null_fields= null_count;
230
212
    share->null_bytes= null_pack_length;
231
213
  }
233
215
 
234
216
  {
235
217
    //Field *field= *reg_field;
236
 
    uint32_t length;
 
218
    uint length;
237
219
    memset(recinfo, 0, sizeof(*recinfo));
238
 
    field->move_field(pos,(unsigned char*) 0,0);
 
220
    field->move_field(pos,(uchar*) 0,0);
239
221
 
240
222
    field->reset();
241
223
    /*
261
243
  //param->recinfo=recinfo;
262
244
  //store_record(table,s->default_values);        // Make empty default record
263
245
 
264
 
  if (thd->variables.tmp_table_size == ~ (uint64_t) 0)    // No limit
 
246
  if (thd->variables.tmp_table_size == ~ (uint64_t) 0)          // No limit
265
247
    share->max_rows= ~(ha_rows) 0;
266
248
  else
267
249
    share->max_rows= (ha_rows) (((share->db_type() == heap_hton) ?
268
 
                                 cmin(thd->variables.tmp_table_size,
269
 
                                      thd->variables.max_heap_table_size) :
 
250
                                 min(thd->variables.tmp_table_size,
 
251
                                     thd->variables.max_heap_table_size) :
270
252
                                 thd->variables.tmp_table_size) /
271
 
                                share->reclength);
272
 
  set_if_bigger(share->max_rows,1);    // For dummy start options
 
253
                                 share->reclength);
 
254
  set_if_bigger(share->max_rows,1);             // For dummy start options
273
255
 
274
256
 
275
257
  //// keyinfo= param->keyinfo;
294
276
      key_part_info->key_type = FIELDFLAG_BINARY;
295
277
      if (!using_unique_constraint)
296
278
      {
297
 
        if (!(key_field= field->new_key_field(thd->mem_root, table,
 
279
        if (!(key_field= field->new_key_field(thd->mem_root, table,
298
280
                                              group_buff,
299
281
                                              field->null_ptr,
300
282
                                              field->null_bit)))
301
 
          goto err;
 
283
          goto err;
302
284
        key_part_info->key_part_flag|= HA_END_SPACE_ARE_EQUAL; //todo need this?
303
285
      }
304
286
      keyinfo->key_length+=  key_part_info->length;
305
287
    }
306
288
  }
307
289
 
308
 
  if (thd->is_fatal_error)        // If end of memory
 
290
  if (thd->is_fatal_error)                              // If end of memory
309
291
    goto err;
310
292
  share->db_record_offset= 1;
311
293
  if (share->db_type() == myisam_hton)
327
309
  table->free_tmp_table(thd);                    /* purecov: inspected */
328
310
  if (temp_pool_slot != MY_BIT_NONE)
329
311
    bitmap_lock_clear_bit(&temp_pool, temp_pool_slot);
330
 
  return(NULL);        /* purecov: inspected */
 
312
  return(NULL);                         /* purecov: inspected */
331
313
}