summaryrefslogtreecommitdiffstats
path: root/mddmgr/mddcoll.hh
blob: c44cb8f206eff2b06e53a8a7c678e8579e1b970e (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
/*
* 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>.
/
/**
 * INCLUDE: mddcoll.hh
 *
 * MODULE:  cachetamgr
 * CLASS:   MDDColl
 *
 * COMMENTS:
 *
*/

#ifndef _MDDCOLL_HH_
#define _MDDCOLL_HH_

#include <iostream>
#include <map>
#include <stdlib.h>

#include "mddobj.hh"
#include "mddcolliter.hh"
#include "mddcoll.hh"
#include "reladminif/oidif.hh"
#include "raslib/error.hh" 
#include "relmddif/mddid.hh"
#include "relcatalogif/collectiontype.hh"     // from catalogif base DBMS interface module

class CollectionType;
class DatabaseIf;

//@ManMemo: Module: {\bf cachetamgr}
/*@Doc:
   An MDDColl object is a collection of references to MDD objects. 
   This class is the abstract base class for all persistent and 
   transient collection classes.
   
   The objects pointed to by the collection exist in a storage domain 
   which may be either the main memory for the transient collections
   or the database for the persistent cases.
   
   An MDDColl is responsible for managing the memory allocated for the 
   MDDObjs which belong to it. In the future, more functions will be provided 
   to allow selected releasing or keeping of MDD objects in an MDDColl.

   {\bf Example }

   {\tt MDDObj* accessedObj; }
  
   {\tt MDDColl* mddObjsColl; }
   
   {\tt mddObjsColl = new ... }
   
   {\tt    // Initialize mddObjsColl to a PersMDDColl or TransMDDColl. } 
   
   {\tt MDDCollIter* objsIt = mddObjsColl->createIterator(); }
  
   {\tt for(int i = 1 ; objsIt->notDone(); i++, objsIt->advance()) }
   
   {\tt \{  }
   
   {\tt accessedObj = objsIt->getElement(); }
   
   {\tt ...}
   
   {\tt } }
  
   {\tt delete objsIt; // Deallocate iterator. }
   
   {\tt ... // Here accessedObj may be used. }
  
   {\tt mddObjsColl->releaseAll(); }
   
   {\bf {\tt ... // In this part accessedObj is no longer valid. } }
   
   {\tt delete mddObjsColl; }
*/

class MDDColl
	{
				
	public:
		///transient collection
		MDDColl(const CollectionType* newType, const char* name = 0);

		static const char* AllCollectionnamesName;

		static MDDColl* getMDDCollection(const char* collName) throw (r_Error);
		/**
			Retrieve a mdd collection from database.
			Throws r_Error::r_Error_ObjectUnknown if the name is unknown.
		*/

		static MDDColl* getMDDCollection(const OId& collOId) throw (r_Error);
		/**
			Retrieve a mdd collection from database.
			Throws r_Error::r_Error_ObjectUnknown if the oid is unknown.
		*/

		static MDDColl* createMDDCollection(const char* name, const CollectionType* ct) throw (r_Error);
		/**
			Throws r_Error::r_Error_NameNotUnique if the name exists already or is NULL.
		*/

		static MDDColl* createMDDCollection(const char* name, const OId& o, const CollectionType* ct) throw (r_Error);
		/**
			Throws r_Error::r_Error_NameNotUnique if the name exists already or is NULL.
			Throws r_Error::r_Error_OIdNotUnique if the oid exists already.
		*/
		static bool dropMDDCollection(const char* name);
		/**
			Delete a mdd collection from database.
			Returns false if the name is unknown.
		*/

		static bool dropMDDCollection(const OId& id);
		/**
			Delete a mdd collection from database.
			Returns false if the oid is unknown.
		*/
		 
		static bool removeMDDObject(const OId& coll, const OId& id);
		/**
			Remove an mdd object from the collection.
			Returns always true.
		*/
		 
		 void printStatus(unsigned int level = 0, ostream& stream = cout) const;
		/**
			Prints current status of the MDD Collection.
		*/
		
		 const CollectionType* getCollectionType() const;
		/**
			Returns the type of this MDD collection.
		*/
		
		 unsigned long getCardinality() const;	
		/**
			Returns the cardinality of the collection.
		*/
		
		 bool getOId(OId& pOId) const;
		/**
			Returns true if collection has an OId.
		*/

		 bool getEOId(EOId& pEOId) const;
		/**
			Returns true0 if collection has an EOId.
		*/
	
		 void insert(const MDDObj* newObj);
		/**
			Inserts reference to MDD object into the collection.
			If the type of the object (transient/persistent) does not fit the collection an exception is thrown.
		*/
	 
		MDDCollIter* createIterator() const;
		/**
			Creates a new iterator for this collection. Returns a pointer to the new allocated iterator object. Returned pointer must be freed afterwards.
		*/

		 void remove(const MDDObj* obj);
		/**
			Remove reference to MDD object from the current collection. The object itself is not obligatorily destroyed from the storage domain where it exists.  If the object does not live in the collection nothing is done.
		*/
		
		 void removeAll(); 
		/**
			Empties the current collection by removing all references to MDD objects from it.
			The objects are not obligatorily destroyed from the storage domain where they exist.
		*/
		
		 void releaseAll();	
		/**
			Releases all dynamic memory allocated for the current collection.
			This method has to be called explicitely, since the destructor doesn't deallocate memory for the elements of the collection.
			The reason for this is to allow the user to use MDDObjs from this collection, even after the collection itself is no longer in main memory.
			This is only true for transient collections.
		*/
		
		 bool isPersistent() const;
		/**
			Tells if collection is persistent.
		*/
		
		 const char* getName() const;

		 ~MDDColl();
		/**
			Doesn't free main memory allocated for the objects of the collection. 
			See ReleaseAll() for an explanation on this issue.  Even if it does nothing, it has to be defined because derived classes may have non-trivial destructors.
		*/
	protected:

		friend class MDDCollIter;

		MDDObj* getMDDObj(const DBMDDObj*) const;
		/**
			Will get from cache/instantiate and return an MDDObj.
			Instantiation: it will just create a new one if isPersistent() == true -> passing it a oid that does not belong to the collection -> problem.
		**/
	
		MDDColl(const DBMDDSetId& coll);

		DBMDDSetId getDBMDDSet() const;
		/**
			Return the reference to the actual persistent object in the base DBMS.
			returned reference must be const -> impossible
		*/

		void insertIntoCache(const MDDObj* objToInsert) const;
		/**
			Insert persistent MDD object in the internal cache.
		*/

		//void removeFromCache(DBMDDObjId& objToGet);

		DBMDDSetId dbColl;
		/**
			Pointer to the actual class stored in the base DBMS.
			Based on the Base DBMS DBMDDSet class from relmddif.
		*/

		typedef std::map<DBMDDObj*, MDDObj*, std::less<DBMDDObj*> > MDDObjMap;
		mutable MDDObjMap mddCache;
		 /** 
			The mdd objs that are instantiated may not be twice in memory.  Therefore it has to be checked if an mdd obj was already created for a specific dbmddobj.
			This is achieved by maintaining this map.
		 */
	};

#endif