test
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"
12 #include "Reflex/Object.h"
13 #include "Reflex/Member.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 Reflex::Type& 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.Name(Reflex::SCOPED) );
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.Name(Reflex::SCOPED)+"\"",
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 Reflex::Type& 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.Name(Reflex::SCOPED) );
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.Name(Reflex::SCOPED)+"\"",
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 Reflex::Type& 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  Reflex::Object refObj( m_objectType, const_cast<void*>(refAddress));
226 
227  bool isNull;
228  refObj.Invoke("operator!",isNull);
229 
230  int refId = 0;
232 
233  if(!isNull){
234  // resolving the ref type
235  std::type_info* refTypeInfo = 0;
236  refObj.Invoke("typeInfo",refTypeInfo);
237  Reflex::Type refType = ClassUtils::lookupDictionary(*refTypeInfo);
238  className = refType.Name(Reflex::SCOPED);
239 
240  // building the dependent buffer
241  MappingElement& depMapping = m_schema.mappingForDependentClass( refType, true );
242 
243  // getting a new valid ref id
244  refId = m_schema.containerSequences().getNextId( MappingRules::sequenceNameForDependentClass( m_schema.containerName(),className ));
245 
246  DependentClassWriter writer;
247  writer.build( refType, depMapping, m_schema, m_operationBuffer->addVolatileBuffer() );
248  void* refData;
249  refObj.Invoke("operator*",refData);
250  writer.write( oid, refId, refData );
251 
252  }
253  // writing in the parent table
254  coral::AttributeList& parentData = m_relationalData->data();
255  parentData[m_columnIndexes[0]].data<std::string>()=className;
256  parentData[m_columnIndexes[1]].data<int>()=refId;
257 }
258 
260  MappingElement& mapping,
261  ContainerSchema& contSchema ):
262  m_writer( objectType, mapping, contSchema ){
263 }
264 
266 }
267 
269  IRelationalData& relationalData,
270  RelationalBuffer& operationBuffer){
271  return m_writer.build( dataElement, relationalData, operationBuffer );
272 }
273 
274 void ora::UniqueRefUpdater::setRecordId( const std::vector<int>& identity ){
275  m_writer.setRecordId( identity );
276 }
277 
280  const void* data ){
281  m_writer.write( oid, data );
282 }
283 
285  MappingElement& mapping,
286  ContainerSchema& contSchema ):
287  m_objectType( objectType ),
288  m_mappingElement( mapping ),
289  m_schema( contSchema ),
290  m_dataElement( 0 ),
291  m_relationalData( 0 ),
292  m_loaders(){
293  m_columnIndexes[0]=-1;
294  m_columnIndexes[1]=-1;
295 }
296 
298  for(std::vector<boost::shared_ptr<RelationalRefLoader> >::const_iterator iL = m_loaders.begin();
299  iL != m_loaders.end(); ++iL ){
300  (*iL)->invalidate();
301  }
302 }
303 
305  IRelationalData& relationalData){
306  m_dataElement = &dataElement;
307  const std::vector<std::string>& columns = m_mappingElement.columnNames();
308  if( columns.size() < 2 ){
309  throwException("Expected column names have not been found in the mapping.",
310  "UniqueRefReader::build");
311  }
312  // booking for ref metadata
313  m_columnIndexes[0] = relationalData.addData( columns[0],typeid(std::string) );
314  // booking for ref id
315  m_columnIndexes[1] = relationalData.addData( columns[1],typeid(int) );
316  m_relationalData = &relationalData;
317  return true;
318 }
319 
321 }
322 
323 void ora::UniqueRefReader::setRecordId( const std::vector<int>& ){
324 }
325 
328  if(!m_dataElement){
329  throwException("The streamer has not been built.",
330  "UniqueRefReader::read");
331  }
332  coral::AttributeList& row = m_relationalData->data();
333  std::string className = row[m_columnIndexes[0]].data<std::string>();
334  int refId = row[m_columnIndexes[1]].data<int>();
335 
336  Reflex::Type refType = ClassUtils::lookupDictionary(className);
337 
338  // building the dependent buffer
339  MappingElement& depMapping = m_schema.mappingForDependentClass( refType );
340 
341  // resolving loader address
342  Reflex::Member loaderMember = m_objectType.MemberByName("m_loader");
343  DataElement& loaderElement = m_dataElement->addChild( loaderMember.Offset(), 0 );
344  void* loaderAddress = loaderElement.address( data );
345  boost::shared_ptr<IPtrLoader>* loaderPtr = static_cast<boost::shared_ptr<IPtrLoader>*>( loaderAddress );
346  // creating new loader
347  boost::shared_ptr<RelationalRefLoader> newLoader( new RelationalRefLoader( refId ) );
348  newLoader->build( refType, depMapping, m_schema );
349  m_loaders.push_back( newLoader );
350  // installing the new loader
351  boost::shared_ptr<IPtrLoader> tmp( newLoader );
352  *loaderPtr = tmp;
353 }
354 
356 }
357 
359  MappingElement& mapping,
360  ContainerSchema& contSchema ):
361  m_objectType( objectType ),
362  m_mapping( mapping ),
363  m_schema( contSchema ){
364 }
365 
367 }
368 
370  return new UniqueRefWriter( m_objectType, m_mapping, m_schema );
371 }
372 
374  return new UniqueRefUpdater( m_objectType, m_mapping, m_schema );
375 }
376 
378  return new UniqueRefReader( m_objectType, m_mapping, m_schema );
379 }
380 
IRelationalReader * newReader()
void * load() const override
UniqueRefUpdater(const Reflex::Type &objectType, MappingElement &mapping, ContainerSchema &contSchema)
IRelationalReader * newReader(const Reflex::Type &dataType, MappingElement &dataMapping)
void * constructObject(const Reflex::Type &typ)
Definition: ClassUtils.cc:127
void * address(const void *topLevelAddress) const
Definition: DataElement.cc:48
void write(int oid, const void *data)
Writes a data element.
bool build(DataElement &offset, IRelationalData &relationalData)
string destination
virtual int addData(const std::string &columnName, const std::type_info &columnType)=0
std::auto_ptr< IRelationalWriter > m_writer
IRelationalUpdater * newUpdater()
DataElement & addChild(size_t declaringScopeOffset, Reflex::OffsetFunction offsetFunction)
Definition: DataElement.cc:26
void setRecordId(const std::vector< int > &identity)
void write(int oId, int refId, const void *data)
bool build(DataElement &dataElement, IRelationalData &relationalData, RelationalBuffer &operationBuffer)
InsertOperation & newInsert(const std::string &tableName)
void read(void *destination)
Reads a data element.
UniqueRefStreamer(const Reflex::Type &objectType, MappingElement &mapping, ContainerSchema &contSchema)
InsertOperation * m_depInsert
void setRecordId(const std::vector< int > &identity)
UniqueRefWriter(const Reflex::Type &objectType, MappingElement &mapping, ContainerSchema &contSchema)
void update(int oid, const void *data)
Updates a data element.
DependentClassReader m_reader
iterator find(const std::string &key)
Retrieves a sub-element.
std::map< std::string, MappingElement >::iterator iterator
Iterator definition.
const std::vector< std::string > & columnNames() const
bool isValid() const override
coral::AttributeList & data()
IRelationalWriter * newWriter(const Reflex::Type &dataType, MappingElement &dataMapping)
void build(const Reflex::Type &objectType, MappingElement &mapping, ContainerSchema &contSchema, RelationalBuffer &operationBuffer)
UniqueRefReader(const Reflex::Type &objectType, MappingElement &mapping, ContainerSchema &contSchema)
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
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:82
std::auto_ptr< IRelationalReader > m_reader
void build(const Reflex::Type &objectType, MappingElement &depMapping, ContainerSchema &contSchema)
std::auto_ptr< SelectOperation > m_depQuery
int addId(const std::string &columnName)
const std::string & tableName() const
static std::string sequenceNameForDependentClass(const std::string &containerName, const std::string &className)
Definition: MappingRules.cc:20
volatile std::atomic< bool > shutdown_flag false
Reflex::Type lookupDictionary(const std::type_info &typeInfo, bool throwFlag=true)
Definition: ClassUtils.cc:93
void build(const Reflex::Type &objectType, MappingElement &mapping, ContainerSchema &contSchema)
std::string className(const T &t)
Definition: ClassName.h:30
IRelationalWriter * newWriter()
bool build(DataElement &dataElement, IRelationalData &relationalData, RelationalBuffer &operationBuffer)
std::string uniqueRefNullLabel()