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 = false) 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 
00281         void flush_postlist_changes() const;
00282 
00284 
00287         void flush();
00288 
00290         void cancel();
00291 
00292         Xapian::docid add_document(const Xapian::Document & document);
00293         Xapian::docid add_document_(Xapian::docid did, const Xapian::Document & document);
00294         // Stop the default implementation of delete_document(term) and
00295         // replace_document(term) from being hidden.  This isn't really
00296         // a problem as we only try to call them through the base class
00297         // (where they aren't hidden) but some compilers generate a warning
00298         // about the hiding.
00299 #if (!defined __GNUC__ && !defined _MSC_VER) || __GNUC__ > 2
00300         using Xapian::Database::Internal::delete_document;
00301         using Xapian::Database::Internal::replace_document;
00302 #endif
00303         void delete_document(Xapian::docid did);
00304         void replace_document(Xapian::docid did, const Xapian::Document & document);
00306 
00307     public:
00319         FlintWritableDatabase(const string &dir, int action, int block_size);
00320 
00321         ~FlintWritableDatabase();
00322 
00325         Xapian::doclength get_doclength(Xapian::docid did) const;
00326         Xapian::doccount get_termfreq(const string & tname) const;
00327         Xapian::termcount get_collection_freq(const string & tname) const;
00328         bool term_exists(const string & tname) const;
00329 
00330         LeafPostList * open_post_list(const string & tname) const;
00331         TermList * open_allterms(const string & prefix) const;
00332 
00333         void add_spelling(const string & word, Xapian::termcount freqinc) const;
00334         void remove_spelling(const string & word, Xapian::termcount freqdec) const;
00335         TermList * open_spelling_wordlist() const;
00336 
00337         TermList * open_synonym_keylist(const string & prefix) const;
00338         void add_synonym(const string & word, const string & synonym) const;
00339         void remove_synonym(const string & word, const string & synonym) const;
00340         void clear_synonyms(const string & word) const;
00341 
00342         void set_metadata(const string & key, const string & value);
00344 };
00345 
00346 #endif /* OM_HGUARD_FLINT_DATABASE_H */

Documentation for Xapian (version 1.0.10).
Generated on 24 Dec 2008 by Doxygen 1.5.2.