summaryrefslogtreecommitdiffstats
path: root/rasodmg/partinsert.cc
blob: 566f2878169a7a5ae89dc7d200d5a340cb717996 (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
/*
* 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: partinsert.cc
 *
 * MODULE:  rasodmg
 * CLASS:   r_Partial_Insert
 *
 * COMMENTS:
 *		None
*/

#include <string.h>
#include <stdio.h>
#include <vector>

#include "raslib/rminit.hh"
#include "raslib/rmdebug.hh"
#include "raslib/minterval.hh"
#include "raslib/error.hh"
#include "rasodmg/storagelayout.hh"
#include "rasodmg/alignedtiling.hh"
#include "rasodmg/gmarray.hh"
#include "rasodmg/database.hh"
#include "rasodmg/oqlquery.hh"
#include "rasodmg/partinsert.hh"



// format string for creating collections; parameters: collection name, set type
const char *r_Partial_Insert::format_create = "CREATE COLLECTION %s %s";
// format string for updating the MDD; parameters: collection name, local oid
const char *r_Partial_Insert::format_update = "UPDATE %s AS x SET x ASSIGN $1 WHERE OID(x) = %f";


r_Partial_Insert::r_Partial_Insert( r_Database &usedb, const char *collname, const char *mddtype, const char *settype, const r_Storage_Layout &stl ) : mydb(usedb)
{
  init_share(collname, mddtype, settype);
  mystl = stl.clone();
}


r_Partial_Insert::r_Partial_Insert( r_Database &usedb, const char *collname, const char *mddtype, const char *settype, const r_Minterval &dom, unsigned int tsize ) : mydb(usedb)
{
  init_share(collname, mddtype, settype);
  r_Aligned_Tiling *tilingObj = new r_Aligned_Tiling(dom, tsize * dom.cell_count());
  mystl = new r_Storage_Layout(tilingObj);
}


r_Partial_Insert::r_Partial_Insert( const r_Partial_Insert &src ) : mydb(src.mydb)
{
  init_share(src.collName, src.mddType, src.setType);
  mystl = src.mystl->clone();
}


r_Partial_Insert::~r_Partial_Insert( void )
{
  if (collName != NULL)
    delete [] collName;
  if (mddType != NULL)
    delete [] mddType;
  if (setType != NULL)
    delete [] setType;
  if (mystl != NULL)
    delete mystl;
}


void r_Partial_Insert::init_share( const char *collname, const char *mddtype, const char *settype )
{
  collName = new char[strlen(collname)+1];
  strcpy(collName, collname);
  mddType = new char[strlen(mddtype)+1];
  strcpy(mddType, mddtype);
  setType = new char[strlen(settype)+1];
  strcpy(setType, settype);
  doUpdate = 0;
}

int r_Partial_Insert::update( r_GMarray *mddPtr, 
  	      r_Data_Format transferFormat,
  	      const char* transferFormatParams,  	      
  	      r_Data_Format storageFormat,  	      
  	      const char* storageFormatParams
  	      )
{
  RMDBGENTER(0, RMDebug::module_rasodmg, "r_Partial_Insert", "update()");
  try {
    mddPtr->set_storage_layout(mystl->clone());
    mddPtr->set_type_by_name(mddType);
  }
  catch (r_Error &err) {
      RMInit::logOut << "r_Partial_Insert::update(): unable to set storage_layout for the currend MDD: "
		     << err.what() << endl;
      return -1;
  }  

  if (doUpdate == 0)
  {
    char *queryBuffer = new char[strlen(format_create) + strlen(collName) + strlen(setType)];
    sprintf(queryBuffer, format_create, collName, setType);
    // first try creating the collection
    try
    {
      // this is basically 3 bytes to long, but that doesn't matter
      myta.begin();
      mydb.set_transfer_format(transferFormat, transferFormatParams);
      mydb.set_storage_format(storageFormat, storageFormatParams);
      r_OQL_Query query(queryBuffer);
      r_oql_execute(query);
      myta.commit();
      RMDBGMIDDLE(0, RMDebug::module_rasodmg, "r_Partial_Insert", "update(): created new collection " << collName << " with type " << setType );
    }
    catch (r_Error &err)
    {
      RMDBGMIDDLE(0, RMDebug::module_rasodmg, "r_Partial_Insert", "update(): can't create collection: " << err.what() );
      myta.abort();
      // failure to create the collection is not an error
    }
    delete [] queryBuffer;

    // now create the root object
    try
    {
      myta.begin();
      mydb.set_transfer_format(transferFormat, transferFormatParams);
      mydb.set_storage_format(storageFormat, storageFormatParams);      
      r_Ref<r_GMarray> mddp = new (&mydb, mddType) r_GMarray(*mddPtr);
      r_Ref<r_Set<r_Ref<r_GMarray> > > mddCollPtr;
      mddCollPtr = (r_Ref<r_Set<r_Ref<r_GMarray> > >)(mydb.lookup_object(collName));
      mddCollPtr->insert_element(mddp);
      myOId = mddp->get_oid();
      myta.commit();
      RMDBGMIDDLE(0, RMDebug::module_rasodmg, "r_Partial_Insert", "update(): reated root object OK, oid = " << myOId );
      doUpdate = 1;
    }
    catch (r_Error &err)
    {
      RMInit::logOut << "r_Partial_Insert::update(): unable to create root object: "
		     << err.what() << endl;
      myta.abort();
      return -1;
    }
  }
  else
  {
    char *queryBuffer = new char[strlen(format_update) + strlen(collName) + 32];
    sprintf(queryBuffer, format_update, collName, myOId.get_local_oid());
    
    // try the update
    try
    {
      myta.begin();
      mydb.set_transfer_format(transferFormat, transferFormatParams);
      mydb.set_storage_format(storageFormat, storageFormatParams);      
      RMDBGMIDDLE(1, RMDebug::module_rasodmg, "r_Partial_Insert", "update(): QUERY: " << queryBuffer );
      r_OQL_Query query(queryBuffer);
      query << (*mddPtr);
      r_oql_execute(query);
      myta.commit();
      RMDBGMIDDLE(0, RMDebug::module_rasodmg, "r_Partial_Insert", "update(): update object OK" );
    }
    catch (r_Error &err)
    {
      RMInit::logOut << "r_Partial_Insert::update(): failed to update marray: "
		     << err.what() << endl;
      myta.abort();
      delete [] queryBuffer;
      RMDBGEXIT(1, RMDebug::module_rasodmg, "r_Partial_Insert", "failed");
      return -1;
    }
    delete [] queryBuffer;
  }
  RMDBGEXIT(1, RMDebug::module_rasodmg, "r_Partial_Insert", "update(): exiting OK" );

  return 0;
}