summaryrefslogtreecommitdiffstats
path: root/indexmgr/mddobjix.cc
blob: 6e85d97c6eae081179c636f506db3fb4884b19c1 (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
/*
* 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: mddobjix.cc
 *
 * MODULE: indexmgr
 * CLASS:	MDDObjIx
 *
 * COMMENTS:
 *	none
 *
*/
static const char rcsid[] = "@(#)mddobjix, MDDObjIx: $Id: mddobjix.cc,v 1.30 2002/07/24 14:33:51 hoefner Exp $";

#include <iostream>
#include <math.h>

#include "indexmgr/mddobjix.hh"
#include "raslib/rmdebug.hh"
#include "tilemgr/tile.hh"
#include "indexmgr/srptindexlogic.hh"
#include "indexmgr/sdirindexlogic.hh"
#include "indexmgr/srcindexlogic.hh"
#include "indexmgr/transdirix.hh"
#include "relindexif/hierindex.hh"
#include "relindexif/dbtcindex.hh"
#include "relindexif/dbrcindexds.hh"
#include "keyobject.hh"
#include "reladminif/lists.h"
#include "relblobif/tileid.hh"

void
MDDObjIx::setNewLastAccess(const r_Minterval& newLastAccess, const std::vector<Tile*>* newLastTiles)
	{
	lastAccess = newLastAccess;
	releasePersTiles();
	lastAccessTiles = *newLastTiles;
	}

void
MDDObjIx::setNewLastAccess(const Tile* newLastTile, bool clear)
	{
	if (clear)
		{	
		releasePersTiles();
		lastAccessTiles.erase(lastAccessTiles.begin(), lastAccessTiles.end());
		}
	if (newLastTile)
		{
		r_Minterval region = newLastTile->getDomain();
		lastAccess = region;
		lastAccessTiles.push_back((Tile*)newLastTile);
		}
	}

bool
MDDObjIx::removeTileFromLastAccesses(const Tile* tileToRemove)
	{
	bool found = false;
	std::vector<Tile*>::iterator iter; 
	for (iter = lastAccessTiles.begin(); iter != lastAccessTiles.end() ; iter++)
		{
		if (*iter == tileToRemove) 
			{
			found = true;
			lastAccessTiles.erase(iter);
			break;
			}
		}
	if (found)
		{
		r_Minterval emptyInterval;
		lastAccess = emptyInterval;
		}
	return found;
	}

std::vector< Tile* >* 
MDDObjIx::lastAccessIntersect(const r_Minterval& searchInter) const
	{
	std::vector< Tile* >* interResult = 0;
	if ((lastAccess.dimension() != 0) && (lastAccess.covers(searchInter))) 
		{
		RMDBGONCE(6, RMDebug::module_indexmgr, "MDDObjIx", "lastAccessIntersect Search in the cache ")
		interResult = new std::vector< Tile* >();
		interResult->reserve(10);
		for (int i = 0; i < lastAccessTiles.size(); i++)
			{
			if (lastAccessTiles[i]->getDomain().intersects_with(searchInter))
				interResult->push_back(lastAccessTiles[i]);
			}
		if (interResult->size() == 0)
			{
			delete interResult;
			interResult = NULL;
			}
		}
	return interResult; 
	}

Tile* 
MDDObjIx::lastAccessPointQuery(const r_Point& searchPoint) const
	{
	Tile* result = 0;
	
	if ((lastAccess.dimension() != 0) && (lastAccess.covers(searchPoint)))	
		{
		for (int i = 0; !result && i < lastAccessTiles.size(); i++)
			{
			if (lastAccessTiles[i]->getDomain().covers(searchPoint))
				{
				result = lastAccessTiles[i];
				}
			}
		}
	return (result); 
	}

void 
MDDObjIx::releasePersTiles() 
	{
	RMDBGENTER(6, RMDebug::module_indexmgr, "MDDObjIx", "releasePersTiles()")
	if (isPersistent())
		{
		Tile* t = NULL;
		for(int i = 0; i < lastAccessTiles.size(); i++)
			{
			t = lastAccessTiles[i];
			delete t;
			lastAccessTiles[i] = NULL;
			}
		lastAccessTiles.clear();
		}
	RMDBGEXIT(6, RMDebug::module_indexmgr, "MDDObjIx", "releasePersTiles()")
	}

void	
MDDObjIx::printStatus(unsigned int level, std::ostream& stream) const
	{
	stream << "MDDObjIx [ last access interval = " << lastAccess << " tile cache size = " << lastAccessTiles.size() << " index structure = ";
	actualIx->printStatus(level, stream);
	} 

DBObjectId 
MDDObjIx::getDBMDDObjIxId() const
	{
	return actualIx;
	} 

r_Minterval
MDDObjIx::getCurrentDomain() const	
	{
	return actualIx->getCoveredDomain();
	}

r_Dimension 
MDDObjIx::getDimension() const
	{
	return actualIx->getDimension();
	}

#ifdef RMANBENCHMARK
void
MDDObjIx::initializeTimerPointers() 
	{
	pointQueryTimer= new RMTimer("DirIx", "pointQuery time	");
	intersectTimer = new RMTimer("DirIx", "intersect time	");
	getTilesTimer = new RMTimer("DirIx", "getTiles time	");
	} 
#endif

MDDObjIx::MDDObjIx(const StorageLayout& sl, const r_Minterval& dim, const BaseType* bt)
	:	cellBaseType(bt),
		myStorageLayout(sl),
		actualIx(0),
		_isPersistent(true)
	{
	RMDBGENTER(10, RMDebug::module_indexmgr, "MDDObjIx", "MDDObjIx(storage, " << dim << ", type)")
	lastAccessTiles.reserve(10); 
	if (bt == NULL)
		{
		RMDBGMIDDLE(10, RMDebug::module_indexmgr, "MDDObjIx", "TransIndex")
		_isPersistent = false;
		}
	initializeLogicStructure();
	if (isPersistent())
		{
		r_Range temp;
		switch(myStorageLayout.getIndexType())
			{
			case r_RPlus_Tree_Index: 
				actualIx = (HierIndexDS*)new DBHierIndex(dim.dimension(), false, true);
				break;
			case r_Reg_Computed_Index:
				actualIx = new DBRCIndexDS(dim, SRCIndexLogic::computeNumberOfTiles(myStorageLayout, dim));
				break;
			case r_Tile_Container_Index: 
				actualIx = (HierIndexDS*)new DBTCIndex(dim.dimension(), false);
				break;
			case r_Directory_Index:
				actualIx = (HierIndexDS*)new DBHierIndex(dim.dimension(), false, true);
				break;	
			case r_Auto_Index:
			default:
				// should never get here. If Auto_Index, a specific index was 
				RMDBGONCE(0, RMDebug::module_indexmgr, "MDDObjIx", "initializeLogicStructure() Auto_Index or unknown index chosen");
				throw r_Error(UNKNOWN_INDEX_TYPE);
				break;
			} 
		}	
	else	{
		//only dirindex supports transient indexes
		actualIx = new TransDirIx(dim.dimension());
		}
#ifdef RMANBENCHMARK
	initializeTimerPointers();
#endif
	RMDBGEXIT(10, RMDebug::module_indexmgr, "MDDObjIx", "MDDObjIx(storage, " << dim << ", type)")
	}

MDDObjIx::MDDObjIx(DBObjectId newDBIx, const StorageLayout& sl, const BaseType* bt)
	:	cellBaseType(bt),
		myStorageLayout(sl),
		actualIx(newDBIx),
		_isPersistent(true)
	{
	initializeLogicStructure();
	lastAccessTiles.reserve(10); 
#ifdef RMANBENCHMARK
	initializeTimerPointers();
#endif
	}

void
MDDObjIx::initializeLogicStructure()
	{
	switch(myStorageLayout.getIndexType())
		{
		case r_RPlus_Tree_Index: 
		case r_Tile_Container_Index: 
			do_getObjs = SRPTIndexLogic::getObjects;
			do_insertObj = SRPTIndexLogic::insertObject2;
			do_pointQuery = SRPTIndexLogic::containPointQuery2;
			do_removeObj = SRPTIndexLogic::removeObject;
			do_intersect = SRPTIndexLogic::intersect2;
			break;
		case r_Reg_Computed_Index: 
			do_getObjs = SRCIndexLogic::getObjects;
			do_insertObj = SRCIndexLogic::insertObject;
			do_pointQuery = SRCIndexLogic::containPointQuery;
			do_removeObj = SRCIndexLogic::removeObject;
			do_intersect = SRCIndexLogic::intersect;
			break;
		case r_Directory_Index:
			// chosen before this
			do_getObjs = SDirIndexLogic::getObjects;
			do_pointQuery = SDirIndexLogic::containPointQuery;
			do_removeObj = SDirIndexLogic::removeObject;
			do_intersect = SDirIndexLogic::intersect;
			do_insertObj = SDirIndexLogic::insertObject;
			break;	
		default:
		case r_Auto_Index:
			// should never get here. If Auto_Index, a specific index was 
			RMInit::logOut << "MDDObjIx::initializeLogicStructure() illegal index (" << myStorageLayout.getIndexType() << ") chosen!" << endl;
			throw r_Error(ILLEGAL_INDEX_TYPE);
			break;
		} 
	}


void	
MDDObjIx::insertTile(const Tile* newTile)
	{
	if (isPersistent())
		{
		((Tile*)newTile)->setPersistent();
		}
	KeyObject t(newTile);
	do_insertObj(actualIx, t, myStorageLayout);
	setNewLastAccess(newTile, false);
	}

bool
MDDObjIx::removeTile(const Tile* tileToRemove) 
	{
	RMDBGENTER(4, RMDebug::module_indexmgr, "MDDObjIx", "removeTile(Tile)")
	bool found = false;
	// removes from cache, if it's there
	removeTileFromLastAccesses(tileToRemove);
	
	// removes from the index itself
	KeyObject t(tileToRemove);
	found = do_removeObj(actualIx, t, myStorageLayout);
	RMDBGEXIT(4, RMDebug::module_indexmgr, "MDDObjIx", "removeTile(Tile)")
	return found;
	}

vector< Tile* >* 
MDDObjIx::intersect(const r_Minterval& searchInter) const
	{
	RMDBGENTER(4, RMDebug::module_indexmgr, "MDDObjIx", "intersect(" << searchInter << ")")
#ifdef RMANBENCHMARK
	if(RManBenchmark >= 3)	intersectTimer->start();
#endif
		
	vector< Tile* >* result = lastAccessIntersect(searchInter);
	if (!result)
		{
		KeyObjectVector resultKeys;
		do_intersect(actualIx, searchInter, resultKeys, myStorageLayout);
		result = new vector< Tile* >();
		if (!resultKeys.empty())
			{
			unsigned int resSize = resultKeys.size();
			result->reserve(resSize); 
			Tile* t = NULL;
			unsigned int i = 0;
			if (isPersistent())
				{
//this checks if there are double tiles in the result
				RMDBGIF(1, RMDebug::module_indexmgr, "MDDObjIx", \
				DomainMap t; \
				DomainMap::iterator it; \
				for (i = 0; i < resSize; i++) \
					{ \
					DomainPair p(resultKeys[i].getObject().getOId(), \
						resultKeys[i].getDomain()); \
					if ((it = t.find(p.first)) != t.end()) \
						{ \
						RMDBGMIDDLE(0, RMDebug::module_indexmgr, "MDDObjIx", \
							"intersect(" << searchInter << \
							") received double tile: " << \
							resultKeys[i]) \
						for (int i = 0; i < resultKeys.size(); i++) \
							{ \
							RMInit::dbgOut << resultKeys[i] << endl; \
							} \
						throw r_Error(TILE_MULTIPLE_TIMES_RETRIEVED); \
						} \
					t.insert(p); \
					} \
				);
				for (i = 0; i < resSize; i++)
					{
					RMDBGMIDDLE(4, RMDebug::module_indexmgr, "MDDObjIx", "received entry " << resultKeys[i])
					result->push_back(new Tile(resultKeys[i].getDomain(), cellBaseType, DBTileId(resultKeys[i].getObject())));
					}
				}
			else	{
				for (i = 0; i < resSize; i++)
					{
					result->push_back(resultKeys[i].getTransObject());
					}
				}
			((MDDObjIx*) this)->setNewLastAccess(searchInter, result);
			}
		}
#ifdef RMANBENCHMARK
	if(RManBenchmark >= 3) intersectTimer->stop();
#endif		
	RMDBGEXIT(4, RMDebug::module_indexmgr, "MDDObjIx", "intersect(" << searchInter << ") " << (void*)result)
	return result;
	}
		
char*	
MDDObjIx::pointQuery(const r_Point& searchPoint)
	{
	char* result = 0;
	
	Tile* resultTile = containPointQuery(searchPoint);

	if(resultTile)
		{
		result = resultTile->getCell(searchPoint); 
		}
	return result;
	}

const char*	
MDDObjIx::pointQuery(const r_Point& searchPoint) const
	{
	const char* result = 0;
	
	Tile* resultTile = containPointQuery(searchPoint);

	if(resultTile)
		{
		result = resultTile->getCell(searchPoint); 
		}
	return result;
	}

Tile*	
MDDObjIx::containPointQuery(const r_Point& searchPoint) const
	{
	
	Tile* resultTile = 0;
 
#ifdef RMANBENCHMARK
	if(RManBenchmark >= 4) pointQueryTimer->start();
#endif		
	resultTile = lastAccessPointQuery(searchPoint);
	if (!resultTile)
		{
		KeyObject resultKey;
		do_pointQuery(actualIx, searchPoint, resultKey, myStorageLayout);
		if (resultKey.isInitialised())
			{
			if (isPersistent())
				{
				resultTile = new Tile(resultKey.getDomain(), cellBaseType, DBTileId(resultKey.getObject()));
				// for rcindex
				((DBObject*)resultKey.getObject().ptr())->setCached(false);
				}
			else	{
				resultTile = resultKey.getTransObject();
				}
			((MDDObjIx*) this)->setNewLastAccess(resultTile);
			}
		}
	
#ifdef RMANBENCHMARK
	if (RManBenchmark >= 4) pointQueryTimer->stop();
#endif			
	return resultTile;
	}

vector< Tile* >* 
MDDObjIx::getTiles() const
	{
#ifdef RMANBENCHMARK
	if(RManBenchmark >= 3)	getTilesTimer->start();
#endif
	vector< Tile* >* result = NULL;
	KeyObjectVector resultKeys;
	do_getObjs(actualIx, resultKeys, myStorageLayout);
	if (!resultKeys.empty())
		{
		result = new vector< Tile* >();
		unsigned int resSize = resultKeys.size();
		result->reserve(resSize); 
		Tile* t = 0;
		if (isPersistent())
			{
//this checks if there are double tiles in the result
			RMDBGIF(1, RMDebug::module_indexmgr, "MDDObjIx", \
			DomainMap tmap; \
			DomainMap::iterator it; \
			for (int cnt = 0; cnt < resSize; cnt++) \
				{ \
				DomainPair p(resultKeys[cnt].getObject().getOId(), \
					resultKeys[cnt].getDomain()); \
				if ((it = tmap.find(p.first)) != tmap.end()) \
					{ \
					RMDBGMIDDLE(0, RMDebug::module_indexmgr, "MDDObjIx", \
						"getTiles() received double tile: " << \
						resultKeys[cnt]) \
					for (int cnt = 0; cnt < resultKeys.size(); cnt++) \
						{ \
						RMInit::dbgOut << resultKeys[cnt] << endl; \
						} \
					throw r_Error(TILE_MULTIPLE_TIMES_RETRIEVED ); \
					} \
				tmap.insert(p); \
				} \
			);
			for (int i = 0; i < resSize; i++)
				{
				result->push_back(new Tile(resultKeys[i].getDomain(), cellBaseType, DBTileId(resultKeys[i].getObject())));
				}
			}
		else	{
			for (int i = 0; i < resultKeys.size(); i++)
				{
				result->push_back(resultKeys[i].getTransObject());
				}
			}
		r_Minterval emptyInterval;
		((MDDObjIx*) this)->setNewLastAccess(emptyInterval, result);
		}
#ifdef RMANBENCHMARK
	if(RManBenchmark >= 3)	getTilesTimer->stop();
#endif	
	return result;
	}

	
bool	
MDDObjIx::isPersistent() const
	{
	return _isPersistent;
	}

MDDObjIx::~MDDObjIx() 
	{
	releasePersTiles();
	actualIx->destroy();
	actualIx = 0;

#ifdef RMANBENCHMARK
	pointQueryTimer->setOutput(0);
	if (pointQueryTimer) delete pointQueryTimer;
	intersectTimer->setOutput(0);
	if (intersectTimer) delete intersectTimer;
	getTilesTimer->setOutput(0);
	if (getTilesTimer) delete getTilesTimer;
#endif	
	}