summaryrefslogtreecommitdiffstats
path: root/mddmgr/mddcoll.cc
blob: 56a1afe51adcf506137256ea160e26edeb483576 (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
/*
* 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>.
/
/**
 * SOURCE: mddcoll.cc
 *
 * MODULE: cachetamgr
 * CLASS:  MDDColl
 *
 * COMMENTS:
 *   none
 *
*/

#include "mymalloc/mymalloc.h"

#include <iostream>
#include "mddcoll.hh"
#include "mddcolliter.hh"
#include "relmddif/dbmddset.hh"
#include "relcatalogif/mdddomaintype.hh"
#include "relcatalogif/settype.hh"
#include "mddobj.hh"
#include "relmddif/dbmddobj.hh"
#include "reladminif/objectbroker.hh"
#include "raslib/rmdebug.hh"
#include "reladminif/oidif.hh"
#include "relcatalogif/collectiontype.hh"   // from base catalogif DBMS interface module
#include "reladminif/databaseif.hh"
#include "relmddif/dbmddset.hh"
#include "reladminif/eoid.hh"
#include "catalogmgr/typefactory.hh"
#include "tilemgr/tile.hh"

#include <cstring>

MDDColl::MDDColl(const CollectionType* newType, const char* name)
	{
	RMDBGONCE(2, RMDebug::module_mddmgr, "MDDColl", "MDDColl(" << newType->getName() << ", " << (name?name:"null") << ") " << (r_Ptr)this); 
	const char* theName = name;
	if (theName == NULL)
		{
		theName = "rasdaman temporary collection";
		}
	dbColl = new DBMDDSet(theName, newType);
	}

const char* 
MDDColl::getName() const
	{
	return dbColl->getName();
	}

const CollectionType* 
MDDColl::getCollectionType() const
	{
	return dbColl->getCollType();
	}

unsigned long
MDDColl::getCardinality() const
	{
	return dbColl->getCardinality();
	}

bool 
MDDColl::getOId(OId& pOId) const
	{
	if (isPersistent())
		{
		pOId = dbColl->getOId();
		return true;
		}
	return false;
	}

bool
MDDColl::getEOId(EOId& pEOId) const
	{
	if (isPersistent())
		{
		pEOId = dbColl->getEOId();
		return true;
		}
	return false;
	}

void
MDDColl::insert(const MDDObj* newObj)
	{
	RMDBGIF(0, RMDebug::module_mddmgr, "MDDColl", if (newObj == 0) \
	 	{ \
		RMInit::dbgOut << "MDDColl::insert(const MDDObj*) assertion failed" << endl;\
		throw r_Error(MDD_NOT_VALID);\
		})
	RMDBGONCE(2, RMDebug::module_mddmgr, "MDDColl", "insert(" << (r_Ptr)newObj << ")")
	dbColl->insert(newObj->getDBMDDObjId());
	insertIntoCache(newObj);
	RMDBGIF(2, RMDebug::module_mddmgr, "MDDColl", dbColl->printStatus(0, RMInit::dbgOut);)
	}

void 
MDDColl::releaseAll() {
	MDDObj* tempMDD = 0;
	while (!mddCache.empty()){
		tempMDD = (*mddCache.begin()).second;
		(*mddCache.begin()).second = NULL;     
		delete tempMDD;
		mddCache.erase(mddCache.begin());
	}
}

MDDColl::~MDDColl()
	{
	RMDBGONCE(2, RMDebug::module_mddmgr, "MDDColl", "~MDDColl() " << (r_Ptr)this); 
	if (isPersistent())
		releaseAll();
	//else released by release transfer structures
	}

MDDColl::MDDColl(const DBMDDSetId& coll) 
	:dbColl(coll)
	{
	}

DBMDDSetId 
MDDColl::getDBMDDSet() const
	{
	return dbColl;
	}

void
MDDColl::insertIntoCache(const MDDObj* objToInsert) const
	{
	RMDBGONCE(2, RMDebug::module_mddmgr, "MDDColl", "insertIntoCache(" << (r_Ptr)objToInsert << ")")
	mddCache[objToInsert->getDBMDDObjId().ptr()] = (MDDObj*)objToInsert;
	}

MDDObj*
MDDColl::getMDDObj(const DBMDDObj* objToGet) const
	{ 
	MDDObj* persMDDObjToGet = NULL;
	MDDObjMap::const_iterator i = mddCache.find((DBMDDObj*)objToGet);
	if (i != mddCache.end())
		persMDDObjToGet = (MDDObj*)(*i).second;
	else	{
		persMDDObjToGet = new MDDObj((DBMDDObj*)objToGet);
		insertIntoCache(persMDDObjToGet);
		}
	return persMDDObjToGet;
	}

bool
MDDColl::isPersistent() const
	{
	return dbColl->isPersistent();
	}

void
MDDColl::printStatus(unsigned int level, std::ostream& stream) const
	{
	dbColl->printStatus(level, stream);
	char* indent = new char[level*2 +1];
	for (int j = 0; j < level*2 ; j++)
		indent[j] = ' ';
	indent[level*2] = '\0';
	stream << indent;
	for (MDDObjMap::iterator i = mddCache.begin(); i != mddCache.end(); i++)
		{
		stream << (r_Ptr)(*i).second;
		}  
	delete[] indent;
	indent=0;
	}

MDDCollIter*
MDDColl::createIterator() const
	{  
	MDDCollIter* iter = new MDDCollIter((MDDColl*)this);
	return iter;
	}

void
MDDColl::remove(const MDDObj* obj)
	{
	if (obj != NULL)   
		{
		DBMDDObjId t2 = obj->getDBMDDObjId();
		dbColl->remove(t2);

		//remove from cache ;(
		MDDObjMap::iterator i = mddCache.find(t2.ptr());
		if(i != mddCache.end()) 
		  {
	   	   RMDBGONCE(2, RMDebug::module_mddmgr, "MDDColl", "remove(" << (r_Ptr)obj << ") found in cache")
                   mddCache.erase(i);
                  }
		else
		  {
		   RMDBGONCE(0, RMDebug::module_mddmgr, "MDDColl", "remove(" << (r_Ptr)obj << ") not in collection cache")
		  }
		}
	else	{
		RMDBGONCE(0, RMDebug::module_mddmgr, "MDDColl", "remove(MDDObj*) NULL");
		throw r_Error(MDD_NOT_VALID);
		}
	}

/*
void
MDDColl::removeFromCache(const PersMDDObj* objToRemove)
	{ 
	DBMDDObj* objIdVoidAddress = objToRemove->getDBMDDObjId().ptr();
	MDDObjMap::iterator i = mddCache.find(objIdVoidAddress);

	if (i != mddCache.end())
		{
		persMDDObjToRemove = (*i).second;
		(*i).second = NULL;
		delete persMDDObjToRemove;
		mddCache.erase(i);
		RMDBGIF(0, RMDebug::module_mddmgr, "MDDColl", \
			if (mddCache.find(objIdVoidAddress) != mddCache.end()) \
				{ \
				RMInit::dbgOut << "MDDColl::removeMDDObjfromCache() object multiple times in cache" << endl; \
				throw r_Error(MDD_EXISTS_MULTIPLE_TIMES); \
				})
		}
	else	{
		RMDBGONCE(0, RMDebug::module_mddmgr, "MDDColl", "removeMDDObjfromCache(" << objToRemove << ") not in collection")
		}
	}
*/

void
MDDColl::removeAll()
	{
	dbColl->removeAll();
	}


const char*
MDDColl::AllCollectionnamesName = "RAS_COLLECTIONNAMES";

MDDColl*
MDDColl::createMDDCollection(const char* name, const CollectionType* ct) throw (r_Error)
	{
	if (name == NULL)
		{
		RMDBGONCE(0, RMDebug::module_mddmgr, "MDDColl", "createMDDColl(NULL, colltype)")
		throw r_Error(r_Error::r_Error_NameNotUnique);
		}
	if (ct == NULL)
		{
		RMDBGONCE(0, RMDebug::module_mddmgr, "MDDColl", "createMDDColl(" << name << ", NULL)")
		throw r_Error(COLLTYPE_NULL);
		}
	if (!ct->isPersistent())
		{
		r_Error t(209);
		t.setTextParameter("type", ct->getName());
		RMDBGONCE(0, RMDebug::module_mddmgr, "MDDColl", "createMDDColl(" << name << ", " << ct->getName() << " not persistent)")
		throw t;
		}
	// may generate an exception:
	DBMDDSetId newDBColl = new DBMDDSet(name, ct);
	return new MDDColl(newDBColl);
	}

MDDColl*
MDDColl::createMDDCollection(const char* name, const OId& o, const CollectionType* ct) throw (r_Error)
	{
	// may generate an exception:
	if (name == NULL)
		{
		RMDBGONCE(0, RMDebug::module_mddmgr, "MDDColl", "createMDDColl(NULL, " << o << ", colltype)")
		throw r_Error(r_Error::r_Error_NameNotUnique);
		}
	if (ct == NULL)
		{
		RMDBGONCE(0, RMDebug::module_mddmgr, "MDDColl", "createMDDColl(" << name << ", " << o << ", NULL)")
		throw r_Error(COLLTYPE_NULL);
		}
	if (!ct->isPersistent())
		{
		r_Error t(209);
		t.setTextParameter("type", ct->getName());
		RMDBGONCE(0, RMDebug::module_mddmgr, "MDDColl", "createMDDColl(" << name << ", " << o << ", " << ct->getName() << " not persistent)")
		throw t;
		}
	DBMDDSetId newDBColl = new DBMDDSet(name, o, ct);
	return new MDDColl(newDBColl);
	}

bool
MDDColl::dropMDDCollection(const char* name) 
	{
	return DBMDDSet::deleteDBMDDSet(name);
	}

bool
MDDColl::dropMDDCollection(const OId& o)
	{
	return DBMDDSet::deleteDBMDDSet(o);
	}

MDDColl*
MDDColl::getMDDCollection(const OId& collOId) throw (r_Error)
	{
	RMDBGENTER(2, RMDebug::module_mddmgr, "MDDColl", "getMDDCollection(" << collOId << ")")
	DBMDDSetId t(collOId);
	//this will throw an exception
	t->isModified();
	MDDColl* retval = new MDDColl(t);
	RMDBGEXIT(2, RMDebug::module_mddmgr, "MDDColl", "getMDDCollection(" << collOId << ") " << retval)
	return retval;
	}

MDDColl*
MDDColl::getMDDCollection(const char* collName) throw (r_Error)
	{
	RMDBGENTER(2, RMDebug::module_mddmgr, "MDDColl", "getMDDCollection(" << collName << ")")
	MDDColl* retval = 0;
	DBMDDSetId dbset;
	if (strcmp(collName, AllCollectionnamesName) == 0)
		{
		r_Minterval transDomain("[0:*]");
		r_Minterval nameDomain("[0:0]");
		const BaseType* bt = TypeFactory::mapType("Char");
		MDDDomainType* mt = new MDDDomainType("RAS_NAMETYPE", bt, transDomain);
		TypeFactory::addTempType(mt);
		CollectionType* ct = new SetType("RAS_NAMESETTYPE", mt);
		TypeFactory::addTempType(ct);
		retval = new MDDColl(ct, AllCollectionnamesName);
		OIdSet* list = ObjectBroker::getAllObjects(OId::MDDCOLLOID);
		MDDObj* transObj = 0;
		Tile* transTile = 0;
		char* transName = 0;
		const char* nameBuffer = 0;
		r_Range namelen = 0;
		while (!list->empty())
			{
			dbset = *(list->begin());
			RMDBGMIDDLE(2, RMDebug::module_mddmgr, "MDDColl", "Coll OId     : " << dbset.getOId())
			nameBuffer = dbset->getName();
			RMDBGMIDDLE(2, RMDebug::module_mddmgr, "MDDColl", "Coll Name    : " << nameBuffer)
			namelen = strlen(nameBuffer);
			RMDBGMIDDLE(2, RMDebug::module_mddmgr, "MDDColl", "Coll Name Len: " << namelen)
			transName = (char*)mymalloc(sizeof(char) * (namelen + 1));
			memset(transName, 0, namelen + 1);
			strcpy(transName, nameBuffer);
			RMDBGMIDDLE(2, RMDebug::module_mddmgr, "MDDColl", "Domain       : " << namelen)
			nameDomain[0].set_high((r_Range)namelen);
			transObj = new MDDObj(mt, nameDomain);
			transTile = new Tile(nameDomain, bt, transName, 0, r_Array);
			transObj->insertTile(transTile);
			retval->insert(transObj);
			list->erase(list->begin());
			}
		delete list;
		}
	else	{
		dbset = DBMDDSet::getDBMDDSet(collName);
		retval = new MDDColl(dbset);
		}
	RMDBGEXIT(2, RMDebug::module_mddmgr, "MDDColl", "getMDDCollection(" << collName << ") " << retval)
	return retval;
	}

bool 
MDDColl::removeMDDObject(const OId& collOId, const OId& mddOId)
	{
	bool retval = true;
	DBMDDSetId coll(collOId);
	DBMDDObjId mdd(mddOId);

	if (coll.is_null())
		{//does not exist
		retval = false;
		}
	else	{
		if (mdd.is_null())
			{
			retval = false;
			}
		else	{
			coll->remove(mdd);
			}
		}
	return retval;
	}