Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
txf- opened this issue Mar 9, 2023 · 7 comments
Open

Comments

@txf-
Copy link

txf- commented Mar 9, 2023

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?

@siara-cc
Copy link
Owner

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.

@txf-
Copy link
Author

txf- commented Mar 10, 2023

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??

@siara-cc
Copy link
Owner

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.

@txf-
Copy link
Author

txf- commented Mar 10, 2023

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.

@siara-cc
Copy link
Owner

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.

@txf-
Copy link
Author

txf- commented Mar 13, 2023

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

@siara-cc
Copy link
Owner

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants