~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/schema_engine/schema.cc

  • Committer: patrick crews
  • Date: 2010-09-16 14:38:00 UTC
  • mfrom: (1768 staging)
  • mto: (1771.1.1 pcrews)
  • mto: This revision was merged to the branch mainline in revision 1772.
  • Revision ID: gleebix@gmail.com-20100916143800-hyu1tcfnjcowfnpb
Merge with trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
60
60
  schema_cache_filled(false)
61
61
{
62
62
  table_definition_ext= DEFAULT_FILE_EXTENSION;
63
 
  pthread_rwlock_init(&schema_lock, NULL);
64
63
  prime();
65
64
}
66
65
 
67
66
Schema::~Schema()
68
67
{
69
 
  pthread_rwlock_destroy(&schema_lock);
70
68
}
71
69
 
72
70
void Schema::prime()
74
72
  CachedDirectory directory(data_home, CachedDirectory::DIRECTORY);
75
73
  CachedDirectory::Entries files= directory.getEntries();
76
74
 
77
 
  pthread_rwlock_wrlock(&schema_lock);
 
75
  mutex.lock();
78
76
 
79
77
  for (CachedDirectory::Entries::iterator fileIter= files.begin();
80
78
       fileIter != files.end(); fileIter++)
98
96
      }
99
97
    }
100
98
  }
101
 
  pthread_rwlock_unlock(&schema_lock);
 
99
  mutex.unlock();
102
100
}
103
101
 
104
102
void Schema::doGetSchemaIdentifiers(SchemaIdentifiers &set_of_names)
105
103
{
106
 
  if (not pthread_rwlock_rdlock(&schema_lock))
 
104
  mutex.lock_shared();
107
105
  {
108
106
    for (SchemaCache::iterator iter= schema_cache.begin();
109
107
         iter != schema_cache.end();
111
109
    {
112
110
      set_of_names.push_back(SchemaIdentifier((*iter).second.name()));
113
111
    }
114
 
    pthread_rwlock_unlock(&schema_lock);
115
 
 
116
 
    return;
117
 
  }
118
 
 
119
 
  // If for some reason getting a lock should fail, we resort to disk
120
 
 
121
 
  CachedDirectory directory(data_home, CachedDirectory::DIRECTORY);
122
 
 
123
 
  CachedDirectory::Entries files= directory.getEntries();
124
 
 
125
 
  for (CachedDirectory::Entries::iterator fileIter= files.begin();
126
 
       fileIter != files.end(); fileIter++)
127
 
  {
128
 
    CachedDirectory::Entry *entry= *fileIter;
129
 
    set_of_names.push_back(entry->filename);
130
 
  }
 
112
  }
 
113
  mutex.unlock_shared();
131
114
}
132
115
 
133
116
bool Schema::doGetSchemaDefinition(const SchemaIdentifier &schema_identifier, message::Schema &schema_message)
134
117
{
135
 
  if (not pthread_rwlock_rdlock(&schema_lock))
 
118
  mutex.lock_shared();
 
119
  SchemaCache::iterator iter= schema_cache.find(schema_identifier.getPath());
 
120
 
 
121
  if (iter != schema_cache.end())
136
122
  {
137
 
    SchemaCache::iterator iter= schema_cache.find(schema_identifier.getPath());
138
 
 
139
 
    if (iter != schema_cache.end())
140
 
    {
141
 
      schema_message.CopyFrom(((*iter).second));
142
 
      pthread_rwlock_unlock(&schema_lock);
143
 
      return true;
144
 
    }
145
 
    pthread_rwlock_unlock(&schema_lock);
146
 
 
147
 
    return false;
 
123
    schema_message.CopyFrom(((*iter).second));
 
124
    mutex.unlock_shared();
 
125
    return true;
148
126
  }
 
127
  mutex.unlock_shared();
149
128
 
150
 
  // Fail to disk based means
151
 
  return readSchemaFile(schema_identifier.getPath(), schema_message);
 
129
  return false;
152
130
}
153
131
 
 
132
 
154
133
bool Schema::doCreateSchema(const drizzled::message::Schema &schema_message)
155
134
{
156
135
  SchemaIdentifier schema_identifier(schema_message.name());
165
144
    return false;
166
145
  }
167
146
 
168
 
  if (not pthread_rwlock_wrlock(&schema_lock))
 
147
  mutex.lock();
169
148
  {
170
 
      pair<SchemaCache::iterator, bool> ret=
171
 
        schema_cache.insert(make_pair(schema_identifier.getPath(), schema_message));
172
 
 
173
 
 
174
 
      if (ret.second == false)
175
 
      {
176
 
        abort(); // If this has happened, something really bad is going down.
177
 
      }
178
 
    pthread_rwlock_unlock(&schema_lock);
 
149
    pair<SchemaCache::iterator, bool> ret=
 
150
      schema_cache.insert(make_pair(schema_identifier.getPath(), schema_message));
 
151
 
 
152
 
 
153
    if (ret.second == false)
 
154
    {
 
155
      abort(); // If this has happened, something really bad is going down.
 
156
    }
179
157
  }
 
158
  mutex.unlock();
180
159
 
181
160
  return true;
182
161
}
214
193
    cerr << dir;
215
194
  }
216
195
 
217
 
  if (not pthread_rwlock_wrlock(&schema_lock))
218
 
  {
219
 
    schema_cache.erase(schema_identifier.getPath());
220
 
    pthread_rwlock_unlock(&schema_lock);
221
 
  }
 
196
  mutex.lock();
 
197
  schema_cache.erase(schema_identifier.getPath());
 
198
  mutex.unlock();
222
199
 
223
200
  return true;
224
201
}
232
209
 
233
210
  if (writeSchemaFile(schema_identifier, schema_message))
234
211
  {
235
 
    if (not pthread_rwlock_wrlock(&schema_lock))
 
212
    mutex.lock();
236
213
    {
237
214
      schema_cache.erase(schema_identifier.getPath());
238
215
 
243
220
      {
244
221
        abort(); // If this has happened, something really bad is going down.
245
222
      }
246
 
 
247
 
      pthread_rwlock_unlock(&schema_lock);
248
 
    }
249
 
    else
250
 
    {
251
 
      abort(); // This would leave us out of sync, suck.
252
 
    }
 
223
    }
 
224
    mutex.unlock();
253
225
  }
254
226
 
255
227
  return true;