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  return destination;
121  }
122 
123  private:
126  std::auto_ptr<IRelationalReader> m_reader;
127  std::auto_ptr<SelectOperation> m_depQuery;
128  };
129 
131  public:
132  explicit RelationalRefLoader( int refId ):
133  m_reader(),
134  m_refId( refId ),
135  m_valid( false ){
136  }
137 
139  }
140 
141  public:
142  void build( const Reflex::Type& objectType, MappingElement& mapping, ContainerSchema& contSchema ){
143  m_reader.build( objectType, mapping, contSchema );
144  m_valid = true;
145  }
146 
147  void* load() const {
148  if(!m_valid){
149  throwException("Ref Loader has been invalidate.",
150  "RelationalRefLoader::load");
151  }
152  return m_reader.read( m_refId );
153  }
154 
155  void invalidate(){
156  m_valid = false;
157  }
158 
159  bool isValid() const{
160  return m_valid;
161  }
162 
163  private:
165  int m_refId;
166  bool m_valid;
167  };
168 }
169 
171  MappingElement& mapping,
172  ContainerSchema& contSchema ):
173  m_objectType( objectType ),
174  m_mappingElement( mapping ),
175  m_schema( contSchema ),
176  m_dataElement( 0 ),
177  m_relationalData( 0 ),
178  m_operationBuffer( 0 ){
179 }
180 
182  static std::string nullLabel("ora::UniqueRef::Null");
183  return nullLabel;
184 }
185 
187 }
188 
190  IRelationalData& relationalData,
191  RelationalBuffer& buffer){
192  m_dataElement = &dataElement;
193 
194  const std::vector<std::string>& columns = m_mappingElement.columnNames();
195  // booking for ref metadata
196  relationalData.addData( columns[0],typeid(std::string) );
197  // booking for ref id
198  relationalData.addData( columns[1],typeid(int) );
199  m_relationalData = &relationalData;
200  m_operationBuffer = &buffer;
201  return true;
202 }
203 
204 void ora::UniqueRefWriter::setRecordId( const std::vector<int>& ){
205 }
206 
209  const void* data ){
210 
211  if(!m_dataElement){
212  throwException("The streamer has not been built.",
213  "UniqueRefWriter::write");
214  }
215 
216  void* refAddress = m_dataElement->address( data );
217 
218  Reflex::Object refObj( m_objectType, const_cast<void*>(refAddress));
219 
220  bool isNull;
221  refObj.Invoke("operator!",isNull);
222 
223  int refId = 0;
224  std::string className = uniqueRefNullLabel();
225 
226  if(!isNull){
227  // resolving the ref type
228  std::type_info* refTypeInfo = 0;
229  refObj.Invoke("typeInfo",refTypeInfo);
230  Reflex::Type refType = ClassUtils::lookupDictionary(*refTypeInfo);
231  className = refType.Name(Reflex::SCOPED);
232 
233  // getting a new valid ref id
234  refId = m_schema.containerSequences().getNextId( MappingRules::sequenceNameForDependentClass( m_schema.containerName(),className ));
235 
236  // building the dependent buffer
237  MappingElement& depMapping = m_schema.mappingForDependentClass( refType, true );
238 
239  DependentClassWriter writer;
240  writer.build( refType, depMapping, m_schema, m_operationBuffer->addVolatileBuffer() );
241  void* refData;
242  refObj.Invoke("operator*",refData);
243  writer.write( oid, refId, refData );
244 
245  }
246  // writing in the parent table
247  coral::AttributeList& parentData = m_relationalData->data();
248  parentData[m_mappingElement.columnNames()[0]].data<std::string>()=className;
249  parentData[m_mappingElement.columnNames()[1]].data<int>()=refId;
250 }
251 
253  MappingElement& mapping,
254  ContainerSchema& contSchema ):
255  m_writer( objectType, mapping, contSchema ){
256 }
257 
259 }
260 
262  IRelationalData& relationalData,
263  RelationalBuffer& operationBuffer){
264  return m_writer.build( dataElement, relationalData, operationBuffer );
265 }
266 
267 void ora::UniqueRefUpdater::setRecordId( const std::vector<int>& identity ){
268  m_writer.setRecordId( identity );
269 }
270 
273  const void* data ){
274  m_writer.write( oid, data );
275 }
276 
278  MappingElement& mapping,
279  ContainerSchema& contSchema ):
280  m_objectType( objectType ),
281  m_mappingElement( mapping ),
282  m_schema( contSchema ),
283  m_dataElement( 0 ),
284  m_relationalData( 0 ),
285  m_loaders(){
286 }
287 
289  for(std::vector<boost::shared_ptr<RelationalRefLoader> >::const_iterator iL = m_loaders.begin();
290  iL != m_loaders.end(); ++iL ){
291  (*iL)->invalidate();
292  }
293 }
294 
296  IRelationalData& relationalData){
297  m_dataElement = &dataElement;
298  const std::vector<std::string>& columns = m_mappingElement.columnNames();
299  // booking for ref metadata
300  relationalData.addData( columns[0],typeid(std::string) );
301  // booking for ref id
302  relationalData.addData( columns[1],typeid(int) );
303  m_relationalData = &relationalData;
304  return true;
305 }
306 
308 }
309 
310 void ora::UniqueRefReader::setRecordId( const std::vector<int>& ){
311 }
312 
315  if(!m_dataElement){
316  throwException("The streamer has not been built.",
317  "UniqueRefReader::read");
318  }
319  coral::AttributeList& row = m_relationalData->data();
320  std::string className = row[m_mappingElement.columnNames()[0]].data<std::string>();
321  int refId = row[m_mappingElement.columnNames()[1]].data<int>();
322 
323  Reflex::Type refType = ClassUtils::lookupDictionary(className);
324 
325  // building the dependent buffer
326  MappingElement& depMapping = m_schema.mappingForDependentClass( refType );
327 
328  // resolving loader address
329  Reflex::Member loaderMember = m_objectType.MemberByName("m_loader");
330  DataElement& loaderElement = m_dataElement->addChild( loaderMember.Offset(), 0 );
331  void* loaderAddress = loaderElement.address( data );
332  boost::shared_ptr<IPtrLoader>* loaderPtr = static_cast<boost::shared_ptr<IPtrLoader>*>( loaderAddress );
333  // creating new loader
334  boost::shared_ptr<RelationalRefLoader> newLoader( new RelationalRefLoader( refId ) );
335  newLoader->build( refType, depMapping, m_schema );
336  m_loaders.push_back( newLoader );
337  // installing the new loader
338  boost::shared_ptr<IPtrLoader> tmp( newLoader );
339  *loaderPtr = tmp;
340 }
341 
343 }
344 
346  MappingElement& mapping,
347  ContainerSchema& contSchema ):
348  m_objectType( objectType ),
349  m_mapping( mapping ),
350  m_schema( contSchema ){
351 }
352 
354 }
355 
357  return new UniqueRefWriter( m_objectType, m_mapping, m_schema );
358 }
359 
361  return new UniqueRefUpdater( m_objectType, m_mapping, m_schema );
362 }
363 
365  return new UniqueRefReader( m_objectType, m_mapping, m_schema );
366 }
367 
IRelationalReader * newReader()
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:118
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)
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.
void addId(const std::string &columnName)
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
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)
iterator end()
Returns an iterator in the end of the sequence.
std::vector< std::vector< double > > tmp
Definition: MVATrainer.cc:100
std::auto_ptr< IRelationalReader > m_reader
void build(const Reflex::Type &objectType, MappingElement &depMapping, ContainerSchema &contSchema)
std::auto_ptr< SelectOperation > m_depQuery
const std::string & tableName() const
void throwException(const std::string &message, const std::string &methodName)
Definition: Exception.cc:10
static std::string sequenceNameForDependentClass(const std::string &containerName, const std::string &className)
Definition: MappingRules.cc:20
Reflex::Type lookupDictionary(const std::type_info &typeInfo, bool throwFlag=true)
Definition: ClassUtils.cc:84
void build(const Reflex::Type &objectType, MappingElement &mapping, ContainerSchema &contSchema)
std::string className(const T &t)
Definition: ClassName.h:30
IRelationalWriter * newWriter()
virtual void addData(const std::string &columnName, const std::type_info &columnType)=0
bool build(DataElement &dataElement, IRelationalData &relationalData, RelationalBuffer &operationBuffer)
std::string uniqueRefNullLabel()