aboutsummaryrefslogtreecommitdiff
path: root/xapian-dump.cc
blob: 39c1c3404b02c13e6d01d1c3dfbe944503d63778 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/* xapian-dump: Create a textual dump of a Xapian database.
 *
 * 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>
 */

/* Currently the dumped data includes:
 *
 * 	All document IDs
 *
 * And for each document ID:
 *
 *	Document data
 *	All document terms
 *	All document values
 */

#include <cstdlib>
#include <iostream>
#include <algorithm>

#include <xapian.h>

using namespace std;

vector<int> UNSERIALIZE;

unsigned int MAX_TERMS = 0;

static void
print_escaped_string (const char *s)
{
    printf ("\"");

    while (*s) {
	if (*s == '"')
	    printf ("\\");
	printf ("%c", *s);
	s++;
    }

    printf ("\"");
}

static void
print_document_terms (Xapian::Document doc)
{
    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");
    }

    for ( ; i < MAX_TERMS; i++)
	printf ("        \"\",\n");

    printf ("    },\n");
}

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();
}

static void
print_document_values (Xapian::Document doc)
{
    Xapian::ValueIterator i;
    int value_no, value_int;
    double value_float;

    for (i = doc.values_begin (); i != doc.values_end (); i++) {
	value_no = i.get_valueno();

	printf ("    ");

	if (vector_int_contains (UNSERIALIZE, value_no)) {
	    value_float = Xapian::sortable_unserialise (*i);
	    value_int = value_float;
	    if (value_int == value_float)
		printf ("%d", value_int);
	    else
		printf ("\"%f\"", value_float);
	} else {
	    print_escaped_string ((*i).c_str ());
	}

	printf (",\n");
    }

}

static void
print_document (Xapian::Database db, Xapian::docid id)
{
    Xapian::Document doc;

    printf ("{\n");

    doc = db.get_document (id);

    printf ("    \"%s\",\n", doc.get_data ().c_str());

    print_document_terms (doc);

    print_document_values (doc);

    printf ("},\n");
}

int
main (int argc, char *argv[])
{
    const char *database_path;
    int i;

    if (argc < 2) {
	fprintf (stderr, "Usage: %s <path-to-xapian-database> [value_nos...]\n",
		 argv[0]);
	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");
	exit (1);
    }

    database_path = argv[1];

    UNSERIALIZE = vector<int> ();

    for (i = 2; i < argc; i++)
	UNSERIALIZE.push_back (atoi (argv[i]));

    try {
	Xapian::Database db;
        Xapian::PostingIterator i;
	Xapian::docid doc_id;

	db = Xapian::Database (database_path);

	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");

	for (i = db.postlist_begin (""); i != db.postlist_end (""); i++) {
	    doc_id = *i;

	    print_document (db, doc_id);
	}

	printf ("};\n");

    } catch (const Xapian::Error &error) {
	cerr << "A Xapian exception occurred: " << error.get_msg () << endl;
	exit (1);
    }

    return 0;
}