summaryrefslogtreecommitdiffstats
path: root/mddmgr/test/test_mddobj.cc
blob: 5bb70c03a1f0a2f9307d32e0a0d08f074f12a232 (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
/*
* 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: test_mddobj.cc
 *
 * MODULE: test for mddobj of mddmgr   
 *
 * COMMENTS:
 *   in order to compile this test program, getDBMDDObjId from 
 *   PersMDDObj has to be made public, which is not the normal 
 *   and expected case.
 *
*/

#include <stdlib.h>
#include <iostream>
#include "mddmgr/perstile.hh"
#include "ulongtype.hh"
#include "mddmgr/persmddobj.hh"
#include "mddmgr/persmddcoll.hh"
#include "mddmgr/mddcoll.hh"
#include "mddmgr/mddcolliter.hh"


#include "raslib/rminit.hh"
RMINITGLOBALS('C')

#include "mddif/dbmddobj.hh"     
#include "raslib/minterval.hh"
#include "raslib/sinterval.hh"

// perhaps used later
static char O2BenchDBName[]  = "PaulaRasDaBase";
static char O2BenchSchemaName[] = "SMRasDaSchema";

static void ClearDB( d_Database &DB );
static void testAccessing();
static void testConstructors();
static void testSearch();
static void testGetFunctions();
static void printInterval(r_Minterval* inter);
void printMemInfo( );


/*************************************************************
 * Function name.: int main( int argc, char** argv)
 *
 * Arguments.....: 
 *   argc: number of arguments given to program
 *   argv: array of char* with arguments
 * Return value..: exit status
 * Description...: none
 ************************************************************/
int
main( int argc, char** argv)
{
  // variables representing O2 database, ta and session
  d_Session  session;
  d_Database database;
  d_Transaction ta;  
  
 
  // initialize the O2 session
  cout << "Initializing O2 session..." << endl;
  session.set_default_env();
  if (session.begin(argc, argv)){
    cerr << "Something wrong to start o2" << endl;
    exit(1);
  } 

  // clear the DB (in case the base already exists)
  cout << "Deleting TestMDDObjContainer of database..." << endl;
  database.open( O2BenchDBName);
  ta.begin( );
  Handle collHandle = 0;
  collHandle = o2_get_root( "TestMDDObjContainer" );
  if ( collHandle ) 
  {
    // I don't know if this is needed. 
    o2_unref_handle ( collHandle );
    database.destroy_persistent_root( "TestMDDObjContainer" );
  }
  ta.commit( );

  // connect to the database
  cout << "Connecting to database " << O2BenchDBName 
       << "..." << endl;
  database.open( O2BenchDBName );

  // create root collection
  cout << "Creating root collection..." << endl;
  ta.begin();
  database.create_persistent_root( "TestMDDObjContainer",
			     "d_List< d_Ref<DBMDDObj> >",
		 	     OL_CONSTANT);
  ta.commit();

  // create indexes and put them in TestMDDObjContainer 
  cout << "Create objects and put in TestMDDObjContainer..." << endl;
  ta.begin();
  testConstructors();
  ta.commit();

  // read index and print contents 
  cout << "Read objects and print contents..." << endl;
  ta.begin();
  testAccessing();
  ta.commit();

   // test search operation and print contents 
  cout << "Search a rectangular region and print contents..." << endl;
  ta.begin();
  testSearch();
  ta.commit();
  
 /* 
   // test get member functions 
  cout << "Get * from object and print result..." << endl;
  ta.begin();
  testGetFunctions();
  ta.commit();
 */

  cout << "Ending O2 session..." << endl;
  session.end();
  database.close();
}

/*************************************************************
 * Function......: testConstructors()
 *
 * Arguments.....: none
 * Return value..: none
 * Description...: constructs Indices and inserts them
 *                 in root collection.
 ************************************************************/

static void testConstructors()
{
  ULongType anyType;
  char anyCell[] = {0,0,0,0};
  unsigned long MDDObj1sz = 0;
  
  cout << "....testConstructors"<< endl;
  
  // read root object
  d_List< d_Ref<DBMDDObj> > objsList("TestMDDObjContainer");
 
  // create MDD Object
  cout << "    mddObj1" << endl;
  
  cout << "       tile 1 = nil, 10-12, 20-24 "<< endl;
  r_Sinterval limits1Obj1(10l,12l);
  r_Sinterval limits2Obj1(20l,24l);
  r_Minterval dom(2);
  dom << limits1Obj1 << limits2Obj1;
  PersTile* tile1Obj1 = new PersTile( dom, &anyType, (const char*) anyCell);
  MDDObj1sz += tile1Obj1->getSize( );
  PersMDDObj* MDDObj1=new PersMDDObj( dom, "ULong");
  MDDObj1->insertTile(tile1Obj1);  
  
  cout << "       tile 2 = nil, 0-400, 22-24 "<< endl;
  dom[0].set_interval(0l,400l);
  dom[1].set_interval(22l,24l);
  PersTile* tile2Obj1 = new PersTile( dom, &anyType, (const char*) anyCell);

  MDDObj1sz += tile1Obj1->getSize( );
  // MDDObj1->insertTile(tile2Obj1);
		      
  cout << "       tile 3 = nil, 0-600, 10-1000 "<< endl;
  dom[0].set_interval(0l,600l);
  dom[1].set_interval(10l,1000l);
  PersTile*  tile3Obj1= new PersTile( dom, &anyType, (const char*) anyCell);
  MDDObj1sz += tile3Obj1->getSize( );
  // MDDObj1->insertTile(tile3Obj1);
  
  vector<Tile*>* newTiles = new vector< Tile* >;
  newTiles->push_back(tile2Obj1);
  newTiles->push_back(tile3Obj1);
  MDDObj1->insertTiles(*newTiles); 
  delete newTiles;
  
  objsList.insert_element_last(MDDObj1->getDBMDDObjId());

  cout << "Size of MDDObj1 : " << MDDObj1sz << "  -  " << MDDObj1sz/1024 << endl;
  printMemInfo( );
  cout << "Delete MDDObj1 ..........." << endl;
  delete MDDObj1;
  printMemInfo( );
  cout << endl << endl;

  
  unsigned long MDDObj2sz = 0;
  cout << "    mddObj2 "<< endl;
  cout << "       tile 1 = nil, 0-19, 20-59, 30-59 "<< endl;
  r_Sinterval limits1Obj2(0l,19l);
  r_Sinterval limits2Obj2(20l,59l);
  r_Sinterval limits3Obj2(30l,59l);
  r_Minterval dom2(3);
  dom2 << limits1Obj2 << limits2Obj2 << limits3Obj2;
  PersTile* tile1Obj2 = new PersTile( dom2, &anyType, (const char*) anyCell);
  MDDObj2sz += tile1Obj2->getSize( );
  PersMDDObj* MDDObj2 = new PersMDDObj( dom2, "ULong");
  MDDObj2->insertTile(tile1Obj2);
  
  cout << "       tile 2 = nil, 20-39, 60-79, 60-89 "<< endl;
  dom2[0].set_interval(20l,39l);
  dom2[1].set_interval(60l,79l);
  dom2[2].set_interval(60l,89l); 
  PersTile* tile2Obj2 = new PersTile( dom2, &anyType, (const char*) anyCell);
  MDDObj2sz += tile2Obj2->getSize( );
  
  MDDObj2->insertTile(tile2Obj2);
  
  objsList.insert_element_last(MDDObj2->getDBMDDObjId());

  cout << "Size of MDDObj2 : " << MDDObj2sz << "  -  " << MDDObj2sz/1024 << endl;
  printMemInfo( );
  cout << "Delete MDDObj2 ..........." << endl;
  delete MDDObj2;  
  printMemInfo( );
  cout << endl << endl;
}

/*************************************************************
 * Function......: testAccessing()
 *
 * Arguments.....: none
 * Return value..: none
 * Description...: reads DirTilesIx's and shows contents
 ************************************************************/

static void testAccessing()
{
  DBMDDObjId accessedObj;
  
  cout << "....testAccessing"<<endl;
  
  // read root object
  d_List< DBMDDObjId > objsList("TestMDDObjContainer");
  // used for iterating
  d_Iterator< DBMDDObjId > objsIt = objsList.create_iterator();
  
  for( int i = 1 ; objsIt.not_done(); i++, objsIt.advance())
  {
    accessedObj = objsIt.get_element();
    cout << "    --"<<i<<". MDD object in list:" << endl;
    accessedObj->printStatus();
    cout << "    -- CellTypeName: " << accessedObj->getCellTypeName( ) << endl;
  }
  
}

/*************************************************************
 * Function......: testSearch()
 *
 * Arguments.....: none
 * Return value..: none
 * Description...: reads Index's and shows contents
 ************************************************************/

static void testSearch()
{
  
  PersMDDColl mddObjsColl ( "TestMDDObjContainer" );
  MDDObj* accessedObj;
  MDDCollIter* objsIt = mddObjsColl.createIterator( );


/* 
  DBMDDObjId accessedObj;  
  
  cout << "....testSearch"<<endl;
  
  // read root object
  d_List< DBMDDObjId > objsList("TestMDDObjContainer");
  // used for iterating
  d_Iterator< DBMDDObjId > objsIt = objsList.create_iterator();
  
*/

  for( int i = 0 ; objsIt->notDone(); i++, objsIt->advance())
  {

    accessedObj = objsIt->getElement();
    
    if (i == 0 || i == 1)
    {
      vector< Tile* >* entriesList = 0;
      
      r_Minterval searchInt1(2); 
      r_Minterval searchInt2(3); 
     
      printMemInfo( );
 
      cout << "    -- " << i+1 << ". MDD object in list. Search for:";
      switch (i) {
      case 0: searchInt1[0].set_interval(10l,20l);
	      searchInt1[1].set_interval(10l,30l);
	      cout << " 10-20, 10-30" << endl;
              entriesList = accessedObj->intersect(searchInt1);
              break;
      case 1: searchInt2[0].set_interval(10l,20l);
	      searchInt2[1].set_interval(10l,30l);
	      searchInt2[2].set_interval(40l,50l);
	      cout << " 10-20, 10-30, 40-50" <<endl;
	      entriesList = accessedObj->intersect(searchInt2);
	      break;
      default: break;
      }

      printMemInfo( );

      cout << "    -- Search result: " << endl; 
      vector<Tile*>::iterator entryIt = entriesList->begin();
          
      while (entryIt !=  entriesList->end())
      {
	 // (*entryIt)->printStatus();
	 r_Minterval tileInterval = (*entryIt)->getDomain();
	 int dimensionality = tileInterval.dimension();
	 
	 cout << "   PersTile printStatus";
         cout << "    domain == " << dimensionality << ": ";
         for (int i = 0; i <dimensionality; i++)
             cout << tileInterval[i].low() << "-" << tileInterval[i].high() <<", ";
         cout << endl << "   Access contents " ;
         char* tileContents = ( *entryIt )->getContents( ); 

         cout << endl;

	 entryIt++;
      }

      printMemInfo( );
      cout << " Release entriesList " << endl;

      delete entriesList;
    }
  }
  delete objsIt;
}

/*************************************************************
 * Function......: testGetFunctions()
 *
 * Arguments.....: none
 * Return value..: none
 * Description...: reads Index's and shows contents
 ************************************************************/

static void testGetFunctions()
{
  
  MDDObj* accessedObj;
  PersMDDColl persMDDObjsColl ( "TestMDDObjContainer" );
  MDDColl* mddObjsColl = &persMDDObjsColl;
 
  MDDCollIter* objsIt = mddObjsColl->createIterator( );

  vector< Tile* >* entriesList;
  
  cout << "....testGetTiles"<<endl;
  
  for( int i = 0 ; objsIt->notDone(); i++, objsIt->advance())
  {
    r_Minterval currDom;
    r_Minterval defDom;
    
    cout << "  " << i << ". Object" << endl;
    accessedObj = objsIt->getElement( );
    
    defDom = accessedObj->getDefinitionDomain( );
    cout << "     GetDefinitionDomain result: ";
    printInterval( &defDom );
    cout << endl;

    currDom = accessedObj->getCurrentDomain( );
    cout << "     GetCurrentDomain result:  ";
    printInterval( &currDom );
    cout << endl;
    
    entriesList = accessedObj->getTiles( );
    cout << "    -- GetTiles result: " << endl; 
    vector<Tile*>::iterator entryIt = entriesList->begin();
          
    while (entryIt !=  entriesList->end())
    {
       // (*entryIt)->printStatus();
       r_Minterval tileInterval = (*entryIt)->getDomain();
       int dimensionality = tileInterval.dimension();
	 
       cout << "   PersTile printStatus";
       cout << "    domain == " << dimensionality << ": ";
       for (int i = 0; i <dimensionality; i++)
         cout << tileInterval[i].low() << "-" << tileInterval[i].high() <<", ";
       cout << endl;
       entryIt++;
    }
    delete entriesList;
  }
  delete objsIt;
  mddObjsColl->releaseAll( );
}

/*************************************************************
 * Function......: 
 *
 * Arguments.....: 
 * Return value..: 
 * Description...: 
 ************************************************************/

static void printInterval(r_Minterval* inter)
{
   for (int i = 0; i <inter->dimension( ); i++)
   {
     (*inter)[i].is_low_fixed( )? cout << (*inter)[i].low( ): cout << "*";
     cout << " - ";
     (*inter)[i].is_high_fixed( )? cout << (*inter)[i].high( ): cout << "*";
     cout << ",";
   }
}

/*************************************************************
 * Function......:
 *
 * Arguments.....:
 * Return value..:
 * Description...:
 ************************************************************/

void printMemInfo( )
{


  // allows to store values in the program
  struct mallinfo meminfo = mallinfo();

  cout << "   Memory Usage Information :  bytes - Kbytes" ;
  cout << endl;

  cout << "      space in arena                 : " << meminfo.arena << " -  " << meminfo.arena/1024 << endl;
  cout << "      number of small blocks         : " << meminfo.smblks << " -  " << meminfo.smblks/1024  << endl;
  cout << "      number of ordinary blocks      : " << meminfo.ordblks  << " -  " << meminfo.ordblks/1024 << endl;
  cout << "      space in free ordinary blocks  : " << meminfo.fordblks  << " -  " << meminfo.fordblks/1024 << endl;
  cout << "      space in used ordinary blocks  : " << meminfo.uordblks  << " -  " << meminfo.uordblks/1024 << endl;

  // cout << "additional space from last call: " << meminfo.uordblks - memUsed < < endl;

}