~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/join_cache.cc

  • Committer: Stewart Smith
  • Date: 2010-02-15 01:56:32 UTC
  • mto: (1273.13.96 build)
  • mto: This revision was merged to the branch mainline in revision 1308.
  • Revision ID: stewart@flamingspork.com-20100215015632-pm7lnxfq5j5uh8kj
move DATABASE() to function plugin. modify parser so that it looks for a function named 'database' when DATABASE() is called. Special case still needed in parser due to hilarity of not-really-reserved words.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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"
35
33
 
36
34
#include <algorithm>
37
35
 
40
38
namespace drizzled
41
39
{
42
40
 
43
 
static uint32_t used_blob_length(CacheField **ptr);
 
41
static uint32_t used_blob_length(CACHE_FIELD **ptr);
44
42
 
45
 
static uint32_t used_blob_length(CacheField **ptr)
 
43
static uint32_t used_blob_length(CACHE_FIELD **ptr)
46
44
{
47
45
  uint32_t length,blob_length;
48
46
  for (length=0 ; *ptr ; ptr++)
62
60
******************************************************************************/
63
61
int join_init_cache(Session *session, JoinTable *tables, uint32_t table_count)
64
62
{
 
63
  register unsigned int i;
65
64
  unsigned int length, blobs;
66
65
  size_t size;
67
 
  CacheField *copy,**blob_ptr;
68
 
  JoinCache  *cache;
 
66
  CACHE_FIELD *copy,**blob_ptr;
 
67
  JOIN_CACHE  *cache;
69
68
  JoinTable *join_tab;
70
69
 
71
70
  cache= &tables[table_count].cache;
72
71
  cache->fields=blobs=0;
73
72
 
74
73
  join_tab= tables;
75
 
  for (unsigned int i= 0; i < table_count ; i++, join_tab++)
 
74
  for (i=0 ; i < table_count ; i++,join_tab++)
76
75
  {
77
76
    if (!join_tab->used_fieldlength)            /* Not calced yet */
78
77
      calc_used_field_length(session, join_tab);
86
85
      join_tab->used_fieldlength += join_tab->table->cursor->ref_length;
87
86
    }
88
87
  }
89
 
  if (!(cache->field=(CacheField*)
90
 
        memory::sql_alloc(sizeof(CacheField)*(cache->fields+table_count*2)+(blobs+1)* sizeof(CacheField*))))
 
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*))))
91
92
  {
92
 
    size= cache->end - cache->buff;
93
 
    global_join_buffer.sub(size);
94
93
    free((unsigned char*) cache->buff);
95
94
    cache->buff=0;
96
95
    return(1);
97
96
  }
98
97
  copy=cache->field;
99
 
  blob_ptr=cache->blob_ptr=(CacheField**)
 
98
  blob_ptr=cache->blob_ptr=(CACHE_FIELD**)
100
99
    (cache->field+cache->fields+table_count*2);
101
100
 
102
101
  length=0;
103
 
  for (unsigned int i= 0 ; i < table_count ; i++)
 
102
  for (i=0 ; i < table_count ; i++)
104
103
  {
105
104
    uint32_t null_fields=0, used_fields;
106
105
    Field **f_ptr,*field;
107
 
    for (f_ptr= tables[i].table->getFields(), used_fields= tables[i].used_fields; used_fields; f_ptr++)
 
106
    for (f_ptr= tables[i].table->field,used_fields= tables[i].used_fields; used_fields; f_ptr++)
108
107
    {
109
108
      field= *f_ptr;
110
109
      if (field->isReadSet())
123
122
    if (null_fields && tables[i].table->getNullFields())
124
123
    {                                           /* must copy null bits */
125
124
      copy->str= tables[i].table->null_flags;
126
 
      copy->length= tables[i].table->getShare()->null_bytes;
 
125
      copy->length= tables[i].table->s->null_bytes;
127
126
      copy->strip=0;
128
127
      copy->blob_field=0;
129
128
      copy->get_rowid= NULL;
166
165
  cache->blobs= blobs;
167
166
  *blob_ptr= NULL;                                      /* End sequentel */
168
167
  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
 
  }
174
168
  if (!(cache->buff= (unsigned char*) malloc(size)))
175
169
    return 1;
176
170
  cache->end= cache->buff+size;
177
 
  cache->reset_cache_write();
178
 
 
 
171
  reset_cache_write(cache);
179
172
  return 0;
180
173
}
181
174
 
182
 
bool JoinCache::store_record_in_cache()
 
175
bool store_record_in_cache(JOIN_CACHE *cache)
183
176
{
184
 
  JoinCache *cache= this;
185
 
  unsigned char *local_pos;
186
 
  CacheField *copy,*end_field;
 
177
  uint32_t length;
 
178
  unsigned char *pos;
 
179
  CACHE_FIELD *copy,*end_field;
187
180
  bool last_record;
188
181
 
189
 
  local_pos= cache->pos;
 
182
  pos= cache->pos;
190
183
  end_field= cache->field+cache->fields;
191
184
 
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
 
 
 
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;
207
190
  /*
208
191
    There is room in cache. Put record there
209
192
  */
214
197
    {
215
198
      if (last_record)
216
199
      {
217
 
        copy->blob_field->get_image(local_pos, copy->length+sizeof(char*), copy->blob_field->charset());
218
 
        local_pos+= copy->length+sizeof(char*);
 
200
        copy->blob_field->get_image(pos, copy->length+sizeof(char*), copy->blob_field->charset());
 
201
        pos+= copy->length+sizeof(char*);
219
202
      }
220
203
      else
221
204
      {
222
 
        copy->blob_field->get_image(local_pos, copy->length, // blob length
 
205
        copy->blob_field->get_image(pos, copy->length, // blob length
223
206
                                    copy->blob_field->charset());
224
 
        memcpy(local_pos + copy->length,copy->str,copy->blob_length);  // Blob data
225
 
        local_pos+= copy->length+copy->blob_length;
 
207
        memcpy(pos+copy->length,copy->str,copy->blob_length);  // Blob data
 
208
        pos+= copy->length+copy->blob_length;
226
209
      }
227
210
    }
228
211
    else
229
212
    {
230
213
      // SemiJoinDuplicateElimination: Get the rowid into table->ref:
231
214
      if (copy->get_rowid)
232
 
        copy->get_rowid->cursor->position(copy->get_rowid->getInsertRecord());
 
215
        copy->get_rowid->cursor->position(copy->get_rowid->record[0]);
233
216
 
234
217
      if (copy->strip)
235
218
      {
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;
 
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;
243
226
      }
244
227
      else
245
228
      {
246
 
        memcpy(local_pos, copy->str, copy->length);
247
 
        local_pos+= copy->length;
 
229
        memcpy(pos,copy->str,copy->length);
 
230
        pos+= copy->length;
248
231
      }
249
232
    }
250
233
  }
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;
 
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;
266
249
}
267
250
 
268
251
/**