LCOV - code coverage report
Current view: top level - api - keymaker.cc (source / functions) Hit Total Coverage
Test: Test Coverage for xapian-core 7028d852e609 Lines: 37 37 100.0 %
Date: 2019-02-17 14:59:59 Functions: 2 2 100.0 %
Branches: 48 64 75.0 %

           Branch data     Line data    Source code
       1                 :            : /** @file keymaker.cc
       2                 :            :  * @brief Build key strings for MSet ordering or collapsing.
       3                 :            :  */
       4                 :            : /* Copyright (C) 2007,2009,2011,2015 Olly Betts
       5                 :            :  * Copyright (C) 2010 Richard Boulton
       6                 :            :  *
       7                 :            :  * This program is free software; you can redistribute it and/or
       8                 :            :  * modify it under the terms of the GNU General Public License as
       9                 :            :  * published by the Free Software Foundation; either version 2 of the
      10                 :            :  * License, or (at your option) any later version.
      11                 :            :  *
      12                 :            :  * This program is distributed in the hope that it will be useful,
      13                 :            :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      14                 :            :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      15                 :            :  * GNU General Public License for more details.
      16                 :            :  *
      17                 :            :  * You should have received a copy of the GNU General Public License
      18                 :            :  * along with this program; if not, write to the Free Software
      19                 :            :  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
      20                 :            :  */
      21                 :            : 
      22                 :            : #include <config.h>
      23                 :            : 
      24                 :            : #include "xapian/keymaker.h"
      25                 :            : 
      26                 :            : #include "xapian/document.h"
      27                 :            : 
      28                 :            : #include <string>
      29                 :            : #include <vector>
      30                 :            : 
      31                 :            : using namespace std;
      32                 :            : 
      33                 :            : namespace Xapian {
      34                 :            : 
      35         [ -  + ]:        242 : KeyMaker::~KeyMaker() { }
      36                 :            : 
      37                 :            : string
      38                 :        548 : MultiValueKeyMaker::operator()(const Xapian::Document & doc) const
      39                 :            : {
      40         [ +  - ]:        548 :     string result;
      41                 :            : 
      42                 :        548 :     auto i = slots.begin();
      43                 :            :     // Don't crash if slots is empty.
      44         [ +  + ]:        548 :     if (rare(i == slots.end())) return result;
      45                 :            : 
      46                 :        503 :     size_t last_not_empty_forwards = 0;
      47                 :            :     while (true) {
      48                 :            :         // All values (except for the last if it's sorted forwards) need to
      49                 :            :         // be adjusted.
      50                 :            :         //
      51                 :            :         // FIXME: allow Xapian::BAD_VALUENO to mean "relevance?"
      52         [ +  - ]:        822 :         string v = doc.get_value(i->slot);
      53                 :        822 :         bool reverse_sort = i->reverse;
      54                 :            : 
      55         [ +  + ]:        822 :         if (v.empty()) {
      56         [ +  - ]:        225 :             v = i->defvalue;
      57                 :            :         }
      58                 :            : 
      59 [ +  + ][ +  + ]:        822 :         if (reverse_sort || !v.empty())
                 [ +  + ]
      60                 :        694 :             last_not_empty_forwards = result.size();
      61                 :            : 
      62 [ +  + ][ +  + ]:        822 :         if (++i == slots.end() && !reverse_sort) {
         [ +  - ][ +  + ]
      63         [ +  + ]:        231 :             if (v.empty()) {
      64                 :            :                 // Trim off all the trailing empty forwards values.
      65         [ +  - ]:         49 :                 result.resize(last_not_empty_forwards);
      66                 :            :             } else {
      67                 :            :                 // No need to adjust the last value if it's sorted forwards.
      68         [ +  - ]:        182 :                 result += v;
      69                 :            :             }
      70                 :        231 :             break;
      71                 :            :         }
      72                 :            : 
      73         [ +  + ]:        591 :         if (reverse_sort) {
      74                 :            :             // For a reverse ordered value, we subtract each byte from '\xff',
      75                 :            :             // except for '\0' which we convert to "\xff\0".  We insert
      76                 :            :             // "\xff\xff" after the encoded value.
      77 [ +  - ][ +  - ]:        724 :             for (string::const_iterator j = v.begin(); j != v.end(); ++j) {
                 [ +  + ]
      78                 :        419 :                 unsigned char ch = static_cast<unsigned char>(*j);
      79         [ +  - ]:        419 :                 result += char(255 - ch);
      80 [ +  + ][ +  - ]:        419 :                 if (ch == 0) result += '\0';
      81                 :            :             }
      82         [ +  - ]:        305 :             result.append("\xff\xff", 2);
      83         [ +  + ]:        305 :             if (i == slots.end()) break;
      84                 :         33 :             last_not_empty_forwards = result.size();
      85                 :            :         } else {
      86                 :            :             // For a forward ordered value (unless it's the last value), we
      87                 :            :             // convert any '\0' to "\0\xff".  We insert "\0\0" after the
      88                 :            :             // encoded value.
      89                 :        286 :             string::size_type j = 0, nul;
      90         [ +  + ]:        298 :             while ((nul = v.find('\0', j)) != string::npos) {
      91                 :         12 :                 ++nul;
      92         [ +  - ]:         12 :                 result.append(v, j, nul - j);
      93         [ +  - ]:         12 :                 result += '\xff';
      94                 :         12 :                 j = nul;
      95                 :            :             }
      96         [ +  - ]:        286 :             result.append(v, j, string::npos);
      97         [ +  + ]:        286 :             if (!v.empty())
      98                 :        207 :                 last_not_empty_forwards = result.size();
      99 [ +  - ][ +  + ]:        822 :             result.append("\0", 2);
     100                 :            :         }
     101                 :        319 :     }
     102                 :        548 :     return result;
     103                 :            : }
     104                 :            : 
     105                 :            : }

Generated by: LCOV version 1.11