summaryrefslogtreecommitdiffstats
path: root/rasodmg/gmarray.cc
blob: 89fa919ff3ad6ffd1b11f1acbf6e28e8e820adc6 (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
/*
* 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: gmarray.cc
 *
 * MODULE: rasodmg
 * CLASS:  r_GMarray
 *
 * COMMENTS:
 *		None
*/

static const char rcsidgarray[] = "@(#)rasodmg, r_GMarray: $Id: gmarray.cc,v 1.45 2003/12/27 23:02:56 rasdev Exp $";

#include <vector>

#ifdef __VISUALC__
#ifndef __EXECUTABLE__
#define __EXECUTABLE__
#define GMARRAY_NOT_SET
#endif
#endif

#include "rasodmg/marray.hh"
#include "rasodmg/database.hh"
#include "rasodmg/storagelayout.hh"
#include "rasodmg/alignedtiling.hh"
#include "clientcomm/clientcomm.hh"

#ifdef GMARRAY_NOT_SET
#undef __EXECUTABLE__
#endif

#include "raslib/rmdebug.hh"
#include "raslib/type.hh"
#include "raslib/marraytype.hh"
#include "raslib/structuretype.hh"

#include <string.h>  // for memcpy()
#include <iostream>
#include <iomanip>



r_GMarray::r_GMarray() throw(r_Error)
  : r_Object(1),
    data(0), 
    data_size(0),
    type_length(0), 
    current_format(r_Array),
    storage_layout(0)
{
  storage_layout = new r_Storage_Layout();
}



r_GMarray::r_GMarray(const r_Minterval& initDomain, r_Bytes initLength, r_Storage_Layout* stl) throw (r_Error)
  : r_Object(1), 
    data(0), 
    data_size(0),
    domain(initDomain), 
    type_length(initLength), 
    current_format(r_Array),
    storage_layout(stl)
{
  int error = 0;
  if (domain.dimension() == 0)
    {
    error = 1;
    }
  else	{
    if (storage_layout == NULL)
      {
      storage_layout = new r_Storage_Layout(new r_Aligned_Tiling(initDomain.dimension()));
      }
    if (!storage_layout->is_compatible(initDomain, initLength))
      {
      error = 2;
      }
    }
  if (error != 0)
    {
    RMInit::logOut << "r_GMarray::r_GMarray(" << initDomain << ", " << initLength << ", ";
    if (storage_layout == NULL)
      RMInit::logOut << "no storage layout";
    else
      RMInit::logOut << *storage_layout;
    RMInit::logOut << ") ";
    if (error == 1)
      {
      RMInit::logOut << "domain is not initialised";
      throw r_Error(DOMAINUNINITIALISED);
      }
    else	{
      RMInit::logOut << "storage layout is not compatible";
      throw r_Error(STORAGERLAYOUTINCOMPATIBLEWITHGMARRAY);
      }
    }
  data_size = domain.cell_count() * initLength;
  data = new char[ data_size ];
  memset(data, 0, data_size);
}



r_GMarray::r_GMarray(const r_GMarray &obj) throw(r_Error)
  : r_Object(obj, 1),
    data(0), 
    data_size(0),
    domain(obj.spatial_domain()),
    type_length(obj.type_length),
    current_format(obj.current_format),
    storage_layout(0)
{
  RMDBCLASS("r_GMarray", "r_GMarray(const r_GMarray)", "rasodmg", __FILE__, __LINE__)

  if (obj.data)
  {
    data_size = obj.data_size;  
    data = new char[ data_size ];
  
    memcpy(data, obj.data, (unsigned int)data_size);
  }

  // clone the storage layout object
  if (obj.storage_layout)
    storage_layout = obj.storage_layout->clone();
  else
    RMInit::logOut << "copy constructor no storage layout" << endl;
}



r_GMarray::r_GMarray(r_GMarray &obj) throw(r_Error)
  : r_Object(obj, 1),
    data(obj.data),
    data_size(obj.data_size),
    domain(obj.spatial_domain()),
    type_length(obj.type_length),
    current_format(obj.current_format),
    storage_layout(0)
{
  RMDBCLASS("r_GMarray", "r_GMarray(r_GMarray)", "rasodmg", __FILE__, __LINE__)

  obj.data_size      = 0;
  obj.data           = 0;
  obj.domain         = r_Minterval();
  obj.type_length    = 0;

  // clone the storage layout object
  if (obj.storage_layout)
    storage_layout = obj.storage_layout->clone();
  else
    RMInit::logOut << "copy constructor (no data) no storage layout" << endl;
}



r_GMarray::~r_GMarray()
{
  r_deactivate();
}



void
r_GMarray::r_deactivate()
{
  RMDBGENTER(2, RMDebug::module_rasodmg, "r_GMarray", "r_deactivate()");
   
  if (data)
  {
    delete[] data;
    data = 0;
  }
  
  // invoke deactivate of members with dynamic memory
  domain.r_deactivate();

  // delete dynamic members
  if (storage_layout) 
  {
    delete storage_layout;  
    storage_layout = 0;
  }

  RMDBGEXIT(2, RMDebug::module_rasodmg, "r_GMarray", "r_deactivate()");
}
    
 

const char*
r_GMarray::operator[] (const r_Point& point) const
  throw(r_Edim_mismatch, r_Eindex_violation)
{   
  return &(data[ domain.cell_offset(point)*type_length ]);   
}



const r_Storage_Layout*
r_GMarray::get_storage_layout() const
{
  return storage_layout;
}


void
r_GMarray::set_storage_layout(r_Storage_Layout *stl) throw(r_Error)
{
  if (!stl->is_compatible(domain, type_length))
  {
    RMInit::logOut << "r_GMarray::set_storage_layout(" << *stl << ") gmarray is not compatible with tiling" << endl;
    RMInit::logOut << "\tgmarray domain   : " << spatial_domain() << endl;
    RMInit::logOut << "\tgmarray type size: " << get_type_length() << endl;
    throw r_Error(STORAGERLAYOUTINCOMPATIBLEWITHGMARRAY);
  }

  if (storage_layout != NULL)
    delete storage_layout;

  storage_layout = stl;
}



const r_GMarray&
r_GMarray::operator=(const r_GMarray& marray)
{
  if (this != &marray)
  {
    if (data)
    {
      delete[] data;
      data = 0;
    }
    
    if (marray.data)
    {
      data_size = marray.data_size;
      data = new char[ data_size ];

      memcpy(data, marray.data, (unsigned int)data_size);  
    }

    if (storage_layout)
    {
      delete storage_layout;
      storage_layout = 0;
    }

    // this has to be changed to a clone() function in future
    if (marray.storage_layout)
      // storage_layout = new r_Storage_Layout(*marray.storage_layout);
      storage_layout = marray.storage_layout->clone();
  
    domain         = marray.domain;
    type_length    = marray.type_length;
    current_format = marray.current_format;    
  }
  
  return *this;
}



void
r_GMarray::insert_obj_into_db()
{
  // Nothing is done in that case. r_Marray objects can just be inserted as elements
  // of a collection which invokes r_GMarray::insert_obj_into_db(const char* collName)
  // of the r_Marray objects.
   
  // r_Database::actual_database->communication->insertSingleMDDObj(this);
  RMInit::logOut << " do nothing " << std::flush;
}



void
r_GMarray::insert_obj_into_db(const char* collName)
{
  // Insert myself in database only if I have a type name, otherwise
  // an exception is thrown.
  if (!type_name)
  {
    r_Error err = r_Error(r_Error::r_Error_DatabaseClassUndefined);
    throw err;
  }

  r_Database::actual_database->getComm()->insertMDD(collName, this);
}




void
r_GMarray::print_status(std::ostream& s) const
{
  const r_Type*       typeSchema     = (r_Base_Type*)((r_GMarray*)this)->get_type_schema();
  const r_Base_Type*  baseTypeSchema = (r_Base_Type*)((r_GMarray*)this)->get_base_type_schema();

  s << "GMarray" << endl;
  s << "  Oid...................: " << get_oid() << endl;
  s << "  Type Structure........: " << (get_type_structure() ? get_type_structure() : "<nn>") << endl;
  s << "  Type Schema...........: " << std::flush;
  if (typeSchema)
    typeSchema->print_status(s);
  else
    s << "<nn>" << std::flush;
  s << endl;
  s << "  Domain................: " << domain << endl;
  s << "  Base Type Schema......: " << std::flush;
  if (baseTypeSchema)
    baseTypeSchema->print_status(s);
  else
    s << "<nn>" << std::flush;
  s << endl;
  s << "  Base Type Length......: " << type_length << endl;
  s << "  Data format.......... : " << current_format << endl;
  s << "  Data size (bytes).... : " << data_size << endl;
}



void
r_GMarray::print_status(std::ostream& s, int hexoutput) const
{
  print_status(s);

  const r_Type*       typeSchema     = (r_Base_Type*)((r_GMarray*)this)->get_type_schema();
  const r_Base_Type*  baseTypeSchema = (r_Base_Type*)((r_GMarray*)this)->get_base_type_schema();

  if (domain.dimension())
  {
     r_Point p(domain.dimension());
     bool done = false;
     r_Dimension i = 0;
  
     // initialize point
     for (i = 0; i < domain.dimension(); i++)
       p << domain[i].low();
                                            
     // iterate over all cells
     while(!done)
     {
       //
       // print cell
       //

       // get cell address
       char* cell = data + domain.cell_offset(p) * type_length;

       if (hexoutput)   
       {
         for (r_Bytes j = 0; j < type_length; j++)
           s << std::hex << (unsigned int)(unsigned char)(cell[j]);
       }
       else {
         if(baseTypeSchema) baseTypeSchema->print_value(cell,  s );
         else s << "<nn>" << std::flush;
       }

       s << "   ";
      
       // increment coordinate
       i = 0;
       while(++p[i] > domain[i].high())
       {
         s << endl;
         p[i] = domain[i].low();
         i++;
         if (i >= domain.dimension())
         {
           done = true;
           break;
         }
       }
       if (i > 1) s << endl;
     }
   }
   else
   {
     s << "Cell value " << std::flush;

     // print cell
     if ((hexoutput) || (!baseTypeSchema))
     {
        for (unsigned int j=0; j<type_length; j++)
          s << std::hex << (unsigned int)(unsigned char)(data[j]);
     }
     else {
         if(baseTypeSchema) baseTypeSchema->print_value(data,  s );
         else s << "<nn>" << std::flush;
     }
     s << endl;
   }

   // turn off hex mode again
   s << std::dec << std::flush;
}




r_GMarray* r_GMarray::intersect(r_Minterval where) const
{
  r_GMarray* tile = new r_GMarray();

  r_Minterval obj_domain = spatial_domain();
  r_Dimension num_dims = obj_domain.dimension();
  r_Bytes tlength = get_type_length();

  char* obj_data = new char[where.cell_count() * tlength];
  tile->set_spatial_domain(where);
  tile->set_type_length(tlength);
  tile->set_array(obj_data);
  tile->set_array_size(where.cell_count() * tlength);

  r_Bytes block_length = where[num_dims-1].high() - where[num_dims-1].low() + 1;
  r_Bytes total = where.cell_count()/block_length;

  for (r_Area cell = 0; cell < total; cell++)
  {
    r_Point p = where.cell_point(cell*block_length);
      
    char* dest_off = obj_data;
    const char* source_off = get_array();
      
    memcpy((void*) (dest_off + where.cell_offset(p)*tlength),
	    (void*) (source_off + obj_domain.cell_offset(p)*tlength),
	    (size_t) (block_length * tlength));
  }
  
  return tile;
}

     

const r_Base_Type* 
r_GMarray::get_base_type_schema()
{
  const r_Type*      typePtr     = r_Object::get_type_schema();
  const r_Base_Type* baseTypePtr = 0;

  if (typePtr)
  {
    if (typePtr->type_id() == r_Type::MARRAYTYPE)
    {
      const r_Marray_Type* marrayTypePtr = (const r_Marray_Type*)typePtr;
      baseTypePtr = &(marrayTypePtr->base_type());
    }
    else
    {
    //whenever this module is done correctly, it should be checked first, what kind of type we are dealing with.  therefore i do not declare a throw clause.
    RMInit::logOut << "r_GMarray::get_base_type_schema() the type retrieved (" << typePtr->name() << ") was not a marray type" << endl;
    throw r_Error(NOTANMARRAYTYPE);
    }
  }

  return baseTypePtr;  
}