2011-08-29 22:35:12 +02:00
|
|
|
/* -*-mode: c++; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8-*- */
|
|
|
|
/*
|
2012-06-11 23:11:14 +02:00
|
|
|
** Copyright (C) 2011-2012 <djcb@djcbsoftware.nl>
|
2011-08-29 22:35:12 +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_PRIV_HH__
|
|
|
|
#define __MU_STORE_PRIV_HH__
|
|
|
|
|
|
|
|
#if HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif /*HAVE_CONFIG_H*/
|
|
|
|
|
|
|
|
#include <cstdio>
|
|
|
|
#include <xapian.h>
|
|
|
|
#include <cstring>
|
|
|
|
#include <stdexcept>
|
|
|
|
|
|
|
|
#include "mu-store.h"
|
|
|
|
#include "mu-contacts.h"
|
2012-06-18 17:02:12 +02:00
|
|
|
#include "mu-str.h"
|
2011-08-30 21:02:28 +02:00
|
|
|
|
|
|
|
class MuStoreError {
|
|
|
|
public:
|
|
|
|
MuStoreError (MuError err, const std::string& what) :
|
|
|
|
_err (err), _what(what) {}
|
|
|
|
MuError mu_error () const { return _err; }
|
|
|
|
const std::string& what() const { return _what; }
|
|
|
|
private:
|
|
|
|
MuError _err;
|
|
|
|
const std::string _what;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2011-08-29 22:35:12 +02:00
|
|
|
struct _MuStore {
|
2011-09-03 09:45:14 +02:00
|
|
|
public:
|
|
|
|
/* create a read-write MuStore */
|
2012-06-18 17:02:12 +02:00
|
|
|
_MuStore (const char *path, const char *contacts_path,
|
2012-06-20 19:33:58 +02:00
|
|
|
bool rebuild) {
|
2011-08-29 22:35:12 +02:00
|
|
|
|
2012-06-20 19:33:58 +02:00
|
|
|
init (path, contacts_path, rebuild, false);
|
2011-08-29 22:35:12 +02:00
|
|
|
|
2011-09-03 09:45:14 +02:00
|
|
|
if (rebuild)
|
|
|
|
_db = new Xapian::WritableDatabase
|
|
|
|
(path, Xapian::DB_CREATE_OR_OVERWRITE);
|
2011-08-29 22:35:12 +02:00
|
|
|
else
|
2011-08-30 21:02:28 +02:00
|
|
|
_db = new Xapian::WritableDatabase
|
|
|
|
(path, Xapian::DB_CREATE_OR_OPEN);
|
|
|
|
|
2011-09-03 09:45:14 +02:00
|
|
|
check_set_version ();
|
2011-08-29 22:35:12 +02:00
|
|
|
|
2011-08-30 21:02:28 +02:00
|
|
|
if (contacts_path) {
|
|
|
|
_contacts = mu_contacts_new (contacts_path);
|
2012-06-18 17:02:12 +02:00
|
|
|
if (!_contacts)
|
2011-08-30 21:02:28 +02:00
|
|
|
throw MuStoreError (MU_ERROR_FILE,
|
|
|
|
("failed to init contacts cache"));
|
2011-08-29 22:35:12 +02:00
|
|
|
}
|
2011-09-03 09:45:14 +02:00
|
|
|
|
|
|
|
MU_WRITE_LOG ("%s: opened %s (batch size: %u) for read-write",
|
2012-03-13 22:06:17 +01:00
|
|
|
__FUNCTION__, this->path(), (unsigned)batch_size());
|
2011-08-29 22:35:12 +02:00
|
|
|
}
|
|
|
|
|
2011-09-03 09:45:14 +02:00
|
|
|
/* create a read-only MuStore */
|
|
|
|
_MuStore (const char *path) {
|
|
|
|
|
2012-06-20 19:33:58 +02:00
|
|
|
init (path, NULL, false, false);
|
2011-09-03 09:45:14 +02:00
|
|
|
_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());
|
|
|
|
}
|
|
|
|
|
2012-06-19 16:59:16 +02:00
|
|
|
void init (const char *path, const char *contacts_path,
|
2011-09-03 09:45:14 +02:00
|
|
|
bool rebuild, bool read_only) {
|
|
|
|
|
2012-06-20 19:33:58 +02:00
|
|
|
_my_addresses = NULL;
|
2011-09-03 09:45:14 +02:00
|
|
|
_batch_size = DEFAULT_BATCH_SIZE;
|
|
|
|
_contacts = 0;
|
|
|
|
_in_transaction = false;
|
|
|
|
_path = path;
|
|
|
|
_processed = 0;
|
|
|
|
_read_only = read_only;
|
|
|
|
_ref_count = 1;
|
|
|
|
_version = NULL;
|
2012-06-19 16:59:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void set_my_addresses (const char **my_addresses) {
|
|
|
|
|
|
|
|
if (_my_addresses) {
|
|
|
|
mu_str_free_list (_my_addresses);
|
|
|
|
_my_addresses = NULL;
|
|
|
|
}
|
|
|
|
|
2012-06-18 17:02:12 +02:00
|
|
|
while (my_addresses && *my_addresses) {
|
|
|
|
_my_addresses = g_slist_prepend
|
|
|
|
(_my_addresses, g_strdup (*my_addresses));
|
|
|
|
++my_addresses;
|
|
|
|
}
|
2011-09-03 09:45:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2011-08-29 22:35:12 +02:00
|
|
|
~_MuStore () {
|
|
|
|
try {
|
2011-08-30 21:02:28 +02:00
|
|
|
if (_ref_count != 0)
|
|
|
|
g_warning ("ref count != 0");
|
|
|
|
|
2011-08-29 22:35:12 +02:00
|
|
|
g_free (_version);
|
|
|
|
|
|
|
|
mu_contacts_destroy (_contacts);
|
|
|
|
if (!_read_only)
|
|
|
|
mu_store_flush (this);
|
|
|
|
|
2012-06-18 17:02:12 +02:00
|
|
|
mu_str_free_list (_my_addresses);
|
|
|
|
|
2011-09-03 09:45:14 +02:00
|
|
|
MU_WRITE_LOG ("closing xapian database with %d document(s)",
|
2011-08-29 22:35:12 +02:00
|
|
|
(int)db_read_only()->get_doccount());
|
|
|
|
delete _db;
|
|
|
|
|
|
|
|
} MU_XAPIAN_CATCH_BLOCK;
|
|
|
|
}
|
|
|
|
|
2011-08-30 21:02:28 +02:00
|
|
|
/* close the old database, and write an empty one on top of it */
|
2011-09-03 09:45:14 +02:00
|
|
|
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);
|
|
|
|
}
|
2011-08-29 22:35:12 +02:00
|
|
|
|
|
|
|
/* get a unique id for this message; note, this function returns a
|
2011-08-30 21:02:28 +02:00
|
|
|
* static buffer -- not reentrant */
|
2011-09-18 13:42:10 +02:00
|
|
|
const char *get_uid_term (const char *path);
|
2011-08-29 22:35:12 +02:00
|
|
|
|
|
|
|
MuContacts* contacts() { return _contacts; }
|
|
|
|
|
|
|
|
const char* version () {
|
|
|
|
g_free (_version);
|
2011-09-03 09:45:14 +02:00
|
|
|
return _version = mu_store_get_metadata (this, MU_STORE_VERSION_KEY,
|
|
|
|
NULL);
|
2011-08-29 22:35:12 +02:00
|
|
|
}
|
|
|
|
|
2011-09-03 09:45:14 +02:00
|
|
|
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; }
|
|
|
|
|
2011-08-30 21:02:28 +02:00
|
|
|
void begin_transaction ();
|
|
|
|
void commit_transaction ();
|
|
|
|
void rollback_transaction ();
|
2011-08-29 22:35:12 +02:00
|
|
|
|
|
|
|
Xapian::WritableDatabase* db_writable() {
|
2011-08-30 21:02:28 +02:00
|
|
|
if (G_UNLIKELY(is_read_only()))
|
|
|
|
throw std::runtime_error ("database is read-only");
|
2011-08-29 22:35:12 +02:00
|
|
|
return (Xapian::WritableDatabase*)_db;
|
|
|
|
}
|
|
|
|
|
2011-08-30 21:02:28 +02:00
|
|
|
Xapian::Database* db_read_only() const { return _db; }
|
|
|
|
|
2011-09-03 09:45:14 +02:00
|
|
|
const char* path () const { return _path.c_str(); }
|
2011-08-30 21:02:28 +02:00
|
|
|
bool is_read_only () const { return _read_only; }
|
2011-08-29 22:35:12 +02:00
|
|
|
|
|
|
|
size_t batch_size () const { return _batch_size;}
|
|
|
|
size_t set_batch_size (size_t n) {
|
2011-09-03 09:45:14 +02:00
|
|
|
return _batch_size = ( n == 0) ? DEFAULT_BATCH_SIZE : n;
|
2011-08-29 22:35:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool in_transaction () const { return _in_transaction; }
|
2011-10-10 07:37:37 +02:00
|
|
|
bool in_transaction (bool in_tx) { return _in_transaction = in_tx; }
|
2011-08-29 22:35:12 +02:00
|
|
|
|
|
|
|
int processed () const { return _processed; }
|
|
|
|
int set_processed (int n) { return _processed = n;}
|
|
|
|
int inc_processed () { return ++_processed; }
|
|
|
|
|
2011-08-30 21:02:28 +02:00
|
|
|
/* MuStore is ref-counted */
|
|
|
|
guint ref () { return ++_ref_count; }
|
2011-09-18 13:42:10 +02:00
|
|
|
guint unref () {
|
|
|
|
if (_ref_count < 1)
|
|
|
|
g_critical ("ref count error");
|
|
|
|
return --_ref_count;
|
|
|
|
}
|
2011-08-29 22:35:12 +02:00
|
|
|
|
2012-06-18 17:02:12 +02:00
|
|
|
GSList *my_addresses () { return _my_addresses; }
|
|
|
|
|
2011-09-03 09:45:14 +02:00
|
|
|
/* 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;
|
2011-08-29 22:35:12 +02:00
|
|
|
|
2011-09-03 09:45:14 +02:00
|
|
|
private:
|
2011-08-29 22:35:12 +02:00
|
|
|
/* transaction handling */
|
|
|
|
bool _in_transaction;
|
|
|
|
int _processed;
|
|
|
|
size_t _batch_size; /* batch size of a xapian transaction */
|
|
|
|
|
|
|
|
/* contacts object to cache all the contact information */
|
|
|
|
MuContacts *_contacts;
|
2011-08-30 21:02:28 +02:00
|
|
|
|
2011-09-03 09:45:14 +02:00
|
|
|
std::string _path;
|
|
|
|
gchar *_version;
|
2011-08-29 22:35:12 +02:00
|
|
|
|
|
|
|
Xapian::Database *_db;
|
|
|
|
bool _read_only;
|
2011-08-30 21:02:28 +02:00
|
|
|
guint _ref_count;
|
2012-06-18 17:02:12 +02:00
|
|
|
|
|
|
|
GSList *_my_addresses;
|
2011-08-29 22:35:12 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
#endif /*__MU_STORE_PRIV_HH__*/
|