xapian-core  1.4.26
backendmanager_chert.cc
Go to the documentation of this file.
1 
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_chert.h"
24 
25 #include "filetests.h"
26 #include "unixcmds.h"
27 
28 using namespace std;
29 
30 #define CACHE_DIRECTORY ".chert"
31 
32 // Use minimum block size to try to tickle more bugs.
33 #define BLOCK_SIZE 2048
34 
36  : BackendManager(datadir_, "chert")
37 {
38  // Ensure the directory we store cached test databases in exists.
40 }
41 
42 string
43 BackendManagerChert::do_get_database_path(const vector<string> & files)
44 {
45  string db_path = CACHE_DIRECTORY "/db";
46  for (const string& file : files) {
47  db_path += "__";
48  db_path += file;
49  }
50 
51  if (!dir_exists(db_path)) {
52  // No cached DB exists. Create at a temporary path and rename
53  // so we don't leave a partial DB in place upon failure.
54  string tmp_path = db_path + ".tmp";
55  // Make sure there's nothing existing at our temporary path.
56  rm_rf(tmp_path);
58  Xapian::WritableDatabase wdb(tmp_path, flags, BLOCK_SIZE);
59  index_files_to_database(wdb, files);
60  wdb.close();
61  if (rename(tmp_path.c_str(), db_path.c_str()) < 0) {
62  throw Xapian::DatabaseError("rename failed", errno);
63  }
64  }
65 
66  return db_path;
67 }
68 
71  const string & file)
72 {
74  string db_path = CACHE_DIRECTORY "/" + name;
75 
76  // We can't use a cached version, as it may have been modified by the
77  // testcase.
78  rm_rf(db_path);
79 
81  Xapian::WritableDatabase wdb(db_path, flags, BLOCK_SIZE);
82  index_files_to_database(wdb, vector<string>(1, file));
83 
84  return wdb;
85 }
86 
87 string
89 {
90  return CACHE_DIRECTORY "/" + name;
91 }
92 
93 string
95 {
96  return CACHE_DIRECTORY "/" + name;
97 }
98 
99 string
101 {
103 }
104 
107 {
110 }
111 
112 string
114 {
115  return CACHE_DIRECTORY "/" + last_wdb_name;
116 }
BackendManagerChert(const BackendManagerChert &)
Don&#39;t allow copying.
const int DB_CREATE
Create a new database.
Definition: constants.h:44
BackendManager subclass for chert databases.
std::string do_get_database_path(const std::vector< std::string > &files)
Get the path of Chert Xapian::Database instance.
std::string get_generated_database_path(const std::string &name)
Get the path to use for generating a database, if supported.
C++ function versions of useful Unix commands.
STL namespace.
Utility functions for testing files.
void rm_rf(const string &filename)
Remove a directory and contents, just like the Unix "rm -rf" command.
Definition: unixcmds.cc:111
bool create_dir_if_needed(const std::string &dirname)
Create the directory dirname if needed.
std::string get_writable_database_path(const std::string &name)
Get the path of Chert Xapian::WritableDatabase instance.
std::string last_wdb_name
The path of the last writable database used.
#define CACHE_DIRECTORY
const int DB_OPEN
Open an existing database.
Definition: constants.h:50
This class provides read/write access to a database.
Definition: database.h:789
#define BLOCK_SIZE
Xapian::WritableDatabase get_writable_database(const std::string &name, const std::string &file)
Create a Chert Xapian::WritableDatabase object indexing a single file.
bool dir_exists(const char *path)
Test if a directory exists.
Definition: filetests.h:136
char name[9]
Definition: dbcheck.cc:55
Xapian::WritableDatabase get_writable_database_again()
Create a WritableDatabase object for the last opened WritableDatabase.
Definition: header.h:151
DatabaseError indicates some sort of database related error.
Definition: error.h:367
std::string get_writable_database_path_again()
Get the path of the last opened WritableDatabase.
virtual void close()
Close the database.
Definition: omdatabase.cc:138
const int DB_BACKEND_CHERT
Use the chert backend.
Definition: constants.h:170
std::string get_compaction_output_path(const std::string &name)
Get a path to compact a database to.
void index_files_to_database(Xapian::WritableDatabase &database, const std::vector< std::string > &files)
Index data from zero or more text files into a database.