LCOV - code coverage report
Current view: top level - tests/harness - backendmanager_honey.cc (source / functions) Hit Total Coverage
Test: Test Coverage for xapian-core 954b5873a738 Lines: 40 42 95.2 %
Date: 2019-06-30 05:20:33 Functions: 6 7 85.7 %
Branches: 28 60 46.7 %

           Branch data     Line data    Source code
       1                 :            : /** @file backendmanager_honey.cc
       2                 :            :  * @brief BackendManager subclass for honey databases
       3                 :            :  */
       4                 :            : /* Copyright (C) 2007,2008,2009,2013,2017,2018 Olly Betts
       5                 :            :  *
       6                 :            :  * This program is free software; you can redistribute it and/or
       7                 :            :  * modify it under the terms of the GNU General Public License as
       8                 :            :  * published by the Free Software Foundation; either version 2 of the
       9                 :            :  * License, or (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 "backendmanager_honey.h"
      24                 :            : 
      25                 :            : #include "filetests.h"
      26                 :            : #include "index_utils.h"
      27                 :            : #include "unixcmds.h"
      28                 :            : 
      29                 :            : #include <cstdio> // For rename().
      30                 :            : 
      31                 :            : using namespace std;
      32                 :            : 
      33                 :            : #define CACHE_DIRECTORY ".honey"
      34                 :            : 
      35                 :            : string
      36                 :         32 : BackendManagerHoney::get_dbtype() const
      37                 :            : {
      38         [ +  - ]:         32 :     return "honey";
      39                 :            : }
      40                 :            : 
      41                 :            : string
      42                 :        293 : BackendManagerHoney::do_get_database_path(const vector<string> & files)
      43                 :            : {
      44         [ +  - ]:        293 :     string dbdir = CACHE_DIRECTORY;
      45         [ +  - ]:        293 :     create_dir_if_needed(dbdir);
      46                 :            : 
      47         [ +  - ]:        586 :     string dbname = "db";
      48                 :        293 :     vector<string>::const_iterator i;
      49         [ +  + ]:        591 :     for (i = files.begin(); i != files.end(); ++i) {
      50         [ +  - ]:        298 :         dbname += "__";
      51         [ +  - ]:        298 :         dbname += *i;
      52                 :            :     }
      53 [ +  - ][ +  - ]:        293 :     string dbpath = dbdir + "/" + dbname;
      54                 :            : 
      55         [ -  + ]:        293 :     if (file_exists(dbpath)) return dbpath;
      56                 :            : 
      57         [ +  - ]:        586 :     string db_source = dbpath + ".src";
      58                 :        293 :     int flags = Xapian::DB_CREATE_OR_OVERWRITE | Xapian::DB_BACKEND_GLASS;
      59                 :            : 
      60         [ +  - ]:        586 :     string tmpfile = dbpath;
      61         [ +  - ]:        293 :     tmpfile += ".tmp";
      62                 :            : 
      63         [ +  - ]:        586 :     Xapian::WritableDatabase db(db_source, flags);
      64 [ +  - ][ +  - ]:        293 :     FileIndexer(get_datadir(), files).index_to(db);
      65         [ +  - ]:        293 :     db.commit();
      66         [ +  - ]:        293 :     db.compact(tmpfile, Xapian::DB_BACKEND_HONEY);
      67         [ +  - ]:        293 :     db.close();
      68                 :            : 
      69         [ +  - ]:        293 :     rm_rf(db_source);
      70                 :            : 
      71                 :        293 :     rename(tmpfile.c_str(), dbpath.c_str());
      72                 :            : 
      73                 :        586 :     return dbpath;
      74                 :            : }
      75                 :            : 
      76                 :            : Xapian::WritableDatabase
      77                 :         30 : BackendManagerHoney::get_generated_database(const string& name)
      78                 :            : {
      79                 :            :     // Create generated database inside glass cache
      80                 :            :     // to prevent a valid glass db inside honey cache
      81                 :            :     // if testsuite was interrupted.
      82                 :         30 :     return generated_sub_manager->get_generated_database(name);
      83                 :            : }
      84                 :            : 
      85                 :            : void
      86                 :         30 : BackendManagerHoney::finalise_generated_database(const string& name)
      87                 :            : {
      88                 :            :     // Convert a glass backend to honey.
      89                 :            : 
      90                 :            :     // path to the temporary generated db
      91                 :            :     string generated_db_path =
      92         [ +  - ]:         30 :         generated_sub_manager->get_generated_database_path(name);
      93                 :            : 
      94                 :            :     // path to honey tmpfile
      95         [ +  - ]:         60 :     string tmpfile = CACHE_DIRECTORY "/" + name;
      96         [ +  - ]:         30 :     tmpfile += ".tmp";
      97                 :            : 
      98                 :            :     // path to final honey db
      99         [ +  - ]:         60 :     string path = CACHE_DIRECTORY "/" + name;
     100                 :            : 
     101         [ +  - ]:         60 :     Xapian::WritableDatabase wdb(generated_db_path);
     102         [ +  - ]:         30 :     wdb.compact(tmpfile, Xapian::DB_BACKEND_HONEY);
     103         [ +  - ]:         30 :     wdb.close();
     104                 :            : 
     105                 :         60 :     rename(tmpfile.c_str(), path.c_str());
     106                 :         30 : }
     107                 :            : 
     108                 :            : Xapian::WritableDatabase
     109                 :          0 : BackendManagerHoney::get_writable_database(const string &, const string &)
     110                 :            : {
     111 [ #  # ][ #  # ]:          0 :     throw Xapian::UnimplementedError("Honey databases don't support writing");
                 [ #  # ]
     112                 :            : }
     113                 :            : 
     114                 :            : string
     115                 :         39 : BackendManagerHoney::get_generated_database_path(const string& name)
     116                 :            : {
     117                 :         39 :     return CACHE_DIRECTORY "/" + name;
     118                 :            : }
     119                 :            : 
     120                 :            : string
     121                 :         11 : BackendManagerHoney::get_compaction_output_path(const string& name)
     122                 :            : {
     123                 :         11 :     return CACHE_DIRECTORY "/" + name;
     124                 :            : }

Generated by: LCOV version 1.11