diff options
-rw-r--r-- | odb/cache-traits.hxx | 21 | ||||
-rw-r--r-- | odb/session.hxx | 87 | ||||
-rw-r--r-- | odb/session.ixx | 45 | ||||
-rw-r--r-- | odb/session.txx | 39 |
4 files changed, 127 insertions, 65 deletions
diff --git a/odb/cache-traits.hxx b/odb/cache-traits.hxx index 5ab5022..dbb304e 100644 --- a/odb/cache-traits.hxx +++ b/odb/cache-traits.hxx @@ -28,13 +28,14 @@ namespace odb typedef odb::pointer_traits<pointer_type> pointer_traits; typedef typename pointer_traits::element_type object_type; typedef typename object_traits<object_type>::id_type id_type; - typedef typename session_type::template position<object_type> position_type; + typedef typename session_type::template cache_position<object_type> + position_type; struct insert_guard { insert_guard () {} insert_guard (const position_type& pos): pos_ (pos) {} - ~insert_guard () {session_type::erase (pos_);} + ~insert_guard () {session_type::_cache_erase (pos_);} const position_type& position () const {return pos_;} @@ -62,7 +63,7 @@ namespace odb static position_type insert (odb::database& db, const id_type& id, const pointer_type& p) { - return session_type::template insert<object_type> (db, id, p); + return session_type::template _cache_insert<object_type> (db, id, p); } static position_type @@ -72,19 +73,19 @@ namespace odb object_traits<object_type>::id ( pointer_traits::get_ref (p))); - return session_type::template insert<object_type> (db, id, p); + return session_type::template _cache_insert<object_type> (db, id, p); } static pointer_type find (odb::database& db, const id_type& id) { - return session_type::template find<object_type> (db, id); + return session_type::template _cache_find<object_type> (db, id); } static void erase (const position_type& p) { - session_type::template erase<object_type> (p); + session_type::template _cache_erase<object_type> (p); } // Notifications. @@ -92,25 +93,25 @@ namespace odb static void persist (const position_type& p) { - session_type::template persist<object_type> (p); + session_type::template _cache_persist<object_type> (p); } static void load (const position_type& p) { - session_type::template load<object_type> (p); + session_type::template _cache_load<object_type> (p); } static void update (odb::database& db, const object_type& obj) { - session_type::template update<object_type> (db, obj); + session_type::template _cache_update<object_type> (db, obj); } static void erase (odb::database& db, const id_type& id) { - session_type::template erase<object_type> (db, id); + session_type::template _cache_erase<object_type> (db, id); } }; diff --git a/odb/session.hxx b/odb/session.hxx index 6c9359c..657a869 100644 --- a/odb/session.hxx +++ b/odb/session.hxx @@ -59,6 +59,8 @@ namespace odb static void reset_current () {current_pointer (0);} + // Pointer versions. + // static session* current_pointer (); @@ -88,19 +90,61 @@ namespace odb // Object cache. // public: - // Position in the cache of an inserted element. The requirements + // Position in the cache of the inserted element. + // + template <typename T> + struct cache_position; + + template <typename T> + cache_position<T> + cache_insert (database_type&, + const typename object_traits<T>::id_type&, + const typename object_traits<T>::pointer_type&); + + template <typename T> + typename object_traits<T>::pointer_type + cache_find (database_type&, + const typename object_traits<T>::id_type&) const; + + template <typename T> + void + cache_erase (const cache_position<T>&); + + template <typename T> + void + cache_erase (database_type&, const typename object_traits<T>::id_type&); + + // Low-level object cache access (iteration, etc). + // + public: + typedef std::map<const std::type_info*, + details::shared_ptr<object_map_base>, + details::type_info_comparator> type_map; + + typedef std::map<database_type*, type_map> database_map; + + database_map& + map () {return db_map_;} + + const database_map& + map () const {return db_map_;} + + // Static cache API as expected by the rest of ODB. + // + public: + // Position in the cache of the inserted element. The requirements // for this class template are: default and copy-constructible as // well as copy-assignable. The default constructor creates an // empty/NULL position. // template <typename T> - struct position + struct cache_position { typedef object_map<T> map; typedef typename map::iterator iterator; - position (): map_ (0) {} - position (map& m, const iterator& p): map_ (&m), pos_ (p) {} + cache_position (): map_ (0) {} + cache_position (map& m, const iterator& p): map_ (&m), pos_ (p) {} map* map_; iterator pos_; @@ -112,52 +156,37 @@ namespace odb // that caused it to be inserted (e.g., load) failed. // template <typename T> - static position<T> - insert (database_type&, - const typename object_traits<T>::id_type&, - const typename object_traits<T>::pointer_type&); + static cache_position<T> + _cache_insert (database_type&, + const typename object_traits<T>::id_type&, + const typename object_traits<T>::pointer_type&); template <typename T> static typename object_traits<T>::pointer_type - find (database_type&, const typename object_traits<T>::id_type&); + _cache_find (database_type&, const typename object_traits<T>::id_type&); template <typename T> static void - erase (const position<T>&); + _cache_erase (const cache_position<T>&); // Notifications. These are called after per-object callbacks for // post_{persist, load, update, erase} events. // template <typename T> static void - persist (const position<T>&) {} + _cache_persist (const cache_position<T>&) {} template <typename T> static void - load (const position<T>&) {} + _cache_load (const cache_position<T>&) {} template <typename T> static void - update (database_type&, const T&) {} + _cache_update (database_type&, const T&) {} template <typename T> static void - erase (database_type&, const typename object_traits<T>::id_type&); - - // Low-level object cache access (iteration, etc). - // - public: - typedef std::map<const std::type_info*, - details::shared_ptr<object_map_base>, - details::type_info_comparator> type_map; - - typedef std::map<database_type*, type_map> database_map; - - database_map& - map () {return db_map_;} - - const database_map& - map () const {return db_map_;} + _cache_erase (database_type&, const typename object_traits<T>::id_type&); protected: database_map db_map_; diff --git a/odb/session.ixx b/odb/session.ixx index 4748cae..b7453ca 100644 --- a/odb/session.ixx +++ b/odb/session.ixx @@ -8,11 +8,54 @@ namespace odb { template <typename T> inline void session:: - erase (const position<T>& p) + cache_erase (const cache_position<T>& p) { // @@ Empty maps are not cleaned up by this version of erase. // if (p.map_ != 0) p.map_->erase (p.pos_); } + + template <typename T> + inline typename session::cache_position<T> session:: + _cache_insert (database_type& db, + const typename object_traits<T>::id_type& id, + const typename object_traits<T>::pointer_type& obj) + { + if (session* s = current_pointer ()) + return s->cache_insert<T> (db, id, obj); + else + return cache_position<T> (); + } + + template <typename T> + inline typename object_traits<T>::pointer_type session:: + _cache_find (database_type& db, const typename object_traits<T>::id_type& id) + { + typedef typename object_traits<T>::pointer_type pointer_type; + + if (const session* s = current_pointer ()) + return s->cache_find<T> (db, id); + else + return pointer_type (); + } + + template <typename T> + inline void session:: + _cache_erase (const cache_position<T>& p) + { + // @@ Empty maps are not cleaned up by this version of erase. + // + if (p.map_ != 0) + p.map_->erase (p.pos_); + } + + template <typename T> + inline void session:: + _cache_erase (database_type& db, + const typename object_traits<T>::id_type& id) + { + if (session* s = current_pointer ()) + s->cache_erase<T> (db, id); + } } diff --git a/odb/session.txx b/odb/session.txx index e89e439..273638c 100644 --- a/odb/session.txx +++ b/odb/session.txx @@ -5,18 +5,14 @@ namespace odb { template <typename T> - typename session::position<T> session:: - insert (database_type& db, - const typename object_traits<T>::id_type& id, - const typename object_traits<T>::pointer_type& obj) + typename session::cache_position<T> session:: + cache_insert (database_type& db, + const typename object_traits<T>::id_type& id, + const typename object_traits<T>::pointer_type& obj) { typedef odb::object_traits<T> object_traits; - session* s (current_pointer ()); - if (s == 0) - return position<T> (); - - type_map& tm (s->db_map_[&db]); + type_map& tm (db_map_[&db]); details::shared_ptr<object_map_base>& pom (tm[&typeid (T)]); if (!pom) @@ -35,22 +31,19 @@ namespace odb if (!r.second) r.first->second = obj; - return position<T> (om, r.first); + return cache_position<T> (om, r.first); } template <typename T> typename object_traits<T>::pointer_type session:: - find (database_type& db, const typename object_traits<T>::id_type& id) + cache_find (database_type& db, + const typename object_traits<T>::id_type& id) const { typedef typename object_traits<T>::pointer_type pointer_type; - const session* s (current_pointer ()); - if (s == 0) - return pointer_type (); - - database_map::const_iterator di (s->db_map_.find (&db)); + database_map::const_iterator di (db_map_.find (&db)); - if (di == s->db_map_.end ()) + if (di == db_map_.end ()) return pointer_type (); const type_map& tm (di->second); @@ -70,15 +63,11 @@ namespace odb template <typename T> void session:: - erase (database_type& db, const typename object_traits<T>::id_type& id) + cache_erase (database_type& db, const typename object_traits<T>::id_type& id) { - session* s (current_pointer ()); - if (s == 0) - return; - - database_map::iterator di (s->db_map_.find (&db)); + database_map::iterator di (db_map_.find (&db)); - if (di == s->db_map_.end ()) + if (di == db_map_.end ()) return; type_map& tm (di->second); @@ -99,6 +88,6 @@ namespace odb tm.erase (ti); if (tm.empty ()) - s->db_map_.erase (di); + db_map_.erase (di); } } |