* rename mu-util-xapian => mu-util-db (and some leftovers)

This commit is contained in:
djcb 2010-08-25 21:40:07 +03:00
parent b50a3dc245
commit ba2cb41585
10 changed files with 579 additions and 35 deletions

View File

@ -85,8 +85,8 @@ libmu_la_SOURCES= \
mu-result.h \
mu-store-xapian.cc \
mu-store-xapian.h \
mu-util-xapian.cc \
mu-util-xapian.h \
mu-util-db.cc \
mu-util-db.h \
mu-util.c \
mu-util.h

View File

@ -36,7 +36,7 @@
#include "mu-msg-str.h"
#include "mu-util.h"
#include "mu-util-xapian.h"
#include "mu-util-db.h"
#include "mu-cmd.h"
@ -329,13 +329,13 @@ mu_cmd_find (MuConfigOptions *opts)
if (!query_params_valid (opts))
return FALSE;
if (mu_util_xapian_db_is_empty (opts->xpath)) {
if (mu_util_db_is_empty (opts->xpath)) {
g_printerr ("The database is empty; "
"use 'mu index' to add some messages\n");
return FALSE;
}
if (!mu_util_xapian_db_version_up_to_date (opts->xpath)) {
if (!mu_util_db_version_up_to_date (opts->xpath)) {
update_warning ();
return FALSE;
}

View File

@ -25,7 +25,7 @@
#include <signal.h>
#include "mu-util.h"
#include "mu-util-xapian.h"
#include "mu-util-db.h"
#include "mu-msg.h"
@ -131,24 +131,24 @@ index_msg_cb (MuIndexStats* stats, void *user_data)
static gboolean
database_version_check_and_update (MuConfigOptions *opts)
{
if (mu_util_xapian_db_is_empty (opts->xpath))
if (mu_util_db_is_empty (opts->xpath))
return TRUE;
/* we empty the database before doing anything */
if (opts->rebuild) {
opts->reindex = TRUE;
g_message ("Clearing database %s", opts->xpath);
return mu_util_xapian_clear_database (opts->xpath);
return mu_util_clear_database (opts->xpath);
}
if (mu_util_xapian_db_version_up_to_date (opts->xpath))
if (mu_util_db_version_up_to_date (opts->xpath))
return TRUE; /* ok, nothing to do */
/* ok, database is not up to date */
if (opts->autoupgrade) {
opts->reindex = TRUE;
g_message ("Auto-upgrade: clearing old database first");
return mu_util_xapian_clear_database (opts->xpath);
return mu_util_clear_database (opts->xpath);
}
update_warning ();

View File

@ -1,5 +1,5 @@
/*
** Copyright (C) 2010 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
** Copyright (C) 2008-2010 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
**
** This program is free software; you can redistribute it and/or modify
1** it under the terms of the GNU General Public License as published by
@ -30,7 +30,7 @@
#include "mu-index.h"
#include "mu-store-xapian.h"
#include "mu-util.h"
#include "mu-util-xapian.h"
#include "mu-util-db.h"
struct _MuIndex {
MuStoreXapian *_xapian;
@ -57,11 +57,11 @@ mu_index_new (const char *xpath)
/* see we need to reindex the database; note, there is a small race-condition
* here, between mu_index_new and mu_index_run. Maybe do the check in
* mu_index_run instead? */
if (mu_util_xapian_db_is_empty (xpath))
if (mu_util_db_is_empty (xpath))
index->_needs_reindex = FALSE;
else {
index->_needs_reindex =
mu_util_xapian_db_version_up_to_date (xpath) ? FALSE : TRUE;
mu_util_db_version_up_to_date (xpath) ? FALSE : TRUE;
}
return index;

38
src/mu-msg-iter-priv.hh Normal file
View File

@ -0,0 +1,38 @@
/*
** Copyright (C) 2008-2010 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
**
** 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 of the License, 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_MSG_PRIV_HH__
#define __MU_MSG_PRIV_HH__
#include <xapian.h>
/**
* create a new MuMsgIter -- basically, an iterator over the search
* results
*
* @param enq a Xapian::Enquiry providing access to search results
* @param batchsize how many results to retrieve at once
*
* @return a new MuMsgIter, or NULL in case of error
*/
MuMsgIter *mu_msg_iter_new (const Xapian::Enquire& enq,
size_t batchsize) G_GNUC_WARN_UNUSED_RESULT;
#endif /*__MU_MSG_PRIV_HH__*/

305
src/mu-msg-iter.cc Normal file
View File

@ -0,0 +1,305 @@
/*
** Copyright (C) 2008-2010 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
**
** 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 of the License, 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.
**
*/
#include <stdlib.h>
#include <iostream>
#include <string.h>
#include <errno.h>
#include "xapian.h"
#include "mu-util.h"
#include "mu-msg-iter.h"
#include "mu-msg-iter-priv.hh"
struct _MuMsgIter {
Xapian::Enquire *_enq;
Xapian::MSet _matches;
Xapian::MSet::const_iterator _cursor;
size_t _batchsize;
size_t _offset;
char* _str[MU_MSG_FIELD_ID_NUM];
bool _is_null;
};
MuMsgIter*
mu_msg_iter_new (const Xapian::Enquire& enq, size_t batchsize)
{
MuMsgIter *iter;
try {
iter = new MuMsgIter;
memset (iter->_str, 0, sizeof(iter->_str));
iter->_enq = new Xapian::Enquire(enq);
iter->_matches = iter->_enq->get_mset (0, batchsize);
if (!iter->_matches.empty()) {
iter->_cursor = iter->_matches.begin();
iter->_is_null = false;
} else
iter->_is_null = true;
iter->_batchsize = batchsize;
iter->_offset = 0;
return iter;
} MU_XAPIAN_CATCH_BLOCK_RETURN(NULL);
}
void
mu_msg_iter_destroy (MuMsgIter *iter)
{
if (iter) {
for (int i = 0; i != MU_MSG_FIELD_ID_NUM; ++i)
g_free (iter->_str[i]);
try {
delete iter->_enq;
delete iter;
} MU_XAPIAN_CATCH_BLOCK;
}
}
MuMsg*
mu_msg_iter_get_msg (MuMsgIter *iter)
{
const char *path;
MuMsg *msg;
g_return_val_if_fail (iter, NULL);
path = mu_msg_iter_get_path (iter);
if (!path) {
g_warning ("%s: no path for message", __FUNCTION__);
return NULL;
}
msg = mu_msg_new (path, NULL);
if (!msg) {
g_warning ("%s: failed to create msg object", __FUNCTION__);
return NULL;
}
return msg;
}
static gboolean
message_is_readable (MuMsgIter *iter)
{
Xapian::Document doc (iter->_cursor.get_document());
const std::string path(doc.get_value(MU_MSG_FIELD_ID_PATH));
if (access (path.c_str(), R_OK) != 0) {
g_debug ("cannot read %s: %s", path.c_str(),
strerror(errno));
return FALSE;
}
return TRUE;
}
static MuMsgIter*
get_next_batch (MuMsgIter *iter)
{
iter->_matches = iter->_enq->get_mset (iter->_offset,
iter->_batchsize);
if (iter->_matches.empty()) {
iter->_cursor = iter->_matches.end();
iter->_is_null = true;
} else {
iter->_cursor = iter->_matches.begin();
iter->_is_null = false;
}
return iter;
}
gboolean
mu_msg_iter_next (MuMsgIter *iter)
{
g_return_val_if_fail (iter, FALSE);
try {
++iter->_offset;
if (++iter->_cursor == iter->_matches.end())
iter = get_next_batch (iter);
if (iter->_cursor == iter->_matches.end())
return FALSE; /* no more matches */
/* the message may not be readable / existing, e.g.,
* because of the database not being fully up to
* date. in that case, we ignore the message. it
* might be nice to auto-delete these messages from
* the db, but that would might screw up the
* search; also, we only have read-only access to
* the db here */
if (!message_is_readable (iter))
return mu_msg_iter_next (iter);
for (int i = 0; i != MU_MSG_FIELD_ID_NUM; ++i) {
g_free (iter->_str[i]);
iter->_str[i] = NULL;
}
return TRUE;
} MU_XAPIAN_CATCH_BLOCK_RETURN(FALSE);
}
gboolean
mu_msg_iter_is_null (MuMsgIter *iter)
{
g_return_val_if_fail (iter, TRUE);
return iter->_is_null;
}
const gchar*
mu_msg_iter_get_field (MuMsgIter *iter, const MuMsgField *field)
{
g_return_val_if_fail (iter, NULL);
g_return_val_if_fail (!mu_msg_iter_is_null(iter), NULL);
g_return_val_if_fail (field, NULL);
try {
MuMsgFieldId id;
id = mu_msg_field_id (field);
if (!iter->_str[id]) { /* cache the value */
Xapian::Document doc (iter->_cursor.get_document());
iter->_str[id] = g_strdup (doc.get_value(id).c_str());
}
return iter->_str[id];
} MU_XAPIAN_CATCH_BLOCK_RETURN(NULL);
}
gint64
mu_msg_iter_get_field_numeric (MuMsgIter *iter,
const MuMsgField *field)
{
g_return_val_if_fail (mu_msg_field_is_numeric(field), -1);
try {
return static_cast<gint64>(
Xapian::sortable_unserialise(
mu_msg_iter_get_field(iter, field)));
} MU_XAPIAN_CATCH_BLOCK_RETURN(static_cast<gint64>(-1));
}
static const gchar*
get_field (MuMsgIter *iter, MuMsgFieldId id)
{
return mu_msg_iter_get_field(iter, mu_msg_field_from_id (id));
}
static long
get_field_number (MuMsgIter *iter, MuMsgFieldId id)
{
const char* str = get_field (iter, id);
return str ? atol (str) : 0;
}
/* hmmm.... is it impossible to get a 0 docid, or just very improbable? */
unsigned int
mu_msg_iter_get_docid (MuMsgIter *iter)
{
g_return_val_if_fail (iter, 0);
try {
return iter->_cursor.get_document().get_docid();
} MU_XAPIAN_CATCH_BLOCK_RETURN (0);
}
const char*
mu_msg_iter_get_path (MuMsgIter *iter)
{
return get_field (iter, MU_MSG_FIELD_ID_PATH);
}
const char*
mu_msg_iter_get_from (MuMsgIter *iter)
{
return get_field (iter, MU_MSG_FIELD_ID_FROM);
}
const char*
mu_msg_iter_get_to (MuMsgIter *iter)
{
return get_field (iter, MU_MSG_FIELD_ID_TO);
}
const char*
mu_msg_iter_get_cc (MuMsgIter *iter)
{
return get_field (iter, MU_MSG_FIELD_ID_CC);
}
const char*
mu_msg_iter_get_subject (MuMsgIter *iter)
{
return get_field (iter, MU_MSG_FIELD_ID_SUBJECT);
}
size_t
mu_msg_iter_get_size (MuMsgIter *iter)
{
return (size_t) get_field_number (iter, MU_MSG_FIELD_ID_SIZE);
}
time_t
mu_msg_iter_get_date (MuMsgIter *iter)
{
return (size_t) get_field_number (iter, MU_MSG_FIELD_ID_DATE);
}
MuMsgFlags
mu_msg_iter_get_flags (MuMsgIter *iter)
{
return (MuMsgFlags) get_field_number (iter, MU_MSG_FIELD_ID_FLAGS);
}
MuMsgPrio
mu_msg_iter_get_prio (MuMsgIter *iter)
{
return (MuMsgPrio) get_field_number (iter, MU_MSG_FIELD_ID_PRIO);
}

198
src/mu-msg-iter.h Normal file
View File

@ -0,0 +1,198 @@
/*
** Copyright (C) 2008-2010 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
**
** 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 of the License, 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_MSG_ITER_H__
#define __MU_MSG_ITER_H__
#include "mu-msg.h"
G_BEGIN_DECLS
struct _MuMsgIter;
typedef struct _MuMsgIter MuMsgIter;
/**
* get the next next message (which you got from
* e.g. mu_query_run)
*
* @param iter a valid MuMsgIter iterator
*
* @return TRUE if it succeeded, FALSE otherwise (e.g., because there
* are no more messages in the query result)
*/
gboolean mu_msg_iter_next (MuMsgIter *iter);
/**
* does the iter point to a real message?
*
* @param iter a valid MuMsgIter iterator
*
* @return TRUE if the iterator points to a message, FALSE other
*/
gboolean mu_msg_iter_is_null (MuMsgIter *iter);
/**
* destroy the sequence of messages; ie. /all/ of them
*
* @param msg a valid MuMsgIter message or NULL
*/
void mu_msg_iter_destroy (MuMsgIter *iter);
/**
* get the corresponding MuMsg for this iter; this requires
* the corresponding message file to be present at the expected place
*
* @param iter a valid MuMsgIter instance
*
* @return a MuMsgGMime instance, or NULL in case of error. Use
* mu_msg_gmime_destroy when the instance is no longer needed
*/
MuMsg* mu_msg_iter_get_msg (MuMsgIter *iter);
/**
* get the document id for the current message
*
* @param iter a valid MuMsgIter iterator
*
* @return the docid or 0 in case of error
*/
unsigned int mu_msg_iter_get_docid (MuMsgIter *iter);
/**
* get the directory path of the message
*
* @param iter a valid MuMsgIter iterator
*
* @return the path, or NULL in case of error
*/
const char* mu_msg_iter_get_path (MuMsgIter *iter);
/**
* get the size of the message
*
* @param iter a valid MuMsgIter iterator
*
* @return the size, or 0 in case of error
*/
size_t mu_msg_iter_get_size (MuMsgIter *iter);
/**
* get the timestamp (ctime) of the message file
*
* @param iter a valid MuMsgIter iterator
*
* @return the size, or 0 in case of error
*/
time_t mu_msg_iter_get_timestamp (MuMsgIter *iter);
/**
* get the sent time of the message
*
* @param iter a valid MuMsgIter iterator
*
* @return the time, or 0 in case of error
*/
time_t mu_msg_iter_get_date (MuMsgIter *iter);
/**
* get the message sender(s) of the message
*
* @param iter a valid MuMsgIter iterator
*
* @return the time, or 0 in case of error
*/
const char* mu_msg_iter_get_from (MuMsgIter *iter);
/**
* get the message recipient (To:) of the message
*
* @param iter a valid MuMsgIter iterator
*
* @return the To-recipient(s), or NULL in case of error
*/
const char* mu_msg_iter_get_to (MuMsgIter *iter);
/**
* get the message recipient (Cc:) of the message
*
* @param iter a valid MuMsgIter iterator
*
* @return the Cc-recipient(s), or NULL in case of error
*/
const char* mu_msg_iter_get_cc (MuMsgIter *iter);
/**
* get the subject of the message
*
* @param iter a valid MuMsgIter iterator
*
* @return the subject, or NULL in case of error
*/
const char* mu_msg_iter_get_subject (MuMsgIter *iter);
/**
* get the message flags
*
* @param iter a valid MuMsgIter iterator
*
* @return the message flags, or MU_MSG_FLAG_UNKNOWN
*/
MuMsgFlags mu_msg_iter_get_flags (MuMsgIter *iter);
/**
* get the message priority
*
* @param iter a valid MuMsgIter iterator
*
* @return the message priority, or MU_MSG_PRIO_NONE
*/
MuMsgPrio mu_msg_iter_get_prio (MuMsgIter *iter);
/**
* get some message field
*
* @param iter a valid MuMsgIter iterator
* @param field the string field to retrieve
*
* @return the field value, or NULL
*/
const gchar* mu_msg_iter_get_field (MuMsgIter *iter,
const MuMsgField *field);
/**
* get some numeric message field
*
* @param iter a valid MuMsgIter iterator
* @param field the numeric field to retrieve
*
* @return the field value, or -1 in case of error
*/
gint64 mu_msg_iter_get_field_numeric (MuMsgIter *iter,
const MuMsgField *field);
G_END_DECLS
#endif /*__MU_MSG_ITER_H__*/

View File

@ -29,7 +29,7 @@
#include "mu-msg-iter-priv.hh"
#include "mu-util.h"
#include "mu-util-xapian.h"
#include "mu-util-db.h"
static void add_prefix (const MuMsgField* field,
@ -137,12 +137,12 @@ mu_query_xapian_new (const char* xpath)
return NULL;
}
if (mu_util_xapian_db_is_empty (xpath)) {
if (mu_util_db_is_empty (xpath)) {
g_warning ("database %s is empty; nothing to do", xpath);
return NULL;
}
if (!mu_util_xapian_db_version_up_to_date (xpath)) {
if (!mu_util_db_version_up_to_date (xpath)) {
g_warning ("%s is not up-to-date, needs a full update",
xpath);
return NULL;

View File

@ -1,5 +1,5 @@
/*
** Copyright (C) 2010 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
** Copyright (C) 2008-2010 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
**
** 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
@ -17,17 +17,20 @@
**
*/
#if HAVE_CONFIG_H
#include "config.h"
#endif /*HAVE_CONFIG_H*/
#include <xapian.h>
#include <cstring>
#include <errno.h>
#include "mu-util.h"
#include "mu-util-xapian.h"
#include "mu-util-db.h"
char*
mu_util_xapian_db_version (const gchar *xpath)
mu_util_db_version (const gchar *xpath)
{
g_return_val_if_fail (xpath, NULL);
@ -50,14 +53,14 @@ mu_util_xapian_db_version (const gchar *xpath)
gboolean
mu_util_xapian_db_version_up_to_date (const gchar *xpath)
mu_util_db_version_up_to_date (const gchar *xpath)
{
gchar *version;
gboolean uptodate;
g_return_val_if_fail (xpath, FALSE);
version = mu_util_xapian_db_version (xpath);
version = mu_util_db_version (xpath);
if (!version)
return FALSE;
@ -69,7 +72,7 @@ mu_util_xapian_db_version_up_to_date (const gchar *xpath)
gboolean
mu_util_xapian_db_is_empty (const gchar* xpath)
mu_util_db_is_empty (const gchar* xpath)
{
g_return_val_if_fail (xpath, TRUE);
@ -89,7 +92,7 @@ mu_util_xapian_db_is_empty (const gchar* xpath)
gboolean
mu_util_xapian_clear_database (const gchar *xpath)
mu_util_clear_database (const gchar *xpath)
{
g_return_val_if_fail (xpath, FALSE);

View File

@ -18,14 +18,14 @@
*/
#ifndef __MU_UTIL_XAPIAN_H__
#define __MU_UTIL_XAPIAN_H__
#ifndef __MU_UTIL_DB_H__
#define __MU_UTIL_DB_H__
#include <glib.h>
G_BEGIN_DECLS
/**
/**
* get the version of the xapian database (ie., the version of the
* 'schema' we are using). If this version != MU_XAPIAN_DB_VERSION,
* it's means we need to a full reindex.
@ -35,10 +35,10 @@ G_BEGIN_DECLS
* @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
*/
gchar* mu_util_xapian_db_version (const gchar *xpath) G_GNUC_WARN_UNUSED_RESULT;
gchar* mu_util_db_version (const gchar *xpath) G_GNUC_WARN_UNUSED_RESULT;
/**
/**
* check whether the database is empty (contains 0 documents); in
* addition, a non-existing database is considered 'empty' too
*
@ -46,18 +46,18 @@ gchar* mu_util_xapian_db_version (const gchar *xpath) G_GNUC_WARN_UNUSED_RESULT;
*
* @return TRUE if the database is empty, FALSE otherwise
*/
gboolean mu_util_xapian_db_is_empty (const gchar *xpath);
gboolean mu_util_db_is_empty (const gchar *xpath);
/**
/**
* check if the 'schema' of the current database is up-to-date
*
* @param xpath path to the xapian database
*
* @return TRUE if it's up-to-date, FALSE otherwise
*/
gboolean mu_util_xapian_db_version_up_to_date (const gchar *xpath);
gboolean mu_util_db_version_up_to_date (const gchar *xpath);
/**
/**
* 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.
@ -66,9 +66,9 @@ gboolean mu_util_xapian_db_version_up_to_date (const gchar *xpath);
*
* @return TRUE if the clearing succeeded, FALSE otherwise.
*/
gboolean mu_util_xapian_clear_database (const gchar *xpath);
gboolean mu_util_clear_database (const gchar *xpath);
G_END_DECLS
#endif /*__MU_UTIL_XAPIAN_H__*/
#endif /*__MU_UTIL_DB_H__*/