1 /* xapian-dump: Create a textual dump of a Xapian database.
3 * Copyright © 2009 Carl Worth
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see http://www.gnu.org/licenses/ .
18 * Author: Carl Worth <cworth@cworth.org>
21 /* Currently the dumped data includes:
25 * And for each document ID:
30 * Things not yet dumped include:
32 * Data associated with a document.
43 vector<int> UNSERIALIZE;
46 print_document_terms (Xapian::Document doc)
48 Xapian::TermIterator i;
52 for (i = doc.termlist_begin (); i != doc.termlist_end (); i++)
53 cout << "\t" << *i << endl;
57 vector_int_contains (vector<int> v, int i)
59 vector<int>::iterator result;
61 result = find (v.begin(), v.end(), i);
63 return result != v.end();
67 print_document_values (Xapian::Document doc)
69 Xapian::ValueIterator i;
70 int value_no, value_int;
75 for (i = doc.values_begin (); i != doc.values_end (); i++) {
76 value_no = i.get_valueno();
78 cout << "\t" << i.get_valueno() << ": ";
80 if (vector_int_contains (UNSERIALIZE, value_no)) {
81 value_float = Xapian::sortable_unserialise (*i);
82 value_int = value_float;
83 if (value_int == value_float)
96 print_document (Xapian::Database db, Xapian::docid id)
100 printf ("Document %u:\n", id);
102 doc = db.get_document (id);
104 print_document_terms (doc);
106 print_document_values (doc);
110 main (int argc, char *argv[])
112 const char *database_path;
116 fprintf (stderr, "Usage: %s <path-to-xapian-database> [value_nos...]\n",
118 fprintf (stderr, "Dumps data from the given database.\n");
119 fprintf (stderr, "The values corresponding to any value numbers given on the command line\n");
120 fprintf (stderr, "will be unserialized to an before being printed.\n");
124 database_path = argv[1];
126 UNSERIALIZE = vector<int> ();
128 for (i = 2; i < argc; i++)
129 UNSERIALIZE.push_back (atoi (argv[i]));
134 Xapian::PostingIterator i;
135 Xapian::docid doc_id;
137 db = Xapian::Database (database_path);
138 for (i = db.postlist_begin (""); i != db.postlist_end (""); i++) {
141 print_document (db, doc_id);
144 } catch (const Xapian::Error &error) {
145 cerr << "A Xapian exception occurred: " << error.get_msg () << endl;