diff options
| author | David Bremner <david@tethera.net> | 2021-03-14 11:01:59 -0300 |
|---|---|---|
| committer | David Bremner <david@tethera.net> | 2021-03-18 08:04:06 -0300 |
| commit | 13efbd0e1c88fcbdb48b4c30c7852c5bf1fbd0b7 (patch) | |
| tree | 601d78a964f42acf4c3d41b813e70fadc27eda82 /lib/open.cc | |
| parent | f0717aa380cfa84c1973f7b03948d93b261a73fe (diff) | |
lib: support reopening databases for write access.
In the future Xapian will apparently support this more conveniently
for the cases other than READ_ONLY => READ_ONLY
Conceptually this function seems to fit better in lib/open.cc;
database.cc is still large enough that moving the function makes
sense.
Diffstat (limited to 'lib/open.cc')
| -rw-r--r-- | lib/open.cc | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/lib/open.cc b/lib/open.cc index 12d3613f..d05a19a7 100644 --- a/lib/open.cc +++ b/lib/open.cc @@ -511,3 +511,49 @@ notmuch_database_create_with_config (const char *database_path, talloc_free (notmuch); return status; } + +notmuch_status_t +notmuch_database_reopen (notmuch_database_t *notmuch, + notmuch_database_mode_t new_mode) +{ + notmuch_database_mode_t cur_mode = _notmuch_database_mode (notmuch); + + if (notmuch->xapian_db == NULL) { + _notmuch_database_log (notmuch, "Cannot reopen closed or nonexistent database\n"); + return NOTMUCH_STATUS_ILLEGAL_ARGUMENT; + } + + try { + if (cur_mode == new_mode && + new_mode == NOTMUCH_DATABASE_MODE_READ_ONLY) { + notmuch->xapian_db->reopen (); + } else { + notmuch->xapian_db->close (); + + delete notmuch->xapian_db; + notmuch->xapian_db = NULL; + /* no need to free the same object twice */ + notmuch->writable_xapian_db = NULL; + + if (new_mode == NOTMUCH_DATABASE_MODE_READ_WRITE) { + notmuch->writable_xapian_db = new Xapian::WritableDatabase (notmuch->xapian_path, + DB_ACTION); + notmuch->xapian_db = notmuch->writable_xapian_db; + } else { + notmuch->xapian_db = new Xapian::Database (notmuch->xapian_path, + DB_ACTION); + } + } + } catch (const Xapian::Error &error) { + if (! notmuch->exception_reported) { + _notmuch_database_log (notmuch, "Error: A Xapian exception reopening database: %s\n", + error.get_msg ().c_str ()); + notmuch->exception_reported = true; + } + return NOTMUCH_STATUS_XAPIAN_EXCEPTION; + } + + notmuch->view++; + notmuch->open = true; + return NOTMUCH_STATUS_SUCCESS; +} |
