sqlite_micro_logger_arduino icon indicating copy to clipboard operation
sqlite_micro_logger_arduino copied to clipboard

Is it possible to have both a read and write context active for a db?

Open txf- opened this issue 1 year ago • 7 comments

If I use fp = fopen("dbfile.db", "r+b");

Could I init both a dblog_write_context and dblog_read_context at the same time? If possible, what are the possible consequences?

txf- avatar Mar 09 '23 17:03 txf-

Please see here for an example. There is no issue of using both for the same handle because both read and write functions that are defined are expected to seek the position before writing or reading. So opening using "r+b" is alright for such operations.

siara-cc avatar Mar 10 '23 02:03 siara-cc

presumably if one would like to read back what has just been written the writes should be flushed and then the entry should be finalized??

txf- avatar Mar 10 '23 09:03 txf-

Read and write contexts are to be used independently. The functions that take read context as parameter do not know about any write context and vice versa. So any pending writes are to be flushed by the caller before any reads can be performed.

siara-cc avatar Mar 10 '23 09:03 siara-cc

I ask because I'm having an issue where if I call dblog_init_for_append(), insert a couple of entries with dblog_append_row_with_values() write an entry the call dblog_finalize(), I can't read back that entry. If I restart the ESP all those entries are also not present.

However If I close the file after finalizing with fclose(), I can both read them and they're retained on reboots.

txf- avatar Mar 10 '23 17:03 txf-

When dblog_finalize() is called, it writes to disk the page that is being written to and calls the flush function defined. So it should be possible to read it back. I can't find it now, but I vaguely remember some such issue where flush() was not working as intended for SPIFFS or something like that. Please try calling flush() more than once.

siara-cc avatar Mar 10 '23 17:03 siara-cc

After I call finalize, do I need to init the db again?

txf- avatar Mar 13 '23 17:03 txf-

After I call finalize, do I need to init the db again?

Yes dblog_read_init() for the read context. However, the file need not be closed as all the File IO are being done in the callback functions. The callback functions can re-use the open file handle.

siara-cc avatar Mar 16 '23 14:03 siara-cc