LCOV - code coverage report
Current view: top level - backends/glass - glass_termlisttable.cc (source / functions) Hit Total Coverage
Test: Test Coverage for xapian-core 954b5873a738 Lines: 34 34 100.0 %
Date: 2019-06-30 05:20:33 Functions: 1 1 100.0 %
Branches: 40 70 57.1 %

           Branch data     Line data    Source code
       1                 :            : /** @file glass_termlisttable.cc
       2                 :            :  * @brief Subclass of GlassTable which holds termlists.
       3                 :            :  */
       4                 :            : /* Copyright (C) 2007,2008,2009,2010 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                 :            : 
      23                 :            : #include "glass_termlisttable.h"
      24                 :            : 
      25                 :            : #include <xapian/document.h>
      26                 :            : #include <xapian/error.h>
      27                 :            : #include <xapian/termiterator.h>
      28                 :            : 
      29                 :            : #include "debuglog.h"
      30                 :            : #include "omassert.h"
      31                 :            : #include "pack.h"
      32                 :            : #include "stringutils.h"
      33                 :            : 
      34                 :            : #include <string>
      35                 :            : 
      36                 :            : using namespace std;
      37                 :            : 
      38                 :            : void
      39                 :     132139 : GlassTermListTable::set_termlist(Xapian::docid did,
      40                 :            :                                  const Xapian::Document & doc,
      41                 :            :                                  Xapian::termcount doclen)
      42                 :            : {
      43                 :            :     LOGCALL_VOID(DB, "GlassTermListTable::set_termlist", did | doc | doclen);
      44                 :            : 
      45         [ +  - ]:     132139 :     Xapian::doccount termlist_size = doc.termlist_count();
      46         [ +  + ]:     132139 :     if (termlist_size == 0) {
      47                 :            :         // doclen is sum(wdf) so should be zero if there are no terms.
      48                 :            :         Assert(doclen == 0);
      49                 :            :         Assert(doc.termlist_begin() == doc.termlist_end());
      50 [ +  - ][ +  - ]:      48126 :         add(make_key(did), string());
                 [ +  - ]
      51                 :     132139 :         return;
      52                 :            :     }
      53                 :            : 
      54         [ +  - ]:      84013 :     string tag;
      55         [ +  - ]:      84013 :     pack_uint(tag, doclen);
      56                 :            : 
      57         [ +  - ]:     168026 :     Xapian::TermIterator t = doc.termlist_begin();
      58         [ +  - ]:      84013 :     if (t != doc.termlist_end()) {
      59         [ +  - ]:      84013 :         pack_uint(tag, termlist_size);
      60         [ +  - ]:      84013 :         string prev_term = *t;
      61                 :            : 
      62         [ +  - ]:      84013 :         tag += char(prev_term.size());
      63         [ +  - ]:      84013 :         tag += prev_term;
      64 [ +  - ][ +  - ]:      84013 :         pack_uint(tag, t.get_wdf());
      65                 :      84013 :         --termlist_size;
      66                 :            : 
      67 [ +  - ][ +  + ]:    2036207 :         while (++t != doc.termlist_end()) {
      68         [ +  - ]:    1952194 :             const string & term = *t;
      69                 :            :             // If there's a shared prefix with the previous term, we don't
      70                 :            :             // store it explicitly, but just store the length of the shared
      71                 :            :             // prefix.  In general, this is a big win.
      72         [ +  - ]:    1952194 :             size_t reuse = common_prefix_length(prev_term, term);
      73                 :            : 
      74                 :            :             // reuse must be <= prev_term.size(), and we know that value while
      75                 :            :             // decoding.  So if the wdf is small enough that we can multiply it
      76                 :            :             // by (prev_term.size() + 1), add reuse and fit the result in a
      77                 :            :             // byte, then we can pack reuse and the wdf into a single byte and
      78                 :            :             // save ourselves a byte.  We actually need to add one to the wdf
      79                 :            :             // before multiplying so that a wdf of 0 can be detected by the
      80                 :            :             // decoder.
      81                 :    1952194 :             size_t packed = 0;
      82         [ +  - ]:    1952194 :             Xapian::termcount wdf = t.get_wdf();
      83                 :            :             // If wdf >= 128, then we aren't going to be able to pack it in so
      84                 :            :             // don't even try to avoid the calculation overflowing and making
      85                 :            :             // us think we can.
      86         [ +  + ]:    1952194 :             if (wdf < 127)
      87                 :    1952182 :                 packed = (wdf + 1) * (prev_term.size() + 1) + reuse;
      88                 :            : 
      89 [ +  + ][ +  + ]:    1952194 :             if (packed && packed < 256) {
      90                 :            :                 // We can pack the wdf into the same byte.
      91         [ +  - ]:    1952179 :                 tag += char(packed);
      92         [ +  - ]:    1952179 :                 tag += char(term.size() - reuse);
      93         [ +  - ]:    1952179 :                 tag.append(term.data() + reuse, term.size() - reuse);
      94                 :            :             } else {
      95         [ +  - ]:         15 :                 tag += char(reuse);
      96         [ +  - ]:         15 :                 tag += char(term.size() - reuse);
      97         [ +  - ]:         15 :                 tag.append(term.data() + reuse, term.size() - reuse);
      98                 :            :                 // FIXME: pack wdf after reuse next time we rejig the format
      99                 :            :                 // incompatibly.
     100         [ +  - ]:         15 :                 pack_uint(tag, wdf);
     101                 :            :             }
     102                 :            : 
     103 [ +  - ][ +  - ]:    1952194 :             prev_term = *t;
     104                 :    1952194 :             --termlist_size;
     105                 :    2036207 :         }
     106                 :            :     }
     107                 :            :     AssertEq(termlist_size, 0);
     108 [ +  - ][ +  - ]:     168026 :     add(make_key(did), tag);
                 [ +  - ]
     109                 :            : }

Generated by: LCOV version 1.11