CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
OraPtrStreamer.cc
Go to the documentation of this file.
3 #include "OraPtrStreamer.h"
4 #include "RelationalOperation.h"
5 #include "MappingElement.h"
6 #include "ContainerSchema.h"
7 #include "ClassUtils.h"
9 // externals
10 #include "CoralBase/Attribute.h"
14 
15 namespace ora {
17  public:
18  OraPtrReadBuffer( const edm::TypeWithDict& objectType, MappingElement& mapping, ContainerSchema& contSchema ):
19  m_objectType( objectType ),
20  m_mapping( mapping ),
21  m_schema( contSchema ),
23  m_query( mapping.tableName(), contSchema.storageSchema() ),
24  m_dataElement( 0 ),
25  m_reader(){
26  }
27 
29  }
30 
31  bool build( DataElement& dataElement ){
32  m_dataElement = &dataElement;
34 
35  const std::vector<std::string>& columns = m_mapping.columnNames();
36  size_t cols = columns.size();
37  for( size_t i=0; i<cols; i++ ){
38  m_query.addWhereId( columns[ i ] );
39  }
40 
41  // Check the type
43  edm::TypeWithDict ptrResolvedType = ClassUtils::resolvedType(ptrType);
44  // Check the component type
45  if ( ! ptrType || !ptrResolvedType ) {
46  throwException( "Missing dictionary information for the type of the pointer \"" +
47  m_objectType.cppName() + "\"",
48  "OraPtrReadBuffer::build" );
49  }
50 
51  std::string ptrTypeName = ptrType.name();
52  // Retrieve the relevant mapping element
53  MappingElement::iterator iMe = m_mapping.find( ptrTypeName );
54  if ( iMe == m_mapping.end() ) {
55  throwException( "Item for \"" + ptrTypeName + "\" not found in the mapping element",
56  "OraPtrReadBuffer::build" );
57  }
58  RelationalStreamerFactory streamerFactory( m_schema );
59  m_reader.reset( streamerFactory.newReader( ptrResolvedType, iMe->second ) );
60  return m_reader->build( m_localElement, m_query );
61  }
62 
63  void* read( const std::vector<int>& fullId ){
64  if(!m_dataElement) throwException( "Read buffer has not been built.","OraPtrReadBuffer::read");
65 
66  if(!fullId.size()) throwException( "Object id set is empty.","OraPtrReadBuffer::read");
67 
68  coral::AttributeList& whereBuff = m_query.whereData();
69  coral::AttributeList::iterator iCol = whereBuff.begin();
70  size_t i=0;
71  for( coral::AttributeList::iterator iCol = whereBuff.begin(); iCol != whereBuff.end(); ++iCol ){
72  if( i<fullId.size() ){
73  iCol->data<int>() = fullId[i];
74  }
75  i++;
76  }
77 
78  std::vector<int> recordId( fullId.size()-1 );
79  for( size_t i=0; i<fullId.size()-1; i++ ){
80  recordId[i] = fullId[i+1];
81  ++i;
82  }
83 
84  m_query.execute();
85  m_reader->select( fullId[0] );
86  void* destination = 0;
87  if( m_query.nextCursorRow() ){
89  m_reader->setRecordId( recordId );
90  m_reader->read( destination );
91  }
92  m_query.clear();
93  m_reader->clear();
94  return destination;
95  }
96 
97  private:
104  std::auto_ptr<IRelationalReader> m_reader;
105  };
106 
108  public:
109  RelationalPtrLoader( OraPtrReadBuffer& buffer, const std::vector<int>& fullId ):
110  m_buffer( buffer ),
111  m_fullId( fullId ),
112  m_valid( true ){
113  }
114 
116  }
117 
118  public:
119  void* load() const override {
120  if(!m_valid){
121  throwException("Ptr Loader has been invalidate.",
122  "RelationalPtrLoader::load");
123  }
124  return m_buffer.read( m_fullId );
125  }
126 
127  void invalidate() override{
128  m_valid = false;
129  }
130 
131  bool isValid() const override{
132  return m_valid;
133  }
134 
135  private:
137  std::vector<int> m_fullId;
138  bool m_valid;
139  };
140 }
141 
143  MappingElement& mapping,
144  ContainerSchema& contSchema ):
145  m_objectType( objectType ),
146  m_mappingElement( mapping ),
147  m_schema( contSchema ),
148  m_localElement(),
149  m_dataElement( 0 ),
150  m_writer(){
151 }
152 
154 }
155 
157  IRelationalData& relationalData,
158  RelationalBuffer& operationBuffer){
159  m_dataElement = &dataElement;
160  m_localElement.clear();
161 
162  // Check the type
163  edm::TypeWithDict ptrType = m_objectType.templateArgumentAt(0);
164  edm::TypeWithDict ptrResolvedType = ClassUtils::resolvedType(ptrType);
165  // Check the component type
166  if ( ! ptrType || !ptrResolvedType ) {
167  throwException( "Missing dictionary information for the type of the pointer \"" +
168  m_objectType.cppName() + "\"",
169  "OraPtrWriter::build" );
170  }
171 
172  std::string ptrTypeName = ptrType.name();
173 // Retrieve the relevant mapping element
174  MappingElement::iterator iMe = m_mappingElement.find( ptrTypeName );
175  if ( iMe == m_mappingElement.end() ) {
176  throwException( "Item for \"" + ptrTypeName + "\" not found in the mapping element",
177  "OraPtrWriter::build" );
178  }
179  RelationalStreamerFactory streamerFactory( m_schema );
180  m_writer.reset( streamerFactory.newWriter( ptrResolvedType, iMe->second ));
181  return m_writer->build( m_localElement, relationalData, operationBuffer );
182 }
183 
184 void ora::OraPtrWriter::setRecordId( const std::vector<int>& identity ){
185  m_writer->setRecordId( identity );
186 }
187 
190  const void* data ){
191 
192  if(!m_dataElement){
193  throwException("The streamer has not been built.",
194  "OraPtrWriter::write");
195  }
196 
197  edm::ObjectWithDict ptrObject( m_objectType, m_dataElement->address( data ) );
198  // first load if required
199  m_objectType.functionMemberByName("load").invoke(ptrObject,nullptr);
200  // then get the data...
201  void* ptrAddress = nullptr;
202  edm::ObjectWithDict ptrAddrObj = edm::ObjectWithDict( edm::TypeWithDict(typeid(void*)), &ptrAddress );
203  m_objectType.functionMemberByName("address").invoke(ptrObject, &ptrAddrObj);
204  m_writer->write( oid, ptrAddress );
205 }
206 
208  MappingElement& mapping,
209  ContainerSchema& contSchema ):
210  m_objectType( objectType ),
211  m_mappingElement( mapping ),
212  m_schema( contSchema ),
213  m_localElement(),
214  m_dataElement( 0 ),
215  m_updater(){
216 }
217 
219 }
220 
222  IRelationalData& relationalData,
223  RelationalBuffer& operationBuffer){
224  m_dataElement = &dataElement;
225  m_localElement.clear();
226 
227  // Check the type
228  edm::TypeWithDict ptrType = m_objectType.templateArgumentAt(0);
229  edm::TypeWithDict ptrResolvedType = ClassUtils::resolvedType(ptrType);
230  // Check the component type
231  if ( ! ptrType || !ptrResolvedType ) {
232  throwException( "Missing dictionary information for the type of the pointer \"" +
233  m_objectType.cppName() + "\"",
234  "OraPtrUpdater::build" );
235  }
236 
237  std::string ptrTypeName = ptrType.name();
238 // Retrieve the relevant mapping element
239  MappingElement::iterator iMe = m_mappingElement.find( ptrTypeName );
240  if ( iMe == m_mappingElement.end() ) {
241  throwException( "Item for \"" + ptrTypeName + "\" not found in the mapping element",
242  "OraPtrUpdater::build" );
243  }
244  RelationalStreamerFactory streamerFactory( m_schema );
245  m_updater.reset( streamerFactory.newUpdater( ptrResolvedType, iMe->second ) );
246  return m_updater->build( m_localElement, relationalData, operationBuffer );
247 }
248 
249 void ora::OraPtrUpdater::setRecordId( const std::vector<int>& identity ){
250  m_updater->setRecordId( identity );
251 }
252 
255  const void* data ){
256  if(!m_dataElement){
257  throwException("The streamer has not been built.",
258  "OraPtrUpdater::update");
259  }
260  edm::ObjectWithDict ptrObject( m_objectType, m_dataElement->address( data ) );
261  // first load if required
262  m_objectType.functionMemberByName("load").invoke(ptrObject, nullptr);
263  void *ptrAddress = nullptr;
264  edm::ObjectWithDict ptrAddrObj = edm::ObjectWithDict( edm::TypeWithDict(typeid(void*)), &ptrAddress );
265  m_objectType.functionMemberByName("address").invoke(ptrObject, &ptrAddrObj);
266  m_updater->update( oid, ptrAddress );
267 }
268 
270  MappingElement& mapping,
271  ContainerSchema& contSchema ):
272  m_objectType( objectType ),
273  m_dataElement( 0 ),
274  m_readBuffer(),
275  m_loaders(),
276  m_tmpIds(){
277  m_readBuffer.reset( new OraPtrReadBuffer( objectType, mapping, contSchema ));
278 }
279 
281  for(std::vector<boost::shared_ptr<IPtrLoader> >::const_iterator iL = m_loaders.begin();
282  iL != m_loaders.end(); ++iL ){
283  (*iL)->invalidate();
284  }
285 }
286 
288  IRelationalData& ){
289  m_dataElement = &dataElement;
290  m_tmpIds.clear();
291  m_tmpIds.push_back(0);
292  return m_readBuffer->build( dataElement );
293 }
294 
296  if(!m_dataElement) throwException( "The streamer has not been built.","OraPtrReader::select");
297  m_tmpIds[0] = oid;
298 }
299 
300 void ora::OraPtrReader::setRecordId( const std::vector<int>& identity ){
301  m_tmpIds.resize( 1+identity.size() );
302  for( size_t i=0;i<identity.size();i++){
303  m_tmpIds[1+i] = identity[i];
304  }
305 }
306 
309  if(!m_dataElement){
310  throwException("The streamer has not been built.",
311  "OraPtrReader::read");
312  }
313  // resolving loader address
314  edm::MemberWithDict loaderMember = m_objectType.dataMemberByName("m_loader");
315  DataElement& loaderElement = m_dataElement->addChild( loaderMember.offset(), 0 );
316  void* loaderAddress = loaderElement.address( data );
317  boost::shared_ptr<IPtrLoader>* loaderPtr = static_cast<boost::shared_ptr<IPtrLoader>*>( loaderAddress );
318  // creating new loader
319  boost::shared_ptr<IPtrLoader> newLoader( new RelationalPtrLoader( *m_readBuffer, m_tmpIds ) );
320  m_loaders.push_back( newLoader );
321  // installing the new loader
322  *loaderPtr = newLoader;
323 }
324 
326 }
327 
328 
330  MappingElement& mapping,
331  ContainerSchema& contSchema ):
332  m_objectType( objectType ),
333  m_mapping( mapping ),
334  m_schema( contSchema ){
335 }
336 
338 }
339 
341  return new OraPtrWriter( m_objectType, m_mapping, m_schema );
342 }
343 
345  return new OraPtrUpdater( m_objectType, m_mapping, m_schema );
346 }
347 
349  return new OraPtrReader( m_objectType, m_mapping, m_schema );
350 }
351 
void update(int oid, const void *data)
Updates a data element.
void setRecordId(const std::vector< int > &identity)
OraPtrReadBuffer(const edm::TypeWithDict &objectType, MappingElement &mapping, ContainerSchema &contSchema)
int i
Definition: DBlmapReader.cc:9
edm::TypeWithDict resolvedType(const edm::TypeWithDict &typ)
Definition: ClassUtils.cc:483
void * address(const void *topLevelAddress) const
Definition: DataElement.cc:49
DataElement * m_dataElement
void * read(const std::vector< int > &fullId)
size_t offset() const
OraPtrUpdater(const edm::TypeWithDict &objectType, MappingElement &mapping, ContainerSchema &contSchema)
IRelationalUpdater * newUpdater()
void write(int oid, const void *data)
Writes a data element.
std::auto_ptr< IRelationalReader > m_reader
int addWhereId(const std::string &columnName)
bool build(DataElement &dataElement)
TypeWithDict templateArgumentAt(size_t index) const
OraPtrReadBuffer & m_buffer
bool build(DataElement &offset, IRelationalData &relationalData)
std::string name() const
std::string cppName() const
void * load() const override
void setRecordId(const std::vector< int > &identity)
void read(void *destination)
Reads a data element.
IRelationalWriter * newWriter()
IRelationalWriter * newWriter(const edm::TypeWithDict &dataType, MappingElement &dataMapping)
SelectOperation m_query
RelationalPtrLoader(OraPtrReadBuffer &buffer, const std::vector< int > &fullId)
iterator find(const std::string &key)
Retrieves a sub-element.
OraPtrStreamer(const edm::TypeWithDict &objectType, MappingElement &mapping, ContainerSchema &contSchema)
std::map< std::string, MappingElement >::iterator iterator
Iterator definition.
const std::vector< std::string > & columnNames() const
void invalidate() override
DataElement & addChild(size_t declaringScopeOffset, size_toffset)
Definition: DataElement.cc:27
std::vector< int > m_fullId
IRelationalReader * newReader()
bool build(DataElement &dataElement, IRelationalData &relationalData, RelationalBuffer &operationBuffer)
void setRecordId(const std::vector< int > &identity)
IRelationalReader * newReader(const edm::TypeWithDict &dataType, MappingElement &dataMapping)
coral::AttributeList & whereData()
std::auto_ptr< OraPtrReadBuffer > m_readBuffer
bool isValid() const override
void throwException(const std::string &message, const std::string &methodName) __attribute__((noreturn))
Definition: Exception.cc:10
IRelationalUpdater * newUpdater(const edm::TypeWithDict &dataType, MappingElement &dataMapping)
iterator end()
Returns an iterator in the end of the sequence.
void * constructObject(const edm::TypeWithDict &typ)
Definition: ClassUtils.cc:202
OraPtrWriter(const edm::TypeWithDict &objectType, MappingElement &mapping, ContainerSchema &contSchema)
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:82
edm::TypeWithDict m_objectType
OraPtrReader(const edm::TypeWithDict &objectType, MappingElement &mapping, ContainerSchema &contSchema)
MappingElement & m_mapping
void select(int oid)
bool build(DataElement &dataElement, IRelationalData &relationalData, RelationalBuffer &operationBuffer)
ContainerSchema & m_schema