mirror of https://github.com/djcb/mu.git
* mu-msg-db.[cc,h]: basic implementation of Xapian::Document backend for MuMsg
(WIP, but compiles)
This commit is contained in:
parent
6f33f76797
commit
b990bcb054
|
@ -70,6 +70,8 @@ libmu_la_SOURCES= \
|
|||
mu-msg-data.h \
|
||||
mu-msg-data-cache.c \
|
||||
mu-msg-data-cache.h \
|
||||
mu-msg-db.c \
|
||||
mu-msg-db.h \
|
||||
mu-msg-fields.c \
|
||||
mu-msg-fields.h \
|
||||
mu-msg-file.c \
|
||||
|
|
|
@ -0,0 +1,98 @@
|
|||
/*
|
||||
** Copyright (C) 2008-2011 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-fields.h"
|
||||
#include "mu-msg-db.h"
|
||||
|
||||
struct _MuMsgDb {
|
||||
_MuMsgDb (const Xapian::Document& doc) : _doc (doc) {}
|
||||
const Xapian::Document doc() const { return _doc; }
|
||||
private:
|
||||
const Xapian::Document& _doc;
|
||||
|
||||
};
|
||||
|
||||
|
||||
MuMsgDb*
|
||||
mu_msg_db_new (XapianDocument *doc, GError **err)
|
||||
{
|
||||
g_return_val_if_fail (doc, NULL);
|
||||
|
||||
try {
|
||||
MuMsgDb *db = new MuMsgDb ((const Xapian::Document&)*doc);
|
||||
return db;
|
||||
|
||||
} MU_XAPIAN_CATCH_BLOCK_G_ERROR_RETURN(err, MU_ERROR_XAPIAN, NULL);
|
||||
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
void
|
||||
mu_msg_db_destroy (MuMsgDb *self)
|
||||
{
|
||||
try {
|
||||
delete self;
|
||||
|
||||
} MU_XAPIAN_CATCH_BLOCK;
|
||||
}
|
||||
|
||||
|
||||
gchar*
|
||||
mu_msg_db_get_str_field (MuMsgDb *self, MuMsgFieldId mfid, gboolean *do_free)
|
||||
{
|
||||
g_return_val_if_fail (self, NULL);
|
||||
g_return_val_if_fail (mu_msg_field_id_is_valid(mfid), NULL);
|
||||
g_return_val_if_fail (mu_msg_field_is_string(mfid), NULL);
|
||||
|
||||
*do_free = TRUE;
|
||||
|
||||
try {
|
||||
const std::string s (self->doc().get_value(mfid));
|
||||
return s.empty() ? NULL : g_strdup (s.c_str());
|
||||
|
||||
} MU_XAPIAN_CATCH_BLOCK_RETURN(NULL);
|
||||
}
|
||||
|
||||
|
||||
gint64
|
||||
mu_msg_db_get_num_field (MuMsgDb *self, MuMsgFieldId mfid)
|
||||
{
|
||||
g_return_val_if_fail (self, -1);
|
||||
g_return_val_if_fail (mu_msg_field_id_is_valid(mfid), -1);
|
||||
g_return_val_if_fail (mu_msg_field_is_numeric(mfid), -1);
|
||||
|
||||
try {
|
||||
const std::string s (self->doc().get_value(mfid));
|
||||
if (s.empty())
|
||||
return -1;
|
||||
else
|
||||
return static_cast<gint64>(Xapian::sortable_unserialise(s));
|
||||
|
||||
} MU_XAPIAN_CATCH_BLOCK_RETURN(-1);
|
||||
|
||||
}
|
||||
|
||||
|
|
@ -0,0 +1,87 @@
|
|||
/*
|
||||
** Copyright (C) 2011 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, 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_DB_H__
|
||||
#define __MU_MSG_DB_H__
|
||||
|
||||
#include <glib.h>
|
||||
|
||||
G_BEGIN_DECLS
|
||||
|
||||
struct _MuMsgDb;
|
||||
typedef struct _MuMsgDb MuMsgDb;
|
||||
|
||||
typedef gpointer XapianDocument;
|
||||
|
||||
/**
|
||||
* create a new MuMsgDb instance
|
||||
*
|
||||
* @param doc a Xapian::Document* (you'll need to cast the
|
||||
* Xapian::Document* to XapianDocument*, because only C (not C++) is
|
||||
* allowed in this header file.
|
||||
* @param err receives error info, or NULL
|
||||
*
|
||||
* @return a new MuMsgDb instance (free with mu_msg_db_destroy), or
|
||||
* NULL in case of error.
|
||||
*/
|
||||
MuMsgDb* mu_msg_db_new (XapianDocument *doc, GError **err);
|
||||
|
||||
|
||||
/**
|
||||
* destroy a MuMsgDb instance -- free all the resources. Note, after
|
||||
* destroying, any strings returned from mu_msg_db_get_str_field with
|
||||
* do_free==FALSE are no longer valid
|
||||
*
|
||||
* @param self a MuMsgDb instance
|
||||
*/
|
||||
void mu_msg_db_destroy (MuMsgDb *self);
|
||||
|
||||
|
||||
/**
|
||||
* get a string parameter from the msgdb
|
||||
*
|
||||
* @param self a MuMsgDb instance
|
||||
* @param mfid a MuMsgFieldId for a string field
|
||||
* @param do_free receives either TRUE or FALSE, where TRUE means that
|
||||
* the caller owns the string, and has to free it (g_free) when done
|
||||
* with it; FALSE means that the MuMsgDb owns the string, and it is
|
||||
* only valid as long as the MuMsgDb is valid (ie., before
|
||||
* mu_msg_db_destroy).
|
||||
*
|
||||
* @return a string for the given field (see do_free), or NULL in case of error
|
||||
*/
|
||||
gchar* mu_msg_db_get_str_field (MuMsgDb *self, MuMsgFieldId mfid, gboolean *do_free);
|
||||
|
||||
/**
|
||||
*
|
||||
* get a numeric parameter from the msgdb
|
||||
*
|
||||
* @param self a MuMsgDb instance
|
||||
* @param mfid a MuMsgFieldId for a numeric field
|
||||
*
|
||||
* @return the numerical value, or -1 in case of error. You'll need to
|
||||
* cast this value to the actual type (e.g. time_t for MU_MSG_FIELD_ID_DATE)
|
||||
*/
|
||||
gint64 mu_msg_db_get_num_field (MuMsgDb *self, MuMsgFieldId mfid);
|
||||
|
||||
|
||||
|
||||
G_END_DECLS
|
||||
|
||||
#endif /*__MU_MSG_DB_H__*/
|
Loading…
Reference in New Issue