LCOV - code coverage report
Current view: top level - backends/honey - honey_synonym.h (source / functions) Hit Total Coverage
Test: Test Coverage for xapian-core 954b5873a738 Lines: 9 9 100.0 %
Date: 2019-06-30 05:20:33 Functions: 4 4 100.0 %
Branches: 6 12 50.0 %

           Branch data     Line data    Source code
       1                 :            : /** @file honey_synonym.h
       2                 :            :  * @brief Synonym data for a honey database.
       3                 :            :  */
       4                 :            : /* Copyright (C) 2005,2007,2008,2009,2011,2014,2016,2017 Olly Betts
       5                 :            :  *
       6                 :            :  * This program is free software; you can redistribute it and/or modify
       7                 :            :  * it under the terms of the GNU General Public License as published by
       8                 :            :  * the Free Software Foundation; either version 2 of the License, or
       9                 :            :  * (at your option) any later version.
      10                 :            :  *
      11                 :            :  * This program is distributed in the hope that it will be useful,
      12                 :            :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      13                 :            :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      14                 :            :  * GNU General Public License for more details.
      15                 :            :  *
      16                 :            :  * You should have received a copy of the GNU General Public License
      17                 :            :  * along with this program; if not, write to the Free Software
      18                 :            :  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
      19                 :            :  */
      20                 :            : 
      21                 :            : #ifndef XAPIAN_INCLUDED_HONEY_SYNONYM_H
      22                 :            : #define XAPIAN_INCLUDED_HONEY_SYNONYM_H
      23                 :            : 
      24                 :            : #include <xapian/types.h>
      25                 :            : 
      26                 :            : #include "api/termlist.h"
      27                 :            : #include "backends/alltermslist.h"
      28                 :            : #include "honey_cursor.h"
      29                 :            : #include "honey_lazytable.h"
      30                 :            : 
      31                 :            : #include <set>
      32                 :            : #include <string>
      33                 :            : 
      34                 :            : class HoneyDatabase;
      35                 :            : 
      36                 :            : namespace Honey {
      37                 :            :     class RootInfo;
      38                 :            : }
      39                 :            : 
      40                 :        710 : class HoneySynonymTable : public HoneyLazyTable {
      41                 :            :     /// The last term which was updated.
      42                 :            :     mutable std::string last_term;
      43                 :            : 
      44                 :            :     /// The synonyms for the last term which was updated.
      45                 :            :     mutable std::set<std::string> last_synonyms;
      46                 :            : 
      47                 :            :   public:
      48                 :            :     /** Create a new HoneySynonymTable object.
      49                 :            :      *
      50                 :            :      *  This method does not create or open the table on disk - you
      51                 :            :      *  must call the create() or open() methods respectively!
      52                 :            :      *
      53                 :            :      *  @param dbdir            The directory the honey database is stored in.
      54                 :            :      *  @param readonly         true if we're opening read-only, else false.
      55                 :            :      */
      56                 :        354 :     HoneySynonymTable(const std::string & dbdir, bool readonly)
      57 [ +  - ][ +  - ]:        354 :         : HoneyLazyTable("synonym", dbdir + "/synonym.", readonly) { }
                 [ +  - ]
      58                 :            : 
      59                 :          1 :     HoneySynonymTable(int fd, off_t offset_, bool readonly)
      60 [ +  - ][ +  - ]:          1 :         : HoneyLazyTable("synonym", fd, offset_, readonly) { }
      61                 :            : 
      62                 :            :     // Merge in batched-up changes.
      63                 :            :     void merge_changes();
      64                 :            : 
      65                 :            :     // Discard batched-up changes.
      66                 :            :     void discard_changes() {
      67                 :            :         last_term.resize(0);
      68                 :            :         last_synonyms.clear();
      69                 :            :     }
      70                 :            : 
      71                 :            :     /** Add a synonym for @a term.
      72                 :            :      *
      73                 :            :      *  If the synonym has already been added, no action is taken.
      74                 :            :      */
      75                 :            :     void add_synonym(const std::string & term, const std::string & synonym);
      76                 :            : 
      77                 :            :     /** Remove a synonym for @a term.
      78                 :            :      *
      79                 :            :      *  If the synonym doesn't exist, no action is taken.
      80                 :            :      */
      81                 :            :     void remove_synonym(const std::string & term, const std::string & synonym);
      82                 :            : 
      83                 :            :     /** Remove all synonyms for @a term.
      84                 :            :      *
      85                 :            :      *  If @a term has no synonyms, no action is taken.
      86                 :            :      */
      87                 :            :     void clear_synonyms(const std::string & term);
      88                 :            : 
      89                 :            :     /** Open synonym termlist for a term.
      90                 :            :      *
      91                 :            :      *  If @a term has no synonyms, NULL is returned.
      92                 :            :      */
      93                 :            :     TermList * open_termlist(const std::string & term) const;
      94                 :            : 
      95                 :            :     /** Override methods of HoneyTable.
      96                 :            :      *
      97                 :            :      *  NB: these aren't virtual, but we always call them on the subclass in
      98                 :            :      *  cases where it matters.
      99                 :            :      *  @{
     100                 :            :      */
     101                 :            : 
     102                 :            :     bool is_modified() const {
     103                 :            :         return !last_term.empty() || HoneyTable::is_modified();
     104                 :            :     }
     105                 :            : 
     106                 :            :     void flush_db() {
     107                 :            :         merge_changes();
     108                 :            :         HoneyTable::flush_db();
     109                 :            :     }
     110                 :            : 
     111                 :            :     void cancel(const Honey::RootInfo & root_info,
     112                 :            :                 honey_revision_number_t rev) {
     113                 :            :         discard_changes();
     114                 :            :         HoneyTable::cancel(root_info, rev);
     115                 :            :     }
     116                 :            : 
     117                 :            :     // @}
     118                 :            : };
     119                 :            : 
     120                 :            : class HoneyCursor;
     121                 :            : 
     122                 :            : class HoneySynonymTermList : public AllTermsList {
     123                 :            :     /// Copying is not allowed.
     124                 :            :     HoneySynonymTermList(const HoneySynonymTermList &);
     125                 :            : 
     126                 :            :     /// Assignment is not allowed.
     127                 :            :     void operator=(const HoneySynonymTermList &);
     128                 :            : 
     129                 :            :     /// Keep a reference to our database to stop it being deleted.
     130                 :            :     Xapian::Internal::intrusive_ptr<const HoneyDatabase> database;
     131                 :            : 
     132                 :            :     /** A cursor which runs through the synonym table reading termnames from
     133                 :            :      *  the keys.
     134                 :            :      */
     135                 :            :     HoneyCursor * cursor;
     136                 :            : 
     137                 :            :     /// The prefix to restrict the terms to.
     138                 :            :     std::string prefix;
     139                 :            : 
     140                 :            :   public:
     141                 :          3 :     HoneySynonymTermList(const HoneyDatabase* database_,
     142                 :            :                          HoneyCursor* cursor_,
     143                 :            :                          const std::string& prefix_)
     144         [ +  - ]:          3 :         : database(database_), cursor(cursor_), prefix(prefix_)
     145                 :            :     {
     146                 :            :         // Set the cursor to its end to signal we haven't started yet.
     147                 :          3 :         cursor->to_end();
     148                 :          3 :     }
     149                 :            : 
     150                 :            :     /// Destructor.
     151                 :            :     ~HoneySynonymTermList();
     152                 :            : 
     153                 :            :     Xapian::termcount get_approx_size() const;
     154                 :            : 
     155                 :            :     /** Returns the current termname.
     156                 :            :      *
     157                 :            :      *  Either next() or skip_to() must have been called before this
     158                 :            :      *  method can be called.
     159                 :            :      */
     160                 :            :     std::string get_termname() const;
     161                 :            : 
     162                 :            :     /// Return the term frequency for the term at the current position.
     163                 :            :     Xapian::doccount get_termfreq() const;
     164                 :            : 
     165                 :            :     /// Return the collection frequency for the term at the current position.
     166                 :            :     Xapian::termcount get_collection_freq() const;
     167                 :            : 
     168                 :            :     /// Advance to the next term in the list.
     169                 :            :     TermList * next();
     170                 :            : 
     171                 :            :     /// Advance to the first term which is >= term.
     172                 :            :     TermList * skip_to(const std::string &term);
     173                 :            : 
     174                 :            :     /// True if we're off the end of the list
     175                 :            :     bool at_end() const;
     176                 :            : };
     177                 :            : 
     178                 :            : #endif // XAPIAN_INCLUDED_HONEY_SYNONYM_H

Generated by: LCOV version 1.11