Revision a2c5a4cb src/pool/PoolSQL.cc

View differences:

src/pool/PoolSQL.cc
87 87
/* -------------------------------------------------------------------------- */
88 88
/* -------------------------------------------------------------------------- */
89 89

  
90
PoolSQL::PoolSQL(SqlDB * _db, const char * _table, bool _cache, bool by_name):
91
    db(_db), table(_table), uses_name_pool(by_name)
90
PoolSQL::PoolSQL(SqlDB * _db, const char * _table):
91
    db(_db), table(_table)
92 92
{
93 93
    pthread_mutex_init(&mutex,0);
94 94
};
......
98 98

  
99 99
PoolSQL::~PoolSQL()
100 100
{
101
    map<int,PoolObjectSQL *>::iterator  it;
101
    vector<PoolObjectSQL *>::iterator it;
102 102

  
103 103
    pthread_mutex_lock(&mutex);
104 104

  
105
    for ( it = pool.begin(); it != pool.end(); it++)
105
    for ( it = pool.begin(); it != pool.end(); ++it)
106 106
    {
107
        it->second->lock();
107
        (*it)->lock();
108 108

  
109
        delete it->second;
109
        delete *it;
110 110
    }
111 111

  
112 112
    pthread_mutex_unlock(&mutex);
......
192 192
        return 0;
193 193
    }
194 194

  
195
    pool.insert(make_pair(objectsql->oid, objectsql));
196

  
197
    if ( uses_name_pool )
198
    {
199
        string okey = key(objectsql->name, objectsql->uid);
200

  
201
        name_pool.insert(make_pair(okey, objectsql));
202
    }
195
    pool.push_back(objectsql);
203 196

  
204 197
    if ( olock == true )
205 198
    {
......
216 209

  
217 210
PoolObjectSQL * PoolSQL::get(const string& name, int ouid, bool olock)
218 211
{
219
    if ( uses_name_pool == false )
220
    {
221
        return 0;
222
    }
223

  
224 212
    lock();
225 213

  
226 214
    string name_key = key(name, ouid);
......
242 230
        return 0;
243 231
    }
244 232

  
245
    pool.insert(make_pair(objectsql->oid, objectsql));
246

  
247
    name_pool.insert(make_pair(name_key, objectsql));
233
    pool.push_back(objectsql);
248 234

  
249 235
    if ( olock == true )
250 236
    {
......
261 247

  
262 248
void PoolSQL::flush_cache(int oid)
263 249
{
264
    int  rc;
265
    PoolObjectSQL * tmp_ptr;
266

  
267
    map<int,PoolObjectSQL *>::iterator  it;
268

  
269
    for (it = pool.begin(); it != pool.end(); )
250
    for (vector<PoolObjectSQL *>::iterator it = pool.begin(); it != pool.end();)
270 251
    {
271 252
        // The object we are looking for in ::get(). Wait until it is unlocked()
272
        if (it->first == oid)
253
        if ((*it)->oid == oid)
273 254
        {
274
            it->second->lock();
255
            (*it)->lock();
275 256
        }
276 257
        else
277 258
        {
278 259
            // Any other locked object is just ignored
279
            rc = pthread_mutex_trylock(&(it->second->mutex));
260
            int rc = pthread_mutex_trylock(&((*it)->mutex));
280 261

  
281 262
            if ( rc == EBUSY ) // In use by other thread
282 263
            {
......
285 266
            }
286 267
        }
287 268

  
288
        tmp_ptr = it->second;
269
        delete *it;
289 270

  
290
        // map::erase does not invalidate iterator, except for the current one
291 271
        it = pool.erase(it);
292

  
293
        if ( uses_name_pool )
294
        {
295
            string okey = key(tmp_ptr->name, tmp_ptr->uid);
296

  
297
            name_pool.erase(okey);
298
        }
299

  
300
        delete tmp_ptr;
301 272
    }
302 273
}
303 274

  
......
306 277

  
307 278
void PoolSQL::flush_cache(const string& name_key)
308 279
{
309
    int  rc;
310
    PoolObjectSQL * tmp_ptr;
311

  
312
    map<string,PoolObjectSQL *>::iterator it;
313

  
314
    for (it = name_pool.begin(); it != name_pool.end(); )
280
    for (vector<PoolObjectSQL *>::iterator it = pool.begin(); it != pool.end();)
315 281
    {
282
        string okey = key((*it)->name, (*it)->uid);
283

  
316 284
        // The object we are looking for in ::get(). Wait until it is unlocked()
317
        if (name_key == it->first)
285
        if ( name_key == okey)
318 286
        {
319
            it->second->lock();
287
            (*it)->lock();
320 288
        }
321 289
        else
322 290
        {
323 291
            // Any other locked object is just ignored
324
            rc = pthread_mutex_trylock(&(it->second->mutex));
292
            int rc = pthread_mutex_trylock(&((*it)->mutex));
325 293

  
326 294
            if ( rc == EBUSY ) // In use by other thread
327 295
            {
......
330 298
            }
331 299
        }
332 300

  
333
        tmp_ptr = it->second;
334

  
335
        // map::erase does not invalidate iterator, except for the current one
336
        it = name_pool.erase(it);
337

  
338
        pool.erase(tmp_ptr->oid);
301
        delete *it;
339 302

  
340
        delete tmp_ptr;
303
        it = pool.erase(it);
341 304
    }
342 305
}
343 306

  
......
346 309

  
347 310
void PoolSQL::clean()
348 311
{
349
    map<int,PoolObjectSQL *>::iterator  it;
312
    vector<PoolObjectSQL *>::iterator it;
350 313

  
351 314
    lock();
352 315

  
353
    for ( it = pool.begin(); it != pool.end(); it++)
316
    for (it = pool.begin(); it != pool.end(); ++it)
354 317
    {
355
        it->second->lock();
318
        (*it)->lock();
356 319

  
357
        delete it->second;
320
        delete *it;
358 321
    }
359 322

  
360 323
    pool.clear();
361 324

  
362
    name_pool.clear();
363

  
364 325
    unlock();
365 326
}
366 327

  

Also available in: Unified diff