~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/table/instance/base.cc

  • Committer: Brian Aker
  • Date: 2011-01-13 01:27:33 UTC
  • mfrom: (2069.4.4 clean)
  • mto: This revision was merged to the branch mainline in revision 2078.
  • Revision ID: brian@gir-3-20110113012733-1xg5mqvu1f0i1urr
Merge in the move for share to be the instance for table.

Show diffs side-by-side

added added

removed removed

Lines of Context:
91
91
 
92
92
extern size_t table_def_size;
93
93
 
94
 
/*****************************************************************************
95
 
  Functions to handle table definition cach (TableShare)
96
 
 *****************************************************************************/
97
 
 
98
 
/*
99
 
  Mark that we are not using table share anymore.
100
 
 
101
 
  SYNOPSIS
102
 
  release()
103
 
  share         Table share
104
 
 
105
 
  IMPLEMENTATION
106
 
  If ref_count goes to zero and (we have done a refresh or if we have
107
 
  already too many open table shares) then delete the definition.
108
 
*/
109
 
 
110
 
void TableShare::release(TableShare *share)
111
 
{
112
 
  bool to_be_deleted= false;
113
 
  safe_mutex_assert_owner(table::Cache::singleton().mutex().native_handle);
114
 
 
115
 
  share->lock();
116
 
  if (!--share->ref_count)
117
 
  {
118
 
    to_be_deleted= true;
119
 
  }
120
 
  share->unlock();
121
 
 
122
 
  if (to_be_deleted)
123
 
  {
124
 
    definition::Cache::singleton().erase(share->getCacheKey());
125
 
  }
126
 
}
127
 
 
128
 
void TableShare::release(TableShare::shared_ptr &share)
129
 
{
130
 
  bool to_be_deleted= false;
131
 
  safe_mutex_assert_owner(table::Cache::singleton().mutex().native_handle);
132
 
 
133
 
  share->lock();
134
 
  if (!--share->ref_count)
135
 
  {
136
 
    to_be_deleted= true;
137
 
  }
138
 
  share->unlock();
139
 
 
140
 
  if (to_be_deleted)
141
 
  {
142
 
    definition::Cache::singleton().erase(share->getCacheKey());
143
 
  }
144
 
}
145
 
 
146
 
void TableShare::release(const TableIdentifier &identifier)
147
 
{
148
 
  TableShare::shared_ptr share= definition::Cache::singleton().find(identifier.getKey());
149
 
  if (share)
150
 
  {
151
 
    share->version= 0;                          // Mark for delete
152
 
    if (share->ref_count == 0)
153
 
    {
154
 
      definition::Cache::singleton().erase(identifier.getKey());
155
 
    }
156
 
  }
157
 
}
158
 
 
159
 
 
160
 
static TableShare::shared_ptr foundTableShare(TableShare::shared_ptr share)
161
 
{
162
 
  /*
163
 
    We found an existing table definition. Return it if we didn't get
164
 
    an error when reading the table definition from file.
165
 
  */
166
 
 
167
 
  /* We must do a lock to ensure that the structure is initialized */
168
 
  if (share->error)
169
 
  {
170
 
    /* Table definition contained an error */
171
 
    share->open_table_error(share->error, share->open_errno, share->errarg);
172
 
 
173
 
    return TableShare::shared_ptr();
174
 
  }
175
 
 
176
 
  share->incrementTableCount();
177
 
 
178
 
  return share;
179
 
}
180
 
 
181
 
/*
182
 
  Get TableShare for a table.
183
 
 
184
 
  get_table_share()
185
 
  session                       Thread handle
186
 
  table_list            Table that should be opened
187
 
  key                   Table cache key
188
 
  key_length            Length of key
189
 
  error                 out: Error code from open_table_def()
190
 
 
191
 
  IMPLEMENTATION
192
 
  Get a table definition from the table definition cache.
193
 
  If it doesn't exist, create a new from the table definition file.
194
 
 
195
 
  NOTES
196
 
  We must have wrlock on table::Cache::singleton().mutex() when we come here
197
 
  (To be changed later)
198
 
 
199
 
  RETURN
200
 
  0  Error
201
 
#  Share for table
202
 
*/
203
 
 
204
 
TableShare::shared_ptr TableShare::getShareCreate(Session *session, 
205
 
                                                  const TableIdentifier &identifier,
206
 
                                                  int &in_error)
207
 
{
208
 
  TableShare::shared_ptr share;
209
 
 
210
 
  in_error= 0;
211
 
 
212
 
  /* Read table definition from cache */
213
 
  if ((share= definition::Cache::singleton().find(identifier.getKey())))
214
 
    return foundTableShare(share);
215
 
 
216
 
  share.reset(new TableShare(message::Table::STANDARD, identifier));
217
 
  
218
 
  if (share->open_table_def(*session, identifier))
219
 
  {
220
 
    in_error= share->error;
221
 
 
222
 
    return TableShare::shared_ptr();
223
 
  }
224
 
  share->ref_count++;                           // Mark in use
225
 
  
226
 
  plugin::EventObserver::registerTableEvents(*share);
227
 
 
228
 
  bool ret= definition::Cache::singleton().insert(identifier.getKey(), share);
229
 
 
230
 
  if (not ret)
231
 
    return TableShare::shared_ptr();
232
 
 
233
 
  return share;
234
 
}
235
94
 
236
95
static enum_field_types proto_field_type_to_drizzle_type(const message::Table::Field &field)
237
96
{
395
254
  table_proto(NULL),
396
255
  storage_engine(NULL),
397
256
  tmp_table(type_arg),
398
 
  ref_count(0),
 
257
  _ref_count(0),
399
258
  null_bytes(0),
400
259
  last_null_bit_pos(0),
401
260
  _field_size(0),
460
319
  table_proto(NULL),
461
320
  storage_engine(NULL),
462
321
  tmp_table(message::Table::INTERNAL),
463
 
  ref_count(0),
 
322
  _ref_count(0),
464
323
  null_bytes(0),
465
324
  last_null_bit_pos(0),
466
325
  _field_size(0),
535
394
  table_proto(NULL),
536
395
  storage_engine(NULL),
537
396
  tmp_table(identifier.getType()),
538
 
  ref_count(0),
 
397
  _ref_count(0),
539
398
  null_bytes(0),
540
399
  last_null_bit_pos(0),
541
400
  _field_size(0),
613
472
  table_proto(NULL),
614
473
  storage_engine(NULL),
615
474
  tmp_table(type_arg),
616
 
  ref_count(0),
 
475
  _ref_count(0),
617
476
  null_bytes(0),
618
477
  last_null_bit_pos(0),
619
478
  _field_size(0),
704
563
 
705
564
TableShare::~TableShare() 
706
565
{
707
 
  assert(ref_count == 0);
708
 
 
709
566
  storage_engine= NULL;
710
567
 
711
568
  delete table_proto;
1785
1642
  if (not error_reported)
1786
1643
    open_table_error(ret, errno, 0);
1787
1644
 
1788
 
  delete outparam.cursor;
 
1645
  boost::checked_delete(outparam.cursor);
1789
1646
  outparam.cursor= 0;                           // For easier error checking
1790
1647
  outparam.db_stat= 0;
1791
1648
  outparam.getMemRoot()->free_root(MYF(0));       // Safe to call on zeroed root