I have an application that reads data into a map and writes them to a sqlite database. The application does a lot of other things and is asynchronous (uses boost asio) except for writing to db part. Here is the sample pseudo code:
struct records;
std::map<int, records> list;
void read() {
// Dump(read) X records into list
// If record id doesn't exist, add it. dirty = true
// If it exists, update all other fields. dirty = true if any fields changed.
}
void writeDB() {
sqlite3_prepare_v2();
for(const auto& record : list) { // iterate map
if(!record.dirty())
continue;
sqlite3_bind(); // bind record fields
sqlite3_step(); // execute sql
sqlite3_reset(); // reset
}
sqlite3_finalize();
}
main() {
while(1) {
read();
writeDB();
}
}
My initial plan was to spawn a new thread inside the for loop which would write each record in the database in parallel. There are two issues.
1) May need to implement db locking.
2) The sqlite bind(), step() and reset()
operate on the same statement. So, the records have to be written sequentially.
I am using C++ 17. Any suggestions on making the database calls async? Thanks for helping!