16 #include "CoralBase/Attribute.h"
18 #include <boost/lexical_cast.hpp>
33 m_buffer.push_back( std::make_pair(oid, data ) );
46 if( iMap == topLevelMapping.
end()){
48 type.Name(Reflex::SCOPED)+
"\"",
49 "WriteBuffer::flush");
54 std::auto_ptr<IRelationalWriter> writer( streamerFactory.
newWriter( type, mapping ) );
55 writer->build( topLevelElement, *topLevelInsert, operationBuffer );
57 for( std::vector<std::pair<int, const void*> >::const_iterator iW =
m_buffer.begin();
60 const void*
data = iW->second;
61 coral::AttributeList& dataBuff = topLevelInsert->data();
62 dataBuff.begin()->data<
int>() = oid;
63 writer->write( oid, data );
64 if( operationBuffer.flush() ) nobj++;
73 std::vector<std::pair<int, const void*> >
m_buffer;
90 m_buffer.push_back( std::make_pair( oid, data ));
98 std::vector<MappingElement> dependentMappings;
101 depDeleter.
build( operationBuffer );
102 dependentMappings.clear();
111 if( iMap == topLevelMapping.
end()){
113 type.Name(Reflex::SCOPED)+
"\"",
114 "UpdateBuffer::flush");
119 std::auto_ptr<IRelationalUpdater> updater( streamerFactory.
newUpdater( type, mapping ));
120 updater->build( topLevelElement, *topLevelUpdate, operationBuffer );
121 for( std::vector<std::pair<int, const void*> >::const_iterator iU =
m_buffer.begin();
124 const void*
data = iU->second;
126 depDeleter.
erase( oid );
127 coral::AttributeList& dataBuff = topLevelUpdate->
data();
128 dataBuff.begin()->data<
int>() = oid;
129 coral::AttributeList& whereDataBuff = topLevelUpdate->
whereData();
130 whereDataBuff.begin()->data<
int>() = oid;
131 updater->update( oid, data );
132 if( operationBuffer.flush()) nobj++;
140 std::vector<std::pair<int, const void*> >
m_buffer;
150 m_topLevelQuery( contSchema.mapping().topElement().tableName(), contSchema.storageSchema() ){
156 if( iMap == topLevelMapping.
end()){
158 m_type.Name(Reflex::SCOPED)+
"\"",
159 "ReadBuffer::ReadBuffer");
172 dataBuff.begin()->data<
int>() = oid;
175 void* destination = 0;
180 throwException(
"Object with item id "+boost::lexical_cast<std::string>(oid)+
" has not been found in the database.",
219 mainDeleter.
build( operationBuffer );
220 std::vector<MappingElement> dependentMappings;
223 depDeleter.
build( operationBuffer );
224 dependentMappings.clear();
226 for( std::vector<int>::const_iterator iD =
m_buffer.begin();
228 depDeleter.
erase( *iD );
229 mainDeleter.erase( *iD );
230 if( operationBuffer.flush() ) nobj++;
246 m_query( schema.mapping().topElement().tableName(), schema.storageSchema() ),
248 m_readBuffer( buffer ){
262 bool prevValid = (m_itemId != -1);
263 bool currValid =
false;
265 if( m_query.nextCursorRow() ){
266 coral::AttributeList& row = m_query.data();
267 m_itemId = row.begin()->data<
int>();
271 if( !currValid && prevValid ) m_query.clear();
277 if( m_itemId != -1 ){
278 ret = m_readBuffer.read( m_itemId );
285 throwException(
"Provided output object type \""+asType.Name(Reflex::SCOPED)+
"\" does not match with the container type \""+
286 type().Name(Reflex::SCOPED)+
"\"",
"IteratorBuffer::getItemAsType");
296 return m_readBuffer.type();
302 unsigned int containerSize,
304 m_dbSchema( session.
schema() ),
305 m_schema( new
ContainerSchema(contId, containerName, className, session) ),
311 m_size( containerSize ),
312 m_containerUpdateTable( session.containerUpdateTable() ),
320 m_dbSchema( session.
schema() ),
321 m_schema( new
ContainerSchema(contId, containerName, containerType, session) ),
328 m_containerUpdateTable( session.containerUpdateTable() ),
333 m_iteratorBuffer.clear();
337 return m_schema->containerId();
341 return m_schema->containerName();
345 return m_schema->className();
349 return m_schema->type();
353 return m_schema->mappingVersion();
361 if(!m_readBuffer.get()){
362 m_readBuffer.reset(
new ReadBuffer( *m_schema ) );
364 if( !m_iteratorBuffer ){
365 m_iteratorBuffer.reset(
new IteratorBuffer(*m_schema, *m_readBuffer ) );
366 m_iteratorBuffer->reset();
368 return m_iteratorBuffer;
374 m_lock = m_dbSchema.containerHeaderTable().lockContainer( m_schema->containerId(), headerData );
375 if(!m_lock)
throwException(
"Container \""+
name()+
"\" has been dropped.",
"DatabaseContainer::lock()");
391 if(!m_schema->dbSession().testDropPermission()){
392 throwException(
"Drop permission has been denied for the current user.",
393 "DatabaseContainer::drop");
396 m_containerUpdateTable.remove( m_schema->containerId() );
400 m_schema->extendIfRequired( dependentType );
405 m_schema->setAccessPermission( principal, forWrite );
410 if(!m_readBuffer.get()){
411 m_readBuffer.reset(
new ReadBuffer( *m_schema ) );
413 return m_readBuffer->read( itemId );
418 if(!m_readBuffer.get()){
419 m_readBuffer.reset(
new ReadBuffer( *m_schema ) );
422 throwException(
"Provided output object type \""+asType.Name(Reflex::SCOPED)+
"\" does not match with the container type \""+
423 type().Name(Reflex::SCOPED)+
"\"",
"DatabaseContainer::fetchItemAsType");
425 return m_readBuffer->read( itemId );
430 if(!m_writeBuffer.get()){
431 m_writeBuffer.reset(
new WriteBuffer( *m_schema ) );
435 if( inputResType.Name()!= contType.Name() && !inputResType.HasBase( contType ) ){
436 throwException(
"Provided input object type=\""+inputResType.Name()+
437 "\" does not match with the container type=\""+contType.Name()+
"\"",
438 "DatabaseContainer::insertItem" );
442 m_writeBuffer->registerForWrite( newId, data );
449 if(!m_updateBuffer.get()){
454 if( inputResType.Name()!= contType.Name() && !inputResType.HasBase( contType ) ){
455 throwException(
"Provided input object type=\""+inputResType.Name()+
"\" does not match with the container type=\""+
456 contType.Name()+
"\".",
457 "DatabaseContainer::updateItem" );
460 m_updateBuffer->registerForUpdate( itemId, data );
464 if(!m_deleteBuffer.get()){
467 m_deleteBuffer->registerForDelete( itemId );
471 size_t prevSize = m_size;
472 if(m_writeBuffer.get()) m_size += m_writeBuffer->flush();
473 if(m_updateBuffer.get()) m_updateBuffer->flush();
474 if(m_deleteBuffer.get()) m_size -= m_deleteBuffer->flush();
475 m_schema->containerSequences().sinchronizeAll();
476 if( prevSize != m_size ){
477 m_containerUpdateTable.takeNote(
id(), m_size );
483 m_schema->dbSession().setObjectName( name, m_schema->containerId(), itemId );
487 return m_schema->dbSession().getNamesForContainer( m_schema->containerId(), destination );
void registerForUpdate(int oid, const void *data)
const std::string & name()
DatabaseContainer(int contId, const std::string &containerName, const std::string &className, unsigned int containerSize, DatabaseSession &session)
WriteBuffer(ContainerSchema &contSchema)
const Reflex::Type & type()
IRelationalReader * newReader(const Reflex::Type &dataType, MappingElement &dataMapping)
void * constructObject(const Reflex::Type &typ)
virtual ~DatabaseContainer()
void updateItem(int itemId, const void *data, const Reflex::Type &type)
ContainerSchema & m_contSchema
UpdateBuffer(ContainerSchema &contSchema)
int insertItem(const void *data, const Reflex::Type &type)
bool getNames(std::vector< std::string > &destination)
void * getItemAsType(const Reflex::Type &type)
DataElement m_topLevelElement
ContainerSchema & m_contSchema
const Reflex::Type & type()
ReadBuffer(ContainerSchema &contSchema)
int addWhereId(const std::string &columnName)
const Reflex::Type & type()
bool mappingForDependentClasses(std::vector< MappingElement > &destination)
const std::string & className()
const MappingElement & topElement() const
void extendSchema(const Reflex::Type &dependentType)
std::vector< std::pair< int, const void * > > m_buffer
IteratorBuffer(ContainerSchema &schema, ReadBuffer &buffer)
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
const std::string & mappingVersion()
int addId(const std::string &columnName)
void build(RelationalBuffer &buffer)
Reflex::Type resolvedType(const Reflex::Type &typ)
void addOrderId(const std::string &columnName)
std::vector< std::pair< int, const void * > > m_buffer
void setAccessPermission(const std::string &principal, bool forWrite)
std::vector< int > m_buffer
IRelationalWriter * newWriter(const Reflex::Type &dataType, MappingElement &dataMapping)
void * fetchItem(int itemId)
coral::AttributeList & whereData()
IRelationalUpdater * newUpdater(const Reflex::Type &dataType, MappingElement &dataMapping)
void registerForWrite(int oid, const void *data)
coral::ISchema & storageSchema()
void throwException(const std::string &message, const std::string &methodName) __attribute__((noreturn))
iterator end()
Returns an iterator in the end of the sequence.
char data[epos_bytes_allocation]
void registerForDelete(int oid)
void setItemName(const std::string &name, int itemId)
MappingTree & mapping(bool writeEnabled=false)
Handle< IteratorBuffer > iteratorBuffer()
const Reflex::Type & m_type
void * fetchItemAsType(int itemId, const Reflex::Type &asType)
const Reflex::Type & type()
static std::string sequenceNameForContainer(const std::string &containerName)
DeleteBuffer(ContainerSchema &contSchema)
const std::string & tableName() const
volatile std::atomic< bool > shutdown_flag false
std::auto_ptr< IRelationalReader > m_reader
ContainerSchema & m_contSchema
std::string className(const T &t)
SelectOperation m_topLevelQuery
bool isType(const Reflex::Type &type, const Reflex::Type &baseType)