CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
UniqueRefStreamer.cc
Go to the documentation of this file.
3 #include "UniqueRefStreamer.h"
4 #include "RelationalOperation.h"
5 #include "MappingElement.h"
6 #include "MappingRules.h"
7 #include "ContainerSchema.h"
8 #include "ClassUtils.h"
10 // externals
11 #include "CoralBase/Attribute.h"
14 
15 namespace ora {
16 
18  public:
19 
21  m_dataElement(),
22  m_writer(),
23  m_depInsert( 0 ){
24  }
25 
26  void build( const edm::TypeWithDict& objectType, MappingElement& mapping,
27  ContainerSchema& contSchema, RelationalBuffer& operationBuffer ){
28 
29  m_depInsert = &operationBuffer.newInsert( mapping.tableName());
30  const std::vector<std::string> columns = mapping.columnNames();
31  for( std::vector<std::string>::const_iterator iC = columns.begin();
32  iC != columns.end(); ++iC){
33  m_depInsert->addId( *iC );
34  }
35 
36  MappingElement::iterator iMe = mapping.find( objectType.cppName() );
37  // the first inner mapping is the relevant...
38  if( iMe == mapping.end()){
39  throwException("Could not find a mapping element for class \""+
40  objectType.cppName()+"\"",
41  "DependentClassWriter::write");
42  }
43  RelationalStreamerFactory streamerFactory( contSchema );
44  m_writer.reset( streamerFactory.newWriter( objectType, iMe->second ) );
45  m_writer->build( m_dataElement, *m_depInsert, operationBuffer );
46  }
47 
49  }
50 
51  void write( int oId, int refId, const void* data ){
52  if( !m_depInsert ){
53  throwException( "DependentClassWriter has not been built.",
54  "DependentClassWriter::write");
55  }
56 
57  coral::AttributeList& dataBuff = m_depInsert->data();
58  coral::AttributeList::iterator iData = dataBuff.begin();
59  iData->data<int>()= oId;
60  ++iData;
61  iData->data<int>()= refId;
62  // transfering the refId
63  std::vector<int> recordId(1,refId);
64  m_writer->setRecordId( recordId);
65  m_writer->write( oId, data );
66  }
67 
68  private:
70  std::auto_ptr<IRelationalWriter> m_writer;
72  };
73 
75  public:
77  m_dataElement(),
78  m_type(),
79  m_reader(),
80  m_depQuery(){
81  }
82 
83  void build( const edm::TypeWithDict& objectType, MappingElement& depMapping, ContainerSchema& contSchema ){
84 
85  m_type = objectType;
86  m_depQuery.reset( new SelectOperation( depMapping.tableName(), contSchema.storageSchema()));
87  m_depQuery->addWhereId( depMapping.columnNames()[ 1 ] );
88  MappingElement::iterator iMap = depMapping.find( m_type.cppName() );
89  // the first inner mapping is the good one ...
90  if( iMap == depMapping.end()){
91  throwException("Could not find a mapping element for class \""+
92  m_type.cppName()+"\"",
93  "DependentClassReadBuffer::ReadBuffer");
94  }
95  MappingElement& mapping = iMap->second;
96  RelationalStreamerFactory streamerFactory( contSchema );
97  m_reader.reset( streamerFactory.newReader( m_type, mapping )) ;
98  m_reader->build( m_dataElement , *m_depQuery );
99  }
100 
102  }
103 
104  void* read( int refId ){
105  if( !m_depQuery.get() ){
106  throwException( "DependentClassReader has not been built.",
107  "DependentClassReader::read");
108  }
109  coral::AttributeList& whereData = m_depQuery->whereData();
110  coral::AttributeList::iterator iWData = whereData.begin();
111  iWData->data<int>() = refId;
112  m_depQuery->execute();
113  m_reader->select( refId );
114  void* destination = 0;
115  if( m_depQuery->nextCursorRow() ){
116  destination = ClassUtils::constructObject( m_type );
117  m_reader->read( destination );
118  }
119  m_depQuery->clear();
120  m_reader->clear();
121  return destination;
122  }
123 
124  private:
127  std::auto_ptr<IRelationalReader> m_reader;
128  std::auto_ptr<SelectOperation> m_depQuery;
129  };
130 
132  public:
133  explicit RelationalRefLoader( int refId ):
134  m_reader(),
135  m_refId( refId ),
136  m_valid( false ){
137  }
138 
140  }
141 
142  public:
143  void build( const edm::TypeWithDict& objectType, MappingElement& mapping, ContainerSchema& contSchema ){
144  m_reader.build( objectType, mapping, contSchema );
145  m_valid = true;
146  }
147 
148  void* load() const override {
149  if(!m_valid){
150  throwException("Ref Loader has been invalidate.",
151  "RelationalRefLoader::load");
152  }
153  return m_reader.read( m_refId );
154  }
155 
156  void invalidate() override{
157  m_valid = false;
158  }
159 
160  bool isValid() const override{
161  return m_valid;
162  }
163 
164  private:
166  int m_refId;
167  bool m_valid;
168  };
169 }
170 
172  MappingElement& mapping,
173  ContainerSchema& contSchema ):
174  m_objectType( objectType ),
175  m_mappingElement( mapping ),
176  m_schema( contSchema ),
177  m_dataElement( 0 ),
178  m_relationalData( 0 ),
179  m_operationBuffer( 0 ){
180  m_columnIndexes[0]=-1;
181  m_columnIndexes[1]=-1;
182 }
183 
185  static const std::string nullLabel("ora::UniqueRef::Null");
186  return nullLabel;
187 }
188 
190 }
191 
193  IRelationalData& relationalData,
194  RelationalBuffer& buffer){
195  m_dataElement = &dataElement;
196 
197  const std::vector<std::string>& columns = m_mappingElement.columnNames();
198  if( columns.size() < 2 ){
199  throwException("Expected column names have not been found in the mapping.",
200  "UniqueRefWriter::build");
201  }
202  // booking for ref metadata
203  m_columnIndexes[0] = relationalData.addData( columns[0],typeid(std::string) );
204  // booking for ref id
205  m_columnIndexes[1] = relationalData.addData( columns[1],typeid(int) );
206  m_relationalData = &relationalData;
207  m_operationBuffer = &buffer;
208  return true;
209 }
210 
211 void ora::UniqueRefWriter::setRecordId( const std::vector<int>& ){
212 }
213 
216  const void* data ){
217 
218  if(!m_dataElement){
219  throwException("The streamer has not been built.",
220  "UniqueRefWriter::write");
221  }
222 
223  void* refAddress = m_dataElement->address( data );
224 
225  edm::ObjectWithDict refObj( m_objectType, const_cast<void*>(refAddress));
226 
227  bool isNull;
228  edm::ObjectWithDict resObj = edm::ObjectWithDict(edm::TypeWithDict(typeid(bool)), &isNull);
229  refObj.typeOf().functionMemberByName("operator!").invoke(refObj, &resObj);
230 
231  int refId = 0;
233 
234  if(!isNull){
235  // resolving the ref type
236  const std::type_info *refTypeInfo = 0;
237  edm::TypeWithDict typeInfoDict( typeid(std::type_info*) );
238  edm::ObjectWithDict refTIObj( typeInfoDict, &refTypeInfo );
239  m_objectType.functionMemberByName("typeInfo").invoke( refObj, &refTIObj);
240  edm::TypeWithDict refType = ClassUtils::lookupDictionary( *refTypeInfo );
241  className = refType.cppName();
242 
243  // building the dependent buffer
244  MappingElement& depMapping = m_schema.mappingForDependentClass( refType, true );
245 
246  // getting a new valid ref id
247  refId = m_schema.containerSequences().getNextId( MappingRules::sequenceNameForDependentClass( m_schema.containerName(),className ));
248 
249  DependentClassWriter writer;
250  writer.build( refType, depMapping, m_schema, m_operationBuffer->addVolatileBuffer() );
251  void* refData = 0;
252  edm::ObjectWithDict refDataObj(edm::TypeWithDict(typeid(void*)), &refData);
253  m_objectType.functionMemberByName("operator*").invoke( refObj, &refDataObj );
254 
255  writer.write( oid, refId, refData );
256 
257  }
258  // writing in the parent table
259  coral::AttributeList& parentData = m_relationalData->data();
260  parentData[m_columnIndexes[0]].data<std::string>()=className;
261  parentData[m_columnIndexes[1]].data<int>()=refId;
262 }
263 
265  MappingElement& mapping,
266  ContainerSchema& contSchema ):
267  m_writer( objectType, mapping, contSchema ){
268 }
269 
271 }
272 
274  IRelationalData& relationalData,
275  RelationalBuffer& operationBuffer){
276  return m_writer.build( dataElement, relationalData, operationBuffer );
277 }
278 
279 void ora::UniqueRefUpdater::setRecordId( const std::vector<int>& identity ){
280  m_writer.setRecordId( identity );
281 }
282 
285  const void* data ){
286  m_writer.write( oid, data );
287 }
288 
290  MappingElement& mapping,
291  ContainerSchema& contSchema ):
292  m_objectType( objectType ),
293  m_mappingElement( mapping ),
294  m_schema( contSchema ),
295  m_dataElement( 0 ),
296  m_relationalData( 0 ),
297  m_loaders(){
298  m_columnIndexes[0]=-1;
299  m_columnIndexes[1]=-1;
300 }
301 
303  for(std::vector<boost::shared_ptr<RelationalRefLoader> >::const_iterator iL = m_loaders.begin();
304  iL != m_loaders.end(); ++iL ){
305  (*iL)->invalidate();
306  }
307 }
308 
310  IRelationalData& relationalData){
311  m_dataElement = &dataElement;
312  const std::vector<std::string>& columns = m_mappingElement.columnNames();
313  if( columns.size() < 2 ){
314  throwException("Expected column names have not been found in the mapping.",
315  "UniqueRefReader::build");
316  }
317  // booking for ref metadata
318  m_columnIndexes[0] = relationalData.addData( columns[0],typeid(std::string) );
319  // booking for ref id
320  m_columnIndexes[1] = relationalData.addData( columns[1],typeid(int) );
321  m_relationalData = &relationalData;
322  return true;
323 }
324 
326 }
327 
328 void ora::UniqueRefReader::setRecordId( const std::vector<int>& ){
329 }
330 
333  if(!m_dataElement){
334  throwException("The streamer has not been built.",
335  "UniqueRefReader::read");
336  }
337  coral::AttributeList& row = m_relationalData->data();
338  std::string className = row[m_columnIndexes[0]].data<std::string>();
339  int refId = row[m_columnIndexes[1]].data<int>();
340 
341  edm::TypeWithDict refType = ClassUtils::lookupDictionary(className);
342 
343  // building the dependent buffer
344  MappingElement& depMapping = m_schema.mappingForDependentClass( refType );
345 
346  // resolving loader address
347  edm::MemberWithDict loaderMember = m_objectType.dataMemberByName("m_loader");
348  DataElement& loaderElement = m_dataElement->addChild( loaderMember.offset(), 0 );
349  void* loaderAddress = loaderElement.address( data );
350  boost::shared_ptr<IPtrLoader>* loaderPtr = static_cast<boost::shared_ptr<IPtrLoader>*>( loaderAddress );
351  // creating new loader
352  boost::shared_ptr<RelationalRefLoader> newLoader( new RelationalRefLoader( refId ) );
353  newLoader->build( refType, depMapping, m_schema );
354  m_loaders.push_back( newLoader );
355  // installing the new loader
356  boost::shared_ptr<IPtrLoader> tmp( newLoader );
357  *loaderPtr = tmp;
358 }
359 
361 }
362 
364  MappingElement& mapping,
365  ContainerSchema& contSchema ):
366  m_objectType( objectType ),
367  m_mapping( mapping ),
368  m_schema( contSchema ){
369 }
370 
372 }
373 
375  return new UniqueRefWriter( m_objectType, m_mapping, m_schema );
376 }
377 
379  return new UniqueRefUpdater( m_objectType, m_mapping, m_schema );
380 }
381 
383  return new UniqueRefReader( m_objectType, m_mapping, m_schema );
384 }
385 
UniqueRefReader(const edm::TypeWithDict &objectType, MappingElement &mapping, ContainerSchema &contSchema)
void build(const edm::TypeWithDict &objectType, MappingElement &mapping, ContainerSchema &contSchema, RelationalBuffer &operationBuffer)
IRelationalReader * newReader()
void build(const edm::TypeWithDict &objectType, MappingElement &depMapping, ContainerSchema &contSchema)
void * load() const override
void * address(const void *topLevelAddress) const
Definition: DataElement.cc:49
void write(int oid, const void *data)
Writes a data element.
bool build(DataElement &offset, IRelationalData &relationalData)
size_t offset() const
virtual int addData(const std::string &columnName, const std::type_info &columnType)=0
std::auto_ptr< IRelationalWriter > m_writer
IRelationalUpdater * newUpdater()
void setRecordId(const std::vector< int > &identity)
void write(int oId, int refId, const void *data)
UniqueRefWriter(const edm::TypeWithDict &objectType, MappingElement &mapping, ContainerSchema &contSchema)
bool build(DataElement &dataElement, IRelationalData &relationalData, RelationalBuffer &operationBuffer)
InsertOperation & newInsert(const std::string &tableName)
TypeWithDict typeOf() const
void read(void *destination)
Reads a data element.
InsertOperation * m_depInsert
std::string cppName() const
void setRecordId(const std::vector< int > &identity)
UniqueRefStreamer(const edm::TypeWithDict &objectType, MappingElement &mapping, ContainerSchema &contSchema)
void update(int oid, const void *data)
Updates a data element.
IRelationalWriter * newWriter(const edm::TypeWithDict &dataType, MappingElement &dataMapping)
void invoke(ObjectWithDict const &obj, ObjectWithDict *ret=nullptr, std::vector< void * > const &values=std::vector< void * >()) const
Call a member function.
DependentClassReader m_reader
iterator find(const std::string &key)
Retrieves a sub-element.
FunctionWithDict functionMemberByName(std::string const &) const
std::map< std::string, MappingElement >::iterator iterator
Iterator definition.
const std::vector< std::string > & columnNames() const
DataElement & addChild(size_t declaringScopeOffset, size_toffset)
Definition: DataElement.cc:27
edm::TypeWithDict lookupDictionary(const std::type_info &typeInfo, bool throwFlag=true)
Definition: ClassUtils.cc:170
bool isValid() const override
coral::AttributeList & data()
IRelationalReader * newReader(const edm::TypeWithDict &dataType, MappingElement &dataMapping)
coral::ISchema & storageSchema()
void setRecordId(const std::vector< int > &identity)
void throwException(const std::string &message, const std::string &methodName) __attribute__((noreturn))
Definition: Exception.cc:10
iterator end()
Returns an iterator in the end of the sequence.
std::vector< std::vector< double > > tmp
Definition: MVATrainer.cc:100
UniqueRefUpdater(const edm::TypeWithDict &objectType, MappingElement &mapping, ContainerSchema &contSchema)
void * constructObject(const edm::TypeWithDict &typ)
Definition: ClassUtils.cc:205
std::auto_ptr< IRelationalReader > m_reader
std::auto_ptr< SelectOperation > m_depQuery
int addId(const std::string &columnName)
const std::string & tableName() const
void build(const edm::TypeWithDict &objectType, MappingElement &mapping, ContainerSchema &contSchema)
static std::string sequenceNameForDependentClass(const std::string &containerName, const std::string &className)
Definition: MappingRules.cc:21
volatile std::atomic< bool > shutdown_flag false
std::string className(const T &t)
Definition: ClassName.h:30
IRelationalWriter * newWriter()
bool build(DataElement &dataElement, IRelationalData &relationalData, RelationalBuffer &operationBuffer)
std::string uniqueRefNullLabel()