~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/join_cache.cc

  • Committer: Monty Taylor
  • Date: 2010-04-22 02:46:23 UTC
  • mto: (1497.3.4 enable-dtrace)
  • mto: This revision was merged to the branch mainline in revision 1527.
  • Revision ID: mordred@inaugust.com-20100422024623-4urw8fi8eraci08p
Don't overwrite the pandora_vc_revinfo file if we don't have new
authoratative information.

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)* sizeof(CACHE_FIELD*))))
91
90
  {
92
 
    size= cache->end - cache->buff;
93
 
    global_join_buffer.sub(size);
94
91
    free((unsigned char*) cache->buff);
95
92
    cache->buff=0;
96
93
    return(1);
97
94
  }
98
95
  copy=cache->field;
99
 
  blob_ptr=cache->blob_ptr=(CacheField**)
 
96
  blob_ptr=cache->blob_ptr=(CACHE_FIELD**)
100
97
    (cache->field+cache->fields+table_count*2);
101
98
 
102
99
  length=0;
103
 
  for (unsigned int i= 0 ; i < table_count ; i++)
 
100
  for (i=0 ; i < table_count ; i++)
104
101
  {
105
102
    uint32_t null_fields=0, used_fields;
106
103
    Field **f_ptr,*field;
107
 
    for (f_ptr= tables[i].table->getFields(), used_fields= tables[i].used_fields; used_fields; f_ptr++)
 
104
    for (f_ptr= tables[i].table->field,used_fields= tables[i].used_fields; used_fields; f_ptr++)
108
105
    {
109
106
      field= *f_ptr;
110
107
      if (field->isReadSet())
123
120
    if (null_fields && tables[i].table->getNullFields())
124
121
    {                                           /* must copy null bits */
125
122
      copy->str= tables[i].table->null_flags;
126
 
      copy->length= tables[i].table->getShare()->null_bytes;
 
123
      copy->length= tables[i].table->s->null_bytes;
127
124
      copy->strip=0;
128
125
      copy->blob_field=0;
129
126
      copy->get_rowid= NULL;
166
163
  cache->blobs= blobs;
167
164
  *blob_ptr= NULL;                                      /* End sequentel */
168
165
  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
166
  if (!(cache->buff= (unsigned char*) malloc(size)))
175
167
    return 1;
176
168
  cache->end= cache->buff+size;
177
 
  cache->reset_cache_write();
178
 
 
 
169
  reset_cache_write(cache);
179
170
  return 0;
180
171
}
181
172
 
182
 
bool JoinCache::store_record_in_cache()
 
173
bool store_record_in_cache(JOIN_CACHE *cache)
183
174
{
184
 
  JoinCache *cache= this;
185
 
  unsigned char *local_pos;
186
 
  CacheField *copy,*end_field;
 
175
  uint32_t length;
 
176
  unsigned char *pos;
 
177
  CACHE_FIELD *copy,*end_field;
187
178
  bool last_record;
188
179
 
189
 
  local_pos= cache->pos;
 
180
  pos= cache->pos;
190
181
  end_field= cache->field+cache->fields;
191
182
 
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
 
 
 
183
  length= cache->length;
 
184
  if (cache->blobs)
 
185
    length+= used_blob_length(cache->blob_ptr);
 
186
  if ((last_record= (length + cache->length > (size_t) (cache->end - pos))))
 
187
    cache->ptr_record= cache->records;
207
188
  /*
208
189
    There is room in cache. Put record there
209
190
  */
214
195
    {
215
196
      if (last_record)
216
197
      {
217
 
        copy->blob_field->get_image(local_pos, copy->length+sizeof(char*), copy->blob_field->charset());
218
 
        local_pos+= copy->length+sizeof(char*);
 
198
        copy->blob_field->get_image(pos, copy->length+sizeof(char*), copy->blob_field->charset());
 
199
        pos+= copy->length+sizeof(char*);
219
200
      }
220
201
      else
221
202
      {
222
 
        copy->blob_field->get_image(local_pos, copy->length, // blob length
 
203
        copy->blob_field->get_image(pos, copy->length, // blob length
223
204
                                    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;
 
205
        memcpy(pos+copy->length,copy->str,copy->blob_length);  // Blob data
 
206
        pos+= copy->length+copy->blob_length;
226
207
      }
227
208
    }
228
209
    else
229
210
    {
230
211
      // SemiJoinDuplicateElimination: Get the rowid into table->ref:
231
212
      if (copy->get_rowid)
232
 
        copy->get_rowid->cursor->position(copy->get_rowid->getInsertRecord());
 
213
        copy->get_rowid->cursor->position(copy->get_rowid->record[0]);
233
214
 
234
215
      if (copy->strip)
235
216
      {
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;
 
217
        unsigned char *str,*end;
 
218
        for (str= copy->str,end= str+copy->length; end > str && end[-1] == ' '; end--)
 
219
        {}
 
220
        length= (uint32_t) (end-str);
 
221
        memcpy(pos+2, str, length);
 
222
        int2store(pos, length);
 
223
        pos+= length+2;
243
224
      }
244
225
      else
245
226
      {
246
 
        memcpy(local_pos, copy->str, copy->length);
247
 
        local_pos+= copy->length;
 
227
        memcpy(pos,copy->str,copy->length);
 
228
        pos+= copy->length;
248
229
      }
249
230
    }
250
231
  }
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;
 
232
  cache->pos= pos;
 
233
  return last_record || (size_t) (cache->end - pos) < cache->length;
 
234
}
 
235
 
 
236
void reset_cache_read(JOIN_CACHE *cache)
 
237
{
 
238
  cache->record_nr= 0;
 
239
  cache->pos= cache->buff;
 
240
}
 
241
 
 
242
void reset_cache_write(JOIN_CACHE *cache)
 
243
{
 
244
  reset_cache_read(cache);
 
245
  cache->records= 0;
 
246
  cache->ptr_record= UINT32_MAX;
266
247
}
267
248
 
268
249
/**