[odb-users] How to cache the queried object pointer?

Justin Huang yohuang at nvidia.com
Tue Nov 5 00:25:40 EST 2019


It is quite weird that if I reorder the declaration of ports_, instances_ and connections_ data members of Module class, ODB would always do not create table for the first one. In below example, there is no 'CREATE TABLE' statement for Modulel_ports. If I move instances_ up above ports_, then ODB would not generate CREATE TABLE statement for Module_instances. 

I can't understand what is going wrong here.


class Module .. {
private:
    std::vector<Port*>               ports_;
    std::vector<Instance*>       instances_;
    std::vector<Connection*> connections_;
}; // class Module

-----Original Message-----
From: Justin Huang 
Sent: Tuesday, November 5, 2019 12:56 PM
To: odb-users at codesynthesis.com
Subject: RE: [odb-users] How to cache the queried object pointer?

It is weird that there was no Module_ports table when I loaded the sqlite3 DB.



-----Original Message-----
From: Justin Huang 
Sent: Tuesday, November 5, 2019 10:47 AM
To: odb-users at codesynthesis.com
Subject: RE: [odb-users] How to cache the queried object pointer?

Hi Boris,

Thanks. It works.

But I saw a weird issue when I use i.load() to query data members.

Below I have a Module class, Instance class and Port class. I persisted the ports() and instances() of Module object, and confirmed that they were persisted in sqlite3 DB.

I tried to use i.load() pointer to access ports(), the returned ports() size is 0, but size of instances() is not 0.

        session s;
        db = std::unique_ptr<odb::database>(create_database (argc, argv));

        // load modules
        {
            odb::transaction t (db->begin());

            typedef odb::result<Module> result;
            result all_modules (db->query<Module> ());

            int count = 0;
            for (result::iterator i(all_modules.begin()); i != all_modules.end(); ++i) {
                std::shared_ptr<Module> p (i.load());
                std::cout << "loading module: " << p->name() << std::endl;
                std::cout << "ports size: "            << p->ports().size() << std::endl;
                std::cout << "instances size: "    << p->instances().size() << std::endl;
                for (auto port: p->ports())
                    std::cout << "loading module port: " << port->name() << std::endl;
             
                for (auto inst: p->instances())
                    std::cout << "loading module inst: " << inst->name() << std::endl;
            }

Any suggestions to debug this?

Thanks.

-------------------------------------------------------
# Module, Port, Instance class declaration
-------------------------------------------------------
#pragma db object
class Module: public NamedHessObject
{
public:
    Module(std::string name);
   ~Module();

    std::vector<Port*>&               ports();
    std::vector<Instance*>&       instances();
    std::vector<Connection*>& connections();
    std::string                                as_string();
    static Module*                       find_by_name(std::string name);

public:
    #pragma db transient
    static std::map<std::string, Module*> modules_;
    static size_t count;

private:
    friend class odb::access;
    Module();

private:
    std::vector<Port*>               ports_;
    std::vector<Instance*>       instances_;
    std::vector<Connection*> connections_; }; // class Module

#pragma db object
class Instance: public NamedHessObject
{
public:
    Instance(std::string name);
   ~Instance();

    Module*                 module();
    void                         module(Module* module);

    Module*                 owner();
    void                         owner(Module* owner);

    std::vector<PortRef *>& portrefs();
    std::string                          as_string();

public:
    static size_t count;

private:
    friend class odb::access;
    Instance();

private:
    Module*                           owner_;  // owner module
    Module*                           module_; // instantiated module
    std::vector<PortRef *>  portrefs_;
}; // class Instance

#pragma db object
class Port: public NamedHessObject
{
public:
    Port(std::string name);
    Port();
   ~Port();

    std::string                direction();
    void                           direction(std::string direction);

    Module*                    owner();
    void                             owner(Module* owner);

    Interface*                 interface();
    void                            interface(Interface* interface);

    std::vector<Connection *>& connections();
    std::string                                  as_string();

public:
    static size_t count;

private:
    friend class odb::access;

private:
    std::string                 direction_;
    Module*                   owner_;       // module this port belongs to.
    Interface*                interface_;   // instantiated interface.
    std::vector<Connection *> connections_; // channels connected to this port inside the owner_ module }; // class Port



-----Original Message-----
From: Boris Kolpackov <boris at codesynthesis.com>
Sent: Friday, November 1, 2019 11:07 PM
To: Justin Huang <yohuang at nvidia.com>
Cc: odb-users at codesynthesis.com
Subject: Re: [odb-users] How to cache the queried object pointer?

Justin Huang <yohuang at nvidia.com> writes:

>            typedef odb::result<Module> result;
>            result all_modules (db->query<Module> ());
> 
>            int count = 0;
>            for (result::iterator i(all_modules.begin()); i != all_modules.end(); ++i) {
>                 auto ptr = i.operator->();
>                 modules.push_back(ptr);

You should use result::iterator::load() (i.load()) to load an object from a query result. See Section 4.4, "Query Result" for details.
-----------------------------------------------------------------------------------
This email message is for the sole use of the intended recipient(s) and may contain
confidential information.  Any unauthorized review, use, disclosure or distribution
is prohibited.  If you are not the intended recipient, please contact the sender by
reply email and destroy all copies of the original message.
-----------------------------------------------------------------------------------



More information about the odb-users mailing list