~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/join_cache.cc

  • Committer: devananda
  • Date: 2009-07-01 17:38:47 UTC
  • mto: (1093.1.7 captain)
  • mto: This revision was merged to the branch mainline in revision 1095.
  • Revision ID: devananda.vdv@gmail.com-20090701173847-3n3mbtessg5ff35e
refactored function/benchmark into plugin/benchmark

Show diffs side-by-side

added added

removed removed

Lines of Context:
27
27
 * @{
28
28
 */
29
29
 
30
 
#include "config.h"
 
30
#include "drizzled/server_includes.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
 
 
36
 
#include <algorithm>
37
 
 
38
 
using namespace std;
39
 
 
40
 
namespace drizzled
41
 
{
42
 
 
43
 
static uint32_t used_blob_length(CacheField **ptr);
44
 
 
45
 
static uint32_t used_blob_length(CacheField **ptr)
 
33
 
 
34
static uint32_t used_blob_length(CACHE_FIELD **ptr);
 
35
 
 
36
static uint32_t used_blob_length(CACHE_FIELD **ptr)
46
37
{
47
38
  uint32_t length,blob_length;
48
39
  for (length=0 ; *ptr ; ptr++)
60
51
  last record is stored with pointers to blobs to support very big
61
52
  records
62
53
******************************************************************************/
63
 
int join_init_cache(Session *session, JoinTable *tables, uint32_t table_count)
 
54
int join_init_cache(Session *session, JOIN_TAB *tables, uint32_t table_count)
64
55
{
 
56
  register unsigned int i;
65
57
  unsigned int length, blobs;
66
58
  size_t size;
67
 
  CacheField *copy,**blob_ptr;
68
 
  JoinCache  *cache;
69
 
  JoinTable *join_tab;
 
59
  CACHE_FIELD *copy,**blob_ptr;
 
60
  JOIN_CACHE  *cache;
 
61
  JOIN_TAB *join_tab;
70
62
 
71
63
  cache= &tables[table_count].cache;
72
64
  cache->fields=blobs=0;
73
65
 
74
66
  join_tab= tables;
75
 
  for (unsigned int i= 0; i < table_count ; i++, join_tab++)
 
67
  for (i=0 ; i < table_count ; i++,join_tab++)
76
68
  {
77
69
    if (!join_tab->used_fieldlength)            /* Not calced yet */
78
70
      calc_used_field_length(session, join_tab);
80
72
    blobs+=join_tab->used_blobs;
81
73
 
82
74
    /* SemiJoinDuplicateElimination: reserve space for rowid */
83
 
    if (join_tab->rowid_keep_flags & JoinTable::KEEP_ROWID)
 
75
    if (join_tab->rowid_keep_flags & JOIN_TAB::KEEP_ROWID)
84
76
    {
85
77
      cache->fields++;
86
 
      join_tab->used_fieldlength += join_tab->table->cursor->ref_length;
 
78
      join_tab->used_fieldlength += join_tab->table->file->ref_length;
87
79
    }
88
80
  }
89
 
  if (!(cache->field=(CacheField*)
90
 
        memory::sql_alloc(sizeof(CacheField)*(cache->fields+table_count*2)+(blobs+1)* sizeof(CacheField*))))
 
81
  if (!(cache->field=(CACHE_FIELD*)
 
82
        sql_alloc(sizeof(CACHE_FIELD)*(cache->fields+table_count*2)+(blobs+1)*
 
83
 
 
84
                  sizeof(CACHE_FIELD*))))
91
85
  {
92
 
    size= cache->end - cache->buff;
93
 
    global_join_buffer.sub(size);
94
 
    free((unsigned char*) cache->buff);
95
 
    cache->buff=0;
96
 
    return(1);
 
86
    free((unsigned char*) cache->buff);         /* purecov: inspected */
 
87
    cache->buff=0;                              /* purecov: inspected */
 
88
    return(1);                          /* purecov: inspected */
97
89
  }
98
90
  copy=cache->field;
99
 
  blob_ptr=cache->blob_ptr=(CacheField**)
 
91
  blob_ptr=cache->blob_ptr=(CACHE_FIELD**)
100
92
    (cache->field+cache->fields+table_count*2);
101
93
 
102
94
  length=0;
103
 
  for (unsigned int i= 0 ; i < table_count ; i++)
 
95
  for (i=0 ; i < table_count ; i++)
104
96
  {
105
97
    uint32_t null_fields=0, used_fields;
106
98
    Field **f_ptr,*field;
107
 
    for (f_ptr= tables[i].table->getFields(), used_fields= tables[i].used_fields; used_fields; f_ptr++)
 
99
    for (f_ptr= tables[i].table->field,used_fields= tables[i].used_fields; used_fields; f_ptr++)
108
100
    {
109
101
      field= *f_ptr;
110
102
      if (field->isReadSet())
123
115
    if (null_fields && tables[i].table->getNullFields())
124
116
    {                                           /* must copy null bits */
125
117
      copy->str= tables[i].table->null_flags;
126
 
      copy->length= tables[i].table->getShare()->null_bytes;
 
118
      copy->length= tables[i].table->s->null_bytes;
127
119
      copy->strip=0;
128
120
      copy->blob_field=0;
129
121
      copy->get_rowid= NULL;
144
136
      cache->fields++;
145
137
    }
146
138
    /* SemiJoinDuplicateElimination: Allocate space for rowid if needed */
147
 
    if (tables[i].rowid_keep_flags & JoinTable::KEEP_ROWID)
 
139
    if (tables[i].rowid_keep_flags & JOIN_TAB::KEEP_ROWID)
148
140
    {
149
 
      copy->str= tables[i].table->cursor->ref;
150
 
      copy->length= tables[i].table->cursor->ref_length;
 
141
      copy->str= tables[i].table->file->ref;
 
142
      copy->length= tables[i].table->file->ref_length;
151
143
      copy->strip=0;
152
144
      copy->blob_field=0;
153
145
      copy->get_rowid= NULL;
154
 
      if (tables[i].rowid_keep_flags & JoinTable::CALL_POSITION)
 
146
      if (tables[i].rowid_keep_flags & JOIN_TAB::CALL_POSITION)
155
147
      {
156
148
        /* We will need to call h->position(): */
157
149
        copy->get_rowid= tables[i].table;
158
150
        /* And those after us won't have to: */
159
 
        tables[i].rowid_keep_flags&=  ~((int)JoinTable::CALL_POSITION);
 
151
        tables[i].rowid_keep_flags&=  ~((int)JOIN_TAB::CALL_POSITION);
160
152
      }
161
153
      copy++;
162
154
    }
165
157
  cache->length= length+blobs*sizeof(char*);
166
158
  cache->blobs= blobs;
167
159
  *blob_ptr= NULL;                                      /* End sequentel */
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
 
  }
 
160
  size= cmax((size_t) session->variables.join_buff_size, (size_t)cache->length);
174
161
  if (!(cache->buff= (unsigned char*) malloc(size)))
175
 
    return 1;
 
162
    return 1;                           /* Don't use cache */ /* purecov: inspected */
176
163
  cache->end= cache->buff+size;
177
 
  cache->reset_cache_write();
178
 
 
 
164
  reset_cache_write(cache);
179
165
  return 0;
180
166
}
181
167
 
182
 
bool JoinCache::store_record_in_cache()
 
168
bool store_record_in_cache(JOIN_CACHE *cache)
183
169
{
184
 
  JoinCache *cache= this;
185
 
  unsigned char *local_pos;
186
 
  CacheField *copy,*end_field;
 
170
  uint32_t length;
 
171
  unsigned char *pos;
 
172
  CACHE_FIELD *copy,*end_field;
187
173
  bool last_record;
188
174
 
189
 
  local_pos= cache->pos;
 
175
  pos= cache->pos;
190
176
  end_field= cache->field+cache->fields;
191
177
 
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
 
 
 
178
  length= cache->length;
 
179
  if (cache->blobs)
 
180
    length+= used_blob_length(cache->blob_ptr);
 
181
  if ((last_record= (length + cache->length > (size_t) (cache->end - pos))))
 
182
    cache->ptr_record= cache->records;
207
183
  /*
208
184
    There is room in cache. Put record there
209
185
  */
214
190
    {
215
191
      if (last_record)
216
192
      {
217
 
        copy->blob_field->get_image(local_pos, copy->length+sizeof(char*), copy->blob_field->charset());
218
 
        local_pos+= copy->length+sizeof(char*);
 
193
        copy->blob_field->get_image(pos, copy->length+sizeof(char*), copy->blob_field->charset());
 
194
        pos+= copy->length+sizeof(char*);
219
195
      }
220
196
      else
221
197
      {
222
 
        copy->blob_field->get_image(local_pos, copy->length, // blob length
 
198
        copy->blob_field->get_image(pos, copy->length, // blob length
223
199
                                    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;
 
200
        memcpy(pos+copy->length,copy->str,copy->blob_length);  // Blob data
 
201
        pos+= copy->length+copy->blob_length;
226
202
      }
227
203
    }
228
204
    else
229
205
    {
230
206
      // SemiJoinDuplicateElimination: Get the rowid into table->ref:
231
207
      if (copy->get_rowid)
232
 
        copy->get_rowid->cursor->position(copy->get_rowid->getInsertRecord());
 
208
        copy->get_rowid->file->position(copy->get_rowid->record[0]);
233
209
 
234
210
      if (copy->strip)
235
211
      {
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;
 
212
        unsigned char *str,*end;
 
213
        for (str= copy->str,end= str+copy->length; end > str && end[-1] == ' '; end--)
 
214
        {}
 
215
        length= (uint32_t) (end-str);
 
216
        memcpy(pos+2, str, length);
 
217
        int2store(pos, length);
 
218
        pos+= length+2;
243
219
      }
244
220
      else
245
221
      {
246
 
        memcpy(local_pos, copy->str, copy->length);
247
 
        local_pos+= copy->length;
 
222
        memcpy(pos,copy->str,copy->length);
 
223
        pos+= copy->length;
248
224
      }
249
225
    }
250
226
  }
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;
 
227
  cache->pos= pos;
 
228
  return last_record || (size_t) (cache->end - pos) < cache->length;
 
229
}
 
230
 
 
231
void reset_cache_read(JOIN_CACHE *cache)
 
232
{
 
233
  cache->record_nr= 0;
 
234
  cache->pos= cache->buff;
 
235
}
 
236
 
 
237
void reset_cache_write(JOIN_CACHE *cache)
 
238
{
 
239
  reset_cache_read(cache);
 
240
  cache->records= 0;
 
241
  cache->ptr_record= UINT32_MAX;
266
242
}
267
243
 
268
244
/**
269
245
  @} (end of group Query_Optimizer)
270
246
*/
271
 
 
272
 
} /* namespace drizzled */