LCOV - code coverage report
Current view: top level - backends/honey - honey_docdata.h (source / functions) Hit Total Coverage
Test: Test Coverage for xapian-core 954b5873a738 Lines: 13 13 100.0 %
Date: 2019-06-30 05:20:33 Functions: 5 5 100.0 %
Branches: 4 8 50.0 %

           Branch data     Line data    Source code
       1                 :            : /** @file honey_docdata.h
       2                 :            :  * @brief Subclass of HoneyTable which holds document data.
       3                 :            :  */
       4                 :            : /* Copyright (C) 2007,2008,2009,2010,2014,2016 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_HONEY_DOCDATA_H
      22                 :            : #define XAPIAN_INCLUDED_HONEY_DOCDATA_H
      23                 :            : 
      24                 :            : #include <xapian/types.h>
      25                 :            : 
      26                 :            : #include "honey_lazytable.h"
      27                 :            : #include "pack.h"
      28                 :            : 
      29                 :            : #include <string>
      30                 :            : 
      31                 :        710 : class HoneyDocDataTable : public HoneyLazyTable {
      32                 :            :   public:
      33                 :       1269 :     static std::string make_key(Xapian::docid did) {
      34                 :       1269 :         std::string key;
      35         [ +  - ]:       1269 :         pack_uint_preserving_sort(key, did);
      36                 :       1269 :         return key;
      37                 :            :     }
      38                 :            : 
      39                 :            :     /** Create a new HoneyDocDataTable object.
      40                 :            :      *
      41                 :            :      *  This method does not create or open the table on disk - you
      42                 :            :      *  must call the create() or open() methods respectively!
      43                 :            :      *
      44                 :            :      *  @param dbdir        The directory the honey database is stored in.
      45                 :            :      *  @param readonly     true if we're opening read-only, else false.
      46                 :            :      */
      47                 :        354 :     HoneyDocDataTable(const std::string & dbdir, bool readonly)
      48         [ +  - ]:        354 :         : HoneyLazyTable("docdata", dbdir + "/docdata.", readonly) { }
      49                 :            : 
      50                 :          1 :     HoneyDocDataTable(int fd, off_t offset_, bool readonly)
      51                 :          1 :         : HoneyLazyTable("docdata", fd, offset_, readonly) { }
      52                 :            : 
      53                 :            :     /** Get the document data for document @a did.
      54                 :            :      *
      55                 :            :      *  If the document doesn't exist, the empty string is returned.
      56                 :            :      *
      57                 :            :      *  @param did      The docid to set the document data for.
      58                 :            :      */
      59                 :       1269 :     std::string get_document_data(Xapian::docid did) const {
      60                 :            :         // We don't store the document data if it is empty.
      61                 :       1269 :         std::string data;
      62 [ +  - ][ +  - ]:       1269 :         (void)get_exact_entry(make_key(did), data);
      63                 :       1269 :         return data;
      64                 :            :     }
      65                 :            : 
      66                 :            :     /** Set the document data for document @a did.
      67                 :            :      *
      68                 :            :      *  If the document might already exist, use replace_document_data()
      69                 :            :      *  instead.
      70                 :            :      *
      71                 :            :      *  @param did      The docid to set the document data for.
      72                 :            :      *  @param data     The document data to set.
      73                 :            :      */
      74                 :            :     void add_document_data(Xapian::docid did, const std::string & data) {
      75                 :            :         // We don't store the document data if it is empty.
      76                 :            :         if (!data.empty())
      77                 :            :             add(make_key(did), data);
      78                 :            :     }
      79                 :            : 
      80                 :            :     /** Replace the document data for document @a did.
      81                 :            :      *
      82                 :            :      *  Any existing data is replaced.
      83                 :            :      *
      84                 :            :      *  @param did      The docid to replace the document data for.
      85                 :            :      *  @param data     The document data to set.
      86                 :            :      */
      87                 :            :     void replace_document_data(Xapian::docid did, const std::string & data) {
      88                 :            :         if (data.empty()) {
      89                 :            :             // We don't store the document data if it is empty.
      90                 :            :             delete_document_data(did);
      91                 :            :             return;
      92                 :            :         }
      93                 :            :         add(make_key(did), data);
      94                 :            :     }
      95                 :            : 
      96                 :            :     /** Delete the document data for document @a did.
      97                 :            :      *
      98                 :            :      *  @param did      The docid to delete the document data for.
      99                 :            :      *
     100                 :            :      *  @return true if document data was actually removed (false means either
     101                 :            :      *               there's no such document, or the document has no data).
     102                 :            :      */
     103                 :            :     bool delete_document_data(Xapian::docid did) { return del(make_key(did)); }
     104                 :            : 
     105                 :            :     void readahead_for_document(Xapian::docid did) const {
     106                 :            :         readahead_key(make_key(did));
     107                 :            :     }
     108                 :            : };
     109                 :            : 
     110                 :            : #endif // XAPIAN_INCLUDED_HONEY_DOCDATA_H

Generated by: LCOV version 1.11