| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | |
|---|
| 21 | #include <config.h> |
|---|
| 22 | |
|---|
| 23 | #include <xapian.h> |
|---|
| 24 | |
|---|
| 25 | #include <iomanip> |
|---|
| 26 | #include <iostream> |
|---|
| 27 | |
|---|
| 28 | #include <cmath> // For log10(). |
|---|
| 29 | #include <cstdlib> // For exit(). |
|---|
| 30 | #include <cstring> // For strcmp() and strrchr(). |
|---|
| 31 | |
|---|
| 32 | using namespace std; |
|---|
| 33 | |
|---|
| 34 | #define PROG_NAME "copydatabase" |
|---|
| 35 | #define PROG_DESC "Perform a document-by-document copy of one or more Xapian databases" |
|---|
| 36 | |
|---|
| 37 | static void |
|---|
| 38 | show_usage(int rc) |
|---|
| 39 | { |
|---|
| 40 | cout << "Usage: "PROG_NAME" SOURCE_DATABASE... DESTINATION_DATABASE\n\n" |
|---|
| 41 | "Options:\n" |
|---|
| 42 | " --help display this help and exit\n" |
|---|
| 43 | " --version output version information and exit" << endl; |
|---|
| 44 | exit(rc); |
|---|
| 45 | } |
|---|
| 46 | |
|---|
| 47 | int |
|---|
| 48 | main(int argc, char **argv) |
|---|
| 49 | try { |
|---|
| 50 | if (argc > 1 && argv[1][0] == '-') { |
|---|
| 51 | if (strcmp(argv[1], "--help") == 0) { |
|---|
| 52 | cout << PROG_NAME" - "PROG_DESC"\n\n"; |
|---|
| 53 | show_usage(0); |
|---|
| 54 | } |
|---|
| 55 | if (strcmp(argv[1], "--version") == 0) { |
|---|
| 56 | cout << PROG_NAME" - "PACKAGE_STRING << endl; |
|---|
| 57 | exit(0); |
|---|
| 58 | } |
|---|
| 59 | } |
|---|
| 60 | |
|---|
| 61 | |
|---|
| 62 | |
|---|
| 63 | if (argc < 3) show_usage(1); |
|---|
| 64 | |
|---|
| 65 | |
|---|
| 66 | |
|---|
| 67 | |
|---|
| 68 | const char *dest = argv[argc - 1]; |
|---|
| 69 | Xapian::WritableDatabase db_out(dest, Xapian::DB_CREATE); |
|---|
| 70 | |
|---|
| 71 | for (int i = 1; i < argc - 1; ++i) { |
|---|
| 72 | char * src = argv[i]; |
|---|
| 73 | if (*src) { |
|---|
| 74 | |
|---|
| 75 | char & ch = src[strlen(src) - 1]; |
|---|
| 76 | if (ch == '/' || ch == '\\') ch = '\0'; |
|---|
| 77 | } |
|---|
| 78 | |
|---|
| 79 | |
|---|
| 80 | Xapian::Database db_in(src); |
|---|
| 81 | |
|---|
| 82 | |
|---|
| 83 | const char * leaf = strrchr(src, '/'); |
|---|
| 84 | #if defined __WIN32__ || defined __EMX__ |
|---|
| 85 | if (!leaf) leaf = strrchr(src, '\\'); |
|---|
| 86 | #endif |
|---|
| 87 | if (leaf) ++leaf; else leaf = src; |
|---|
| 88 | |
|---|
| 89 | |
|---|
| 90 | Xapian::doccount dbsize = db_in.get_doccount(); |
|---|
| 91 | if (dbsize == 0) { |
|---|
| 92 | cout << leaf << ": empty!" << endl; |
|---|
| 93 | } else { |
|---|
| 94 | |
|---|
| 95 | int width = static_cast<int>(log10(double(dbsize))) + 1; |
|---|
| 96 | |
|---|
| 97 | Xapian::doccount c = 0; |
|---|
| 98 | Xapian::PostingIterator it = db_in.postlist_begin(""); |
|---|
| 99 | while (it != db_in.postlist_end("")) { |
|---|
| 100 | db_out.add_document(db_in.get_document(*it)); |
|---|
| 101 | |
|---|
| 102 | |
|---|
| 103 | |
|---|
| 104 | |
|---|
| 105 | |
|---|
| 106 | ++c; |
|---|
| 107 | if (c <= 10 || (dbsize - c) % 13 == 0) { |
|---|
| 108 | cout << '\r' << leaf << ": "; |
|---|
| 109 | cout << setw(width) << c << '/' << dbsize << flush; |
|---|
| 110 | } |
|---|
| 111 | |
|---|
| 112 | ++it; |
|---|
| 113 | } |
|---|
| 114 | |
|---|
| 115 | cout << endl; |
|---|
| 116 | } |
|---|
| 117 | |
|---|
| 118 | cout << "Copying spelling data..." << flush; |
|---|
| 119 | Xapian::TermIterator spellword = db_in.spellings_begin(); |
|---|
| 120 | while (spellword != db_in.spellings_end()) { |
|---|
| 121 | db_out.add_spelling(*spellword, spellword.get_termfreq()); |
|---|
| 122 | ++spellword; |
|---|
| 123 | } |
|---|
| 124 | cout << " done." << endl; |
|---|
| 125 | |
|---|
| 126 | cout << "Copying synonym data..." << flush; |
|---|
| 127 | Xapian::TermIterator synkey = db_in.synonym_keys_begin(); |
|---|
| 128 | while (synkey != db_in.synonym_keys_end()) { |
|---|
| 129 | string key = *synkey; |
|---|
| 130 | Xapian::TermIterator syn = db_in.synonyms_begin(key); |
|---|
| 131 | while (syn != db_in.synonyms_end(key)) { |
|---|
| 132 | db_out.add_synonym(key, *syn); |
|---|
| 133 | ++syn; |
|---|
| 134 | } |
|---|
| 135 | ++synkey; |
|---|
| 136 | } |
|---|
| 137 | cout << " done." << endl; |
|---|
| 138 | } |
|---|
| 139 | |
|---|
| 140 | cout << "Flushing..." << flush; |
|---|
| 141 | |
|---|
| 142 | db_out.flush(); |
|---|
| 143 | cout << " done." << endl; |
|---|
| 144 | } catch (const Xapian::Error & e) { |
|---|
| 145 | cerr << '\n' << argv[0] << ": " << e.get_description() << endl; |
|---|
| 146 | exit(1); |
|---|
| 147 | } |
|---|