LCOV - code coverage report
Current view: top level - backends/glass - glass_synonym.h (source / functions) Hit Total Coverage
Test: Test Coverage for xapian-core 954b5873a738 Lines: 25 25 100.0 %
Date: 2019-06-30 05:20:33 Functions: 8 8 100.0 %
Branches: 14 22 63.6 %

           Branch data     Line data    Source code
       1                 :            : /** @file glass_synonym.h
       2                 :            :  * @brief Synonym data for a glass 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_GLASS_SYNONYM_H
      22                 :            : #define XAPIAN_INCLUDED_GLASS_SYNONYM_H
      23                 :            : 
      24                 :            : #include <xapian/types.h>
      25                 :            : 
      26                 :            : #include "backends/alltermslist.h"
      27                 :            : #include "glass_lazytable.h"
      28                 :            : #include "api/termlist.h"
      29                 :            : 
      30                 :            : #include <set>
      31                 :            : #include <string>
      32                 :            : 
      33                 :            : class GlassDatabase;
      34                 :            : 
      35                 :            : namespace Glass {
      36                 :            :     class RootInfo;
      37                 :            : }
      38                 :            : using Glass::RootInfo;
      39                 :            : 
      40                 :       8256 : class GlassSynonymTable : public GlassLazyTable {
      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 GlassSynonymTable 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 glass database is stored in.
      54                 :            :      *  @param readonly         true if we're opening read-only, else false.
      55                 :            :      */
      56                 :       3819 :     GlassSynonymTable(const std::string & dbdir, bool readonly)
      57 [ +  - ][ +  - ]:       3819 :         : GlassLazyTable("synonym", dbdir + "/synonym.", readonly) { }
                 [ +  - ]
      58                 :            : 
      59                 :        309 :     GlassSynonymTable(int fd, off_t offset_, bool readonly)
      60 [ +  - ][ +  - ]:        309 :         : GlassLazyTable("synonym", fd, offset_, readonly) { }
      61                 :            : 
      62                 :            :     // Merge in batched-up changes.
      63                 :            :     void merge_changes();
      64                 :            : 
      65                 :            :     // Discard batched-up changes.
      66                 :        251 :     void discard_changes() {
      67                 :        251 :         last_term.resize(0);
      68                 :        251 :         last_synonyms.clear();
      69                 :        251 :     }
      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);
      94                 :            : 
      95                 :            :     /** Override methods of GlassTable.
      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                 :       4179 :     bool is_modified() const {
     103 [ +  + ][ +  + ]:       4179 :         return !last_term.empty() || GlassTable::is_modified();
     104                 :            :     }
     105                 :            : 
     106                 :       9125 :     void flush_db() {
     107                 :       9125 :         merge_changes();
     108                 :       9125 :         GlassTable::flush_db();
     109                 :       9125 :     }
     110                 :            : 
     111                 :        251 :     void cancel(const RootInfo & root_info, glass_revision_number_t rev) {
     112                 :        251 :         discard_changes();
     113                 :        251 :         GlassTable::cancel(root_info, rev);
     114                 :        251 :     }
     115                 :            : 
     116                 :            :     // @}
     117                 :            : };
     118                 :            : 
     119                 :            : class GlassCursor;
     120                 :            : 
     121                 :            : class GlassSynonymTermList : public AllTermsList {
     122                 :            :     /// Copying is not allowed.
     123                 :            :     GlassSynonymTermList(const GlassSynonymTermList &);
     124                 :            : 
     125                 :            :     /// Assignment is not allowed.
     126                 :            :     void operator=(const GlassSynonymTermList &);
     127                 :            : 
     128                 :            :     /// Keep a reference to our database to stop it being deleted.
     129                 :            :     Xapian::Internal::intrusive_ptr<const GlassDatabase> database;
     130                 :            : 
     131                 :            :     /** A cursor which runs through the synonym table reading termnames from
     132                 :            :      *  the keys.
     133                 :            :      */
     134                 :            :     GlassCursor * cursor;
     135                 :            : 
     136                 :            :     /// The prefix to restrict the terms to.
     137                 :            :     string prefix;
     138                 :            : 
     139                 :            :   public:
     140                 :      30883 :     GlassSynonymTermList(Xapian::Internal::intrusive_ptr<const GlassDatabase> database_,
     141                 :            :                          GlassCursor * cursor_,
     142                 :            :                          const string & prefix_)
     143         [ +  - ]:      30883 :             : database(database_), cursor(cursor_), prefix(prefix_)
     144                 :            :     {
     145                 :            :         // Position the cursor on the highest key before the first key we want,
     146                 :            :         // so that the first call to next() will put us on the first key we
     147                 :            :         // want.
     148         [ +  + ]:      30883 :         if (prefix.empty()) {
     149         [ +  - ]:         22 :             cursor->rewind();
     150                 :            :         } else {
     151                 :            :             // Seek to the first key before one with the desired prefix.
     152         [ +  - ]:      30861 :             cursor->find_entry_lt(prefix);
     153                 :            :         }
     154                 :      30883 :     }
     155                 :            : 
     156                 :            :     /// Destructor.
     157                 :            :     ~GlassSynonymTermList();
     158                 :            : 
     159                 :            :     Xapian::termcount get_approx_size() const;
     160                 :            : 
     161                 :            :     /** Returns the current termname.
     162                 :            :      *
     163                 :            :      *  Either next() or skip_to() must have been called before this
     164                 :            :      *  method can be called.
     165                 :            :      */
     166                 :            :     string get_termname() const;
     167                 :            : 
     168                 :            :     /// Return the term frequency for the term at the current position.
     169                 :            :     Xapian::doccount get_termfreq() const;
     170                 :            : 
     171                 :            :     /// Return the collection frequency for the term at the current position.
     172                 :            :     Xapian::termcount get_collection_freq() const;
     173                 :            : 
     174                 :            :     /// Advance to the next term in the list.
     175                 :            :     TermList * next();
     176                 :            : 
     177                 :            :     /// Advance to the first term which is >= tname.
     178                 :            :     TermList * skip_to(const string &tname);
     179                 :            : 
     180                 :            :     /// True if we're off the end of the list
     181                 :            :     bool at_end() const;
     182                 :            : };
     183                 :            : 
     184                 :            : #endif // XAPIAN_INCLUDED_GLASS_SYNONYM_H

Generated by: LCOV version 1.11