backends/flint/flint_database.h

Go to the documentation of this file.
00001 /* flint_database.h: C++ class definition for flint database
00002  *
00003  * Copyright 1999,2000,2001 BrightStation PLC
00004  * Copyright 2002 Ananova Ltd
00005  * Copyright 2002,2003,2004,2005,2006,2007 Olly Betts
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License as
00009  * published by the Free Software Foundation; either version 2 of the
00010  * License, or (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
00020  * USA
00021  */
00022 
00023 #ifndef OM_HGUARD_FLINT_DATABASE_H
00024 #define OM_HGUARD_FLINT_DATABASE_H
00025 
00026 #include "database.h"
00027 #include "flint_positionlist.h"
00028 #include "flint_postlist.h"
00029 #include "flint_record.h"
00030 #include "flint_spelling.h"
00031 #include "flint_synonym.h"
00032 #include "flint_termlisttable.h"
00033 #include "flint_values.h"
00034 #include "flint_version.h"
00035 #include "flint_lock.h"
00036 
00037 #include "flint_types.h"
00038 
00039 #include <map>
00040 
00041 class FlintTermList;
00042 class FlintAllDocsPostList;
00043 
00044 const int XAPIAN_DB_READONLY = 0;
00045 
00049 class FlintDatabase : public Xapian::Database::Internal {
00050     friend class FlintWritableDatabase;
00051     friend class FlintTermList;
00052     friend class FlintPostList;
00053     friend class FlintAllTermsList;
00054     friend class FlintAllDocsPostList;
00055     private:
00058         std::string db_dir;
00059 
00062         bool readonly;
00063 
00068         FlintVersion version_file;
00069 
00076         mutable FlintPostListTable postlist_table;
00077 
00080         FlintPositionListTable position_table;
00081 
00084         FlintTermListTable termlist_table;
00085 
00088         FlintValueTable value_table;
00089 
00092         mutable FlintSynonymTable synonym_table;
00093 
00096         mutable FlintSpellingTable spelling_table;
00097 
00106         FlintRecordTable record_table;
00107 
00109         FlintLock lock;
00110 
00112         mutable flint_totlen_t total_length;
00113 
00115         mutable Xapian::docid lastdocid;
00116 
00118         void read_metainfo();
00119 
00123         bool database_exists();
00124 
00128         void create_and_open_tables(unsigned int blocksize);
00129 
00135         void open_tables_consistent();
00136 
00140         void get_database_write_lock();
00141 
00147         void open_tables(flint_revision_number_t revision);
00148 
00154         flint_revision_number_t get_revision_number() const;
00155 
00161         flint_revision_number_t get_next_revision_number() const;
00162 
00173         void set_revision_number(flint_revision_number_t new_revision);
00174 
00178         void reopen();
00179 
00185         void modifications_failed(flint_revision_number_t old_revision,
00186                                   flint_revision_number_t new_revision,
00187                                   const std::string & msg);
00188 
00197         void apply();
00198 
00201         void cancel();
00202 
00203     public:
00223         FlintDatabase(const string &db_dir_, int action = XAPIAN_DB_READONLY,
00224                        unsigned int block_size = 0u);
00225 
00226         ~FlintDatabase();
00227 
00230         Xapian::doccount  get_doccount() const;
00231         Xapian::docid get_lastdocid() const;
00232         Xapian::doclength get_avlength() const;
00233         Xapian::doclength get_doclength(Xapian::docid did) const;
00234         Xapian::doccount get_termfreq(const string & tname) const;
00235         Xapian::termcount get_collection_freq(const string & tname) const;
00236         bool term_exists(const string & tname) const;
00237         bool has_positions() const;
00238 
00239         LeafPostList * open_post_list(const string & tname) const;
00240         Xapian::Document::Internal * open_document(Xapian::docid did, bool lazy) const;
00241 
00242         PositionList * open_position_list(Xapian::docid did, const string & term) const;
00243         TermList * open_term_list(Xapian::docid did) const;
00244         TermList * open_allterms(const string & prefix) const;
00245 
00246         TermList * open_spelling_termlist(const string & word) const;
00247         TermList * open_spelling_wordlist() const;
00248         Xapian::doccount get_spelling_frequency(const string & word) const;
00249 
00250         TermList * open_synonym_termlist(const string & term) const;
00251         TermList * open_synonym_keylist(const string & prefix) const;
00252 
00253         string get_metadata(const string & key) const;
00254         TermList * open_metadata_keylist(const std::string &prefix) const;
00256 };
00257 
00260 class FlintWritableDatabase : public FlintDatabase {
00262         mutable map<string, pair<Xapian::termcount_diff, Xapian::termcount_diff> >
00263                 freq_deltas;
00264 
00266         mutable map<Xapian::docid, Xapian::termcount> doclens;
00267 
00269         mutable map<string, map<Xapian::docid,
00270                                 pair<char, Xapian::termcount> > > mod_plists;
00271 
00275         mutable Xapian::doccount change_count;
00276 
00278         Xapian::doccount flush_threshold;
00279 
00286         mutable Xapian::Document::Internal * modify_shortcut_document;
00287 
00290         mutable Xapian::docid modify_shortcut_docid;
00291 
00293         void flush_postlist_changes() const;
00294 
00301         void add_freq_delta(const string & tname,
00302                             Xapian::termcount_diff tf_delta,
00303                             Xapian::termcount_diff cf_delta);
00304 
00311         void insert_mod_plist(Xapian::docid did,
00312                               const string & tname,
00313                               Xapian::termcount wdf);
00314 
00326         void update_mod_plist(Xapian::docid did,
00327                               const string & tname,
00328                               char type,
00329                               Xapian::termcount wdf);
00330 
00332 
00335         void flush();
00336 
00338         void cancel();
00339 
00340         Xapian::docid add_document(const Xapian::Document & document);
00341         Xapian::docid add_document_(Xapian::docid did, const Xapian::Document & document);
00342         // Stop the default implementation of delete_document(term) and
00343         // replace_document(term) from being hidden.  This isn't really
00344         // a problem as we only try to call them through the base class
00345         // (where they aren't hidden) but some compilers generate a warning
00346         // about the hiding.
00347 #if (!defined __GNUC__ && !defined _MSC_VER) || __GNUC__ > 2
00348         using Xapian::Database::Internal::delete_document;
00349         using Xapian::Database::Internal::replace_document;
00350 #endif
00351         void delete_document(Xapian::docid did);
00352         void replace_document(Xapian::docid did, const Xapian::Document & document);
00353 
00354         Xapian::Document::Internal * open_document(Xapian::docid did,
00355                                                    bool lazy = false) const;
00356 
00358 
00359     public:
00371         FlintWritableDatabase(const string &dir, int action, int block_size);
00372 
00373         ~FlintWritableDatabase();
00374 
00377         Xapian::doclength get_doclength(Xapian::docid did) const;
00378         Xapian::doccount get_termfreq(const string & tname) const;
00379         Xapian::termcount get_collection_freq(const string & tname) const;
00380         bool term_exists(const string & tname) const;
00381 
00382         LeafPostList * open_post_list(const string & tname) const;
00383         TermList * open_allterms(const string & prefix) const;
00384 
00385         void add_spelling(const string & word, Xapian::termcount freqinc) const;
00386         void remove_spelling(const string & word, Xapian::termcount freqdec) const;
00387         TermList * open_spelling_wordlist() const;
00388 
00389         TermList * open_synonym_keylist(const string & prefix) const;
00390         void add_synonym(const string & word, const string & synonym) const;
00391         void remove_synonym(const string & word, const string & synonym) const;
00392         void clear_synonyms(const string & word) const;
00393 
00394         void set_metadata(const string & key, const string & value);
00395         void invalidate_doc_object(Xapian::Document::Internal * obj) const;
00397 };
00398 
00399 #endif /* OM_HGUARD_FLINT_DATABASE_H */

Documentation for Xapian (version 1.0.20).
Generated on 28 Apr 2010 by Doxygen 1.5.2.