summaryrefslogtreecommitdiff
path: root/cpp/src/Freeze/SharedDb.cpp
blob: 511525c130693e87cec53eb498e65804ab2e3a1e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
// **********************************************************************
//
// Copyright (c) 2003-2005 ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************

#include <Freeze/SharedDb.h>
#include <IceUtil/StaticMutex.h>
#include <Freeze/Exception.h>
#include <Freeze/Util.h>
#include <Freeze/Catalog.h>

using namespace std;
using namespace IceUtil;
using namespace Ice;
using namespace Freeze;

namespace
{

StaticMutex _mapMutex = ICE_STATIC_MUTEX_INITIALIZER;
StaticMutex _refCountMutex = ICE_STATIC_MUTEX_INITIALIZER;  

const string _catalogName = "__catalog";

inline void
checkTypes(const SharedDb& sharedDb, const string& key, const string& value)
{
    if(key != sharedDb.key())
    {
	DatabaseException ex(__FILE__, __LINE__);
	ex.message = sharedDb.dbName() + "'s key type is " + sharedDb.key() + ", not " + key;
	throw ex;
    }
    if(value != sharedDb.value())
    {
	DatabaseException ex(__FILE__, __LINE__);
	ex.message = sharedDb.dbName() + "'s value type is " + sharedDb.value() + ", not " + value;
	throw ex;
    }
}
}

extern "C" 
{
static int customCompare(DB* db, const DBT* dbt1, const DBT* dbt2)
{
    SharedDb* me = static_cast<SharedDb*>(db->app_private);
    Byte* first = static_cast<Byte*>(dbt1->data);
    Key k1(first, first + dbt1->size);
    first = static_cast<Byte*>(dbt2->data);
    Key k2(first, first + dbt2->size);

    return me->getKeyCompare()->compare(k1, k2);
}
}

Freeze::SharedDb::SharedDbMap* Freeze::SharedDb::sharedDbMap = 0;

const string&
Freeze::catalogName()
{
    return _catalogName;
}

SharedDbPtr 
Freeze::SharedDb::get(const ConnectionIPtr& connection, 
		      const string& dbName,
		      const string& key,
		      const string& value,
		      const KeyCompareBasePtr& keyCompare,
		      const vector<MapIndexBasePtr>& indices,
		      bool createDb)
{
    if(dbName == _catalogName)
    {
	//
	// We don't want to lock the _mapMutex to retrieve the catalog
	//
	
	SharedDbPtr result = connection->dbEnv()->getCatalog();
	checkTypes(*result, key, value);
	return result;
    }
    
    StaticMutex::Lock lock(_mapMutex);

    if(sharedDbMap == 0)
    {
	sharedDbMap = new SharedDbMap;
    }

    MapKey mapKey;
    mapKey.envName = connection->envName();
    mapKey.communicator = connection->communicator();
    mapKey.dbName = dbName;

    {
	SharedDbMap::iterator p = sharedDbMap->find(mapKey);
	if(p != sharedDbMap->end())
	{
	    checkTypes(*(p->second), key, value);
	    p->second->connectIndices(indices);
	    return p->second;
	}
    }

    //
    // MapKey not found, let's create and open a new Db
    //
    auto_ptr<SharedDb> result(new SharedDb(mapKey, key, value, connection, 
					   keyCompare, indices, createDb));
    
    //
    // Insert it into the map
    //
    pair<SharedDbMap::iterator, bool> insertResult;
    insertResult= sharedDbMap->insert(SharedDbMap::value_type(mapKey, result.get()));
    assert(insertResult.second);
    
    return result.release();
}

