1 /* backendmanager.cc: manage backends for testsuite
3 * Copyright 1999,2000,2001 BrightStation PLC
4 * Copyright 2002 Ananova Ltd
5 * Copyright 2002,2003,2004,2005,2006,2007,2008,2009,2010,2011,2012,2013,2016,2017,2018 Olly Betts
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.
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.
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
28 # include <valgrind/memcheck.h>
31 #include "safeerrno.h"
38 #include <sys/types.h>
39 #include "safesysstat.h"
41 #include "filetests.h"
42 #include "index_utils.h"
43 #include "backendmanager.h"
49 BackendManager::index_files_to_database(Xapian::WritableDatabase
& database
,
50 const vector
<string
> & files
)
52 FileIndexer(datadir
, files
).index_to(database
);
55 /** Create the directory dirname if needed. Returns true if the
56 * directory was created and false if it was already there. Throws
57 * an exception if there was an error (eg not a directory).
60 BackendManager::create_dir_if_needed(const string
&dirname
)
62 // create a directory if not present
64 int result
= stat(dirname
.c_str(), &sbuf
);
67 throw Xapian::DatabaseOpeningError("Can't stat directory", errno
);
68 if (mkdir(dirname
.c_str(), 0700) < 0)
69 throw Xapian::DatabaseOpeningError("Can't create directory", errno
);
70 return true; // Successfully created a directory.
72 if (!S_ISDIR(sbuf
.st_mode
))
73 throw Xapian::DatabaseOpeningError("Is not a directory.");
74 return false; // Already a directory.
77 #ifdef XAPIAN_HAS_INMEMORY_BACKEND
78 Xapian::WritableDatabase
79 BackendManager::getwritedb_inmemory(const vector
<string
> &files
)
81 Xapian::WritableDatabase
db(string(), Xapian::DB_BACKEND_INMEMORY
);
82 index_files_to_database(db
, files
);
87 #ifdef XAPIAN_HAS_GLASS_BACKEND
89 BackendManager::createdb_glass(const vector
<string
> &files
)
91 string parent_dir
= ".glass";
92 create_dir_if_needed(parent_dir
);
94 string dbdir
= parent_dir
+ "/db";
95 for (vector
<string
>::const_iterator i
= files
.begin();
96 i
!= files
.end(); ++i
) {
100 // If the database is readonly, we can reuse it if it exists.
101 if (create_dir_if_needed(dbdir
)) {
102 // Directory was created, so do the indexing.
103 Xapian::WritableDatabase
db(dbdir
,
104 Xapian::DB_CREATE
|Xapian::DB_BACKEND_GLASS
, 2048);
105 index_files_to_database(db
, files
);
111 Xapian::WritableDatabase
112 BackendManager::getwritedb_glass(const string
& name
,
113 const vector
<string
> & files
)
115 string dbdir
= getwritedb_glass_path(name
);
117 // For a writable database we need to start afresh each time.
119 (void)create_dir_if_needed(dbdir
);
121 // directory was created, so do the indexing.
122 Xapian::WritableDatabase
db(dbdir
,
123 Xapian::DB_CREATE
|Xapian::DB_BACKEND_GLASS
, 2048);
124 index_files_to_database(db
, files
);
129 BackendManager::getwritedb_glass_path(const string
& name
)
131 string parent_dir
= ".glass";
132 create_dir_if_needed(parent_dir
);
134 string dbdir
= parent_dir
;
141 #ifdef XAPIAN_HAS_CHERT_BACKEND
143 BackendManager::createdb_chert(const vector
<string
> &files
)
145 string parent_dir
= ".chert";
146 create_dir_if_needed(parent_dir
);
148 string dbdir
= parent_dir
+ "/db";
149 for (vector
<string
>::const_iterator i
= files
.begin();
150 i
!= files
.end(); ++i
) {
154 // If the database is readonly, we can reuse it if it exists.
155 if (create_dir_if_needed(dbdir
)) {
156 // Directory was created, so do the indexing.
157 Xapian::WritableDatabase
db(dbdir
,
158 Xapian::DB_CREATE
|Xapian::DB_BACKEND_CHERT
, 2048);
159 index_files_to_database(db
, files
);
165 Xapian::WritableDatabase
166 BackendManager::getwritedb_chert(const string
& name
,
167 const vector
<string
> & files
)
169 string dbdir
= getwritedb_chert_path(name
);
171 // For a writable database we need to start afresh each time.
173 (void)create_dir_if_needed(dbdir
);
175 // directory was created, so do the indexing.
176 Xapian::WritableDatabase
db(dbdir
,
177 Xapian::DB_CREATE
|Xapian::DB_BACKEND_CHERT
, 2048);
178 index_files_to_database(db
, files
);
183 BackendManager::getwritedb_chert_path(const string
& name
)
185 string parent_dir
= ".chert";
186 create_dir_if_needed(parent_dir
);
188 string dbdir
= parent_dir
;
196 BackendManager::~BackendManager() { }
199 BackendManager::get_dbtype() const
205 BackendManager::do_get_database_path(const vector
<string
> &)
207 throw Xapian::InvalidArgumentError("Path isn't meaningful for this database type");
211 BackendManager::do_get_database(const vector
<string
> & files
)
213 return Xapian::Database(do_get_database_path(files
));
217 BackendManager::get_database(const vector
<string
> & files
)
219 return do_get_database(files
);
223 BackendManager::get_database(const string
& file
)
225 return do_get_database(vector
<string
>(1, file
));
229 BackendManager::get_database(const std::string
&dbname
,
230 void (*gen
)(Xapian::WritableDatabase
&,
231 const std::string
&),
232 const std::string
&arg
)
234 string dbleaf
= "db__";
236 const string
& path
= get_writable_database_path(dbleaf
);
238 // InMemory doesn't have a path but we want to support generated
240 Xapian::WritableDatabase wdb
= get_writable_database(path
, path
);
245 if (path_exists(path
)) {
247 return Xapian::Database(path
);
248 } catch (const Xapian::DatabaseOpeningError
&) {
253 string
tmp_dbleaf(dbleaf
);
255 string
tmp_path(path
);
259 Xapian::WritableDatabase wdb
= get_writable_database(tmp_dbleaf
,
263 rename(tmp_path
.c_str(), path
.c_str());
264 // For multi, the shards will use the temporary name, but that's not really
267 return Xapian::Database(path
);
271 BackendManager::get_database_path(const std::string
&dbname
,
272 void (*gen
)(Xapian::WritableDatabase
&,
273 const std::string
&),
274 const std::string
&arg
)
276 string dbleaf
= "db__";
278 const string
& path
= get_writable_database_path(dbleaf
);
279 if (path_exists(path
)) {
281 (void)Xapian::Database(path
);
283 } catch (const Xapian::DatabaseOpeningError
&) {
288 string
tmp_dbleaf(dbleaf
);
290 string
tmp_path(path
);
294 Xapian::WritableDatabase wdb
= get_writable_database(tmp_dbleaf
,
298 rename(tmp_path
.c_str(), path
.c_str());
304 BackendManager::get_database_path(const vector
<string
> & files
)
306 return do_get_database_path(files
);
310 BackendManager::get_database_path(const string
& file
)
312 return do_get_database_path(vector
<string
>(1, file
));
315 Xapian::WritableDatabase
316 BackendManager::get_writable_database(const string
&, const string
&)
318 throw Xapian::InvalidArgumentError("Attempted to open a disabled database");
322 BackendManager::get_writable_database_path(const std::string
&)
324 throw Xapian::InvalidArgumentError("Path isn't meaningful for this database type");
328 BackendManager::get_remote_database(const vector
<string
> &, unsigned int)
330 string msg
= "BackendManager::get_remote_database() called for non-remote database (type is ";
333 throw Xapian::InvalidOperationError(msg
);
337 BackendManager::get_writable_database_as_database()
339 string msg
= "Backend ";
341 msg
+= " doesn't support get_writable_database_as_database()";
342 throw Xapian::InvalidOperationError(msg
);
345 Xapian::WritableDatabase
346 BackendManager::get_writable_database_again()
348 string msg
= "Backend ";
350 msg
+= " doesn't support get_writable_database_again()";
351 throw Xapian::InvalidOperationError(msg
);
355 BackendManager::clean_up()
360 BackendManager::get_xapian_progsrv_command()
363 if (RUNNING_ON_VALGRIND
) {
364 return "./runsrv " XAPIAN_PROGSRV
;
367 return XAPIAN_PROGSRV
;