~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/join_cache.cc

  • Committer: Andrew Hutchings
  • Date: 2011-01-21 11:23:19 UTC
  • mto: (2100.1.1 build)
  • mto: This revision was merged to the branch mainline in revision 2101.
  • Revision ID: andrew@linuxjedi.co.uk-20110121112319-nj1cvg0yt3nnf2rr
Add errors page to drizzle client docs
Add link to specific error in migration docs
Minor changes to migration docs

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
3
 *
4
 
 *  Copyright (C) 2008-2009 Sun Microsystems
 
4
 *  Copyright (C) 2008-2009 Sun Microsystems, Inc.
5
5
 *
6
6
 *  This program is free software; you can redistribute it and/or modify
7
7
 *  it under the terms of the GNU General Public License as published by
30
30
#include "config.h"
31
31
#include "drizzled/sql_select.h" /* include join.h */
32
32
#include "drizzled/field/blob.h"
 
33
#include "drizzled/drizzled.h"
 
34
#include "drizzled/internal/my_sys.h"
33
35
 
34
36
#include <algorithm>
35
37
 
38
40
namespace drizzled
39
41
{
40
42
 
41
 
static uint32_t used_blob_length(CACHE_FIELD **ptr);
 
43
static uint32_t used_blob_length(CacheField **ptr);
42
44
 
43
 
static uint32_t used_blob_length(CACHE_FIELD **ptr)
 
45
static uint32_t used_blob_length(CacheField **ptr)
44
46
{
45
47
  uint32_t length,blob_length;
46
48
  for (length=0 ; *ptr ; ptr++)
60
62
******************************************************************************/
61
63
int join_init_cache(Session *session, JoinTable *tables, uint32_t table_count)
62
64
{
63
 
  register unsigned int i;
64
65
  unsigned int length, blobs;
65
66
  size_t size;
66
 
  CACHE_FIELD *copy,**blob_ptr;
67
 
  JOIN_CACHE  *cache;
 
67
  CacheField *copy,**blob_ptr;
 
68
  JoinCache  *cache;
68
69
  JoinTable *join_tab;
69
70
 
70
71
  cache= &tables[table_count].cache;
71
72
  cache->fields=blobs=0;
72
73
 
73
74
  join_tab= tables;
74
 
  for (i=0 ; i < table_count ; i++,join_tab++)
 
75
  for (unsigned int i= 0; i < table_count ; i++, join_tab++)
75
76
  {
76
77
    if (!join_tab->used_fieldlength)            /* Not calced yet */
77
78
      calc_used_field_length(session, join_tab);
85
86
      join_tab->used_fieldlength += join_tab->table->cursor->ref_length;
86
87
    }
87
88
  }
88
 
  if (!(cache->field=(CACHE_FIELD*)
89
 
        memory::sql_alloc(sizeof(CACHE_FIELD)*(cache->fields+table_count*2)+(blobs+1)*
90
 
 
91
 
                  sizeof(CACHE_FIELD*))))
 
89
  if (!(cache->field=(CacheField*)
 
90
        memory::sql_alloc(sizeof(CacheField)*(cache->fields+table_count*2)+(blobs+1)* sizeof(CacheField*))))
92
91
  {
 
92
    size= cache->end - cache->buff;
 
93
    global_join_buffer.sub(size);
93
94
    free((unsigned char*) cache->buff);
94
95
    cache->buff=0;
95
96
    return(1);
96
97
  }
97
98
  copy=cache->field;
98
 
  blob_ptr=cache->blob_ptr=(CACHE_FIELD**)
 
99
  blob_ptr=cache->blob_ptr=(CacheField**)
99
100
    (cache->field+cache->fields+table_count*2);
100
101
 
101
102
  length=0;
102
 
  for (i=0 ; i < table_count ; i++)
 
103
  for (unsigned int i= 0 ; i < table_count ; i++)
103
104
  {
104
105
    uint32_t null_fields=0, used_fields;
105
106
    Field **f_ptr,*field;
106
 
    for (f_ptr= tables[i].table->field,used_fields= tables[i].used_fields; used_fields; f_ptr++)
 
107
    for (f_ptr= tables[i].table->getFields(), used_fields= tables[i].used_fields; used_fields; f_ptr++)
107
108
    {
108
109
      field= *f_ptr;
109
110
      if (field->isReadSet())
122
123
    if (null_fields && tables[i].table->getNullFields())
123
124
    {                                           /* must copy null bits */
124
125
      copy->str= tables[i].table->null_flags;
125
 
      copy->length= tables[i].table->s->null_bytes;
 
126
      copy->length= tables[i].table->getShare()->null_bytes;
126
127
      copy->strip=0;
127
128
      copy->blob_field=0;
128
129
      copy->get_rowid= NULL;
165
166
  cache->blobs= blobs;
166
167
  *blob_ptr= NULL;                                      /* End sequentel */
167
168
  size= max((size_t) session->variables.join_buff_size, (size_t)cache->length);
 
169
  if (not global_join_buffer.add(size))
 
170
  {
 
171
    my_error(ER_OUT_OF_GLOBAL_JOINMEMORY, MYF(ME_ERROR+ME_WAITTANG));
 
172
    return 1;
 
173
  }
168
174
  if (!(cache->buff= (unsigned char*) malloc(size)))
169
175
    return 1;
170
176
  cache->end= cache->buff+size;
171
 
  reset_cache_write(cache);
 
177
  cache->reset_cache_write();
 
178
 
172
179
  return 0;
173
180
}
174
181
 
175
 
bool store_record_in_cache(JOIN_CACHE *cache)
 
182
bool JoinCache::store_record_in_cache()
176
183
{
177
 
  uint32_t length;
178
 
  unsigned char *pos;
179
 
  CACHE_FIELD *copy,*end_field;
 
184
  JoinCache *cache= this;
 
185
  unsigned char *local_pos;
 
186
  CacheField *copy,*end_field;
180
187
  bool last_record;
181
188
 
182
 
  pos= cache->pos;
 
189
  local_pos= cache->pos;
183
190
  end_field= cache->field+cache->fields;
184
191
 
185
 
  length= cache->length;
186
 
  if (cache->blobs)
187
 
    length+= used_blob_length(cache->blob_ptr);
188
 
  if ((last_record= (length + cache->length > (size_t) (cache->end - pos))))
189
 
    cache->ptr_record= cache->records;
 
192
  {
 
193
    uint32_t local_length;
 
194
 
 
195
    local_length= cache->length;
 
196
    if (cache->blobs)
 
197
    {
 
198
      local_length+= used_blob_length(cache->blob_ptr);
 
199
    }
 
200
 
 
201
    if ((last_record= (local_length + cache->length > (size_t) (cache->end - local_pos))))
 
202
    {
 
203
      cache->ptr_record= cache->records;
 
204
    }
 
205
  }
 
206
 
190
207
  /*
191
208
    There is room in cache. Put record there
192
209
  */
197
214
    {
198
215
      if (last_record)
199
216
      {
200
 
        copy->blob_field->get_image(pos, copy->length+sizeof(char*), copy->blob_field->charset());
201
 
        pos+= copy->length+sizeof(char*);
 
217
        copy->blob_field->get_image(local_pos, copy->length+sizeof(char*), copy->blob_field->charset());
 
218
        local_pos+= copy->length+sizeof(char*);
202
219
      }
203
220
      else
204
221
      {
205
 
        copy->blob_field->get_image(pos, copy->length, // blob length
 
222
        copy->blob_field->get_image(local_pos, copy->length, // blob length
206
223
                                    copy->blob_field->charset());
207
 
        memcpy(pos+copy->length,copy->str,copy->blob_length);  // Blob data
208
 
        pos+= copy->length+copy->blob_length;
 
224
        memcpy(local_pos + copy->length,copy->str,copy->blob_length);  // Blob data
 
225
        local_pos+= copy->length+copy->blob_length;
209
226
      }
210
227
    }
211
228
    else
212
229
    {
213
230
      // SemiJoinDuplicateElimination: Get the rowid into table->ref:
214
231
      if (copy->get_rowid)
215
 
        copy->get_rowid->cursor->position(copy->get_rowid->record[0]);
 
232
        copy->get_rowid->cursor->position(copy->get_rowid->getInsertRecord());
216
233
 
217
234
      if (copy->strip)
218
235
      {
219
 
        unsigned char *str,*end;
220
 
        for (str= copy->str,end= str+copy->length; end > str && end[-1] == ' '; end--)
221
 
        {}
222
 
        length= (uint32_t) (end-str);
223
 
        memcpy(pos+2, str, length);
224
 
        int2store(pos, length);
225
 
        pos+= length+2;
 
236
        unsigned char *str, *local_end;
 
237
        for (str= copy->str,local_end= str+copy->length; local_end > str && local_end[-1] == ' '; local_end--) {}
 
238
 
 
239
        uint32_t local_length= (uint32_t) (local_end - str);
 
240
        memcpy(local_pos+2, str, local_length);
 
241
        int2store(local_pos, local_length);
 
242
        local_pos+= local_length+2;
226
243
      }
227
244
      else
228
245
      {
229
 
        memcpy(pos,copy->str,copy->length);
230
 
        pos+= copy->length;
 
246
        memcpy(local_pos, copy->str, copy->length);
 
247
        local_pos+= copy->length;
231
248
      }
232
249
    }
233
250
  }
234
 
  cache->pos= pos;
235
 
  return last_record || (size_t) (cache->end - pos) < cache->length;
236
 
}
237
 
 
238
 
void reset_cache_read(JOIN_CACHE *cache)
239
 
{
240
 
  cache->record_nr= 0;
241
 
  cache->pos= cache->buff;
242
 
}
243
 
 
244
 
void reset_cache_write(JOIN_CACHE *cache)
245
 
{
246
 
  reset_cache_read(cache);
247
 
  cache->records= 0;
248
 
  cache->ptr_record= UINT32_MAX;
 
251
  cache->pos= local_pos;
 
252
  return last_record || (size_t) (cache->end - local_pos) < cache->length;
 
253
}
 
254
 
 
255
void JoinCache::reset_cache_read()
 
256
{
 
257
  record_nr= 0;
 
258
  pos= buff;
 
259
}
 
260
 
 
261
void JoinCache::reset_cache_write()
 
262
{
 
263
  reset_cache_read();
 
264
  records= 0;
 
265
  ptr_record= UINT32_MAX;
249
266
}
250
267
 
251
268
/**