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"
11 #include "Reflex/Object.h"
12 #include "Reflex/Member.h"
13 
14 namespace ora {
16  public:
17  OraPtrReadBuffer( const Reflex::Type& objectType, MappingElement& mapping, ContainerSchema& contSchema ):
18  m_objectType( objectType ),
19  m_mapping( mapping ),
20  m_schema( contSchema ),
22  m_query( mapping.tableName(), contSchema.storageSchema() ),
23  m_dataElement( 0 ),
24  m_reader(){
25  }
26 
28  }
29 
30  bool build( DataElement& dataElement ){
31  m_dataElement = &dataElement;
33 
34  const std::vector<std::string>& columns = m_mapping.columnNames();
35  size_t cols = columns.size();
36  for( size_t i=0; i<cols; i++ ){
37  m_query.addWhereId( columns[ i ] );
38  }
39 
40  // Check the type
41  Reflex::Type ptrType = m_objectType.TemplateArgumentAt(0);
42  Reflex::Type ptrResolvedType = ClassUtils::resolvedType(ptrType);
43  // Check the component type
44  if ( ! ptrType || !ptrResolvedType ) {
45  throwException( "Missing dictionary information for the type of the pointer \"" +
46  m_objectType.Name(Reflex::SCOPED|Reflex::FINAL) + "\"",
47  "OraPtrReadBuffer::build" );
48  }
49 
50  std::string ptrTypeName = ptrType.Name();
51  // Retrieve the relevant mapping element
52  MappingElement::iterator iMe = m_mapping.find( ptrTypeName );
53  if ( iMe == m_mapping.end() ) {
54  throwException( "Item for \"" + ptrTypeName + "\" not found in the mapping element",
55  "OraPtrReadBuffer::build" );
56  }
57  RelationalStreamerFactory streamerFactory( m_schema );
58  m_reader.reset( streamerFactory.newReader( ptrResolvedType, iMe->second ) );
59  return m_reader->build( m_localElement, m_query );
60  }
61 
62  void* read( const std::vector<int>& fullId ){
63  if(!m_dataElement) throwException( "Read buffer has not been built.","OraPtrReadBuffer::read");
64 
65  if(!fullId.size()) throwException( "Object id set is empty.","OraPtrReadBuffer::read");
66 
67  coral::AttributeList& whereBuff = m_query.whereData();
68  coral::AttributeList::iterator iCol = whereBuff.begin();
69  size_t i=0;
70  for( coral::AttributeList::iterator iCol = whereBuff.begin(); iCol != whereBuff.end(); ++iCol ){
71  if( i<fullId.size() ){
72  iCol->data<int>() = fullId[i];
73  }
74  i++;
75  }
76 
77  std::vector<int> recordId( fullId.size()-1 );
78  for( size_t i=0; i<fullId.size()-1; i++ ){
79  recordId[i] = fullId[i+1];
80  ++i;
81  }
82 
83  m_query.execute();
84  m_reader->select( fullId[0] );
85  void* destination = 0;
86  if( m_query.nextCursorRow() ){
87  destination = ClassUtils::constructObject( m_objectType.TemplateArgumentAt(0) );
88  m_reader->setRecordId( recordId );
89  m_reader->read( destination );
90  }
91  m_query.clear();
92  m_reader->clear();
93  return destination;
94  }
95 
96  private:
103  std::auto_ptr<IRelationalReader> m_reader;
104  };
105 
107  public:
108  RelationalPtrLoader( OraPtrReadBuffer& buffer, const std::vector<int>& fullId ):
109  m_buffer( buffer ),
110  m_fullId( fullId ),
111  m_valid( true ){
112  }
113 
115  }
116 
117  public:
118  void* load() const {
119  if(!m_valid){
120  throwException("Ptr Loader has been invalidate.",
121  "RelationalPtrLoader::load");
122  }
123  return m_buffer.read( m_fullId );
124  }
125 
126  void invalidate(){
127  m_valid = false;
128  }
129 
130  bool isValid() const{
131  return m_valid;
132  }
133 
134  private:
136  std::vector<int> m_fullId;
137  bool m_valid;
138  };
139 }
140 
142  MappingElement& mapping,
143  ContainerSchema& contSchema ):
144  m_objectType( objectType ),
145  m_mappingElement( mapping ),
146  m_schema( contSchema ),
147  m_localElement(),
148  m_dataElement( 0 ),
149  m_writer(){
150 }
151 
153 }
154 
156  IRelationalData& relationalData,
157  RelationalBuffer& operationBuffer){
158  m_dataElement = &dataElement;
159  m_localElement.clear();
160 
161  // Check the type
162  Reflex::Type ptrType = m_objectType.TemplateArgumentAt(0);
163  Reflex::Type ptrResolvedType = ClassUtils::resolvedType(ptrType);
164  // Check the component type
165  if ( ! ptrType || !ptrResolvedType ) {
166  throwException( "Missing dictionary information for the type of the pointer \"" +
167  m_objectType.Name(Reflex::SCOPED|Reflex::FINAL) + "\"",
168  "OraPtrWriter::build" );
169  }
170 
171  std::string ptrTypeName = ptrType.Name();
172 // Retrieve the relevant mapping element
173  MappingElement::iterator iMe = m_mappingElement.find( ptrTypeName );
174  if ( iMe == m_mappingElement.end() ) {
175  throwException( "Item for \"" + ptrTypeName + "\" not found in the mapping element",
176  "OraPtrWriter::build" );
177  }
178  RelationalStreamerFactory streamerFactory( m_schema );
179  m_writer.reset( streamerFactory.newWriter( ptrResolvedType, iMe->second ));
180  return m_writer->build( m_localElement, relationalData, operationBuffer );
181 }
182 
183 void ora::OraPtrWriter::setRecordId( const std::vector<int>& identity ){
184  m_writer->setRecordId( identity );
185 }
186 
189  const void* data ){
190 
191  if(!m_dataElement){
192  throwException("The streamer has not been built.",
193  "OraPtrWriter::write");
194  }
195 
196  Reflex::Object ptrObject( m_objectType, m_dataElement->address( data ) );
197  // first load if required
198  ptrObject.Invoke("load",0);
199  // then get the data...
200  void* ptrAddress = 0;
201  ptrObject.Invoke("address",ptrAddress);
202  m_writer->write( oid, ptrAddress );
203 }
204 
206  MappingElement& mapping,
207  ContainerSchema& contSchema ):
208  m_objectType( objectType ),
209  m_mappingElement( mapping ),
210  m_schema( contSchema ),
211  m_localElement(),
212  m_dataElement( 0 ),
213  m_updater(){
214 }
215 
217 }
218 
220  IRelationalData& relationalData,
221  RelationalBuffer& operationBuffer){
222  m_dataElement = &dataElement;
223  m_localElement.clear();
224 
225  // Check the type
226  Reflex::Type ptrType = m_objectType.TemplateArgumentAt(0);
227  Reflex::Type ptrResolvedType = ClassUtils::resolvedType(ptrType);
228  // Check the component type
229  if ( ! ptrType || !ptrResolvedType ) {
230  throwException( "Missing dictionary information for the type of the pointer \"" +
231  m_objectType.Name(Reflex::SCOPED|Reflex::FINAL) + "\"",
232  "OraPtrUpdater::build" );
233  }
234 
235  std::string ptrTypeName = ptrType.Name();
236 // Retrieve the relevant mapping element
237  MappingElement::iterator iMe = m_mappingElement.find( ptrTypeName );
238  if ( iMe == m_mappingElement.end() ) {
239  throwException( "Item for \"" + ptrTypeName + "\" not found in the mapping element",
240  "OraPtrUpdater::build" );
241  }
242  RelationalStreamerFactory streamerFactory( m_schema );
243  m_updater.reset( streamerFactory.newUpdater( ptrResolvedType, iMe->second ) );
244  return m_updater->build( m_localElement, relationalData, operationBuffer );
245 }
246 
247 void ora::OraPtrUpdater::setRecordId( const std::vector<int>& identity ){
248  m_updater->setRecordId( identity );
249 }
250 
253  const void* data ){
254  if(!m_dataElement){
255  throwException("The streamer has not been built.",
256  "OraPtrUpdater::update");
257  }
258  Reflex::Object ptrObject( m_objectType, m_dataElement->address( data ) );
259  // first load if required
260  ptrObject.Invoke("load",0);
261  void* ptrAddress = 0;
262  ptrObject.Invoke("address",ptrAddress);
263  m_updater->update( oid, ptrAddress );
264 }
265 
267  MappingElement& mapping,
268  ContainerSchema& contSchema ):
269  m_objectType( objectType ),
270  m_dataElement( 0 ),
271  m_readBuffer(),
272  m_loaders(),
273  m_tmpIds(){
274  m_readBuffer.reset( new OraPtrReadBuffer( objectType, mapping, contSchema ));
275 }
276 
278  for(std::vector<boost::shared_ptr<IPtrLoader> >::const_iterator iL = m_loaders.begin();
279  iL != m_loaders.end(); ++iL ){
280  (*iL)->invalidate();
281  }
282 }
283 
285  IRelationalData& ){
286  m_dataElement = &dataElement;
287  m_tmpIds.clear();
288  m_tmpIds.push_back(0);
289  return m_readBuffer->build( dataElement );
290 }
291 
293  if(!m_dataElement) throwException( "The streamer has not been built.","OraPtrReader::select");
294  m_tmpIds[0] = oid;
295 }
296 
297 void ora::OraPtrReader::setRecordId( const std::vector<int>& identity ){
298  m_tmpIds.resize( 1+identity.size() );
299  for( size_t i=0;i<identity.size();i++){
300  m_tmpIds[1+i] = identity[i];
301  }
302 }
303 
306  if(!m_dataElement){
307  throwException("The streamer has not been built.",
308  "OraPtrReader::read");
309  }
310  // resolving loader address
311  Reflex::Member loaderMember = m_objectType.MemberByName("m_loader");
312  DataElement& loaderElement = m_dataElement->addChild( loaderMember.Offset(), 0 );
313  void* loaderAddress = loaderElement.address( data );
314  boost::shared_ptr<IPtrLoader>* loaderPtr = static_cast<boost::shared_ptr<IPtrLoader>*>( loaderAddress );
315  // creating new loader
316  boost::shared_ptr<IPtrLoader> newLoader( new RelationalPtrLoader( *m_readBuffer, m_tmpIds ) );
317  m_loaders.push_back( newLoader );
318  // installing the new loader
319  *loaderPtr = newLoader;
320 }
321 
323 }
324 
325 
327  MappingElement& mapping,
328  ContainerSchema& contSchema ):
329  m_objectType( objectType ),
330  m_mapping( mapping ),
331  m_schema( contSchema ){
332 }
333 
335 }
336 
338  return new OraPtrWriter( m_objectType, m_mapping, m_schema );
339 }
340 
342  return new OraPtrUpdater( m_objectType, m_mapping, m_schema );
343 }
344 
346  return new OraPtrReader( m_objectType, m_mapping, m_schema );
347 }
348 
void update(int oid, const void *data)
Updates a data element.
void setRecordId(const std::vector< int > &identity)
int i
Definition: DBlmapReader.cc:9
OraPtrWriter(const Reflex::Type &objectType, MappingElement &mapping, ContainerSchema &contSchema)
OraPtrReadBuffer(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
DataElement * m_dataElement
void * read(const std::vector< int > &fullId)
OraPtrReader(const Reflex::Type &objectType, MappingElement &mapping, ContainerSchema &contSchema)
IRelationalUpdater * newUpdater()
void write(int oid, const void *data)
Writes a data element.
DataElement & addChild(size_t declaringScopeOffset, Reflex::OffsetFunction offsetFunction)
Definition: DataElement.cc:26
OraPtrStreamer(const Reflex::Type &objectType, MappingElement &mapping, ContainerSchema &contSchema)
std::auto_ptr< IRelationalReader > m_reader
int addWhereId(const std::string &columnName)
bool build(DataElement &dataElement)
OraPtrReadBuffer & m_buffer
bool build(DataElement &offset, IRelationalData &relationalData)
void setRecordId(const std::vector< int > &identity)
void read(void *destination)
Reads a data element.
IRelationalWriter * newWriter()
SelectOperation m_query
RelationalPtrLoader(OraPtrReadBuffer &buffer, const std::vector< int > &fullId)
iterator find(const std::string &key)
Retrieves a sub-element.
Reflex::Type m_objectType
std::map< std::string, MappingElement >::iterator iterator
Iterator definition.
const std::vector< std::string > & columnNames() const
std::vector< int > m_fullId
IRelationalReader * newReader()
bool build(DataElement &dataElement, IRelationalData &relationalData, RelationalBuffer &operationBuffer)
Reflex::Type resolvedType(const Reflex::Type &typ)
Definition: ClassUtils.cc:404
void setRecordId(const std::vector< int > &identity)
OraPtrUpdater(const Reflex::Type &objectType, MappingElement &mapping, ContainerSchema &contSchema)
IRelationalWriter * newWriter(const Reflex::Type &dataType, MappingElement &dataMapping)
coral::AttributeList & whereData()
IRelationalUpdater * newUpdater(const Reflex::Type &dataType, MappingElement &dataMapping)
std::auto_ptr< OraPtrReadBuffer > m_readBuffer
iterator end()
Returns an iterator in the end of the sequence.
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:82
MappingElement & m_mapping
void throwException(const std::string &message, const std::string &methodName)
Definition: Exception.cc:10
void select(int oid)
bool build(DataElement &dataElement, IRelationalData &relationalData, RelationalBuffer &operationBuffer)
ContainerSchema & m_schema