test
CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
CArrayStreamer.cc
Go to the documentation of this file.
2 #include "CArrayStreamer.h"
3 #include "ClassUtils.h"
4 #include "MappingElement.h"
5 #include "ContainerSchema.h"
6 #include "RelationalBuffer.h"
10 #include "ArrayHandlerFactory.h"
11 #include "IArrayHandler.h"
12 // externals
13 #include "CoralBase/Attribute.h"
14 #include "RelationalAccess/IBulkOperation.h"
16 
18  MappingElement& mapping,
19  ContainerSchema& contSchema ):
20  m_objectType( objectType ),
21  m_mappingElement( mapping ),
22  m_schema( contSchema ),
23  m_recordId(),
24  m_localElement( ),
25  m_offset( 0 ),
26  m_insertOperation( 0 ),
27  m_arrayHandler(),
28  m_dataWriter(){
29 }
30 
32 }
33 
36  RelationalBuffer& operationBuffer ){
37 
38  m_localElement.clear();
39  m_recordId.clear();
40  // allocate for the index...
41  m_recordId.push_back(0);
42 
43  // Check the array type
44  edm::TypeWithDict arrayType = m_objectType.toType();
45  edm::TypeWithDict arrayResolvedType = ClassUtils::resolvedType(arrayType);
46  // Check the component type
47  if ( ! arrayType || !arrayResolvedType ) {
48  throwException( "Missing dictionary information for the element type of the array \"" +
49  m_objectType.cppName() + "\"",
50  "CArrayWriter::build" );
51  }
52 
53  RelationalStreamerFactory streamerFactory( m_schema );
54 
55  // first open the insert on the extra table...
56  m_insertOperation = &operationBuffer.newMultiRecordInsert( m_mappingElement.tableName() );
57  const std::vector<std::string>& columns = m_mappingElement.columnNames();
58  if( !columns.size() ){
59  throwException( "Id columns not found in the mapping.",
60  "CArrayWriter::build");
61  }
62  for( size_t i=0; i<columns.size(); i++ ){
63  m_insertOperation->addId( columns[ i ] );
64  }
65 
66  m_offset = &offset;
67 
68  m_arrayHandler.reset( ArrayHandlerFactory::newArrayHandler( m_objectType ) );
69 
70  std::string arrayTypeName = arrayType.name();
71  // Retrieve the relevant mapping element
72  MappingElement::iterator iMe = m_mappingElement.find( arrayTypeName );
73  if ( iMe == m_mappingElement.end() ) {
74  throwException( "Item for \"" + arrayTypeName + "\" not found in the mapping element",
75  "CArrayWriter::build" );
76  }
77 
78  m_dataWriter.reset( streamerFactory.newWriter( arrayResolvedType, iMe->second ));
79  m_dataWriter->build( m_localElement, *m_insertOperation, operationBuffer );
80  return true;
81 }
82 
83 void ora::CArrayWriter::setRecordId( const std::vector<int>& identity ){
84  m_recordId.clear();
85  for(size_t i=0;i<identity.size();i++) {
86  m_recordId.push_back( identity[i] );
87  }
88  m_recordId.push_back( 0 );
89 }
90 
92  const void* inputData ){
93 
94  if(!m_offset){
95  throwException("The streamer has not been built.",
96  "CArrayWriter::write");
97  }
98  const std::vector<std::string>& columns = m_mappingElement.columnNames();
99  if( columns.size() != m_recordId.size()+1){
100  throwException( "Record id elements provided are not matching with the mapped id columns.",
101  "CArrayWriter::write");
102  }
103 
104  void* data = m_offset->address( inputData );
105 
106  // Use the iterator to loop over the elements of the container.
107  size_t containerSize = m_arrayHandler->size( data );
108 
109  if ( containerSize == 0 ) return;
110 
111  size_t startElementIndex = m_arrayHandler->startElementIndex( data );
112 
113  std::auto_ptr<IArrayIteratorHandler> iteratorHandler( m_arrayHandler->iterate( data ) );
114 
115  InsertCache& bulkOperation = m_insertOperation->setUp( containerSize-startElementIndex+1 );
116 
117  for ( size_t iIndex = startElementIndex; iIndex < containerSize; ++iIndex ) {
118 
119  m_recordId[m_recordId.size()-1] = iIndex;
120  coral::AttributeList& dataBuff = m_insertOperation->data();
121 
122  dataBuff[ columns[0] ].data<int>() = oid;
123  for( size_t i = 1;i < columns.size(); i++ ){
124  dataBuff[ columns[i] ].data<int>() = m_recordId[i-1];
125  }
126 
127  void* objectReference = iteratorHandler->object();
128 
129  m_dataWriter->setRecordId( m_recordId );
130  m_dataWriter->write( oid, objectReference );
131 
132  bulkOperation.processNextIteration();
133  // Increment the iterator
134  iteratorHandler->increment();
135  }
136 
137  m_arrayHandler->finalize( const_cast<void*>( data ) );
138 
139 }
140 
142  MappingElement& mapping,
143  ContainerSchema& contSchema ):
144  m_deleter( mapping ),
145  m_writer( objectType, mapping, contSchema ){
146 }
147 
149 }
150 
152  IRelationalData& relationalData,
153  RelationalBuffer& operationBuffer){
154  m_deleter.build( operationBuffer );
155  m_writer.build( offset, relationalData, operationBuffer );
156  return true;
157 }
158 
159 void ora::CArrayUpdater::setRecordId( const std::vector<int>& identity ){
160  m_writer.setRecordId( identity );
161 }
162 
164  const void* data ){
165  m_deleter.erase( oid );
166  m_writer.write( oid, data );
167 }
168 
170  MappingElement& mapping,
171  ContainerSchema& contSchema ):
172  m_objectType( objectType ),
173  m_mappingElement( mapping ),
174  m_schema( contSchema ),
175  m_recordId(),
176  m_localElement( ),
177  m_offset(0 ),
178  m_query(),
179  m_arrayHandler(),
180  m_dataReader(){
181 }
182 
184 }
185 
187  IRelationalData& relationalData ){
188 
189  m_localElement.clear();
190 
191  m_recordId.clear();
192  // allocate for the index...
193  m_recordId.push_back(0);
194 
195  // Check the array type
196  edm::TypeWithDict arrayType = m_objectType.toType();
197  edm::TypeWithDict arrayResolvedType = ClassUtils::resolvedType(arrayType);
198  // Check the component type
199  if ( ! arrayType || !arrayResolvedType ) {
200  throwException( "Missing dictionary information for the element type of the array \"" +
201  m_objectType.cppName() + "\"",
202  "CArrayReader::build" );
203  }
204 
205  RelationalStreamerFactory streamerFactory( m_schema );
206 
207  // first open the insert on the extra table...
208  m_query.reset(new MultiRecordSelectOperation( m_mappingElement.tableName(), m_schema.storageSchema() ));
209  m_query->addWhereId( m_mappingElement.pkColumn() );
210  std::vector<std::string> recIdCols = m_mappingElement.recordIdColumns();
211  for( size_t i=0; i<recIdCols.size(); i++ ){
212  m_query->addId( recIdCols[ i ] );
213  m_query->addOrderId( recIdCols[ i ] );
214  }
215 
216  m_offset = &offset;
217 
218  m_arrayHandler.reset( ArrayHandlerFactory::newArrayHandler( m_objectType ) );
219 
220  std::string arrayTypeName = arrayType.name();
221 
222  // Retrieve the relevant mapping element
223  MappingElement::iterator iMe = m_mappingElement.find( arrayTypeName );
224  if ( iMe == m_mappingElement.end() ) {
225  throwException( "Item for \"" + arrayTypeName + "\" not found in the mapping element",
226  "CArrayReader::build" );
227  }
228 
229  m_dataReader.reset( streamerFactory.newReader( arrayResolvedType, iMe->second ) );
230  m_dataReader->build( m_localElement, *m_query );
231  return true;
232 }
233 
235  if(!m_query.get()){
236  throwException("The streamer has not been built.",
237  "CArrayReader::select");
238  }
239  coral::AttributeList& whereData = m_query->whereData();
240  whereData[ m_mappingElement.pkColumn() ].data<int>() = oid;
241  m_query->execute();
242  m_dataReader->select( oid );
243 }
244 
245 void ora::CArrayReader::setRecordId( const std::vector<int>& identity ){
246  m_recordId.clear();
247  for(size_t i=0;i<identity.size();i++) {
248  m_recordId.push_back( identity[i] );
249  }
250  // allocate the element for the index...
251  m_recordId.push_back( 0 );
252 }
253 
254 void ora::CArrayReader::read( void* destinationData ) {
255  if(!m_offset){
256  throwException("The streamer has not been built.",
257  "CArrayReader::read");
258  }
259  void* address = m_offset->address( destinationData );
260 
261  edm::TypeWithDict iteratorDereferenceReturnType = m_arrayHandler->iteratorReturnType();
262 
263  bool isElementFundamental = iteratorDereferenceReturnType.isFundamental();
264 
265  std::string positionColumn = m_mappingElement.posColumn();
266 
267  size_t arraySize = ClassUtils::arrayLength( m_objectType );
268 
269  m_arrayHandler->clear( address );
270 
271  size_t cursorSize = m_query->selectionSize(m_recordId, m_recordId.size()-1);
272  unsigned int i=0;
273  while ( i< cursorSize ){
274 
275  m_recordId[m_recordId.size()-1] = (int)i;
276  m_query->selectRow( m_recordId );
277  coral::AttributeList& row = m_query->data();
278 
279  int arrayIndex = row[positionColumn].data< int >();
280 
281  // Create a new element for the array
282  void* objectData = 0;
283 
284  if(arrayIndex >= (int)arraySize){
285  throwException("Found more element then array size.",
286  "CArrayReader::read");
287 
288  }
289 
290  // the memory has been allocated already!
291  objectData = static_cast<char*>(address)+arrayIndex*iteratorDereferenceReturnType.size();
292 
293  if(!isElementFundamental){
294  // in this case the initialization is required: use default constructor...
295  iteratorDereferenceReturnType.construct();// edm::TypeWithDict(0,0),std::vector< void* >(),objectData);
296  }
297 
298  m_dataReader->setRecordId( m_recordId );
299  m_dataReader->read( objectData );
300 
301  ++i;
302  }
303 
304  m_arrayHandler->finalize( address );
305 
306 }
307 
309  if(m_dataReader.get()) m_dataReader->clear();
310 }
311 
313  MappingElement& mapping,
314  ContainerSchema& contSchema ):
315  m_objectType( objectType ),
316  m_mapping( mapping ),
317  m_schema( contSchema ){
318 }
319 
321 }
322 
324  return new CArrayWriter( m_objectType, m_mapping, m_schema );
325 }
326 
328  return new CArrayUpdater( m_objectType, m_mapping, m_schema );
329 }
330 
332  return new CArrayReader( m_objectType, m_mapping, m_schema );
333 }
int i
Definition: DBlmapReader.cc:9
edm::TypeWithDict resolvedType(const edm::TypeWithDict &typ)
Definition: ClassUtils.cc:486
IRelationalReader * newReader()
bool build(DataElement &offset, IRelationalData &relationalData, RelationalBuffer &operationBuffer)
void select(int oid)
void update(int oid, const void *data)
Updates a data element.
CArrayWriter(const edm::TypeWithDict &objectType, MappingElement &mapping, ContainerSchema &contSchema)
Constructor.
CArrayStreamer(const edm::TypeWithDict &objectType, MappingElement &mapping, ContainerSchema &contSchema)
TypeWithDict toType() const
void setRecordId(const std::vector< int > &identity)
std::string name() const
void read(void *address)
Reads a data element.
IRelationalWriter * newWriter(const edm::TypeWithDict &dataType, MappingElement &dataMapping)
static IArrayHandler * newArrayHandler(const edm::TypeWithDict &arrayType)
CArrayUpdater(const edm::TypeWithDict &objectType, MappingElement &mapping, ContainerSchema &contSchema)
Constructor.
bool isFundamental() const
std::map< std::string, MappingElement >::iterator iterator
Iterator definition.
IRelationalUpdater * newUpdater()
MultiRecordInsertOperation & newMultiRecordInsert(const std::string &tableName)
IRelationalReader * newReader(const edm::TypeWithDict &dataType, MappingElement &dataMapping)
bool build(DataElement &offset, IRelationalData &relationalData, RelationalBuffer &operationBuffer)
void write(int oid, const void *data)
Writes a data element.
CArrayReader(const edm::TypeWithDict &objectType, MappingElement &mapping, ContainerSchema &contSchema)
Constructor.
size_t arrayLength(const edm::TypeWithDict &typ)
Definition: ClassUtils.cc:493
bool build(DataElement &offset, IRelationalData &relationalData)
void throwException(const std::string &message, const std::string &methodName) __attribute__((noreturn))
Definition: Exception.cc:10
size_t size() const
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:82
void setRecordId(const std::vector< int > &identity)
tuple columns
Definition: mps_check.py:210
virtual ~CArrayWriter()
ObjectWithDict construct() const
IRelationalWriter * newWriter()
tuple inputData
Definition: idDealer.py:72
void setRecordId(const std::vector< int > &identity)