2019-07-31 16:19:23 +02:00
|
|
|
/*
|
2020-02-03 16:38:12 +01:00
|
|
|
** Copyright (C) 2020 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
|
2019-07-31 16:19:23 +02:00
|
|
|
**
|
|
|
|
** This program is free software; you can redistribute it and/or modify it
|
|
|
|
** under the terms of the GNU General Public License as published by the
|
|
|
|
** Free Software Foundation; either version 3, or (at your option) any
|
|
|
|
** later version.
|
|
|
|
**
|
|
|
|
** This program is distributed in the hope that it will be useful,
|
|
|
|
** but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
** GNU General Public License for more details.
|
|
|
|
**
|
|
|
|
** You should have received a copy of the GNU General Public License
|
|
|
|
** along with this program; if not, write to the Free Software Foundation,
|
|
|
|
** Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
**
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __MU_STORE_HH__
|
|
|
|
#define __MU_STORE_HH__
|
|
|
|
|
2020-02-10 00:00:42 +01:00
|
|
|
#include <mu-msg.h>
|
|
|
|
|
2019-07-31 16:19:23 +02:00
|
|
|
#ifdef __cplusplus
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2020-06-27 10:47:34 +02:00
|
|
|
#include <mutex>
|
2019-07-31 16:19:23 +02:00
|
|
|
#include <ctime>
|
|
|
|
|
2020-06-27 10:47:34 +02:00
|
|
|
|
|
|
|
#include "mu-contacts.hh"
|
|
|
|
#include <xapian.h>
|
|
|
|
|
2020-02-03 16:38:12 +01:00
|
|
|
#include <utils/mu-utils.hh>
|
2020-06-27 10:47:34 +02:00
|
|
|
#include <index/mu-indexer.hh>
|
2020-02-03 16:38:12 +01:00
|
|
|
|
2019-07-31 16:19:23 +02:00
|
|
|
namespace Mu {
|
|
|
|
|
|
|
|
class Store {
|
|
|
|
public:
|
2020-06-27 10:47:34 +02:00
|
|
|
using Id = unsigned; /**< Id for a message in the store (internally,
|
|
|
|
* corresponds to a Xapian document-id) */
|
|
|
|
static constexpr Id InvalidId = 0; /**< Invalid store id */
|
|
|
|
|
2019-07-31 16:19:23 +02:00
|
|
|
/**
|
|
|
|
* Construct a store for an existing document database
|
|
|
|
*
|
|
|
|
* @param path path to the database
|
|
|
|
* @param readonly whether to open the database in read-only mode
|
|
|
|
*/
|
|
|
|
Store (const std::string& path, bool readonly=true);
|
|
|
|
|
2020-06-27 10:47:34 +02:00
|
|
|
|
|
|
|
struct Config {
|
|
|
|
size_t max_message_size{};
|
|
|
|
/**< maximum size (in bytes) for a message, or 0 for default */
|
|
|
|
size_t batch_size{};
|
|
|
|
/**< size of batches before committing, or 0 for default */
|
|
|
|
};
|
|
|
|
|
2019-07-31 16:19:23 +02:00
|
|
|
/**
|
|
|
|
* Construct a store for a not-yet-existing document database
|
|
|
|
*
|
2019-11-06 16:13:39 +01:00
|
|
|
* @param path path to the database
|
2019-07-31 16:19:23 +02:00
|
|
|
* @param maildir maildir to use for this store
|
2020-06-27 10:47:34 +02:00
|
|
|
* @param personal_addresses addresses that should be recognized as
|
2020-02-03 16:38:12 +01:00
|
|
|
* 'personal' for identifying personal messages.
|
2019-07-31 16:19:23 +02:00
|
|
|
*/
|
2020-02-03 16:38:12 +01:00
|
|
|
Store (const std::string& path, const std::string& maildir,
|
2020-06-27 10:47:34 +02:00
|
|
|
const StringVec& personal_addresses, const Config& conf);
|
2019-07-31 16:19:23 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* DTOR
|
|
|
|
*/
|
|
|
|
~Store();
|
|
|
|
|
2020-06-27 10:47:34 +02:00
|
|
|
struct Metadata {
|
|
|
|
std::string database_path; /**< Full path to the Xapian database */
|
|
|
|
std::string schema_version; /**< Database schema version */
|
|
|
|
std::time_t created; /**< database creation time */
|
2019-07-31 16:19:23 +02:00
|
|
|
|
2020-06-27 10:47:34 +02:00
|
|
|
bool read_only; /**< Is the database opened read-only? */
|
|
|
|
size_t batch_size; /**< Maximum database tranasction batch size */
|
2019-07-31 16:19:23 +02:00
|
|
|
|
2020-06-27 10:47:34 +02:00
|
|
|
std::string root_maildir; /**< Absolute path to the top-level maildir */
|
|
|
|
|
|
|
|
StringVec personal_addresses; /**< Personal e-mail addresses */
|
|
|
|
size_t max_message_size; /**< Maximus allowed message size */
|
|
|
|
};
|
2019-07-31 16:19:23 +02:00
|
|
|
|
|
|
|
/**
|
2020-06-27 10:47:34 +02:00
|
|
|
* Get metadata about this store.
|
2019-07-31 16:19:23 +02:00
|
|
|
*
|
2020-06-27 10:47:34 +02:00
|
|
|
* @return the metadata
|
2019-07-31 16:19:23 +02:00
|
|
|
*/
|
2020-06-27 10:47:34 +02:00
|
|
|
const Metadata& metadata() const;
|
2019-07-31 16:19:23 +02:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
2020-06-27 10:47:34 +02:00
|
|
|
* Get the Contacts object for this store
|
2019-07-31 16:19:23 +02:00
|
|
|
*
|
2020-06-27 10:47:34 +02:00
|
|
|
* @return the Contacts object
|
2019-07-31 16:19:23 +02:00
|
|
|
*/
|
2020-06-27 10:47:34 +02:00
|
|
|
const Contacts& contacts() const;
|
2019-07-31 16:19:23 +02:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
2020-06-27 10:47:34 +02:00
|
|
|
* Get the Indexer associated with this store. It is an error
|
|
|
|
* to call this on a read-only store.
|
2019-07-31 16:19:23 +02:00
|
|
|
*
|
2020-06-27 10:47:34 +02:00
|
|
|
* @return the indexer.
|
2019-07-31 16:19:23 +02:00
|
|
|
*/
|
2020-06-27 10:47:34 +02:00
|
|
|
Indexer& indexer();
|
2019-07-31 16:19:23 +02:00
|
|
|
|
|
|
|
/**
|
2020-01-30 23:20:34 +01:00
|
|
|
* Add a message to the store.
|
2019-07-31 16:19:23 +02:00
|
|
|
*
|
2020-01-30 23:20:34 +01:00
|
|
|
* @param path the message path.
|
|
|
|
*
|
|
|
|
* @return the doc id of the added message
|
2019-07-31 16:19:23 +02:00
|
|
|
*/
|
2020-06-27 10:47:34 +02:00
|
|
|
Id add_message (const std::string& path);
|
2019-07-31 16:19:23 +02:00
|
|
|
|
2020-06-10 08:04:47 +02:00
|
|
|
/**
|
|
|
|
* Update a message in the store.
|
|
|
|
*
|
|
|
|
* @param msg a message
|
2020-06-27 10:47:34 +02:00
|
|
|
* @param id the id for this message
|
2020-06-10 08:04:47 +02:00
|
|
|
*
|
|
|
|
* @return false in case of failure; true ottherwise.
|
|
|
|
*/
|
2020-06-27 10:47:34 +02:00
|
|
|
bool update_message (MuMsg *msg, Id id);
|
2020-06-10 08:04:47 +02:00
|
|
|
|
2019-07-31 16:19:23 +02:00
|
|
|
/**
|
2020-06-27 10:47:34 +02:00
|
|
|
* Remove a message from the store. It will _not_ remove the message
|
|
|
|
* fromt he file system.
|
2019-07-31 16:19:23 +02:00
|
|
|
*
|
2020-01-30 23:20:34 +01:00
|
|
|
* @param path the message path.
|
|
|
|
*
|
|
|
|
* @return true if removing happened; false otherwise.
|
2019-07-31 16:19:23 +02:00
|
|
|
*/
|
2020-01-30 23:20:34 +01:00
|
|
|
bool remove_message (const std::string& path);
|
|
|
|
|
2020-02-10 00:00:42 +01:00
|
|
|
/**
|
2020-06-27 10:47:34 +02:00
|
|
|
* Remove a number if messages from the store. It will _not_ remove the
|
|
|
|
* message fromt he file system.
|
2020-02-10 00:00:42 +01:00
|
|
|
*
|
2020-06-27 10:47:34 +02:00
|
|
|
* @param ids vector with store ids for the message
|
|
|
|
*/
|
|
|
|
void remove_messages (const std::vector<Id>& ids);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove a message from the store. It will _not_ remove the message
|
|
|
|
* fromt he file system.
|
|
|
|
*
|
|
|
|
* @param id the store id for the message
|
|
|
|
*/
|
|
|
|
void remove_message (Id id) { remove_messages({id}); }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find message in the store.
|
|
|
|
*
|
|
|
|
* @param id doc id for the message to find
|
2020-02-10 00:00:42 +01:00
|
|
|
*
|
|
|
|
* @return a message (owned by caller), or nullptr
|
|
|
|
*/
|
2020-06-27 10:47:34 +02:00
|
|
|
MuMsg* find_message (Id id) const;
|
2019-07-31 16:19:23 +02:00
|
|
|
|
|
|
|
/**
|
2020-01-30 23:20:34 +01:00
|
|
|
* does a certain message exist in the store already?
|
|
|
|
*
|
|
|
|
* @param path the message path
|
|
|
|
*
|
|
|
|
* @return true if the message exists in the store, false otherwise
|
|
|
|
*/
|
|
|
|
bool contains_message (const std::string& path) const;
|
|
|
|
|
2020-06-27 10:47:34 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Prototype for the ForEachFunc
|
|
|
|
*
|
|
|
|
* @param id :t store Id for the message
|
|
|
|
* @param path: the absolute path to the message
|
|
|
|
*
|
|
|
|
* @return true if for_each should continue; false to quit
|
|
|
|
*/
|
|
|
|
using ForEachFunc = std::function<bool(Id, const std::string&)>;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Call @param func for each document in the store. This takes a lock on
|
|
|
|
* the store, so the func should _not_ call any other Store:: methods.
|
|
|
|
*
|
|
|
|
* @param func a functio
|
|
|
|
*
|
|
|
|
* @return the number of times func was invoked
|
|
|
|
*/
|
|
|
|
size_t for_each (ForEachFunc func);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the timestamp for some message, or 0 if not found
|
|
|
|
*
|
|
|
|
* @param path the path
|
|
|
|
*
|
|
|
|
* @return the timestamp, or 0 if not found
|
|
|
|
*/
|
|
|
|
time_t message_tstamp (const std::string& path) const;
|
|
|
|
|
2020-01-30 23:20:34 +01:00
|
|
|
/**
|
|
|
|
* Get the timestamp for some directory
|
2019-07-31 16:19:23 +02:00
|
|
|
*
|
|
|
|
* @param path the path
|
|
|
|
*
|
|
|
|
* @return the timestamp, or 0 if not found
|
|
|
|
*/
|
2020-01-30 23:20:34 +01:00
|
|
|
time_t dirstamp (const std::string& path) const;
|
2019-07-31 16:19:23 +02:00
|
|
|
|
|
|
|
/**
|
2020-01-30 23:20:34 +01:00
|
|
|
* Set the timestamp for some directory
|
2019-07-31 16:19:23 +02:00
|
|
|
*
|
|
|
|
* @param path a filesystem path
|
|
|
|
* @param tstamp the timestamp for that path
|
|
|
|
*/
|
2020-01-30 23:20:34 +01:00
|
|
|
void set_dirstamp (const std::string& path, time_t tstamp);
|
2019-07-31 16:19:23 +02:00
|
|
|
|
2020-01-30 23:20:34 +01:00
|
|
|
/**
|
|
|
|
* Get the number of documents in the document database
|
|
|
|
*
|
|
|
|
* @return the number
|
|
|
|
*/
|
|
|
|
std::size_t size() const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Is the database empty?
|
|
|
|
*
|
|
|
|
* @return true or false
|
|
|
|
*/
|
|
|
|
bool empty() const;
|
2019-07-31 16:19:23 +02:00
|
|
|
|
|
|
|
/**
|
2020-06-27 10:47:34 +02:00
|
|
|
* Commit the current group of modifcations (i.e., transaction) to disk;
|
|
|
|
* This rarely needs to be called explicitly, as Store will take care of
|
|
|
|
* it.
|
2019-07-31 16:19:23 +02:00
|
|
|
*/
|
2020-06-27 10:47:34 +02:00
|
|
|
void commit();
|
2019-07-31 16:19:23 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a reference to the private data. For internal use.
|
|
|
|
*
|
|
|
|
* @return private reference.
|
|
|
|
*/
|
2020-06-27 10:47:34 +02:00
|
|
|
struct Private;
|
2019-07-31 16:19:23 +02:00
|
|
|
std::unique_ptr<Private>& priv() { return priv_; }
|
|
|
|
const std::unique_ptr<Private>& priv() const { return priv_; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::unique_ptr<Private> priv_;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace Mu
|
|
|
|
|
|
|
|
|
|
|
|
#endif /*__cplusplus*/
|
|
|
|
|
|
|
|
#include <glib.h>
|
|
|
|
#include <inttypes.h>
|
2019-12-16 20:44:35 +01:00
|
|
|
#include <utils/mu-util.h>
|
2019-07-31 16:19:23 +02:00
|
|
|
#include <mu-contacts.hh>
|
|
|
|
|
|
|
|
G_BEGIN_DECLS
|
|
|
|
|
|
|
|
struct MuStore_;
|
|
|
|
typedef struct MuStore_ MuStore;
|
|
|
|
|
|
|
|
/* http://article.gmane.org/gmane.comp.search.xapian.general/3656 */
|
|
|
|
#define MU_STORE_MAX_TERM_LENGTH (240)
|
|
|
|
|
|
|
|
/**
|
|
|
|
* create a new read-only Xapian store, for querying documents
|
|
|
|
*
|
|
|
|
* @param path the path to the database
|
|
|
|
* @param err to receive error info or NULL. err->code is MuError value
|
|
|
|
*
|
|
|
|
* @return a new MuStore object with ref count == 1, or NULL in case of error;
|
|
|
|
* free with mu_store_unref
|
|
|
|
*/
|
|
|
|
MuStore* mu_store_new_readable (const char* xpath, GError **err)
|
|
|
|
G_GNUC_MALLOC G_GNUC_WARN_UNUSED_RESULT;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* increase the reference count for this store with 1
|
|
|
|
*
|
|
|
|
* @param store a valid store object
|
|
|
|
*
|
|
|
|
* @return the same store with increased ref count, or NULL in case of
|
|
|
|
* error
|
|
|
|
*/
|
|
|
|
MuStore* mu_store_ref (MuStore *store);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* decrease the reference count for this store with 1
|
|
|
|
*
|
|
|
|
* @param store a valid store object
|
|
|
|
*
|
|
|
|
* @return NULL
|
|
|
|
*/
|
|
|
|
MuStore* mu_store_unref (MuStore *store);
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* we need this when using Xapian::(Writable)Database* from C
|
|
|
|
*/
|
|
|
|
typedef gpointer XapianDatabase;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get the underlying read-only database object for this store; not that this
|
|
|
|
* pointer becomes in valid after mu_store_destroy
|
|
|
|
*
|
|
|
|
* @param store a valid store
|
|
|
|
*
|
|
|
|
* @return a Xapian::Database (you'll need to cast in C++), or
|
|
|
|
* NULL in case of error.
|
|
|
|
*/
|
|
|
|
XapianDatabase* mu_store_get_read_only_database (MuStore *store);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get the version of the xapian database (ie., the version of the
|
|
|
|
* 'schema' we are using). If this version != MU_STORE_SCHEMA_VERSION,
|
|
|
|
* it's means we need to a full reindex.
|
|
|
|
*
|
|
|
|
* @param store the store to inspect
|
|
|
|
*
|
|
|
|
* @return the version of the database as a newly allocated string
|
|
|
|
* (free with g_free); if there is no version yet, it will return NULL
|
|
|
|
*/
|
|
|
|
const char* mu_store_schema_version (const MuStore* store);
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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; (unsigned)-1 in
|
|
|
|
* case of error
|
|
|
|
*/
|
|
|
|
unsigned mu_store_count (const MuStore *store, GError **err);
|
|
|
|
|
|
|
|
#define MU_STORE_INVALID_DOCID 0
|
|
|
|
|
|
|
|
G_END_DECLS
|
|
|
|
|
|
|
|
#endif /* __MU_STORE_HH__ */
|