LCOV - code coverage report
Current view: top level - backends/honey - honey_synonym.cc (source / functions) Hit Total Coverage
Test: Test Coverage for xapian-core 954b5873a738 Lines: 12 106 11.3 %
Date: 2019-06-30 05:20:33 Functions: 3 13 23.1 %
Branches: 7 162 4.3 %

           Branch data     Line data    Source code
       1                 :            : /** @file honey_synonym.cc
       2                 :            :  * @brief Synonym data for a honey database.
       3                 :            :  */
       4                 :            : /* Copyright (C) 2004,2005,2006,2007,2008,2009,2011,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                 :            : #include <config.h>
      22                 :            : #include "honey_synonym.h"
      23                 :            : 
      24                 :            : #include "xapian/error.h"
      25                 :            : 
      26                 :            : #include "honey_cursor.h"
      27                 :            : #include "honey_database.h"
      28                 :            : #include "debuglog.h"
      29                 :            : #include "stringutils.h"
      30                 :            : #include "api/vectortermlist.h"
      31                 :            : 
      32                 :            : #include <set>
      33                 :            : #include <string>
      34                 :            : #include <vector>
      35                 :            : 
      36                 :            : using namespace std;
      37                 :            : 
      38                 :            : // We XOR the length values with this so that they are more likely to coincide
      39                 :            : // with lower case ASCII letters, which are likely to be common.  This means
      40                 :            : // that zlib should do a better job of compressing tag values.
      41                 :            : #define MAGIC_XOR_VALUE 96
      42                 :            : 
      43                 :            : void
      44                 :          0 : HoneySynonymTable::merge_changes()
      45                 :            : {
      46         [ #  # ]:          0 :     if (last_term.empty()) return;
      47                 :            : 
      48         [ #  # ]:          0 :     if (last_synonyms.empty()) {
      49                 :          0 :         del(last_term);
      50                 :            :     } else {
      51         [ #  # ]:          0 :         string tag;
      52                 :            : 
      53                 :          0 :         set<string>::const_iterator i;
      54         [ #  # ]:          0 :         for (i = last_synonyms.begin(); i != last_synonyms.end(); ++i) {
      55                 :          0 :             const string & synonym = *i;
      56         [ #  # ]:          0 :             tag += uint8_t(synonym.size() ^ MAGIC_XOR_VALUE);
      57         [ #  # ]:          0 :             tag += synonym;
      58                 :            :         }
      59                 :            : 
      60         [ #  # ]:          0 :         add(last_term, tag);
      61                 :          0 :         last_synonyms.clear();
      62                 :            :     }
      63                 :          0 :     last_term.resize(0);
      64                 :            : }
      65                 :            : 
      66                 :            : void
      67                 :          0 : HoneySynonymTable::add_synonym(const string & term, const string & synonym)
      68                 :            : {
      69         [ #  # ]:          0 :     if (last_term != term) {
      70         [ #  # ]:          0 :         merge_changes();
      71         [ #  # ]:          0 :         last_term = term;
      72                 :            : 
      73         [ #  # ]:          0 :         string tag;
      74 [ #  # ][ #  # ]:          0 :         if (get_exact_entry(term, tag)) {
      75                 :          0 :             const char * p = tag.data();
      76                 :          0 :             const char * end = p + tag.size();
      77         [ #  # ]:          0 :             while (p != end) {
      78                 :            :                 size_t len;
      79 [ #  # ][ #  # ]:          0 :                 if (p == end ||
                 [ #  # ]
      80                 :          0 :                     (len = uint8_t(*p) ^ MAGIC_XOR_VALUE) >= size_t(end - p))
      81 [ #  # ][ #  # ]:          0 :                     throw Xapian::DatabaseCorruptError("Bad synonym data");
                 [ #  # ]
      82                 :          0 :                 ++p;
      83 [ #  # ][ #  # ]:          0 :                 last_synonyms.insert(string(p, len));
      84                 :          0 :                 p += len;
      85                 :            :             }
      86                 :          0 :         }
      87                 :            :     }
      88                 :            : 
      89                 :          0 :     last_synonyms.insert(synonym);
      90                 :          0 : }
      91                 :            : 
      92                 :            : void
      93                 :          0 : HoneySynonymTable::remove_synonym(const string & term, const string & synonym)
      94                 :            : {
      95         [ #  # ]:          0 :     if (last_term != term) {
      96         [ #  # ]:          0 :         merge_changes();
      97         [ #  # ]:          0 :         last_term = term;
      98                 :            : 
      99         [ #  # ]:          0 :         string tag;
     100 [ #  # ][ #  # ]:          0 :         if (get_exact_entry(term, tag)) {
     101                 :          0 :             const char * p = tag.data();
     102                 :          0 :             const char * end = p + tag.size();
     103         [ #  # ]:          0 :             while (p != end) {
     104                 :            :                 size_t len;
     105 [ #  # ][ #  # ]:          0 :                 if (p == end ||
                 [ #  # ]
     106                 :          0 :                     (len = uint8_t(*p) ^ MAGIC_XOR_VALUE) >= size_t(end - p))
     107 [ #  # ][ #  # ]:          0 :                     throw Xapian::DatabaseCorruptError("Bad synonym data");
                 [ #  # ]
     108                 :          0 :                 ++p;
     109 [ #  # ][ #  # ]:          0 :                 last_synonyms.insert(string(p, len));
     110                 :          0 :                 p += len;
     111                 :            :             }
     112                 :          0 :         }
     113                 :            :     }
     114                 :            : 
     115                 :          0 :     last_synonyms.erase(synonym);
     116                 :          0 : }
     117                 :            : 
     118                 :            : void
     119                 :          0 : HoneySynonymTable::clear_synonyms(const string & term)
     120                 :            : {
     121                 :            :     // We don't actually ever need to merge_changes() here, but it's quite
     122                 :            :     // likely that someone might clear_synonyms() and then add_synonym() for
     123                 :            :     // the same term.  The alternative we could otherwise optimise for (modify
     124                 :            :     // synonyms for a term, then clear those for another, then modify those for
     125                 :            :     // the first term again) seems much less likely.
     126         [ #  # ]:          0 :     if (last_term == term) {
     127                 :          0 :         last_synonyms.clear();
     128                 :            :     } else {
     129                 :          0 :         merge_changes();
     130                 :          0 :         last_term = term;
     131                 :            :     }
     132                 :          0 : }
     133                 :            : 
     134                 :            : TermList *
     135                 :          0 : HoneySynonymTable::open_termlist(const string & term) const
     136                 :            : {
     137                 :          0 :     vector<string> synonyms;
     138                 :            : 
     139 [ #  # ][ #  # ]:          0 :     if (last_term == term) {
     140         [ #  # ]:          0 :         if (last_synonyms.empty()) return NULL;
     141                 :            : 
     142         [ #  # ]:          0 :         synonyms.reserve(last_synonyms.size());
     143                 :          0 :         set<string>::const_iterator i;
     144         [ #  # ]:          0 :         for (i = last_synonyms.begin(); i != last_synonyms.end(); ++i) {
     145         [ #  # ]:          0 :             synonyms.push_back(*i);
     146                 :            :         }
     147                 :            :     } else {
     148         [ #  # ]:          0 :         string tag;
     149 [ #  # ][ #  # ]:          0 :         if (!get_exact_entry(term, tag)) return NULL;
     150                 :            : 
     151                 :          0 :         const char * p = tag.data();
     152                 :          0 :         const char * end = p + tag.size();
     153 [ #  # ][ #  # ]:          0 :         while (p != end) {
     154                 :            :             size_t len;
     155 [ #  # ][ #  # ]:          0 :             if (p == end ||
                 [ #  # ]
     156                 :          0 :                 (len = uint8_t(*p) ^ MAGIC_XOR_VALUE) >= size_t(end - p))
     157 [ #  # ][ #  # ]:          0 :                 throw Xapian::DatabaseCorruptError("Bad synonym data");
                 [ #  # ]
     158                 :          0 :             ++p;
     159 [ #  # ][ #  # ]:          0 :             synonyms.push_back(string(p, len));
     160                 :          0 :             p += len;
     161                 :          0 :         }
     162                 :            :     }
     163                 :            : 
     164 [ #  # ][ #  # ]:          0 :     return new VectorTermList(synonyms.begin(), synonyms.end());
     165                 :            : }
     166                 :            : 
     167                 :            : ///////////////////////////////////////////////////////////////////////////
     168                 :            : 
     169                 :          9 : HoneySynonymTermList::~HoneySynonymTermList()
     170                 :            : {
     171                 :            :     LOGCALL_DTOR(DB, "HoneySynonymTermList");
     172         [ -  + ]:          3 :     delete cursor;
     173         [ -  + ]:          6 : }
     174                 :            : 
     175                 :            : Xapian::termcount
     176                 :          0 : HoneySynonymTermList::get_approx_size() const
     177                 :            : {
     178                 :            :     // This is an over-estimate, but we only use this value to build a balanced
     179                 :            :     // or-tree, and it'll do a decent enough job for that.
     180                 :          0 :     return database->synonym_table.get_approx_entry_count();
     181                 :            : }
     182                 :            : 
     183                 :            : string
     184                 :          0 : HoneySynonymTermList::get_termname() const
     185                 :            : {
     186                 :            :     LOGCALL(DB, string, "HoneySynonymTermList::get_termname", NO_ARGS);
     187                 :            :     Assert(cursor);
     188                 :            :     Assert(!cursor->current_key.empty());
     189                 :            :     Assert(!at_end());
     190                 :          0 :     RETURN(cursor->current_key);
     191                 :            : }
     192                 :            : 
     193                 :            : Xapian::doccount
     194                 :          0 : HoneySynonymTermList::get_termfreq() const
     195                 :            : {
     196                 :            :     throw Xapian::InvalidOperationError("HoneySynonymTermList::get_termfreq() "
     197 [ #  # ][ #  # ]:          0 :                                         "not meaningful");
                 [ #  # ]
     198                 :            : }
     199                 :            : 
     200                 :            : Xapian::termcount
     201                 :          0 : HoneySynonymTermList::get_collection_freq() const
     202                 :            : {
     203                 :            :     throw Xapian::InvalidOperationError("HoneySynonymTermList::"
     204                 :            :                                         "get_collection_freq() "
     205 [ #  # ][ #  # ]:          0 :                                         "not meaningful");
                 [ #  # ]
     206                 :            : }
     207                 :            : 
     208                 :            : TermList *
     209                 :          3 : HoneySynonymTermList::next()
     210                 :            : {
     211                 :            :     LOGCALL(DB, TermList *, "HoneySynonymTermList::next", NO_ARGS);
     212                 :            :     Assert(!at_end());
     213                 :            : 
     214         [ +  - ]:          3 :     if (cursor->after_end()) {
     215                 :            :         // This is the first action on a new HoneySynonymTermList.
     216         [ -  + ]:          3 :         if (cursor->find_entry_ge(prefix))
     217                 :          0 :             RETURN(NULL);
     218                 :            :     } else {
     219                 :          0 :         cursor->next();
     220                 :            :     }
     221 [ -  + ][ #  # ]:          3 :     if (cursor->after_end() || !startswith(cursor->current_key, prefix)) {
                 [ +  - ]
     222                 :            :         // We've reached the end of the prefixed terms.
     223         [ +  - ]:          3 :         delete cursor;
     224                 :          3 :         cursor = NULL;
     225                 :            :     }
     226                 :            : 
     227                 :          3 :     RETURN(NULL);
     228                 :            : }
     229                 :            : 
     230                 :            : TermList *
     231                 :          0 : HoneySynonymTermList::skip_to(const string &term)
     232                 :            : {
     233                 :            :     LOGCALL(DB, TermList *, "HoneySynonymTermList::skip_to", term);
     234                 :            :     Assert(!at_end());
     235                 :            : 
     236 [ #  # ][ #  # ]:          0 :     if (cursor->after_end() && prefix > term) {
                 [ #  # ]
     237                 :            :         // This is the first action on a new HoneySynonymTermList and we were
     238                 :            :         // asked to skip to a term before the prefix - this ought to leave us
     239                 :            :         // on the first term with the specified prefix.
     240                 :          0 :         RETURN(skip_to(prefix));
     241                 :            :     }
     242                 :            : 
     243         [ #  # ]:          0 :     if (!cursor->find_entry_ge(term)) {
     244                 :            :         // The exact term we asked for isn't there, so check if the next
     245                 :            :         // term after it also has the right prefix.
     246 [ #  # ][ #  # ]:          0 :         if (cursor->after_end() || !startswith(cursor->current_key, prefix)) {
                 [ #  # ]
     247                 :            :             // We've reached the end of the prefixed terms.
     248         [ #  # ]:          0 :             delete cursor;
     249                 :          0 :             cursor = NULL;
     250                 :            :         }
     251                 :            :     }
     252                 :          0 :     RETURN(NULL);
     253                 :            : }
     254                 :            : 
     255                 :            : bool
     256                 :          3 : HoneySynonymTermList::at_end() const
     257                 :            : {
     258                 :            :     LOGCALL(DB, bool, "HoneySynonymTermList::at_end", NO_ARGS);
     259                 :          3 :     RETURN(cursor == NULL);
     260                 :            : }

Generated by: LCOV version 1.11