diff --git a/src/mu-store-priv.hh b/src/mu-store-priv.hh index f60e4821..e8fa7594 100644 --- a/src/mu-store-priv.hh +++ b/src/mu-store-priv.hh @@ -47,32 +47,20 @@ private: struct _MuStore { +public: + /* create a read-write MuStore */ + _MuStore (const char *path, const char *contacts_path, bool rebuild) { -/* by default, use transactions of 30000 messages */ -#define MU_STORE_DEFAULT_BATCH_SIZE 30000 - /* http://article.gmane.org/gmane.comp.search.xapian.general/3656 */ -#define MU_STORE_MAX_TERM_LENGTH 240 + init (path, contacts_path, rebuild, false); - _MuStore (const char *path, const char *contacts_path, bool read_only): - _in_transaction (false), _processed (0), - _batch_size(MU_STORE_DEFAULT_BATCH_SIZE), - _contacts(0), _path (0), _version(0), - _db(0), _read_only(read_only), _ref_count (1) { - - if (!check_path ()) - throw MuStoreError (MU_ERROR_FILE, "invalid_path"); - - _path = g_strdup (path); - - if (read_only) - _db = new Xapian::Database (path); + if (rebuild) + _db = new Xapian::WritableDatabase + (path, Xapian::DB_CREATE_OR_OVERWRITE); else _db = new Xapian::WritableDatabase (path, Xapian::DB_CREATE_OR_OPEN); - if (!check_version ()) - throw MuStoreError (MU_ERROR_XAPIAN_NOT_UP_TO_DATE, - ("xapian db version check failed")); + check_set_version (); if (contacts_path) { _contacts = mu_contacts_new (contacts_path); @@ -80,24 +68,64 @@ struct _MuStore { throw MuStoreError (MU_ERROR_FILE, ("failed to init contacts cache")); } - MU_WRITE_LOG ("%s: opened %s (batch size: %u)", + + MU_WRITE_LOG ("%s: opened %s (batch size: %u) for read-write", __FUNCTION__, this->path(), batch_size()); } + /* create a read-only MuStore */ + _MuStore (const char *path) { + + init (path, NULL, false, false); + + _db = new Xapian::Database (path); + if (mu_store_needs_upgrade(this)) + throw MuStoreError (MU_ERROR_XAPIAN_NOT_UP_TO_DATE, + ("store needs an upgrade")); + + MU_WRITE_LOG ("%s: opened %s read-only", __FUNCTION__, this->path()); + } + + void init (const char *path, const char *contacts_path, + bool rebuild, bool read_only) { + + _batch_size = DEFAULT_BATCH_SIZE; + _contacts = 0; + _in_transaction = false; + _path = path; + _processed = 0; + _read_only = read_only; + _ref_count = 1; + _version = NULL; + } + + void check_set_version () { + /* check version...*/ + gchar *version; + version = mu_store_get_metadata (this, MU_STORE_VERSION_KEY, NULL); + if (!version) + mu_store_set_metadata (this, MU_STORE_VERSION_KEY, + MU_STORE_SCHEMA_VERSION, NULL); + else if (g_strcmp0 (version, MU_STORE_SCHEMA_VERSION) != 0) { + g_free (version); + throw MuStoreError (MU_ERROR_XAPIAN_NOT_UP_TO_DATE, + ("store needs an upgrade")); + } else + g_free (version); + } + ~_MuStore () { try { if (_ref_count != 0) g_warning ("ref count != 0"); g_free (_version); - g_free (_path); mu_contacts_destroy (_contacts); - if (!_read_only) mu_store_flush (this); - MU_WRITE_LOG ("closing xapian database with %d documents", + MU_WRITE_LOG ("closing xapian database with %d document(s)", (int)db_read_only()->get_doccount()); delete _db; @@ -105,7 +133,20 @@ struct _MuStore { } /* close the old database, and write an empty one on top of it */ - void clear(); + void clear () { + if (is_read_only()) + throw std::runtime_error ("database is read-only"); + + // clear the database + db_writable()->close (); + delete _db; + _db = new Xapian::WritableDatabase + (path(), Xapian::DB_CREATE_OR_OVERWRITE); + + // clear the contacts cache + if (_contacts) + mu_contacts_clear (_contacts); + } /* get a unique id for this message; note, this function returns a * static buffer -- not reentrant */ @@ -116,10 +157,16 @@ struct _MuStore { const char* version () { g_free (_version); - return _version = - mu_store_get_metadata (this, MU_STORE_VERSION_KEY); + return _version = mu_store_get_metadata (this, MU_STORE_VERSION_KEY, + NULL); } + void set_version (const char *version) { + mu_store_set_metadata (this, MU_STORE_VERSION_KEY, version, NULL); + } + + static unsigned max_term_length() { return MAX_TERM_LENGTH; } + void begin_transaction (); void commit_transaction (); void rollback_transaction (); @@ -132,12 +179,12 @@ struct _MuStore { Xapian::Database* db_read_only() const { return _db; } - const char* path () const { return _path; } + const char* path () const { return _path.c_str(); } bool is_read_only () const { return _read_only; } size_t batch_size () const { return _batch_size;} size_t set_batch_size (size_t n) { - return _batch_size = ( n == 0) ? MU_STORE_DEFAULT_BATCH_SIZE : n; + return _batch_size = ( n == 0) ? DEFAULT_BATCH_SIZE : n; } bool in_transaction () const { return _in_transaction; } @@ -151,13 +198,12 @@ struct _MuStore { guint ref () { return ++_ref_count; } guint unref () { return --_ref_count; } + /* by default, use transactions of 30000 messages */ + static const unsigned DEFAULT_BATCH_SIZE = 30000; + /* http://article.gmane.org/gmane.comp.search.xapian.general/3656 */ + static const unsigned MAX_TERM_LENGTH = 240; + private: - - bool check_version (); - bool check_path () { - return true; // FIXME - } - /* transaction handling */ bool _in_transaction; int _processed; @@ -166,8 +212,8 @@ private: /* contacts object to cache all the contact information */ MuContacts *_contacts; - char *_path; - mutable char *_version; + std::string _path; + gchar *_version; Xapian::Database *_db; bool _read_only; diff --git a/src/mu-store-read.cc b/src/mu-store-read.cc index 8f3f213b..d0e03e29 100644 --- a/src/mu-store-read.cc +++ b/src/mu-store-read.cc @@ -39,29 +39,6 @@ #include "mu-flags.h" #include "mu-contacts.h" - -bool -_MuStore::check_version () -{ - const gchar *version; - version = mu_store_version (this); - - /* no version yet? it must be a new db then; we'll set the version */ - if (!version) { - if (!mu_store_set_metadata (this, MU_STORE_VERSION_KEY, - MU_STORE_SCHEMA_VERSION)) { - g_warning ("failed to set database version"); - return FALSE; - } - return TRUE; /* ok, done. */ - } - - /* we have a version, but is it the right one? */ - return mu_store_needs_upgrade (this) ? FALSE : TRUE; -} - - - /* get a unique id for this message; note, this function returns a * static buffer -- not reentrant */ const char* @@ -91,14 +68,13 @@ mu_store_new_read_only (const char* xpath, GError **err) g_return_val_if_fail (xpath, NULL); try { - return new _MuStore (xpath, NULL, true); + return new _MuStore (xpath); } catch (const MuStoreError& merr) { - g_set_error (err, 0, merr.mu_error(), "%s", merr.what().c_str()); - } MU_XAPIAN_CATCH_BLOCK_G_ERROR(err,MU_ERROR_XAPIAN); + } MU_XAPIAN_CATCH_BLOCK_G_ERROR(err, MU_ERROR_XAPIAN); return NULL; } @@ -113,18 +89,20 @@ mu_store_is_read_only (MuStore *store) return store->is_read_only() ? TRUE : FALSE; } MU_XAPIAN_CATCH_BLOCK_RETURN(FALSE); + } unsigned -mu_store_count (MuStore *store) +mu_store_count (MuStore *store, GError **err) { - g_return_val_if_fail (store, 0); + g_return_val_if_fail (store, (unsigned)-1); try { return store->db_read_only()->get_doccount(); - } MU_XAPIAN_CATCH_BLOCK_RETURN(0); + } MU_XAPIAN_CATCH_BLOCK_G_ERROR_RETURN(err, MU_ERROR_XAPIAN, + (unsigned)-1); } @@ -148,7 +126,7 @@ mu_store_needs_upgrade (MuStore *store) char* -mu_store_get_metadata (MuStore *store, const char *key) +mu_store_get_metadata (MuStore *store, const char *key, GError **err) { g_return_val_if_fail (store, NULL); g_return_val_if_fail (key, NULL); @@ -157,7 +135,7 @@ mu_store_get_metadata (MuStore *store, const char *key) const std::string val (store->db_read_only()->get_metadata (key)); return val.empty() ? NULL : g_strdup (val.c_str()); - } MU_XAPIAN_CATCH_BLOCK_RETURN(NULL); + } MU_XAPIAN_CATCH_BLOCK_G_ERROR_RETURN(err, MU_ERROR_XAPIAN, NULL); } @@ -171,7 +149,7 @@ mu_store_get_read_only_database (MuStore *store) gboolean -mu_store_contains_message (MuStore *store, const char* path) +mu_store_contains_message (MuStore *store, const char* path, GError **err) { g_return_val_if_fail (store, FALSE); g_return_val_if_fail (path, FALSE); @@ -180,13 +158,14 @@ mu_store_contains_message (MuStore *store, const char* path) const std::string uid (store->get_message_uid(path)); return store->db_read_only()->term_exists (uid) ? TRUE: FALSE; - } MU_XAPIAN_CATCH_BLOCK_RETURN (FALSE); + } MU_XAPIAN_CATCH_BLOCK_G_ERROR_RETURN(err, MU_ERROR_XAPIAN, FALSE); + } time_t -mu_store_get_timestamp (MuStore *store, const char* msgpath) +mu_store_get_timestamp (MuStore *store, const char* msgpath, GError **err) { char *stampstr; time_t rv; @@ -194,7 +173,7 @@ mu_store_get_timestamp (MuStore *store, const char* msgpath) g_return_val_if_fail (store, 0); g_return_val_if_fail (msgpath, 0); - stampstr = mu_store_get_metadata (store, msgpath); + stampstr = mu_store_get_metadata (store, msgpath, err); if (!stampstr) return (time_t)0; @@ -208,7 +187,7 @@ mu_store_get_timestamp (MuStore *store, const char* msgpath) MuError mu_store_foreach (MuStore *self, - MuStoreForeachFunc func, void *user_data) + MuStoreForeachFunc func, void *user_data, GError **err) { g_return_val_if_fail (self, MU_ERROR); g_return_val_if_fail (func, MU_ERROR); @@ -233,7 +212,8 @@ mu_store_foreach (MuStore *self, return res; } - } MU_XAPIAN_CATCH_BLOCK_RETURN (MU_ERROR); + } MU_XAPIAN_CATCH_BLOCK_G_ERROR_RETURN(err, MU_ERROR_XAPIAN, + MU_ERROR_XAPIAN); return MU_OK; } diff --git a/src/mu-store-write.cc b/src/mu-store-write.cc index 80cd6683..f1a22d01 100644 --- a/src/mu-store-write.cc +++ b/src/mu-store-write.cc @@ -66,26 +66,6 @@ _MuStore::rollback_transaction () { } - -/* close the old database, and write an empty one on top of it */ -void -_MuStore::clear () -{ - if (is_read_only()) - throw std::runtime_error ("database is read-only"); - - // clear the database - db_writable()->close (); - delete _db; - _db = new Xapian::WritableDatabase - (path(), Xapian::DB_CREATE_OR_OVERWRITE); - - // clear the contacts cache - if (_contacts) - mu_contacts_clear (_contacts); -} - - /* we cache these prefix strings, so we don't have to allocate the all * the time; this should save 10-20 string allocs per message */ G_GNUC_CONST static const std::string& @@ -143,25 +123,21 @@ add_synonyms (MuStore *store) MuStore* mu_store_new_writable (const char* xpath, const char *contacts_cache, - GError **err) + gboolean rebuild, GError **err) { g_return_val_if_fail (xpath, NULL); try { - MuStore *store; + try { + MuStore *store; + store = new _MuStore (xpath, contacts_cache, + rebuild ? true : false); + add_synonyms (store); + return store; - store = new _MuStore (xpath, contacts_cache, false); - add_synonyms (store); + } MU_STORE_CATCH_BLOCK_RETURN(err,NULL); - return store; - - } catch (const MuStoreError& merr) { - g_set_error (err, 0, merr.mu_error(), - "%s", merr.what().c_str()); - - } MU_XAPIAN_CATCH_BLOCK_G_ERROR(err,MU_ERROR_XAPIAN); - - return NULL; + } MU_XAPIAN_CATCH_BLOCK_G_ERROR_RETURN (err,MU_ERROR_XAPIAN, NULL); } @@ -175,30 +151,35 @@ mu_store_set_batch_size (MuStore *store, guint batchsize) gboolean -mu_store_set_metadata (MuStore *store, const char *key, const char *val) +mu_store_set_metadata (MuStore *store, const char *key, const char *val, + GError **err) { g_return_val_if_fail (store, FALSE); g_return_val_if_fail (key, FALSE); g_return_val_if_fail (val, FALSE); try { - store->db_writable()->set_metadata (key, val); - return TRUE; + try { + store->db_writable()->set_metadata (key, val); + return TRUE; + } MU_STORE_CATCH_BLOCK_RETURN(err, FALSE); - } MU_XAPIAN_CATCH_BLOCK; + } MU_XAPIAN_CATCH_BLOCK_G_ERROR_RETURN(err, MU_ERROR_XAPIAN, FALSE); - return FALSE; } gboolean -mu_store_clear (MuStore *store) +mu_store_clear (MuStore *store, GError **err) { g_return_val_if_fail (store, FALSE); try { - store->clear(); - return TRUE; + try { + store->clear(); + return TRUE; + + } MU_STORE_CATCH_BLOCK_RETURN(err, FALSE); } MU_XAPIAN_CATCH_BLOCK_RETURN(FALSE); } @@ -347,7 +328,7 @@ add_terms_values_str (Xapian::Document& doc, char *val, if (mu_msg_field_xapian_term(mfid)) doc.add_term (prefix(mfid) + - std::string(val, 0, MU_STORE_MAX_TERM_LENGTH)); + std::string(val, 0, _MuStore::MAX_TERM_LENGTH)); } @@ -427,15 +408,16 @@ each_part (MuMsg *msg, MuMsgPart *part, PartData *pdata) if (mu_msg_part_looks_like_attachment (part, TRUE) && (part->file_name)) { - char val[MU_STORE_MAX_TERM_LENGTH + 1]; + char val[MuStore::MAX_TERM_LENGTH + 1]; strncpy (val, part->file_name, sizeof(val)); /* now, let's create a terms... */ mu_str_normalize_in_place (val, TRUE); mu_str_ascii_xapian_escape_in_place (val); - pdata->_doc.add_term (prefix(pdata->_mfid) + - std::string(val, 0, MU_STORE_MAX_TERM_LENGTH)); + pdata->_doc.add_term + (prefix(pdata->_mfid) + + std::string(val, 0, MuStore::MAX_TERM_LENGTH)); } } @@ -563,8 +545,7 @@ each_contact_info (MuMsgContact *contact, MsgDoc *msgdoc) if (!mu_str_is_empty(contact->address)) { char *escaped = mu_str_ascii_xapian_escape (contact->address); msgdoc->_doc->add_term - (std::string (pfx + escaped, 0, - MU_STORE_MAX_TERM_LENGTH)); + (std::string (pfx + escaped, 0, MuStore::MAX_TERM_LENGTH)); g_free (escaped); /* store it also in our contacts cache */ @@ -625,6 +606,7 @@ mu_store_store_msg (MuStore *store, MuMsg *msg, gboolean replace) } +/* FIXME: use GError */ gboolean mu_store_store_path (MuStore *store, const char *path) { @@ -688,7 +670,7 @@ mu_store_remove_path (MuStore *store, const char *msgpath) gboolean mu_store_set_timestamp (MuStore *store, const char* msgpath, - time_t stamp) + time_t stamp, GError **err) { char buf[21]; @@ -696,7 +678,7 @@ mu_store_set_timestamp (MuStore *store, const char* msgpath, g_return_val_if_fail (msgpath, FALSE); sprintf (buf, "%" G_GUINT64_FORMAT, (guint64)stamp); - return mu_store_set_metadata (store, msgpath, buf); + return mu_store_set_metadata (store, msgpath, buf, err); } diff --git a/src/mu-store.h b/src/mu-store.h index b9fd5528..a7fdf0d1 100644 --- a/src/mu-store.h +++ b/src/mu-store.h @@ -42,7 +42,7 @@ typedef struct _MuStore MuStore; * of error; free with mu_store_unref */ MuStore* mu_store_new_writable (const char *xpath, const char *ccachepath, - GError **err) + gboolean rebuild, GError **err) G_GNUC_MALLOC G_GNUC_WARN_UNUSED_RESULT; @@ -117,7 +117,6 @@ typedef gpointer XapianDatabase; XapianDatabase* mu_store_get_read_only_database (MuStore *store); - /** * set the Xapian batch size for this store. Normally, there's no need * to use this function as the default is good enough; however, if you @@ -132,17 +131,16 @@ void mu_store_set_batch_size (MuStore *store, guint batchsize); - - /** * get the numbers of documents in the database * * @param index a valid MuStore instance + * @param err to receive error info or NULL. err->code is MuError value * - * @return the number of documents in the database; 0 in case of error - * or an empty database + * @return the number of documents in the database; (unsigned)-1 in + * case of error */ -unsigned mu_store_count (MuStore *store); +unsigned mu_store_count (MuStore *store, GError **err); /** * get a version string for the database; it's a const string, which @@ -210,10 +208,12 @@ gboolean mu_store_remove_path (MuStore *store, const char* msgpath); * * @param store a store * @param path the message path + * @param err to receive error info or NULL. err->code is MuError value * * @return TRUE if the message exists, FALSE otherwise */ -gboolean mu_store_contains_message (MuStore *store, const char* path); +gboolean mu_store_contains_message (MuStore *store, const char* path, + GError **err); /** * store a timestamp for a directory @@ -221,22 +221,24 @@ gboolean mu_store_contains_message (MuStore *store, const char* path); * @param store a valid store * @param msgpath path to a maildir * @param stamp a timestamp + * @param err to receive error info or NULL. err->code is MuError value * * @return TRUE if setting the timestamp succeeded, FALSE otherwise */ gboolean mu_store_set_timestamp (MuStore *store, const char* msgpath, - time_t stamp); + time_t stamp, GError **err); /** * get the timestamp for a directory * * @param store a valid store * @param msgpath path to a maildir + * @param err to receive error info or NULL. err->code is MuError value * * @return the timestamp, or 0 in case of error */ -time_t mu_store_get_timestamp (MuStore *store, - const char* msgpath); +time_t mu_store_get_timestamp (MuStore *store, const char* msgpath, + GError **err); /** @@ -256,6 +258,7 @@ gboolean mu_store_is_read_only (MuStore *store); * @param self a valid store * @param func a callback function to to call for each document * @param user_data a user pointer passed to the callback function + * @param err to receive error info or NULL. err->code is MuError value * * @return MU_OK if all went well, MU_STOP if the foreach was interrupted, * MU_ERROR in case of error @@ -263,7 +266,7 @@ gboolean mu_store_is_read_only (MuStore *store); typedef MuError (*MuStoreForeachFunc) (const char* path, void *user_data); MuError mu_store_foreach (MuStore *self, MuStoreForeachFunc func, - void *user_data); + void *user_data, GError **err); /** * set metadata for this MuStore @@ -271,21 +274,24 @@ MuError mu_store_foreach (MuStore *self, MuStoreForeachFunc func, * @param store a store * @param key metadata key * @param val metadata value + * @param err to receive error info or NULL. err->code is the MuError value * * @return TRUE if succeeded, FALSE otherwise */ -gboolean mu_store_set_metadata (MuStore *store, const char *key, const char *val); +gboolean mu_store_set_metadata (MuStore *store, const char *key, const char *val, + GError **err); /** * get metadata for this MuStore * * @param store a store * @param key the metadata key + * @param err to receive error info or NULL. err->code is MuError value * * @return the value of the metadata (gfree when done with it), or * NULL in case of error */ -char* mu_store_get_metadata (MuStore *store, const char *key) +char* mu_store_get_metadata (MuStore *store, const char *key, GError **err) G_GNUC_WARN_UNUSED_RESULT; @@ -312,18 +318,17 @@ gchar* mu_store_database_version (const gchar *xpath) G_GNUC_WARN_UNUSED_RESULT; */ gboolean mu_store_needs_upgrade (MuStore *store); - - /** * clear the database, ie., remove all of the contents. This is a * destructive operation, but the database can be restored be doing a * full scan of the maildirs. Also, clear the contacts cache file * * @param store a MuStore object + * @param err to receive error info or NULL. err->code is MuError value * * @return TRUE if the clearing succeeded, FALSE otherwise. */ -gboolean mu_store_clear (MuStore *store); +gboolean mu_store_clear (MuStore *store, GError **err); /**