[odb-users] Question about sessions
Boris Kolpackov
boris at codesynthesis.com
Tue Nov 14 06:08:55 EST 2023
Javier Gutierrez <javier.gutierrez at web.de> writes:
> As I understand when using persist(), load(), find() or loaded by query the
> session-enabled object's pointer is stored in the session.
> Can you confirm if following works?
>
> person p ("John", "Doe"); // Does this need to be a pointer in order to work?
> session s;
> transaction t (db.begin ());
> unsigned long id (db.persist (p)) // p is cached in s.
> t.commit ();
>From Section 11.1, "Object Cache":
"The per-object caching policies depend on the object pointer kind (Section
6.5, "Using Custom Smart Pointers"). Objects with a unique pointer, such as
std::auto_ptr or std::unique_ptr, as an object pointer are never cached since
it is not possible to have two such pointers pointing to the same object. When
an object is persisted via a pointer or loaded as a dynamically allocated
instance, objects with both raw and shared pointers as object pointers are
cached. If an object is persisted as a reference or loaded into a
pre-allocated instance, the object is only cached if its object pointer is a
raw pointer."
https://www.codesynthesis.com/products/odb/doc/manual.xhtml#11
> typedef odb::query<person> query;
> typedef odb::result<person> result;
> query q (query::first == "John" && query::last == "Doe");
> result r (db.query<person> (q)); // Does this retrieve from session?
If it is cached, then yes.
> Additionally, is there a way to check how many objects are stored in the
> session (using the out of the box session class) ?
If you look at <odb/session.hxx>, you will see the following API:
// 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_;}
While it doesn't provide the count directly, you should be able to
calculate it with a bit of effort.
More information about the odb-users
mailing list