summaryrefslogtreecommitdiffstats
path: root/relmddif/dbmddobj.pgc
blob: 58f41d076dfa24433cdf7ee3f7e5e7ee325dd8ed (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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
// This is -*- C++ -*-

/*
* This file is part of rasdaman community.
*
* Rasdaman community is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Rasdaman community is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with rasdaman community.  If not, see <http://www.gnu.org/licenses/>.
*
* Copyright 2003, 2004, 2005, 2006, 2007, 2008, 2009 Peter Baumann /
rasdaman GmbH.
*
* For more information please see <http://www.rasdaman.org>
* or contact Peter Baumann via <baumann@rasdaman.com>.
*/
/*************************************************************
 *
 *
 * PURPOSE:
 *
 *
 * COMMENTS:
 *
 ************************************************************/


#include "reladminif/oidif.hh"
#include "reladminif/dbref.hh"
#include "reladminif/sqlerror.hh"
#include "reladminif/externs.h"
#include "raslib/rmdebug.hh"
#include "catalogmgr/typefactory.hh"
#include "relcatalogif/mddbasetype.hh"
#include "relcatalogif/basetype.hh"
#include "reladminif/objectbroker.hh"
#include "relcatalogif/dbminterval.hh"
#include "relstorageif/dbstoragelayout.hh"
#include "dbmddobj.hh"
#include "relindexif/indexid.hh"
#include "indexmgr/indexds.hh"

#include "debug-srv.hh"


DBMDDObj::DBMDDObj()
	:	DBObject(),
		myDomain(NULL),
                objIxId(),
		mddType(NULL),
		persistentRefCount(0),
		storageLayoutId(new DBStorageLayout())
{
	RMDBGENTER(7, RMDebug::module_mddif, "DBMDDObj", "DBMDDObj()");
	ENTER( "DBMDDObj::DBMDDObj" );

	objecttype = OId::MDDOID;
	myDomain = new DBMinterval();
	myDomain->setPersistent(true);
	storageLayoutId->setPersistent(true);

	LEAVE( "DBMDDObj::DBMDDObj" );
	RMDBGEXIT(7, RMDebug::module_mddif, "DBMDDObj", "DBMDDObj()");
}

DBMDDObj::DBMDDObj(const OId& id) throw (r_Error)
	:	DBObject(id),
		myDomain(NULL),
                objIxId(),
		persistentRefCount(0),
		mddType(NULL),
		storageLayoutId((double)0)
{
	RMDBGENTER(7, RMDebug::module_mddif, "DBMDDObj", "DBMDDObj(" << myOId << ")");
	ENTER( "DBMDDObj::DBMDDObj, id=" << id );

	objecttype = OId::MDDOID;
	readFromDb();

	LEAVE( "DBMDDObj::DBMDDObj" );
	RMDBGEXIT(7, RMDebug::module_mddif, "DBMDDObj", "DBMDDObj(" << myOId << ")");
}

DBMDDObj::DBMDDObj(	const MDDBaseType* newMDDType,
			const r_Minterval& domain,
			const DBObjectId& newObjIx,
			const DBStorageLayoutId& newSL,
			const OId& newOId) throw (r_Error)
	:	DBObject(),
		objIxId(newObjIx.getOId()),
		mddType(newMDDType),
		persistentRefCount(0),
		storageLayoutId(newSL),
		myDomain(NULL)
{
	RMDBGENTER(7, RMDebug::module_mddif, "DBMDDObj", "DBMDDObj(" << newMDDType->getName() << ", " << domain << ", Ix " << newObjIx.getOId() << ", Sl " << newSL.getOId() << ", " << newOId << ")");
	ENTER( "DBMDDObj::DBMDDObj" );

	objecttype = OId::MDDOID;
	EXEC SQL BEGIN DECLARE SECTION;
		long	testoid1;
	EXEC SQL END DECLARE SECTION;

	testoid1 = newOId.getCounter();

	TALK( "EXEC SQL SELECT MDDId INTO :testoid1 FROM RAS_MDDOBJECTS WHERE MDDId = " << testoid1 );
	EXEC SQL SELECT
			MDDId
		INTO
			:testoid1
		FROM
			RAS_MDDOBJECTS
		WHERE
			MDDId = :testoid1;
	if (SQLCODE != SQLNODATAFOUND)
	{
		((DBObjectId)newObjIx)->setPersistent(false);
		((DBObject*)newSL.ptr())->setPersistent(false);
		if (SQLCODE == SQLOK)
		{
			RMDBGMIDDLE(1, RMDebug::module_mddif, "DBMDDObj", "OId is already there");
			RMInit::logOut << "DBMDDObj::DBMDDObj(...) OId already exists" << endl;
			throw r_Error(r_Error::r_Error_OIdNotUnique);
		}
		else
		{
			check("DBMDDObj(type, domain, index, layout, oid)");
			generateException();
		}
	}
	if (newMDDType->isPersistent())
		mddType = newMDDType;
	else
		mddType = (const MDDBaseType*)TypeFactory::addMDDType(newMDDType);
	myDomain = new DBMinterval(domain);
	_isPersistent = true;
	_isModified = true;
	myOId = newOId;
	setPersistent(true);

	LEAVE( "DBMDDObj::DBMDDObj, " << newMDDType->getName() << ", " << domain << ", Ix " << newObjIx.getOId() << ", " << myOId << ") " << myOId );
	RMDBGEXIT(7, RMDebug::module_mddif, "DBMDDObj", "DBMDDObj(" << newMDDType->getName() << ", " << domain << ", Ix " << newObjIx.getOId() << ", " << myOId << ") " << myOId);
}


DBMDDObj::DBMDDObj(const DBMDDObj& old)
	:	DBObject(old),
		objIxId(),
		mddType(NULL),
		persistentRefCount(0),
		storageLayoutId(),
		myDomain(NULL)
{
	RMDBGENTER(7, RMDebug::module_mddif, "DBMDDObj", "DBMDDObj(const DBMDDObj&" <<  old.getOId() << ")");
	ENTER( "DBMDDObj::DBMDDObj" );

	if (old.myDomain)
	{
		if (old.myDomain->isPersistent())
		{
			myDomain = (DBMinterval*)ObjectBroker::getObjectByOId(old.myDomain->getOId());
		}
		else
		{
			myDomain = new DBMinterval(*old.myDomain);
			myDomain->setPersistent(true);
		}
	}
	else
	{
		myDomain = NULL;
	}

        objIxId = old.objIxId;
	storageLayoutId = old.storageLayoutId;
	persistentRefCount = old.persistentRefCount;
	mddType = old.mddType;

	LEAVE( "DBMDDObj::DBMDDObj" );
	RMDBGEXIT(7, RMDebug::module_mddif, "DBMDDObj", "DBMDDObj(const DBMDDObj& " << old.getOId() << ")");
}

DBMDDObj::DBMDDObj(const MDDBaseType* newMDDType, const r_Minterval& domain, const DBObjectId& newObjIx, const DBStorageLayoutId& newSL)
	:	DBObject(),
		objIxId(newObjIx),
		mddType(NULL),
		persistentRefCount(0),
		storageLayoutId(newSL),
		myDomain(NULL)
{
	RMDBGENTER(7, RMDebug::module_mddif, "DBMDDObj", "DBMDDObj(" << newMDDType->getName() << ", " << domain << ", Ix " << newObjIx.getOId() << ", Sl " << newSL.getOId() << ")");
	ENTER( "DBMDDObj::DBMDDObj" );

	objecttype = OId::MDDOID;
	myDomain = new DBMinterval(domain);
	mddType = newMDDType;
/*only if it is a persistent mdd
	if (newMDDType->isPersistent())
		mddType = (MDDBaseType*)newMDDType;
	else
		mddType = (MDDBaseType*)TypeFactory::addMDDType(newMDDType);
	setPersistent(true);
*/

	LEAVE( "DBMDDObj::DBMDDObj" );
	RMDBGEXIT(7, RMDebug::module_mddif, "DBMDDObj", "DBMDDObj(" << newMDDType->getName() << ", " << domain << ", Ix " << newObjIx.getOId() << ") " << myOId);
}

DBMDDObj::~DBMDDObj()
{
	RMDBGENTER(7, RMDebug::module_mddif, "DBMDDObj", "~DBMDDObj() " << myOId);
	ENTER( "DBMDDObj::~DBMDDObj" );

	validate();
	if (myDomain)
		delete myDomain;
	myDomain = NULL;

	LEAVE( "DBMDDObj::~DBMDDObj" );
	RMDBGEXIT(7, RMDebug::module_mddif, "DBMDDObj", "~DBMDDObj() " << myOId);
}

DBStorageLayoutId
DBMDDObj::getDBStorageLayout() const
{
	return storageLayoutId;
}

r_Bytes
DBMDDObj::getMemorySize() const
{
	return DBObject::getMemorySize() + sizeof(long) + sizeof(MDDBaseType*) + sizeof(DBMinterval*) + sizeof(OId) + myDomain->getMemorySize() + mddType->getMemorySize() + sizeof(OId);
}

const MDDBaseType* 
DBMDDObj::getMDDBaseType() const
{
	return mddType;
}

const BaseType* 
DBMDDObj::getCellType() const
{
	return mddType->getBaseType();
}
	
r_Dimension
DBMDDObj::dimensionality() const
{
	return myDomain->dimension();
}

void
DBMDDObj::setCached(bool ic)
{
	DBObject::setCached(ic);
	if (myDomain)
		myDomain->setCached(ic);
}

//this should only receive an setPersistent(false)
void
DBMDDObj::setPersistent(bool o) throw (r_Error)
{
	RMDBGENTER(7, RMDebug::module_mddif, "DBMDDObj", "setPersistent(" << (int)o << ") " << myOId);
	ENTER( "DBMDDObj::setPersistent, o=" << o );

	DBObject::setPersistent(o);
	if (!o)
		setCached(false);
	if (myDomain)
		myDomain->setPersistent(o);
	DBObjectId testIx(objIxId);
	if (testIx.is_null())
	{
		RMDBGMIDDLE(0, RMDebug::module_mddif, "DBMDDObj", "index object is not valid " << myOId << " index " << objIxId.getOId());
		throw r_Error(INDEX_OF_MDD_IS_NULL);
	}
	else
	{
		testIx->setPersistent(o);
		if (o)
		{
			objIxId.release();
		}
	}

	if (storageLayoutId.is_null())
	{
		RMDBGMIDDLE(0, RMDebug::module_mddif, "DBMDDObj", "layout object is not valid " << myOId << " layout " << storageLayoutId.getOId());
		RMInit::logOut << "DBMDDObj::setPersistent() layout object is not valid" << endl;
		throw r_Error(STORAGE_OF_MDD_IS_NULL);
	}
	else
	{
		storageLayoutId->setPersistent(o);
	}
	if (o && !mddType->isPersistent())
		mddType = (const MDDBaseType*)TypeFactory::addMDDType(mddType);
	
	LEAVE( "DBMDDObj::setPersistent, o=" << o );
	RMDBGEXIT(7, RMDebug::module_mddif, "DBMDDObj", "setPersistent(" << (int)o << ") " << myOId);
}

const char*
DBMDDObj::getCellTypeName() const
{
	return mddType->getBaseType()->getTypeName();
}

r_Minterval
DBMDDObj::getDefinitionDomain() const
{
	return *myDomain;
}

r_Bytes 
DBMDDObj::getHeaderSize() const
{
	r_Bytes sz = sizeof(MDDBaseType*) + sizeof(r_Minterval*) + sizeof(DBObjectId) + sizeof(DBObject) + sizeof(DBStorageLayoutId);
	return sz;
}

void 
DBMDDObj::printStatus(unsigned int level, ostream& stream) const
{
	DBObject::printStatus(level, stream);
	stream << *myDomain << endl;
	mddType->printStatus(level + 1, stream);
	DBObjectId testIx(objIxId);
	if (!testIx.is_null())
		testIx->printStatus(level + 1, stream);
	else
		stream << "index is invalid " << objIxId.getOId();
	if (storageLayoutId.is_null())
		stream << "storagelayout is invalid " << storageLayoutId.getOId();
	else
		storageLayoutId->printStatus(level + 1, stream);
}

void
DBMDDObj::setIx(const DBObjectId& newIx)
{
	ENTER( "DBMDDObj::setIx" );

	if (isPersistent())
	{
		if (objIxId.getOId() != newIx.getOId())
		{
			objIxId = newIx.getOId();
			setModified();
		}
	}
	else
	{
		objIxId = newIx;
	}

	LEAVE( "DBMDDObj::setIx" );
}

void
DBMDDObj::updateInDb() throw (r_Error)
{
	RMDBGENTER(7, RMDebug::module_mddif, "DBMDDObj", "updateInDb() " << myOId);
	ENTER( "DBMDDObj::updateInDb" );

	EXEC SQL BEGIN DECLARE SECTION;
		long	mddoid3;
		double	objindex3;
		long	persRefCount3;
	EXEC SQL END DECLARE SECTION;
	
	objindex3 = objIxId.getOId();
	mddoid3 = myOId.getCounter();
	persRefCount3 = persistentRefCount; 

	TALK( "EXEC SQL UPDATE RAS_MDDOBJECTS SET PersRefCount = " << persRefCount3 << ", NodeOId = " << objindex3 << " WHERE MDDId = " << mddoid3 );
	EXEC SQL UPDATE RAS_MDDOBJECTS SET PersRefCount = :persRefCount3, NodeOId = :objindex3
		WHERE MDDId = :mddoid3;
	if (SQLCODE != SQLOK)
	{
		check("DBMDDObj::updateInDb()\0");
		generateException();
	}

	DBObject::updateInDb();

	LEAVE( "DBMDDObj::updateInDb" );
	RMDBGEXIT(7, RMDebug::module_mddif, "DBMDDObj", "updateInDb() " << myOId);
}

void
DBMDDObj::insertInDb() throw (r_Error)
{
	RMDBGENTER(7, RMDebug::module_mddif, "DBMDDObj", "insertInDb() " << myOId);
	ENTER( "DBMDDObj::insertInDb" );

	EXEC SQL BEGIN DECLARE SECTION;
		long	mddoid;
		double	basetypeid;
		double	storage;
		long	domainid;
		double	objindex;
		long	persRefCount;
	EXEC SQL END DECLARE SECTION;

	storage = storageLayoutId->getOId();
	objindex = objIxId.getOId();
	mddoid = myOId.getCounter();
	basetypeid = mddType->getOId();
	domainid = myDomain->getOId().getCounter();
	persRefCount = persistentRefCount; 

	TALK( "EXEC SQL INSERT INTO RAS_MDDOBJECTS ( MDDId, BaseTypeOId, DomainId, PersRefCount, NodeOId, StorageOId) VALUES	( " << mddoid << "," << basetypeid<< "," << domainid<< "," << persRefCount << "," << objindex << "," << storage << ")" );
	EXEC SQL INSERT INTO RAS_MDDOBJECTS ( MDDId, BaseTypeOId, DomainId, PersRefCount, NodeOId, StorageOId)
		VALUES	( :mddoid, :basetypeid, :domainid, :persRefCount, :objindex, :storage);
	if (SQLCODE != SQLOK)
	{
		check("DBMDDObj::insertInDb()");
		generateException();
	}

	DBObject::insertInDb();

	LEAVE( "DBMDDObj::insertInDb" );
	RMDBGEXIT(7, RMDebug::module_mddif, "DBMDDObj", "insertInDb() " << myOId);
}

void
DBMDDObj::deleteFromDb() throw (r_Error)
{
	RMDBGENTER(7, RMDebug::module_mddif, "DBMDDObj", "deleteFromDb() " << myOId);
	ENTER( "DBMDDObj::deleteFromDb" );

	EXEC SQL BEGIN DECLARE SECTION;
		long	mddoid1;
	EXEC SQL END DECLARE SECTION;

	mddoid1 = myOId.getCounter();
	
	TALK( "EXEC SQL DELETE FROM RAS_MDDOBJECTS WHERE MDDId = " << mddoid1 );
	EXEC SQL DELETE FROM RAS_MDDOBJECTS WHERE MDDId = :mddoid1;
	if (SQLCODE != SQLOK)
	{
		check("DBMDDObj::deleteFromDb()\0");
		generateException();
	}
	DBObject::deleteFromDb();

	LEAVE( "DBMDDObj::deleteFromDb" );
	RMDBGEXIT(7, RMDebug::module_mddif, "DBMDDObj", "deleteFromDb() " << myOId);
}

void
DBMDDObj::readFromDb() throw (r_Error)
{
	RMDBGENTER(7, RMDebug::module_mddif, "DBMDDObj", "readFromDb() " << myOId);
	ENTER( "DBMDDObj::readFromDb" );

#ifdef RMANBENCHMARK
	DBObject::readTimer.resume();
#endif
	EXEC SQL BEGIN DECLARE SECTION;
		long	mddoid2;
		double	basetypeid2;
		long	domainid2;
		double	objindex2;
		long	persRefCount2;
		double	storage2;
	EXEC SQL END DECLARE SECTION;

	mddoid2 = myOId.getCounter();

	TALK( "EXEC SQL SELECT BaseTypeOId, DomainId, PersRefCount, NodeOId, StorageOId INTO :basetypeid2, :domainid2, :persRefCount2, :objindex2, :storage2 FROM RAS_MDDOBJECTS WHERE MDDId = " << mddoid2 );

	EXEC SQL SELECT BaseTypeOId, DomainId, PersRefCount, NodeOId, StorageOId
		INTO :basetypeid2, :domainid2, :persRefCount2, :objindex2, :storage2
		FROM RAS_MDDOBJECTS
		WHERE MDDId = :mddoid2;
	if (SQLCODE != SQLOK)
	{
		if (SQLCODE == SQLNODATAFOUND)
		{
			RMDBGMIDDLE(7, RMDebug::module_mddif, "DBMDDObj", "object not found");
			throw r_Error(r_Error::r_Error_ObjectUnknown);
		}
		else
		{
			check("DBMDDObj::readFromDb()\0");
			generateException();
		}
	}
		
        objIxId = OId(objindex2);
	storageLayoutId = OId(storage2);
	persistentRefCount = persRefCount2;
	mddType = (MDDBaseType*)ObjectBroker::getObjectByOId(OId(basetypeid2));
	myDomain = (DBMinterval*)ObjectBroker::getObjectByOId(OId(domainid2, OId::DBMINTERVALOID));
	myDomain->setCached(true);

	DBObject::readFromDb();
#ifdef RMANBENCHMARK
	DBObject::readTimer.pause();
#endif

	LEAVE( "DBMDDObj::readFromDb" );
	RMDBGEXIT(7, RMDebug::module_mddif, "DBMDDObj", "readFromDb() " << myOId);
}
	
	
DBObjectId
DBMDDObj::getDBIndexDS() const
{
	return objIxId;
}
	
int
DBMDDObj::getPersRefCount() const
{
	return persistentRefCount;
}

void
DBMDDObj::incrementPersRefCount()
{
	persistentRefCount++;
	setModified();
}

void
DBMDDObj::decrementPersRefCount()
{
	persistentRefCount--;
	if (persistentRefCount == 0)
		setPersistent(false);
	setModified();
}