Freeze::SharedDbPtr 
Freeze::SharedDb::openCatalog(SharedDbEnv& dbEnv)
{
    StaticMutex::Lock lock(_mapMutex);

    if(sharedDbMap == 0)
    {
	sharedDbMap = new SharedDbMap;
    }

    MapKey mapKey;
    mapKey.envName = dbEnv.getEnvName();
    mapKey.communicator = dbEnv.getCommunicator();
    mapKey.dbName = _catalogName;

    auto_ptr<SharedDb> result(new SharedDb(mapKey, dbEnv.getEnv()));

    //
    // Insert it into the map
    //
    pair<SharedDbMap::iterator, bool> insertResult 
	= sharedDbMap->insert(SharedDbMap::value_type(mapKey, result.get()));
    
    if(!insertResult.second)
    {
	//
	// That's very wrong: the catalog is associated with another env
	//
	assert(0);
	DatabaseException ex(__FILE__, __LINE__);
	ex.message = "Catalog already opened";
	throw ex;
    }
    
    return result.release();
}


Freeze::SharedDb::~SharedDb()
{
    if(_trace >= 1)
    {
	Trace out(_mapKey.communicator->getLogger(), "Freeze.Map");
	out << "closing Db \"" << _mapKey.dbName << "\"";
    }

    cleanup(false);
}

void Freeze::SharedDb::__incRef()
{
    if(_trace >= 2)
    {
	Trace out(_mapKey.communicator->getLogger(), "Freeze.Map");
	out << "incremeting reference count for Db \"" << _mapKey.dbName << "\"";
    }

    IceUtil::StaticMutex::Lock lock(_refCountMutex);
    _refCount++;
}

void Freeze::SharedDb::__decRef()
{
    if(_trace >= 2)
    {
	Trace out(_mapKey.communicator->getLogger(), "Freeze.Map");
	out << "removing reference count for Db \"" << _mapKey.dbName << "\"";
    }

    IceUtil::StaticMutex::Lock lock(_refCountMutex);
    if(--_refCount == 0)
    {
        IceUtil::StaticMutex::TryLock mapLock(_mapMutex);
	if(!mapLock.acquired())
	{
	    //
	    // Reacquire mutex in proper order and check again
	    //
	    lock.release();
	    mapLock.acquire();
	    lock.acquire();
	    if(_refCount > 0)
	    {
		return;
	    }
	}

	//
	// Remove from map
	//
	size_t one;
	one = sharedDbMap->erase(_mapKey);
	assert(one == 1);

	if(sharedDbMap->size() == 0)
	{
	    delete sharedDbMap;
	    sharedDbMap = 0;
	}

	//
	// Keep lock to prevent somebody else to re-open this Db
	// before it's closed.
	//
	delete this;
    }
}

  
Freeze::SharedDb::SharedDb(const MapKey& mapKey, 
			   const string& key,
			   const string& value,
			   const ConnectionIPtr& connection, 
			   const KeyCompareBasePtr& keyCompare,
			   const vector<MapIndexBasePtr>& indices,
			   bool createDb) :
    Db(connection->dbEnv()->getEnv(), 0),
    _mapKey(mapKey),
    _refCount(0),
    _trace(connection->trace()),
    _keyCompare(keyCompare)
{
    if(_trace >= 1)
    {
	Trace out(_mapKey.communicator->getLogger(), "Freeze.Map");
	out << "opening Db \"" << _mapKey.dbName << "\"";
    }

    ConnectionPtr catalogConnection = 
	createConnection(_mapKey.communicator, connection->dbEnv()->getEnvName());
    Catalog catalog(catalogConnection, _catalogName);
    
    Catalog::iterator ci = catalog.find(_mapKey.dbName);
    
    if(ci != catalog.end())
    {
	if(ci->second.evictor)
	{
	    DatabaseException ex(__FILE__, __LINE__);
	    ex.message = _mapKey.dbName + " is an evictor database";
	    throw ex;
	}

	_key = ci->second.key;
	_value = ci->second.value;
	checkTypes(*this, key, value);
    }
    else
    {
	_key = key;
	_value = value;
    }

    set_app_private(this);
    if(_keyCompare->compareEnabled())
    {
	set_bt_compare(&customCompare);
    }

    try
    {
	TransactionPtr tx = catalogConnection->beginTransaction();
	DbTxn* txn = getTxn(tx);

	u_int32_t flags = DB_THREAD;
	if(createDb)
	{
	    flags |= DB_CREATE;
	}
	open(txn, _mapKey.dbName.c_str(), 0, DB_BTREE, flags, FREEZE_DB_MODE);

	for(vector<MapIndexBasePtr>::const_iterator p = indices.begin();
	    p != indices.end(); ++p)
	{
	    const MapIndexBasePtr& indexBase = *p; 
	    assert(indexBase->_impl == 0);
	    assert(indexBase->_communicator == 0);
	    indexBase->_communicator = connection->communicator();

	    auto_ptr<MapIndexI> indexI(new MapIndexI(connection, *this, txn, createDb, indexBase));
	    
#ifndef NDEBUG
	    bool inserted = 
#endif
		_indices.insert(IndexMap::value_type(indexBase->name(), indexI.get())).second;
	    assert(inserted);
	    
	    indexBase->_impl = indexI.release();
	}

	if(ci == catalog.end())
	{
	    CatalogData catalogData;
	    catalogData.evictor = false;
	    catalogData.key = key;
	    catalogData.value = value;
	    catalog.put(Catalog::value_type(_mapKey.dbName, catalogData));
	}
	
	tx->commit();
    }
    catch(const ::DbException& dx)
    {
	TransactionPtr tx = catalogConnection->currentTransaction();
	if(tx != 0)
	{
	    try
	    {
		tx->rollback();
	    }
	    catch(...)
	    {
	    }
	}

	cleanup(true);

	if(dx.get_errno() == ENOENT)
	{
	    NotFoundException ex(__FILE__, __LINE__);
	    ex.message = dx.what();
	    throw ex;
	}
	else
	{
	    DatabaseException ex(__FILE__, __LINE__);
	    ex.message = dx.what();
	    throw ex;
	}
    }
    catch(...)
    {
	TransactionPtr tx = catalogConnection->currentTransaction();
	if(tx != 0)
	{
	    try
	    {
		tx->rollback();
	    }
	    catch(...)
	    {
	    }
	}

	cleanup(true);
	throw;
    }
}


