Unsolved Advice on Moving my Db class to a separate thread.
-
Advice for moving database to a different thread.
Hello, I want to move my database to a different thread, to help performance.
Currently I have a class called DbManager that handles all database communication.
I also have several classes that inherit
QAbstractitemModel
and are instantiated in QML.
Those classes are as follows : ActorModel, MovieModel, ImageModel.
All of those classes have a pointer to the same instance of DbManager inside them.Those QAbstractModels are visible in QML with the help of “View” Components such as
GridView
.Right now every time a view wants to query the database for information, the main thread needs to wait until the database query member function in DbManager returns. This sometimes have adverse effect on performance.
For example, now if
GridView
invokes thefetchMore’
method inside the ActorModel class which in turn queries the database for more actors. In this case the GUI would hang until that method returns.I want to move DbManager to a different thread,so that the previous example would execute as follows:
GridView
invokes thefetchMore
method inside the ActorModel. ActorModel passes the request to DbManager and returns. When DbManager retrieves the needed information, it emits a signal to ActorModel and thenGridView
makes the necessary updates.This way the GUI thread doesn’t need to wait until DbManager finishes it’s job.
So far I have found few problems with that approach:
-
First, as far as I understand, I can’t send the actual information
received from the database from DbManger to ActorModel in a signal. -
Second, There are multiple model classes and each of them can query
the database multiple times, also there could be multiple instances of the same model class. When I send a signal from DbManager I
need to be able to differentiate between the different models and
different calls to the database from the same model.
The solution I'm thinking about is sending a unique identifier with each database related request, When DbManager fetches the information, it stores it in hash table that has the Unique identifier as key, and the needed information as value. Then emits a signal notifying all model classes that a db call has been completed, but only the model class with the correct identifier would know it should access DbManager's hash table and retrieve the information it wanted.
What I'm thinking about doing is making a queue and a hashtable inside DbManager, when let's say ActorModel wants to query the database it will place it's unique id and the SQL it wants to execute in the queue. (EX: 'ActorModel1','SELECT * FROM ACTORS')
The Db would pick it up ,execute the SQL and put the results in a hashtable (EX:'ActorModel1','Angelina Jolie, Brad Pitt, Nicolas Cage') and then emit a signal 'ActorModel1 query ready' to all models. And only the model with the id ActorModel1 would know it should access the DbManager hashtable.
So while there are items in the queue, DbManager would execute SQL queries.
What I want to know is if I'm on the right path, and if what I'm planning to do makes any sense, or if there are much better solutions to this?
-