2 * @brief Compact a database, or merge and compact several.
4 /* Copyright (C) 2003,2004,2005,2006,2007,2008,2009,2010,2015 Olly Betts
5 * Copyright (C) 2008 Lemur Consulting Ltd
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
30 #include "gnu_getopt.h"
34 #define PROG_NAME "xapian-compact"
35 #define PROG_DESC "Compact a database, or merge and compact several"
39 #define OPT_NO_RENUMBER 3
41 static void show_usage() {
42 cout
<< "Usage: " PROG_NAME
" [OPTIONS] SOURCE_DATABASE... DESTINATION_DATABASE\n\n"
44 " -b, --blocksize=B Set the blocksize in bytes (e.g. 4096) or K (e.g. 4K)\n"
45 " (must be between 2K and 64K and a power of 2, default 8K)\n"
46 " -n, --no-full Disable full compaction\n"
47 " -F, --fuller Enable fuller compaction (not recommended if you plan to\n"
48 " update the compacted database)\n"
49 " -m, --multipass If merging more than 3 databases, merge the postlists in\n"
50 " multiple passes (which is generally faster but requires\n"
51 " more disk space for temporary files)\n"
52 " --no-renumber Preserve the numbering of document ids (useful if you have\n"
53 " external references to them, or have set them to match\n"
54 " unique ids from an external source). Currently this\n"
55 " option is only supported when merging databases if they\n"
56 " have disjoint ranges of used document ids\n"
57 " -s, --single-file Produce a single file database (not supported for chert)\n"
58 " --help display this help and exit\n"
59 " --version output version information and exit" << endl
;
62 class MyCompactor
: public Xapian::Compactor
{
66 MyCompactor() : quiet(false) { }
68 void set_quiet(bool quiet_
) { quiet
= quiet_
; }
70 void set_status(const string
& table
, const string
& status
);
73 resolve_duplicate_metadata(const string
& key
,
79 MyCompactor::set_status(const string
& table
, const string
& status
)
84 cout
<< '\r' << table
<< ": " << status
<< endl
;
86 cout
<< table
<< " ..." << flush
;
90 MyCompactor::resolve_duplicate_metadata(const string
& key
,
96 if (tags
[0] != tags
[n
]) {
97 cerr
<< "Warning: duplicate user metadata key with different tag value - picking value from first source database with a non-empty value" << endl
;
105 main(int argc
, char **argv
)
107 const char * opts
= "b:nFmqs";
108 static const struct option long_opts
[] = {
109 {"fuller", no_argument
, 0, 'F'},
110 {"no-full", no_argument
, 0, 'n'},
111 {"multipass", no_argument
, 0, 'm'},
112 {"blocksize", required_argument
, 0, 'b'},
113 {"no-renumber", no_argument
, 0, OPT_NO_RENUMBER
},
114 {"single-file", no_argument
, 0, 's'},
115 {"quiet", no_argument
, 0, 'q'},
116 {"help", no_argument
, 0, OPT_HELP
},
117 {"version", no_argument
, 0, OPT_VERSION
},
121 MyCompactor compactor
;
122 Xapian::Compactor::compaction_level level
= Xapian::Compactor::FULL
;
124 size_t block_size
= 0;
127 while ((c
= gnu_getopt_long(argc
, argv
, opts
, long_opts
, 0)) != -1) {
131 block_size
= strtoul(optarg
, &p
, 10);
132 if (block_size
<= 64 && (*p
== 'K' || *p
== 'k')) {
136 if (*p
|| block_size
< 2048 || block_size
> 65536 ||
137 (block_size
& (block_size
- 1)) != 0) {
138 cerr
<< PROG_NAME
": Bad value '" << optarg
139 << "' passed for blocksize, must be a power of 2 between 2K and 64K"
146 level
= compactor
.STANDARD
;
149 level
= compactor
.FULLER
;
152 flags
|= Xapian::DBCOMPACT_MULTIPASS
;
154 case OPT_NO_RENUMBER
:
155 flags
|= Xapian::DBCOMPACT_NO_RENUMBER
;
158 flags
|= Xapian::DBCOMPACT_SINGLE_FILE
;
161 compactor
.set_quiet(true);
164 cout
<< PROG_NAME
" - " PROG_DESC
"\n\n";
168 cout
<< PROG_NAME
" - " PACKAGE_STRING
<< endl
;
176 if (argc
- optind
< 2) {
181 // Path to the database to create.
182 string destdir
= argv
[argc
- 1];
185 Xapian::Database src
;
186 for (int i
= optind
; i
< argc
- 1; ++i
) {
187 src
.add_database(Xapian::Database(argv
[i
]));
189 src
.compact(destdir
, level
| flags
, block_size
, compactor
);
190 } catch (const Xapian::Error
&error
) {
191 cerr
<< argv
[0] << ": " << error
.get_description() << endl
;
193 } catch (const char * msg
) {
194 cerr
<< argv
[0] << ": " << msg
<< endl
;