2009-10-13 17:50:20 +02:00
|
|
|
/* xapian-dump: Create a textual dump of a Xapian database.
|
2009-10-13 17:20:36 +02:00
|
|
|
*
|
2009-10-13 16:50:04 +02:00
|
|
|
* Copyright © 2009 Carl Worth
|
|
|
|
*
|
|
|
|
* 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, see http://www.gnu.org/licenses/ .
|
|
|
|
*
|
|
|
|
* Author: Carl Worth <cworth@cworth.org>
|
|
|
|
*/
|
|
|
|
|
2009-10-13 17:50:20 +02:00
|
|
|
/* Currently the dumped data includes:
|
|
|
|
*
|
|
|
|
* All document IDs
|
|
|
|
*
|
|
|
|
* And for each document ID:
|
|
|
|
*
|
2009-10-13 22:18:32 +02:00
|
|
|
* Document data
|
|
|
|
* All document terms
|
|
|
|
* All document values
|
2009-10-13 17:50:20 +02:00
|
|
|
*/
|
|
|
|
|
2009-10-13 16:50:04 +02:00
|
|
|
#include <cstdlib>
|
2009-10-13 17:20:36 +02:00
|
|
|
#include <iostream>
|
2009-10-13 18:36:25 +02:00
|
|
|
#include <algorithm>
|
2009-10-13 16:50:04 +02:00
|
|
|
|
|
|
|
#include <xapian.h>
|
|
|
|
|
2009-10-13 17:20:36 +02:00
|
|
|
using namespace std;
|
|
|
|
|
2009-10-13 18:36:25 +02:00
|
|
|
vector<int> UNSERIALIZE;
|
|
|
|
|
2009-10-19 05:49:43 +02:00
|
|
|
unsigned int MAX_TERMS = 0;
|
|
|
|
|
|
|
|
static void
|
|
|
|
print_escaped_string (const char *s)
|
|
|
|
{
|
|
|
|
printf ("\"");
|
|
|
|
|
|
|
|
while (*s) {
|
|
|
|
if (*s == '"')
|
|
|
|
printf ("\\");
|
|
|
|
printf ("%c", *s);
|
|
|
|
s++;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf ("\"");
|
|
|
|
}
|
|
|
|
|
2009-10-13 17:29:59 +02:00
|
|
|
static void
|
2009-10-13 17:50:20 +02:00
|
|
|
print_document_terms (Xapian::Document doc)
|
2009-10-13 17:29:59 +02:00
|
|
|
{
|
2009-10-19 05:49:43 +02:00
|
|
|
Xapian::TermIterator it;
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
printf (" {\n");
|
|
|
|
|
|
|
|
for (it = doc.termlist_begin (), i = 0;
|
|
|
|
it != doc.termlist_end ();
|
|
|
|
it++, i++)
|
|
|
|
{
|
|
|
|
printf (" ");
|
|
|
|
print_escaped_string ((*it).c_str());
|
|
|
|
printf (",\n");
|
|
|
|
}
|
2009-10-13 17:29:59 +02:00
|
|
|
|
2009-10-19 05:49:43 +02:00
|
|
|
for ( ; i < MAX_TERMS; i++)
|
|
|
|
printf (" \"\",\n");
|
2009-10-13 17:29:59 +02:00
|
|
|
|
2009-10-19 05:49:43 +02:00
|
|
|
printf (" },\n");
|
2009-10-13 17:29:59 +02:00
|
|
|
}
|
|
|
|
|
2009-10-13 18:36:25 +02:00
|
|
|
static int
|
|
|
|
vector_int_contains (vector<int> v, int i)
|
|
|
|
{
|
|
|
|
vector<int>::iterator result;
|
|
|
|
|
|
|
|
result = find (v.begin(), v.end(), i);
|
|
|
|
|
|
|
|
return result != v.end();
|
|
|
|
}
|
|
|
|
|
2009-10-13 17:50:20 +02:00
|
|
|
static void
|
|
|
|
print_document_values (Xapian::Document doc)
|
|
|
|
{
|
|
|
|
Xapian::ValueIterator i;
|
2009-10-13 18:36:25 +02:00
|
|
|
int value_no, value_int;
|
|
|
|
double value_float;
|
2009-10-13 17:50:20 +02:00
|
|
|
|
2009-10-13 18:36:25 +02:00
|
|
|
for (i = doc.values_begin (); i != doc.values_end (); i++) {
|
|
|
|
value_no = i.get_valueno();
|
|
|
|
|
2009-10-19 05:49:43 +02:00
|
|
|
printf (" ");
|
2009-10-13 18:36:25 +02:00
|
|
|
|
|
|
|
if (vector_int_contains (UNSERIALIZE, value_no)) {
|
|
|
|
value_float = Xapian::sortable_unserialise (*i);
|
|
|
|
value_int = value_float;
|
|
|
|
if (value_int == value_float)
|
2009-10-19 05:49:43 +02:00
|
|
|
printf ("%d", value_int);
|
2009-10-13 18:36:25 +02:00
|
|
|
else
|
2009-10-19 05:49:43 +02:00
|
|
|
printf ("\"%f\"", value_float);
|
2009-10-13 18:36:25 +02:00
|
|
|
} else {
|
2009-10-19 05:49:43 +02:00
|
|
|
print_escaped_string ((*i).c_str ());
|
2009-10-13 18:36:25 +02:00
|
|
|
}
|
|
|
|
|
2009-10-19 05:49:43 +02:00
|
|
|
printf (",\n");
|
2009-10-13 18:36:25 +02:00
|
|
|
}
|
2009-10-19 05:49:43 +02:00
|
|
|
|
2009-10-13 17:50:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
print_document (Xapian::Database db, Xapian::docid id)
|
|
|
|
{
|
|
|
|
Xapian::Document doc;
|
|
|
|
|
2009-10-19 05:49:43 +02:00
|
|
|
printf ("{\n");
|
2009-10-13 17:50:20 +02:00
|
|
|
|
|
|
|
doc = db.get_document (id);
|
|
|
|
|
2009-10-19 05:49:43 +02:00
|
|
|
printf (" \"%s\",\n", doc.get_data ().c_str());
|
2009-10-13 22:18:32 +02:00
|
|
|
|
2009-10-13 17:50:20 +02:00
|
|
|
print_document_terms (doc);
|
|
|
|
|
|
|
|
print_document_values (doc);
|
2009-10-19 05:49:43 +02:00
|
|
|
|
|
|
|
printf ("},\n");
|
2009-10-13 17:50:20 +02:00
|
|
|
}
|
|
|
|
|
2009-10-13 16:50:04 +02:00
|
|
|
int
|
|
|
|
main (int argc, char *argv[])
|
|
|
|
{
|
2009-10-13 17:20:36 +02:00
|
|
|
const char *database_path;
|
2009-10-13 18:36:25 +02:00
|
|
|
int i;
|
2009-10-13 17:20:36 +02:00
|
|
|
|
2009-10-13 16:50:04 +02:00
|
|
|
if (argc < 2) {
|
2009-10-13 18:36:25 +02:00
|
|
|
fprintf (stderr, "Usage: %s <path-to-xapian-database> [value_nos...]\n",
|
2009-10-13 16:50:04 +02:00
|
|
|
argv[0]);
|
2009-10-13 18:36:25 +02:00
|
|
|
fprintf (stderr, "Dumps data from the given database.\n");
|
|
|
|
fprintf (stderr, "The values corresponding to any value numbers given on the command line\n");
|
|
|
|
fprintf (stderr, "will be unserialized to an before being printed.\n");
|
2009-10-13 16:50:04 +02:00
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
|
2009-10-13 17:20:36 +02:00
|
|
|
database_path = argv[1];
|
|
|
|
|
2009-10-13 18:36:25 +02:00
|
|
|
UNSERIALIZE = vector<int> ();
|
|
|
|
|
|
|
|
for (i = 2; i < argc; i++)
|
|
|
|
UNSERIALIZE.push_back (atoi (argv[i]));
|
|
|
|
|
2009-10-13 17:20:36 +02:00
|
|
|
try {
|
|
|
|
Xapian::Database db;
|
|
|
|
Xapian::PostingIterator i;
|
|
|
|
Xapian::docid doc_id;
|
|
|
|
|
|
|
|
db = Xapian::Database (database_path);
|
2009-10-19 05:49:43 +02:00
|
|
|
|
|
|
|
for (i = db.postlist_begin (""); i != db.postlist_end (""); i++) {
|
|
|
|
Xapian::Document doc;
|
|
|
|
|
|
|
|
doc_id = *i;
|
|
|
|
|
|
|
|
doc = db.get_document (doc_id);
|
|
|
|
|
|
|
|
if (doc.termlist_count () > MAX_TERMS)
|
|
|
|
MAX_TERMS = doc.termlist_count ();
|
|
|
|
}
|
|
|
|
|
|
|
|
printf ("#define MAX_TERMS %d\n\n", MAX_TERMS);
|
|
|
|
|
|
|
|
printf ("typedef struct {\n"
|
|
|
|
" char data[255];\n"
|
|
|
|
" char terms[MAX_TERMS][255];\n"
|
|
|
|
" char message_id[255];\n"
|
|
|
|
" char thread_id[4096];\n"
|
|
|
|
" time_t time;\n"
|
|
|
|
"} document_dump_t;\n\n");
|
|
|
|
|
|
|
|
printf ("document_dump_t dump[] = {\n");
|
|
|
|
|
2009-10-13 17:20:36 +02:00
|
|
|
for (i = db.postlist_begin (""); i != db.postlist_end (""); i++) {
|
|
|
|
doc_id = *i;
|
2009-10-13 17:29:59 +02:00
|
|
|
|
|
|
|
print_document (db, doc_id);
|
2009-10-13 17:20:36 +02:00
|
|
|
}
|
|
|
|
|
2009-10-19 05:49:43 +02:00
|
|
|
printf ("};\n");
|
|
|
|
|
2009-10-13 17:20:36 +02:00
|
|
|
} catch (const Xapian::Error &error) {
|
|
|
|
cerr << "A Xapian exception occurred: " << error.get_msg () << endl;
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
|
2009-10-13 16:50:04 +02:00
|
|
|
return 0;
|
|
|
|
}
|