LCOV - code coverage report
Current view: top level - backends/glass - glass_synonym.cc (source / functions) Hit Total Coverage
Test: Test Coverage for xapian-core 954b5873a738 Lines: 82 99 82.8 %
Date: 2019-06-30 05:20:33 Functions: 11 13 84.6 %
Branches: 74 150 49.3 %

           Branch data     Line data    Source code
       1                 :            : /** @file glass_synonym.cc
       2                 :            :  * @brief Synonym data for a glass 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 "glass_synonym.h"
      23                 :            : 
      24                 :            : #include "xapian/error.h"
      25                 :            : 
      26                 :            : #include "glass_cursor.h"
      27                 :            : #include "glass_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                 :      40158 : GlassSynonymTable::merge_changes()
      45                 :            : {
      46         [ +  + ]:      40304 :     if (last_term.empty()) return;
      47                 :            : 
      48         [ +  + ]:        152 :     if (last_synonyms.empty()) {
      49                 :         10 :         del(last_term);
      50                 :            :     } else {
      51         [ +  - ]:        142 :         string tag;
      52                 :            : 
      53                 :        142 :         set<string>::const_iterator i;
      54         [ +  + ]:        293 :         for (i = last_synonyms.begin(); i != last_synonyms.end(); ++i) {
      55                 :        151 :             const string & synonym = *i;
      56         [ +  - ]:        151 :             tag += uint8_t(synonym.size() ^ MAGIC_XOR_VALUE);
      57         [ +  - ]:        151 :             tag += synonym;
      58                 :            :         }
      59                 :            : 
      60 [ +  - ][ +  - ]:        142 :         add(last_term, tag);
      61                 :        142 :         last_synonyms.clear();
      62                 :            :     }
      63                 :        146 :     last_term.resize(0);
      64                 :            : }
      65                 :            : 
      66                 :            : void
      67                 :        151 : GlassSynonymTable::add_synonym(const string & term, const string & synonym)
      68                 :            : {
      69         [ +  + ]:        151 :     if (last_term != term) {
      70         [ +  - ]:        142 :         merge_changes();
      71         [ +  - ]:        142 :         last_term = term;
      72                 :            : 
      73         [ +  - ]:        142 :         string tag;
      74 [ +  + ][ -  + ]:        142 :         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                 :        142 :         }
      87                 :            :     }
      88                 :            : 
      89                 :        149 :     last_synonyms.insert(synonym);
      90                 :        149 : }
      91                 :            : 
      92                 :            : void
      93                 :          5 : GlassSynonymTable::remove_synonym(const string & term, const string & synonym)
      94                 :            : {
      95         [ +  - ]:          5 :     if (last_term != term) {
      96         [ +  + ]:          5 :         merge_changes();
      97         [ +  - ]:          3 :         last_term = term;
      98                 :            : 
      99         [ +  - ]:          3 :         string tag;
     100 [ +  - ][ +  + ]:          3 :         if (get_exact_entry(term, tag)) {
     101                 :          2 :             const char * p = tag.data();
     102                 :          2 :             const char * end = p + tag.size();
     103         [ +  + ]:          6 :             while (p != end) {
     104                 :            :                 size_t len;
     105 [ +  - ][ -  + ]:          4 :                 if (p == end ||
                 [ -  + ]
     106                 :          8 :                     (len = uint8_t(*p) ^ MAGIC_XOR_VALUE) >= size_t(end - p))
     107 [ #  # ][ #  # ]:          0 :                     throw Xapian::DatabaseCorruptError("Bad synonym data");
                 [ #  # ]
     108                 :          4 :                 ++p;
     109 [ +  - ][ +  - ]:          4 :                 last_synonyms.insert(string(p, len));
     110                 :          4 :                 p += len;
     111                 :            :             }
     112                 :          3 :         }
     113                 :            :     }
     114                 :            : 
     115                 :          3 :     last_synonyms.erase(synonym);
     116                 :          3 : }
     117                 :            : 
     118                 :            : void
     119                 :          5 : GlassSynonymTable::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         [ -  + ]:          5 :     if (last_term == term) {
     127                 :          0 :         last_synonyms.clear();
     128                 :            :     } else {
     129                 :          5 :         merge_changes();
     130                 :          3 :         last_term = term;
     131                 :            :     }
     132                 :          3 : }
     133                 :            : 
     134                 :            : TermList *
     135                 :     103032 : GlassSynonymTable::open_termlist(const string & term)
     136                 :            : {
     137                 :     103032 :     vector<string> synonyms;
     138                 :            : 
     139 [ +  - ][ +  + ]:     103032 :     if (last_term == term) {
     140         [ +  + ]:          5 :         if (last_synonyms.empty()) return NULL;
     141                 :            : 
     142         [ +  - ]:          2 :         synonyms.reserve(last_synonyms.size());
     143                 :          2 :         set<string>::const_iterator i;
     144         [ +  + ]:          6 :         for (i = last_synonyms.begin(); i != last_synonyms.end(); ++i) {
     145         [ +  - ]:          4 :             synonyms.push_back(*i);
     146                 :            :         }
     147                 :            :     } else {
     148         [ +  - ]:     103027 :         string tag;
     149 [ +  + ][ +  + ]:     103027 :         if (!get_exact_entry(term, tag)) return NULL;
     150                 :            : 
     151                 :      30636 :         const char * p = tag.data();
     152                 :      30636 :         const char * end = p + tag.size();
     153 [ +  + ][ +  + ]:     133693 :         while (p != end) {
     154                 :            :             size_t len;
     155 [ +  - ][ -  + ]:      30668 :             if (p == end ||
                 [ -  + ]
     156                 :      61336 :                 (len = uint8_t(*p) ^ MAGIC_XOR_VALUE) >= size_t(end - p))
     157 [ #  # ][ #  # ]:          0 :                 throw Xapian::DatabaseCorruptError("Bad synonym data");
                 [ #  # ]
     158                 :      30668 :             ++p;
     159 [ +  - ][ +  - ]:      30668 :             synonyms.push_back(string(p, len));
     160                 :      30668 :             p += len;
     161                 :     103027 :         }
     162                 :            :     }
     163                 :            : 
     164 [ +  - ][ +  - ]:     103032 :     return new VectorTermList(synonyms.begin(), synonyms.end());
     165                 :            : }
     166                 :            : 
     167                 :            : ///////////////////////////////////////////////////////////////////////////
     168                 :            : 
     169                 :      92649 : GlassSynonymTermList::~GlassSynonymTermList()
     170                 :            : {
     171                 :            :     LOGCALL_DTOR(DB, "GlassSynonymTermList");
     172         [ +  - ]:      30883 :     delete cursor;
     173         [ -  + ]:      61766 : }
     174                 :            : 
     175                 :            : Xapian::termcount
     176                 :      20586 : GlassSynonymTermList::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                 :      20586 :     return database->synonym_table.get_entry_count();
     181                 :            : }
     182                 :            : 
     183                 :            : string
     184                 :      27933 : GlassSynonymTermList::get_termname() const
     185                 :            : {
     186                 :            :     LOGCALL(DB, string, "GlassSynonymTermList::get_termname", NO_ARGS);
     187                 :            :     Assert(cursor);
     188                 :            :     Assert(!cursor->current_key.empty());
     189                 :            :     Assert(!at_end());
     190                 :      27933 :     RETURN(cursor->current_key);
     191                 :            : }
     192                 :            : 
     193                 :            : Xapian::doccount
     194                 :          0 : GlassSynonymTermList::get_termfreq() const
     195                 :            : {
     196 [ #  # ][ #  # ]:          0 :     throw Xapian::InvalidOperationError("GlassSynonymTermList::get_termfreq() not meaningful");
                 [ #  # ]
     197                 :            : }
     198                 :            : 
     199                 :            : Xapian::termcount
     200                 :          0 : GlassSynonymTermList::get_collection_freq() const
     201                 :            : {
     202 [ #  # ][ #  # ]:          0 :     throw Xapian::InvalidOperationError("GlassSynonymTermList::get_collection_freq() not meaningful");
                 [ #  # ]
     203                 :            : }
     204                 :            : 
     205                 :            : TermList *
     206                 :      30992 : GlassSynonymTermList::next()
     207                 :            : {
     208                 :            :     LOGCALL(DB, TermList *, "GlassSynonymTermList::next", NO_ARGS);
     209                 :            :     Assert(!at_end());
     210                 :            : 
     211                 :      30992 :     cursor->next();
     212 [ +  + ][ +  + ]:      30992 :     if (!cursor->after_end() && !startswith(cursor->current_key, prefix)) {
                 [ +  + ]
     213                 :            :         // We've reached the end of the prefixed terms.
     214                 :      10004 :         cursor->to_end();
     215                 :            :     }
     216                 :            : 
     217                 :      30992 :     RETURN(NULL);
     218                 :            : }
     219                 :            : 
     220                 :            : TermList *
     221                 :      37978 : GlassSynonymTermList::skip_to(const string &tname)
     222                 :            : {
     223                 :            :     LOGCALL(DB, TermList *, "GlassSynonymTermList::skip_to", tname);
     224                 :            :     Assert(!at_end());
     225                 :            : 
     226         [ +  + ]:      37978 :     if (!cursor->find_entry_ge(tname)) {
     227                 :            :         // The exact term we asked for isn't there, so check if the next
     228                 :            :         // term after it also has the right prefix.
     229 [ +  + ][ -  + ]:      27244 :         if (!cursor->after_end() && !startswith(cursor->current_key, prefix)) {
                 [ -  + ]
     230                 :            :             // We've reached the end of the prefixed terms.
     231                 :          0 :             cursor->to_end();
     232                 :            :         }
     233                 :            :     }
     234                 :      37978 :     RETURN(NULL);
     235                 :            : }
     236                 :            : 
     237                 :            : bool
     238                 :      74260 : GlassSynonymTermList::at_end() const
     239                 :            : {
     240                 :            :     LOGCALL(DB, bool, "GlassSynonymTermList::at_end", NO_ARGS);
     241                 :      74260 :     RETURN(cursor->after_end());
     242                 :            : }

Generated by: LCOV version 1.11