~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/join_cache.cc

  • Committer: Brian Aker
  • Date: 2009-08-18 07:20:29 UTC
  • mfrom: (1117.1.9 merge)
  • Revision ID: brian@gaz-20090818072029-s9ch5lcmltxwidn7
Merge of Brian

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, Inc.
 
4
 *  Copyright (C) 2008-2009 Sun Microsystems
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
27
27
 * @{
28
28
 */
29
29
 
30
 
#include <config.h>
31
 
 
32
 
#include <drizzled/sql_select.h> /* include join.h */
33
 
#include <drizzled/field/blob.h>
34
 
#include <drizzled/drizzled.h>
35
 
#include <drizzled/internal/my_sys.h>
36
 
#include <drizzled/table.h>
37
 
#include <drizzled/session.h>
 
30
#include "drizzled/server_includes.h"
 
31
#include "drizzled/sql_select.h" /* include join.h */
 
32
#include "drizzled/field/blob.h"
38
33
 
39
34
#include <algorithm>
40
35
 
41
36
using namespace std;
42
37
 
43
 
namespace drizzled
44
 
{
45
 
 
46
 
static uint32_t used_blob_length(CacheField **ptr);
47
 
 
48
 
static uint32_t used_blob_length(CacheField **ptr)
 
38
static uint32_t used_blob_length(CACHE_FIELD **ptr);
 
39
 
 
40
static uint32_t used_blob_length(CACHE_FIELD **ptr)
49
41
{
50
42
  uint32_t length,blob_length;
51
43
  for (length=0 ; *ptr ; ptr++)
65
57
******************************************************************************/
66
58
int join_init_cache(Session *session, JoinTable *tables, uint32_t table_count)
67
59
{
 
60
  register unsigned int i;
68
61
  unsigned int length, blobs;
69
62
  size_t size;
70
 
  CacheField *copy,**blob_ptr;
71
 
  JoinCache  *cache;
 
63
  CACHE_FIELD *copy,**blob_ptr;
 
64
  JOIN_CACHE  *cache;
72
65
  JoinTable *join_tab;
73
66
 
74
67
  cache= &tables[table_count].cache;
75
68
  cache->fields=blobs=0;
76
69
 
77
70
  join_tab= tables;
78
 
  for (unsigned int i= 0; i < table_count ; i++, join_tab++)
 
71
  for (i=0 ; i < table_count ; i++,join_tab++)
79
72
  {
80
73
    if (!join_tab->used_fieldlength)            /* Not calced yet */
81
74
      calc_used_field_length(session, join_tab);
86
79
    if (join_tab->rowid_keep_flags & JoinTable::KEEP_ROWID)
87
80
    {
88
81
      cache->fields++;
89
 
      join_tab->used_fieldlength += join_tab->table->cursor->ref_length;
 
82
      join_tab->used_fieldlength += join_tab->table->file->ref_length;
90
83
    }
91
84
  }
92
 
  if (!(cache->field=(CacheField*)
93
 
        memory::sql_alloc(sizeof(CacheField)*(cache->fields+table_count*2)+(blobs+1)* sizeof(CacheField*))))
 
85
  if (!(cache->field=(CACHE_FIELD*)
 
86
        sql_alloc(sizeof(CACHE_FIELD)*(cache->fields+table_count*2)+(blobs+1)*
 
87
 
 
88
                  sizeof(CACHE_FIELD*))))
94
89
  {
95
 
    size= cache->end - cache->buff;
96
 
    global_join_buffer.sub(size);
97
 
    free((unsigned char*) cache->buff);
98
 
    cache->buff=0;
99
 
    return(1);
 
90
    free((unsigned char*) cache->buff);         /* purecov: inspected */
 
91
    cache->buff=0;                              /* purecov: inspected */
 
92
    return(1);                          /* purecov: inspected */
100
93
  }
101
94
  copy=cache->field;
102
 
  blob_ptr=cache->blob_ptr=(CacheField**)
 
95
  blob_ptr=cache->blob_ptr=(CACHE_FIELD**)
103
96
    (cache->field+cache->fields+table_count*2);
104
97
 
105
98
  length=0;
106
 
  for (unsigned int i= 0 ; i < table_count ; i++)
 
99
  for (i=0 ; i < table_count ; i++)
107
100
  {
108
101
    uint32_t null_fields=0, used_fields;
109
102
    Field **f_ptr,*field;
110
 
    for (f_ptr= tables[i].table->getFields(), used_fields= tables[i].used_fields; used_fields; f_ptr++)
 
103
    for (f_ptr= tables[i].table->field,used_fields= tables[i].used_fields; used_fields; f_ptr++)
111
104
    {
112
105
      field= *f_ptr;
113
106
      if (field->isReadSet())
126
119
    if (null_fields && tables[i].table->getNullFields())
127
120
    {                                           /* must copy null bits */
128
121
      copy->str= tables[i].table->null_flags;
129
 
      copy->length= tables[i].table->getShare()->null_bytes;
 
122
      copy->length= tables[i].table->s->null_bytes;
130
123
      copy->strip=0;
131
124
      copy->blob_field=0;
132
125
      copy->get_rowid= NULL;
149
142
    /* SemiJoinDuplicateElimination: Allocate space for rowid if needed */
150
143
    if (tables[i].rowid_keep_flags & JoinTable::KEEP_ROWID)
151
144
    {
152
 
      copy->str= tables[i].table->cursor->ref;
153
 
      copy->length= tables[i].table->cursor->ref_length;
 
145
      copy->str= tables[i].table->file->ref;
 
146
      copy->length= tables[i].table->file->ref_length;
154
147
      copy->strip=0;
155
148
      copy->blob_field=0;
156
149
      copy->get_rowid= NULL;
169
162
  cache->blobs= blobs;
170
163
  *blob_ptr= NULL;                                      /* End sequentel */
171
164
  size= max((size_t) session->variables.join_buff_size, (size_t)cache->length);
172
 
  if (not global_join_buffer.add(size))
173
 
  {
174
 
    my_error(ER_OUT_OF_GLOBAL_JOINMEMORY, MYF(ME_ERROR+ME_WAITTANG));
175
 
    return 1;
176
 
  }
177
165
  if (!(cache->buff= (unsigned char*) malloc(size)))
178
 
    return 1;
 
166
    return 1;                           /* Don't use cache */ /* purecov: inspected */
179
167
  cache->end= cache->buff+size;
180
 
  cache->reset_cache_write();
181
 
 
 
168
  reset_cache_write(cache);
182
169
  return 0;
183
170
}
184
171
 
185
 
bool JoinCache::store_record_in_cache()
 
172
bool store_record_in_cache(JOIN_CACHE *cache)
186
173
{
187
 
  JoinCache *cache= this;
188
 
  unsigned char *local_pos;
189
 
  CacheField *copy,*end_field;
 
174
  uint32_t length;
 
175
  unsigned char *pos;
 
176
  CACHE_FIELD *copy,*end_field;
190
177
  bool last_record;
191
178
 
192
 
  local_pos= cache->pos;
 
179
  pos= cache->pos;
193
180
  end_field= cache->field+cache->fields;
194
181
 
195
 
  {
196
 
    uint32_t local_length;
197
 
 
198
 
    local_length= cache->length;
199
 
    if (cache->blobs)
200
 
    {
201
 
      local_length+= used_blob_length(cache->blob_ptr);
202
 
    }
203
 
 
204
 
    if ((last_record= (local_length + cache->length > (size_t) (cache->end - local_pos))))
205
 
    {
206
 
      cache->ptr_record= cache->records;
207
 
    }
208
 
  }
209
 
 
 
182
  length= cache->length;
 
183
  if (cache->blobs)
 
184
    length+= used_blob_length(cache->blob_ptr);
 
185
  if ((last_record= (length + cache->length > (size_t) (cache->end - pos))))
 
186
    cache->ptr_record= cache->records;
210
187
  /*
211
188
    There is room in cache. Put record there
212
189
  */
217
194
    {
218
195
      if (last_record)
219
196
      {
220
 
        copy->blob_field->get_image(local_pos, copy->length+sizeof(char*), copy->blob_field->charset());
221
 
        local_pos+= copy->length+sizeof(char*);
 
197
        copy->blob_field->get_image(pos, copy->length+sizeof(char*), copy->blob_field->charset());
 
198
        pos+= copy->length+sizeof(char*);
222
199
      }
223
200
      else
224
201
      {
225
 
        copy->blob_field->get_image(local_pos, copy->length, // blob length
 
202
        copy->blob_field->get_image(pos, copy->length, // blob length
226
203
                                    copy->blob_field->charset());
227
 
        memcpy(local_pos + copy->length,copy->str,copy->blob_length);  // Blob data
228
 
        local_pos+= copy->length+copy->blob_length;
 
204
        memcpy(pos+copy->length,copy->str,copy->blob_length);  // Blob data
 
205
        pos+= copy->length+copy->blob_length;
229
206
      }
230
207
    }
231
208
    else
232
209
    {
233
210
      // SemiJoinDuplicateElimination: Get the rowid into table->ref:
234
211
      if (copy->get_rowid)
235
 
        copy->get_rowid->cursor->position(copy->get_rowid->getInsertRecord());
 
212
        copy->get_rowid->file->position(copy->get_rowid->record[0]);
236
213
 
237
214
      if (copy->strip)
238
215
      {
239
 
        unsigned char *str, *local_end;
240
 
        for (str= copy->str,local_end= str+copy->length; local_end > str && local_end[-1] == ' '; local_end--) {}
241
 
 
242
 
        uint32_t local_length= (uint32_t) (local_end - str);
243
 
        memcpy(local_pos+2, str, local_length);
244
 
        int2store(local_pos, local_length);
245
 
        local_pos+= local_length+2;
 
216
        unsigned char *str,*end;
 
217
        for (str= copy->str,end= str+copy->length; end > str && end[-1] == ' '; end--)
 
218
        {}
 
219
        length= (uint32_t) (end-str);
 
220
        memcpy(pos+2, str, length);
 
221
        int2store(pos, length);
 
222
        pos+= length+2;
246
223
      }
247
224
      else
248
225
      {
249
 
        memcpy(local_pos, copy->str, copy->length);
250
 
        local_pos+= copy->length;
 
226
        memcpy(pos,copy->str,copy->length);
 
227
        pos+= copy->length;
251
228
      }
252
229
    }
253
230
  }
254
 
  cache->pos= local_pos;
255
 
  return last_record || (size_t) (cache->end - local_pos) < cache->length;
256
 
}
257
 
 
258
 
void JoinCache::reset_cache_read()
259
 
{
260
 
  record_nr= 0;
261
 
  pos= buff;
262
 
}
263
 
 
264
 
void JoinCache::reset_cache_write()
265
 
{
266
 
  reset_cache_read();
267
 
  records= 0;
268
 
  ptr_record= UINT32_MAX;
 
231
  cache->pos= pos;
 
232
  return last_record || (size_t) (cache->end - pos) < cache->length;
 
233
}
 
234
 
 
235
void reset_cache_read(JOIN_CACHE *cache)
 
236
{
 
237
  cache->record_nr= 0;
 
238
  cache->pos= cache->buff;
 
239
}
 
240
 
 
241
void reset_cache_write(JOIN_CACHE *cache)
 
242
{
 
243
  reset_cache_read(cache);
 
244
  cache->records= 0;
 
245
  cache->ptr_record= UINT32_MAX;
269
246
}
270
247
 
271
248
/**
272
249
  @} (end of group Query_Optimizer)
273
250
*/
274
 
 
275
 
} /* namespace drizzled */