00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00042 #if !defined( _OESS_1__DB__CLN__SLICE_IMAGE_HPP_ )
00043 #define _OESS_1__DB__CLN__SLICE_IMAGE_HPP_
00044
00045 #include <map>
00046
00047 #include <auto_ptr_3/h/obj_ptr.hpp>
00048
00049 #include <oess_1/defs/h/ex.hpp>
00050
00051 #include <oess_1/db/h/declspec.hpp>
00052
00053 #include <oess_1/db/cln/h/errno.hpp>
00054 #include <oess_1/db/cln/h/slice_index_base.hpp>
00055 #include <oess_1/db/cln/h/slice_image_base.hpp>
00056
00057 namespace oess_1
00058 {
00059
00060 namespace db
00061 {
00062
00063 namespace cln
00064 {
00065
00082 template< class Item >
00083 class slice_image_storage_t
00084 {
00085 public :
00086 slice_image_storage_t()
00087 {}
00088 virtual ~slice_image_storage_t()
00089 {}
00090
00092 virtual void
00093 insert(
00094 const oess_1::ent_id_t & id,
00095 const Item & value ) = 0;
00096
00098 virtual void
00099 erase(
00100 const oess_1::ent_id_t & id ) = 0;
00101
00103 virtual void
00104 update(
00105 const oess_1::ent_id_t & id,
00106 const Item & value ) = 0;
00107
00109 virtual void
00110 get(
00111 const oess_1::ent_id_t & id,
00112 auto_ptr_3::soft_obj_ptr_t< Item > & o ) = 0;
00113
00115 virtual void
00116 clear() = 0;
00117
00119
00122 virtual oess_1::ent_id_t
00123 next(
00124 const oess_1::ent_id_t & id ) = 0;
00125
00126 private :
00128 slice_image_storage_t(
00129 const slice_image_storage_t< Item > & );
00130 slice_image_storage_t< Item > &
00131 operator=(
00132 const slice_image_storage_t< Item > & );
00133 };
00134
00152 template< class Item >
00153 class no_slice_ram_storage_t :
00154 public slice_image_storage_t< Item >
00155 {
00156 private :
00158 oess_1::db::cln::db_t & m_db;
00160 std::string m_slice_name;
00161
00162 public :
00164 no_slice_ram_storage_t(
00165 oess_1::db::cln::db_t & db,
00166 const std::string & slice_name )
00167 :
00168 m_db( db )
00169 , m_slice_name( slice_name )
00170 {}
00171 virtual ~no_slice_ram_storage_t()
00172 {}
00173
00175 virtual void
00176 insert(
00177 const oess_1::ent_id_t & id,
00178 const Item & value )
00179 {}
00180
00182 virtual void
00183 erase(
00184 const oess_1::ent_id_t & id )
00185 {}
00186
00188 virtual void
00189 update(
00190 const oess_1::ent_id_t & id,
00191 const Item & value )
00192 {}
00193
00195 virtual void
00196 get(
00197 const oess_1::ent_id_t & id,
00198 auto_ptr_3::soft_obj_ptr_t< Item > & o )
00199 {
00200 o.reset( auto_ptr_3::destroyable_obj_t< Item >( new Item() ) );
00201 m_db.ent_load( id, *o );
00202 }
00203
00205 virtual void
00206 clear()
00207 {}
00208
00211 virtual oess_1::ent_id_t
00212 next(
00213 const oess_1::ent_id_t & id )
00214 {
00215 return m_db.ent_find_next( m_slice_name, id );
00216 }
00217 };
00218
00228 template< class Item >
00229 class slice_ram_storage_t :
00230 public slice_image_storage_t< Item >
00231 {
00232 private :
00234 oess_1::db::cln::db_t & m_db;
00236 std::string m_slice_name;
00237
00240 typedef
00241 oess_1::ent_id_t
00242 id_t;
00244
00247 typedef
00248 std::map< id_t, Item >
00249 slice_in_ram_t;
00250
00251 slice_in_ram_t m_slice_in_ram;
00253
00254 public :
00256 slice_ram_storage_t(
00257 oess_1::db::cln::db_t & db,
00258 const std::string & slice_name )
00259 :
00260 m_db( db )
00261 , m_slice_name( slice_name )
00262 {}
00263 virtual ~slice_ram_storage_t()
00264 {}
00265
00267 virtual void
00268 insert(
00269 const oess_1::ent_id_t & id,
00270 const Item & value )
00271 {
00272 m_slice_in_ram.insert(
00273 typename slice_in_ram_t::value_type( id, value ) );
00274 }
00275
00277 virtual void
00278 erase(
00279 const oess_1::ent_id_t & id )
00280 {
00281 m_slice_in_ram.erase( id );
00282 }
00283
00285 virtual void
00286 update(
00287 const oess_1::ent_id_t & id,
00288 const Item & value )
00289 {
00290 m_slice_in_ram[ id ] = value;
00291 }
00292
00294 virtual void
00295 get(
00296 const oess_1::ent_id_t & id,
00297 auto_ptr_3::soft_obj_ptr_t< Item > & o )
00298 {
00299 typename slice_in_ram_t::iterator it = m_slice_in_ram.find( id );
00300 if( m_slice_in_ram.end() != it )
00301 o.reset( auto_ptr_3::nondestroyable_obj_t< Item >(
00302 it->second ) );
00303 else
00304 OESS_THROW_LOGIC( oess_1::db::cln::err::c_unknown_ent_id,
00305 "can't find entity " << id << " in RAM cache" )
00306 }
00307
00310 virtual void
00311 clear()
00312 {
00313 m_slice_in_ram.clear();
00314 }
00315
00320 virtual oess_1::ent_id_t
00321 next(
00322 const oess_1::ent_id_t & id )
00323 {
00324 if( id )
00325 {
00326 typename slice_in_ram_t::iterator it = m_slice_in_ram.find( id );
00327 if( m_slice_in_ram.end() != it )
00328 {
00329 ++it;
00330 if( m_slice_in_ram.end() != it )
00331 return it->first;
00332 }
00333 }
00334 else
00335 if( m_slice_in_ram.size() )
00336 return m_slice_in_ram.begin()->first;
00337
00338 return oess_1::ent_id_t();
00339 }
00340 };
00341
00348 template<
00349 class Item,
00350 class Obj_storage = no_slice_ram_storage_t< Item > >
00351 class slice_image_t :
00352 public slice_image_iface_t< Item >
00353 {
00355 typedef slice_image_iface_t< Item > base_t;
00356
00357 private :
00359
00363 slice_image_storage_t< Item > * m_obj_storage;
00364
00365 public:
00375 slice_image_t(
00377 oess_1::db::cln::db_t & db,
00379 const std::string & slice_name,
00380 unsigned int flags = 0 )
00381 throw( std::exception )
00382 :
00383 base_t( db, slice_name, flags )
00384 , m_obj_storage( new Obj_storage( db, slice_name ) )
00385 {
00386 if( base_t::auto_load & flags )
00387 reload();
00388 }
00389 virtual ~slice_image_t()
00390 {
00391 delete m_obj_storage;
00392 }
00393
00395 void
00396 reload()
00397 {
00398 clear();
00399
00400
00401
00402 db_t & d = base_t::query_db();
00403 const std::string & s = base_t::slice_name();
00404
00405 oess_1::ent_id_t id;
00406 while( id = d.ent_find_next( s, id ) )
00407 {
00408 Item o;
00409 d.ent_load( id, o );
00410 m_obj_storage->insert( id, o );
00411 base_t::lo_refresh_indexes( id, o );
00412 }
00413 }
00414
00416 void
00417 clear()
00418 {
00419 m_obj_storage->clear();
00420 base_t::lo_clear();
00421 }
00422
00425 virtual oess_1::ent_id_t
00426 insert( const Item & o )
00427 {
00428 oess_1::ent_id_t id = base_t::lo_insert( o );
00429 m_obj_storage->insert( id, o );
00430
00431 return id;
00432 }
00433
00436 virtual void
00437 erase( const oess_1::ent_id_t & id )
00438 {
00439 base_t::lo_erase( id );
00440 m_obj_storage->erase( id );
00441 }
00442
00445 virtual void
00446 update(
00447 const ent_id_t & id,
00448 const Item & o )
00449 {
00450 base_t::lo_update( id, o );
00451 m_obj_storage->update( id, o );
00452 }
00453
00454 virtual void
00455 get(
00456 const ent_id_t & id,
00457 auto_ptr_3::soft_obj_ptr_t< Item > & o ) const
00458 {
00459 m_obj_storage->get( id, o );
00460 }
00461
00462 virtual oess_1::ent_id_t
00463 next(
00464 const oess_1::ent_id_t & id ) const
00465 {
00466 return m_obj_storage->next( id );
00467 }
00468
00470 virtual void
00471 attach(
00472 slice_index_base_t & index,
00477 unsigned int flags = 0 )
00478 {
00479 base_t::attach( index, flags );
00480
00481
00482
00483 auto_ptr_3::soft_obj_ptr_t< Item > o;
00484 oess_1::ent_id_t id;
00485 while( id = m_obj_storage->next( id ) )
00486 {
00487 m_obj_storage->get( id, o );
00488 index.on_insert( id, *o );
00489 }
00490 }
00491 };
00492
00493 }
00494
00495 }
00496
00497 }
00498
00499 #endif
00500