From 26e36b3a9d7b49d46ecfa69b447482251acba8ac Mon Sep 17 00:00:00 2001 From: Karen Arutyunov Date: Wed, 24 Jan 2024 16:53:38 +0300 Subject: Turn libodb repository into package for muti-package repository --- libodb/odb/session.txx | 90 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 libodb/odb/session.txx (limited to 'libodb/odb/session.txx') diff --git a/libodb/odb/session.txx b/libodb/odb/session.txx new file mode 100644 index 0000000..d74fe0f --- /dev/null +++ b/libodb/odb/session.txx @@ -0,0 +1,90 @@ +// file : odb/session.txx +// license : GNU GPL v2; see accompanying LICENSE file + +namespace odb +{ + template + typename session::cache_position session:: + cache_insert (database_type& db, + const typename object_traits::id_type& id, + const typename object_traits::pointer_type& obj) + { + type_map& tm (db_map_[&db]); + details::shared_ptr& pom (tm[&typeid (T)]); + + if (!pom) + pom.reset (new (details::shared) object_map); + + object_map& om (static_cast&> (*pom)); + + typename object_map::value_type vt (id, obj); + std::pair::iterator, bool> r (om.insert (vt)); + + // In what situation may we possibly attempt to reinsert the object? + // For example, when the user loads the same object in two different + // instances (i.e., load into a pre-allocated object). In this case + // we should probably update our entries accordingly. + // + if (!r.second) + r.first->second = obj; + + return cache_position (om, r.first); + } + + template + typename object_traits::pointer_type session:: + cache_find (database_type& db, + const typename object_traits::id_type& id) const + { + typedef typename object_traits::pointer_type pointer_type; + + database_map::const_iterator di (db_map_.find (&db)); + + if (di == db_map_.end ()) + return pointer_type (); + + const type_map& tm (di->second); + type_map::const_iterator ti (tm.find (&typeid (T))); + + if (ti == tm.end ()) + return pointer_type (); + + const object_map& om (static_cast&> (*ti->second)); + typename object_map::const_iterator oi (om.find (id)); + + if (oi == om.end ()) + return pointer_type (); + + return oi->second; + } + + template + void session:: + cache_erase (database_type& db, const typename object_traits::id_type& id) + { + database_map::iterator di (db_map_.find (&db)); + + if (di == db_map_.end ()) + return; + + type_map& tm (di->second); + type_map::iterator ti (tm.find (&typeid (T))); + + if (ti == tm.end ()) + return; + + object_map& om (static_cast&> (*ti->second)); + typename object_map::iterator oi (om.find (id)); + + if (oi == om.end ()) + return; + + om.erase (oi); + + if (om.empty ()) + tm.erase (ti); + + if (tm.empty ()) + db_map_.erase (di); + } +} -- cgit v1.1