Freeze::SharedDb::SharedDb(const MapKey& mapKey, DbEnv* env) :
    Db(env, 0),
    _mapKey(mapKey),
    _key(CatalogKeyCodec::typeId()),
    _value(CatalogValueCodec::typeId()),
    _refCount(0)
{
    _trace = _mapKey.communicator->getProperties()->getPropertyAsInt("Freeze.Trace.Map");

    if(_trace >= 1)
    {
	Trace out(_mapKey.communicator->getLogger(), "Freeze.Db");
	out << "opening Db \"" << _mapKey.dbName << "\"";
    }

    try
    {
	u_int32_t flags = DB_THREAD | DB_CREATE | DB_AUTO_COMMIT;
	open(0, _mapKey.dbName.c_str(), 0, DB_BTREE, flags, FREEZE_DB_MODE);
    }
    catch(const ::DbException& dx)
    {
	DatabaseException ex(__FILE__, __LINE__);
	ex.message = dx.what();
	throw ex;
    }
}

void
Freeze::SharedDb::connectIndices(const vector<MapIndexBasePtr>& indices) const
{
    for(vector<MapIndexBasePtr>::const_iterator p = indices.begin();
	p != indices.end(); ++p)
    {
	const MapIndexBasePtr& indexBase = *p; 
	assert(indexBase->_impl == 0);

	IndexMap::const_iterator q = _indices.find(indexBase->name());
	assert(q != _indices.end());
	indexBase->_impl = q->second;
	indexBase->_communicator = _mapKey.communicator;
    }
}

void
Freeze::SharedDb::cleanup(bool noThrow)
{
    try
    {
	for(IndexMap::iterator p = _indices.begin(); p != _indices.end(); ++p)
	{
	    delete p->second;
	}
	_indices.clear();
	
	close(0);
    }
    catch(const ::DbException& dx)
    {
	if(!noThrow)
	{
	    DatabaseException ex(__FILE__, __LINE__);
	    ex.message = dx.what();
	    throw ex;
	}
    }
}