]> git.notmuchmail.org Git - notmuch/blob - xapian-dump.cc
xapian-dump: Add values to the dump as well.
[notmuch] / xapian-dump.cc
1 /* xapian-dump: Create a textual dump of a Xapian database.
2  *
3  * Copyright © 2009 Carl Worth
4  *
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.
9  *
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.
14  *
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/ .
17  *
18  * Author: Carl Worth <cworth@cworth.org>
19  */
20
21 /* Currently the dumped data includes:
22  *
23  *      All document IDs
24  *
25  * And for each document ID:
26  *
27  *      All terms
28  *      All values
29  *
30  * Things not yet dumped include:
31  *
32  * Data associated with a document.
33  */
34
35 #include <cstdlib>
36 #include <iostream>
37
38 #include <xapian.h>
39
40 using namespace std;
41
42 static void
43 print_document_terms (Xapian::Document doc)
44 {
45     Xapian::TermIterator i;
46
47     printf ("Terms:\n");
48
49     for (i = doc.termlist_begin (); i != doc.termlist_end (); i++)
50         cout << "\t" << *i << endl;
51 }
52
53 static void
54 print_document_values (Xapian::Document doc)
55 {
56     Xapian::ValueIterator i;
57
58     printf ("Values:\n");
59
60     for (i = doc.values_begin (); i != doc.values_end (); i++)
61         cout << "\t" << i.get_valueno() << ": " << *i << endl;
62 }
63
64 static void
65 print_document (Xapian::Database db, Xapian::docid id)
66 {
67     Xapian::Document doc;
68
69     printf ("Document %u:\n", id);
70
71     doc = db.get_document (id);
72
73     print_document_terms (doc);
74
75     print_document_values (doc);
76 }
77
78 int
79 main (int argc, char *argv[])
80 {
81     const char *database_path;
82
83     if (argc < 2) {
84         fprintf (stderr, "Usage: %s <path-to-xapian-database>\n",
85                  argv[0]);
86         exit (1);
87     }
88
89     database_path = argv[1];
90
91     try {
92
93         Xapian::Database db;
94         Xapian::PostingIterator i;
95         Xapian::docid doc_id;
96
97         db = Xapian::Database (database_path);
98         for (i = db.postlist_begin (""); i != db.postlist_end (""); i++) {
99             doc_id = *i;
100
101             print_document (db, doc_id);
102         }
103
104     } catch (const Xapian::Error &error) {
105         cerr << "A Xapian exception occurred: " << error.get_msg () << endl;
106         exit (1);
107     }
108
109     return 0;
110 }