LCOV - code coverage report
Current view: top level - tests/harness - backendmanager_glass.cc (source / functions) Hit Total Coverage
Test: Test Coverage for xapian-core 7028d852e609 Lines: 40 41 97.6 %
Date: 2019-02-17 14:59:59 Functions: 9 9 100.0 %
Branches: 25 48 52.1 %

           Branch data     Line data    Source code
       1                 :            : /** @file backendmanager_glass.cc
       2                 :            :  * @brief BackendManager subclass for glass databases.
       3                 :            :  */
       4                 :            : /* Copyright (C) 2007,2008,2009,2013,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_glass.h"
      24                 :            : 
      25                 :            : #include "filetests.h"
      26                 :            : #include "unixcmds.h"
      27                 :            : 
      28                 :            : using namespace std;
      29                 :            : 
      30                 :            : #define CACHE_DIRECTORY ".glass"
      31                 :            : 
      32                 :            : // Use minimum block size to try to tickle more bugs.
      33                 :            : #define BLOCK_SIZE 2048
      34                 :            : 
      35                 :         12 : BackendManagerGlass::BackendManagerGlass(const string& datadir_)
      36         [ +  - ]:         12 :     : BackendManager(datadir_)
      37                 :            : {
      38                 :            :     // Ensure the directory we store cached test databases in exists.
      39 [ +  - ][ +  - ]:         12 :     (void)create_dir_if_needed(CACHE_DIRECTORY);
      40                 :         12 : }
      41                 :            : 
      42                 :            : std::string
      43                 :        283 : BackendManagerGlass::get_dbtype() const
      44                 :            : {
      45         [ +  - ]:        283 :     return "glass";
      46                 :            : }
      47                 :            : 
      48                 :            : string
      49                 :        835 : BackendManagerGlass::do_get_database_path(const vector<string> & files)
      50                 :            : {
      51         [ +  - ]:        835 :     string db_path = CACHE_DIRECTORY "/db";
      52         [ +  + ]:       1687 :     for (const string& file : files) {
      53         [ +  - ]:        852 :         db_path += "__";
      54         [ +  - ]:        852 :         db_path += file;
      55                 :            :     }
      56                 :            : 
      57         [ +  + ]:        835 :     if (!dir_exists(db_path)) {
      58                 :            :         // No cached DB exists.  Create at a temporary path and rename
      59                 :            :         // so we don't leave a partial DB in place upon failure.
      60         [ +  - ]:         22 :         string tmp_path = db_path + ".tmp";
      61                 :            :         // Make sure there's nothing existing at our temporary path.
      62         [ +  - ]:         22 :         rm_rf(tmp_path);
      63                 :         22 :         auto flags = Xapian::DB_CREATE|Xapian::DB_BACKEND_GLASS;
      64         [ +  - ]:         44 :         Xapian::WritableDatabase wdb(tmp_path, flags, BLOCK_SIZE);
      65         [ +  - ]:         22 :         index_files_to_database(wdb, files);
      66         [ +  - ]:         22 :         wdb.close();
      67         [ -  + ]:         22 :         if (rename(tmp_path.c_str(), db_path.c_str()) < 0) {
      68 [ #  # ][ #  # ]:          0 :             throw Xapian::DatabaseError("rename failed", errno);
      69                 :         22 :         }
      70                 :            :     }
      71                 :            : 
      72                 :        835 :     return db_path;
      73                 :            : }
      74                 :            : 
      75                 :            : Xapian::WritableDatabase
      76                 :        333 : BackendManagerGlass::get_writable_database(const string & name,
      77                 :            :                                            const string & file)
      78                 :            : {
      79         [ +  - ]:        333 :     last_wdb_name = name;
      80         [ +  - ]:        333 :     string db_path = CACHE_DIRECTORY "/" + name;
      81                 :            : 
      82                 :            :     // We can't use a cached version, as it may have been modified by the
      83                 :            :     // testcase.
      84         [ +  - ]:        333 :     rm_rf(db_path);
      85                 :            : 
      86                 :        333 :     auto flags = Xapian::DB_CREATE|Xapian::DB_BACKEND_GLASS;
      87         [ +  - ]:        333 :     Xapian::WritableDatabase wdb(db_path, flags, BLOCK_SIZE);
      88 [ +  - ][ +  - ]:        333 :     index_files_to_database(wdb, vector<string>(1, file));
      89                 :            : 
      90                 :        333 :     return wdb;
      91                 :            : }
      92                 :            : 
      93                 :            : string
      94                 :        219 : BackendManagerGlass::get_writable_database_path(const string & name)
      95                 :            : {
      96                 :        219 :     return CACHE_DIRECTORY "/" + name;
      97                 :            : }
      98                 :            : 
      99                 :            : string
     100                 :         13 : BackendManagerGlass::get_compaction_output_path(const string& name)
     101                 :            : {
     102                 :         13 :     return CACHE_DIRECTORY "/" + name;
     103                 :            : }
     104                 :            : 
     105                 :            : string
     106                 :         36 : BackendManagerGlass::get_generated_database_path(const std::string & name)
     107                 :            : {
     108                 :         36 :     return BackendManagerGlass::get_writable_database_path(name);
     109                 :            : }
     110                 :            : 
     111                 :            : Xapian::WritableDatabase
     112                 :          4 : BackendManagerGlass::get_writable_database_again()
     113                 :            : {
     114                 :          8 :     return Xapian::WritableDatabase(CACHE_DIRECTORY "/" + last_wdb_name,
     115         [ +  + ]:          5 :                                     Xapian::DB_OPEN|Xapian::DB_BACKEND_GLASS);
     116                 :            : }
     117                 :            : 
     118                 :            : string
     119                 :         43 : BackendManagerGlass::get_writable_database_path_again()
     120                 :            : {
     121                 :         43 :     return CACHE_DIRECTORY "/" + last_wdb_name;
     122                 :            : }

Generated by: LCOV version 1.11