xapian-core  1.4.31
xapian-compact.cc
Go to the documentation of this file.
1 
4 /* Copyright (C) 2003-2026 Olly Betts
5  * Copyright (C) 2008 Lemur Consulting Ltd
6  *
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.
11  *
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.
16  *
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
20  * USA
21  */
22 
23 #include <config.h>
24 
25 #include <xapian.h>
26 
27 #include <cstdlib>
28 #include <iostream>
29 
30 #include "gnu_getopt.h"
31 
32 using namespace std;
33 
34 #define PROG_NAME "xapian-compact"
35 #define PROG_DESC "Compact a database, or merge and compact several"
36 
37 #define OPT_HELP 1
38 #define OPT_VERSION 2
39 #define OPT_NO_RENUMBER 3
40 
41 static void show_usage() {
42  cout << "Usage: " PROG_NAME " [OPTIONS] SOURCE_DATABASE... DESTINATION_DATABASE\n\n"
43 "Options:\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 No effect for glass since Xapian 1.4.31\n"
48 " -m, --multipass If merging more than 3 databases, merge the postlists in\n"
49 " multiple passes (which is generally faster but requires\n"
50 " more disk space for temporary files)\n"
51 " --no-renumber Preserve the numbering of document ids (useful if you have\n"
52 " external references to them, or have set them to match\n"
53 " unique ids from an external source). Currently this\n"
54 " option is only supported when merging databases if they\n"
55 " have disjoint ranges of used document ids\n"
56 " -s, --single-file Produce a single file database (not supported for chert)\n"
57 " --help display this help and exit\n"
58 " --version output version information and exit\n";
59 }
60 
62  bool quiet;
63 
64  public:
65  MyCompactor() : quiet(false) { }
66 
67  void set_quiet(bool quiet_) { quiet = quiet_; }
68 
69  void set_status(const string& table, const string& status) override;
70 
71  string
72  resolve_duplicate_metadata(const string & key,
73  size_t n,
74  const string tags[]) override;
75 };
76 
77 void
78 MyCompactor::set_status(const string & table, const string & status)
79 {
80  if (quiet)
81  return;
82  if (!status.empty())
83  cout << '\r' << table << ": " << status << '\n';
84  else
85  cout << table << " ..." << flush;
86 }
87 
88 string
90  size_t n,
91  const string tags[])
92 {
93  (void)key;
94  while (--n) {
95  if (tags[0] != tags[n]) {
96  cerr << "Warning: duplicate user metadata key with different tag "
97  "value - picking value from first source database with a "
98  "non-empty value\n";
99  break;
100  }
101  }
102  return tags[0];
103 }
104 
105 int
106 main(int argc, char **argv)
107 {
108  const char * opts = "b:nFmqs";
109  static const struct option long_opts[] = {
110  {"fuller", no_argument, 0, 'F'},
111  {"no-full", no_argument, 0, 'n'},
112  {"multipass", no_argument, 0, 'm'},
113  {"blocksize", required_argument, 0, 'b'},
114  {"no-renumber", no_argument, 0, OPT_NO_RENUMBER},
115  {"single-file", no_argument, 0, 's'},
116  {"quiet", no_argument, 0, 'q'},
117  {"help", no_argument, 0, OPT_HELP},
118  {"version", no_argument, 0, OPT_VERSION},
119  {NULL, 0, 0, 0}
120  };
121 
122  MyCompactor compactor;
124  unsigned flags = 0;
125  size_t block_size = 0;
126 
127  int c;
128  while ((c = gnu_getopt_long(argc, argv, opts, long_opts, 0)) != -1) {
129  switch (c) {
130  case 'b': {
131  char *p;
132  block_size = strtoul(optarg, &p, 10);
133  if (block_size <= 64 && (*p == 'K' || *p == 'k')) {
134  ++p;
135  block_size *= 1024;
136  }
137  if (*p || block_size < 2048 || block_size > 65536 ||
138  (block_size & (block_size - 1)) != 0) {
139  cerr << PROG_NAME": Bad value '" << optarg
140  << "' passed for blocksize, must be a power of 2 "
141  "between 2K and 64K\n";
142  exit(1);
143  }
144  break;
145  }
146  case 'n':
147  level = compactor.STANDARD;
148  break;
149  case 'F':
150  level = compactor.FULLER;
151  break;
152  case 'm':
154  break;
155  case OPT_NO_RENUMBER:
157  break;
158  case 's':
160  break;
161  case 'q':
162  compactor.set_quiet(true);
163  break;
164  case OPT_HELP:
165  cout << PROG_NAME " - " PROG_DESC "\n\n";
166  show_usage();
167  exit(0);
168  case OPT_VERSION:
169  cout << PROG_NAME " - " PACKAGE_STRING "\n";
170  exit(0);
171  default:
172  show_usage();
173  exit(1);
174  }
175  }
176 
177  if (argc - optind < 2) {
178  show_usage();
179  exit(1);
180  }
181 
182  // Path to the database to create.
183  string destdir = argv[argc - 1];
184 
185  try {
186  Xapian::Database src;
187  for (int i = optind; i < argc - 1; ++i) {
188  src.add_database(Xapian::Database(argv[i]));
189  }
190  src.compact(destdir, level | flags, block_size, compactor);
191  } catch (const Xapian::Error &error) {
192  cerr << argv[0] << ": " << error.get_description() << '\n';
193  exit(1);
194  } catch (const char * msg) {
195  cerr << argv[0] << ": " << msg << '\n';
196  exit(1);
197  }
198 }
void set_status(const string &table, const string &status) override
Update progress.
void set_quiet(bool quiet_)
string resolve_duplicate_metadata(const string &key, size_t n, const string tags[]) override
Resolve multiple user metadata entries with the same key.
Compact a database, or merge and compact several.
Definition: compactor.h:42
compaction_level
Compaction level.
Definition: compactor.h:48
@ FULL
Split items whenever it saves space (the default).
Definition: compactor.h:52
@ FULLER
Allow oversize items to save more space (not recommended if you ever plan to update the compacted dat...
Definition: compactor.h:58
@ STANDARD
Don't split items unnecessarily.
Definition: compactor.h:50
This class is used to access a database, or a group of databases.
Definition: database.h:68
void compact(const std::string &output, unsigned flags=0, int block_size=0)
Produce a compact version of this database.
Definition: database.h:633
void add_database(const Database &database)
Add an existing database (or group of databases) to those accessed by this object.
Definition: omdatabase.cc:148
All exceptions thrown by Xapian are subclasses of Xapian::Error.
Definition: error.h:43
std::string get_description() const
Return a string describing this object.
Definition: error.cc:93
#define PACKAGE_STRING
Definition: config.h:340
int optind
Definition: getopt.cc:94
char * optarg
Definition: getopt.cc:79
Wrappers to allow GNU getopt to be used cleanly from C++ code.
#define no_argument
Definition: gnu_getopt.h:79
#define required_argument
Definition: gnu_getopt.h:80
int gnu_getopt_long(int argc_, char *const *argv_, const char *shortopts_, const struct option *longopts_, int *optind_)
Definition: gnu_getopt.h:97
#define false
Definition: header.h:9
const int DBCOMPACT_MULTIPASS
If merging more than 3 databases, merge the postlists in multiple passes.
Definition: constants.h:262
const int DBCOMPACT_NO_RENUMBER
Use the same document ids in the output as in the input(s).
Definition: constants.h:256
const int DBCOMPACT_SINGLE_FILE
Produce a single-file database.
Definition: constants.h:268
static void show_usage()
#define OPT_VERSION
int main(int argc, char **argv)
#define PROG_NAME
#define OPT_NO_RENUMBER
#define PROG_DESC
#define OPT_HELP
static bool tags
static const char * opts
static const struct option long_opts[]
Public interfaces for the Xapian library.