From 856a438b33184959b64864f1afdf5a6a2fd6b0d2 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Wed, 16 Jan 2013 16:31:29 +0200 Subject: Make session cache management functions static, add notifications --- common/session/custom/session.hxx | 64 ++++++++++++++++++++++++++------------- 1 file changed, 43 insertions(+), 21 deletions(-) (limited to 'common/session/custom/session.hxx') diff --git a/common/session/custom/session.hxx b/common/session/custom/session.hxx index bb60a4b..3b1789a 100644 --- a/common/session/custom/session.hxx +++ b/common/session/custom/session.hxx @@ -26,14 +26,13 @@ private: session (const session&); session& operator= (const session&); - // Current session interface. + // Session for the current thread. This can be implemented in pretty + // much any way that makes sense to the application. It can be a global + // session as we have here. In multi-threaded applications we could use + // TLS instead. // public: - static bool - has_current (); - - static session& - current (); + static session* current; // Change tracking interface. // @@ -61,23 +60,30 @@ private: mark () = 0; }; + enum object_state + { + tracking, // Tracking any modifications by storing the original copy. + changed, // Known to be changed. + flushed // Flushed but not yet committed/rolled back. + }; + template - struct object_state + struct object_data { typedef typename odb::object_traits::pointer_type pointer_type; explicit - object_state (pointer_type o): obj (o), flushed_ (false) {} + object_data (pointer_type o): obj (o), state (tracking) {} pointer_type obj; pointer_type orig; - bool flushed_; + object_state state; }; template struct object_map: object_map_base, std::map::id_type, - object_state > + object_data > { virtual void flush (odb::database&); @@ -95,37 +101,53 @@ public: typedef object_map map; typedef typename map::iterator iterator; - position () {} + position (): map_ (0) {} position (map& m, const iterator& p): map_ (&m), pos_ (p) {} map* map_; iterator pos_; }; + // Cache management. + // template - position + static position insert (odb::database&, const typename odb::object_traits::id_type&, const typename odb::object_traits::pointer_type&); template + static typename odb::object_traits::pointer_type + find (odb::database&, const typename odb::object_traits::id_type&); + + template + static void + erase (const position& p) + { + if (p.map_ != 0) + p.map_->erase (p.pos_); + } + + // Notifications. + // + template static void - initialize (const position&); + persist (const position& p) + { + load (p); + } template - typename odb::object_traits::pointer_type - find (odb::database&, const typename odb::object_traits::id_type&) const; + static void + load (const position&); template - void - erase (odb::database&, const typename odb::object_traits::id_type&); + static void + update (odb::database&, const T&); template static void - erase (const position& p) - { - p.map_->erase (p.pos_); - } + erase (odb::database&, const typename odb::object_traits::id_type&); private: typedef std::map