cos-dbupdate.cxx
author Stiletto <blasux@blasux.ru>
Sat, 12 Feb 2011 17:03:08 +0300
changeset 6 f0c87fb62b66
parent 4 64b70fc4f30e
permissions -rw-r--r--
example updater, two metadata extractors
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4
64b70fc4f30e header fixes
Stiletto <blasux@blasux.ru>
parents: 3
diff changeset
     1
/* cos-dbupdate.cxx:
64b70fc4f30e header fixes
Stiletto <blasux@blasux.ru>
parents: 3
diff changeset
     2
 *   based on quickstartindex.cc: Simplest possible indexer
0
56281d90869c Initial commit
Stiletto <blasux@blasux.ru>
parents:
diff changeset
     3
 *
56281d90869c Initial commit
Stiletto <blasux@blasux.ru>
parents:
diff changeset
     4
 * ----START-LICENCE----
56281d90869c Initial commit
Stiletto <blasux@blasux.ru>
parents:
diff changeset
     5
 * Copyright 1999,2000,2001 BrightStation PLC
56281d90869c Initial commit
Stiletto <blasux@blasux.ru>
parents:
diff changeset
     6
 * Copyright 2003,2004 Olly Betts
4
64b70fc4f30e header fixes
Stiletto <blasux@blasux.ru>
parents: 3
diff changeset
     7
 * Copyright 2011 Stiletto <blasux@blasux.ru>
0
56281d90869c Initial commit
Stiletto <blasux@blasux.ru>
parents:
diff changeset
     8
 *
56281d90869c Initial commit
Stiletto <blasux@blasux.ru>
parents:
diff changeset
     9
 * This program is free software; you can redistribute it and/or
56281d90869c Initial commit
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    10
 * modify it under the terms of the GNU General Public License as
56281d90869c Initial commit
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    11
 * published by the Free Software Foundation; either version 2 of the
56281d90869c Initial commit
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    12
 * License, or (at your option) any later version.
56281d90869c Initial commit
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    13
 *
56281d90869c Initial commit
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    14
 * This program is distributed in the hope that it will be useful,
56281d90869c Initial commit
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
56281d90869c Initial commit
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
56281d90869c Initial commit
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    17
 * GNU General Public License for more details.
56281d90869c Initial commit
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    18
 *
56281d90869c Initial commit
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    19
 * You should have received a copy of the GNU General Public License
56281d90869c Initial commit
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    20
 * along with this program; if not, write to the Free Software
56281d90869c Initial commit
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    21
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
56281d90869c Initial commit
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    22
 * USA
56281d90869c Initial commit
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    23
 */
56281d90869c Initial commit
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    24
56281d90869c Initial commit
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    25
#include <xapian.h>
56281d90869c Initial commit
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    26
#include <iostream>
56281d90869c Initial commit
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    27
#include <cstdlib>
56281d90869c Initial commit
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    28
56281d90869c Initial commit
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    29
using namespace std;
56281d90869c Initial commit
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    30
56281d90869c Initial commit
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    31
int main(int argc, char **argv)
56281d90869c Initial commit
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    32
{
56281d90869c Initial commit
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    33
    // Simplest possible options parsing: we just require three or more
56281d90869c Initial commit
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    34
    // parameters.
56281d90869c Initial commit
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    35
    if(argc < 2) {
56281d90869c Initial commit
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    36
        cout << "usage: " << argv[0] <<
56281d90869c Initial commit
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    37
                " <path to database>" << endl;
56281d90869c Initial commit
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    38
        exit(1);
56281d90869c Initial commit
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    39
    }
56281d90869c Initial commit
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    40
56281d90869c Initial commit
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    41
    // Catch any Xapian::Error exceptions thrown
56281d90869c Initial commit
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    42
    try {
56281d90869c Initial commit
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    43
        // Make the database
56281d90869c Initial commit
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    44
        Xapian::WritableDatabase database(argv[1], Xapian::DB_CREATE_OR_OPEN);
56281d90869c Initial commit
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    45
        while (1) {
56281d90869c Initial commit
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    46
            string fname;
56281d90869c Initial commit
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    47
            getline(cin,fname);
56281d90869c Initial commit
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    48
            if (cin.eof()||fname.empty())
56281d90869c Initial commit
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    49
                break;
56281d90869c Initial commit
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    50
            Xapian::Document newdocument;
56281d90869c Initial commit
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    51
            newdocument.set_data(fname);
6
f0c87fb62b66 example updater, two metadata extractors
Stiletto <blasux@blasux.ru>
parents: 4
diff changeset
    52
            string fnameterm = "FILENAME:"+fname;
f0c87fb62b66 example updater, two metadata extractors
Stiletto <blasux@blasux.ru>
parents: 4
diff changeset
    53
            if (fnameterm.length()>245)
f0c87fb62b66 example updater, two metadata extractors
Stiletto <blasux@blasux.ru>
parents: 4
diff changeset
    54
                fnameterm.erase(245);
f0c87fb62b66 example updater, two metadata extractors
Stiletto <blasux@blasux.ru>
parents: 4
diff changeset
    55
            newdocument.add_posting(fnameterm, 0);
0
56281d90869c Initial commit
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    56
            int i=1;
56281d90869c Initial commit
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    57
            while (1) {
56281d90869c Initial commit
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    58
                string term;
56281d90869c Initial commit
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    59
                getline(cin,term);
56281d90869c Initial commit
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    60
                if (cin.eof()||term.empty())
56281d90869c Initial commit
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    61
                    break;
6
f0c87fb62b66 example updater, two metadata extractors
Stiletto <blasux@blasux.ru>
parents: 4
diff changeset
    62
                if (term.length()>245)
f0c87fb62b66 example updater, two metadata extractors
Stiletto <blasux@blasux.ru>
parents: 4
diff changeset
    63
                    term.erase(245);
0
56281d90869c Initial commit
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    64
                cout << "TERM <" << term << ">" << endl;
56281d90869c Initial commit
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    65
                newdocument.add_posting(term, i++);
56281d90869c Initial commit
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    66
            }
6
f0c87fb62b66 example updater, two metadata extractors
Stiletto <blasux@blasux.ru>
parents: 4
diff changeset
    67
            database.replace_document(fnameterm,newdocument);
0
56281d90869c Initial commit
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    68
            cout << "EOFILE <" << fname << ">" << endl;
56281d90869c Initial commit
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    69
        }
56281d90869c Initial commit
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    70
    } catch(const Xapian::Error &error) {
56281d90869c Initial commit
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    71
        cout << "Exception: "  << error.get_msg() << endl;
56281d90869c Initial commit
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    72
    }
56281d90869c Initial commit
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    73
}