Add DB_BACKEND_INMEMORY; deprecate InMemory::open()
[xapian.git] / xapian-core / tests / harness / backendmanager.cc
blob40aaf9ca7d72abfc73809b862f9d948802899a82
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 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
20 * USA
23 #include <config.h>
25 #include <xapian.h>
27 #ifdef HAVE_VALGRIND
28 # include <valgrind/memcheck.h>
29 #endif
31 #include "safeerrno.h"
33 #include <cstdio>
34 #include <fstream>
35 #include <string>
36 #include <vector>
38 #include <sys/types.h>
39 #include "safesysstat.h"
41 #include "index_utils.h"
42 #include "backendmanager.h"
43 #include "unixcmds.h"
45 using namespace std;
47 void
48 BackendManager::index_files_to_database(Xapian::WritableDatabase & database,
49 const vector<string> & files)
51 FileIndexer(datadir, files).index_to(database);
54 /** Create the directory dirname if needed. Returns true if the
55 * directory was created and false if it was already there. Throws
56 * an exception if there was an error (eg not a directory).
58 bool
59 BackendManager::create_dir_if_needed(const string &dirname)
61 // create a directory if not present
62 struct stat sbuf;
63 int result = stat(dirname.c_str(), &sbuf);
64 if (result < 0) {
65 if (errno != ENOENT)
66 throw Xapian::DatabaseOpeningError("Can't stat directory");
67 if (mkdir(dirname.c_str(), 0700) < 0)
68 throw Xapian::DatabaseOpeningError("Can't create directory");
69 return true; // Successfully created a directory.
71 if (!S_ISDIR(sbuf.st_mode))
72 throw Xapian::DatabaseOpeningError("Is not a directory.");
73 return false; // Already a directory.
76 #ifdef XAPIAN_HAS_INMEMORY_BACKEND
77 Xapian::WritableDatabase
78 BackendManager::getwritedb_inmemory(const vector<string> &files)
80 Xapian::WritableDatabase db(string(), Xapian::DB_BACKEND_INMEMORY);
81 index_files_to_database(db, files);
82 return db;
84 #endif
86 #ifdef XAPIAN_HAS_GLASS_BACKEND
87 string
88 BackendManager::createdb_glass(const vector<string> &files)
90 string parent_dir = ".glass";
91 create_dir_if_needed(parent_dir);
93 string dbdir = parent_dir + "/db";
94 for (vector<string>::const_iterator i = files.begin();
95 i != files.end(); ++i) {
96 dbdir += '=';
97 dbdir += *i;
99 // If the database is readonly, we can reuse it if it exists.
100 if (create_dir_if_needed(dbdir)) {
101 // Directory was created, so do the indexing.
102 Xapian::WritableDatabase db(dbdir,
103 Xapian::DB_CREATE|Xapian::DB_BACKEND_GLASS, 2048);
104 index_files_to_database(db, files);
105 db.commit();
107 return dbdir;
110 Xapian::WritableDatabase
111 BackendManager::getwritedb_glass(const string & name,
112 const vector<string> & files)
114 string dbdir = getwritedb_glass_path(name);
116 // For a writable database we need to start afresh each time.
117 rm_rf(dbdir);
118 (void)create_dir_if_needed(dbdir);
120 // directory was created, so do the indexing.
121 Xapian::WritableDatabase db(dbdir,
122 Xapian::DB_CREATE|Xapian::DB_BACKEND_GLASS, 2048);
123 index_files_to_database(db, files);
124 return db;
127 std::string
128 BackendManager::getwritedb_glass_path(const string & name)
130 string parent_dir = ".glass";
131 create_dir_if_needed(parent_dir);
133 string dbdir = parent_dir;
134 dbdir += '/';
135 dbdir += name;
136 return dbdir;
138 #endif
140 #ifdef XAPIAN_HAS_CHERT_BACKEND
141 string
142 BackendManager::createdb_chert(const vector<string> &files)
144 string parent_dir = ".chert";
145 create_dir_if_needed(parent_dir);
147 string dbdir = parent_dir + "/db";
148 for (vector<string>::const_iterator i = files.begin();
149 i != files.end(); ++i) {
150 dbdir += '=';
151 dbdir += *i;
153 // If the database is readonly, we can reuse it if it exists.
154 if (create_dir_if_needed(dbdir)) {
155 // Directory was created, so do the indexing.
156 Xapian::WritableDatabase db(dbdir,
157 Xapian::DB_CREATE|Xapian::DB_BACKEND_CHERT, 2048);
158 index_files_to_database(db, files);
159 db.commit();
161 return dbdir;
164 Xapian::WritableDatabase
165 BackendManager::getwritedb_chert(const string & name,
166 const vector<string> & files)
168 string dbdir = getwritedb_chert_path(name);
170 // For a writable database we need to start afresh each time.
171 rm_rf(dbdir);
172 (void)create_dir_if_needed(dbdir);
174 // directory was created, so do the indexing.
175 Xapian::WritableDatabase db(dbdir,
176 Xapian::DB_CREATE|Xapian::DB_BACKEND_CHERT, 2048);
177 index_files_to_database(db, files);
178 return db;
181 std::string
182 BackendManager::getwritedb_chert_path(const string & name)
184 string parent_dir = ".chert";
185 create_dir_if_needed(parent_dir);
187 string dbdir = parent_dir;
188 dbdir += '/';
189 dbdir += name;
190 return dbdir;
193 #endif
195 BackendManager::~BackendManager() { }
197 std::string
198 BackendManager::get_dbtype() const
200 return "none";
203 string
204 BackendManager::do_get_database_path(const vector<string> &)
206 throw Xapian::InvalidArgumentError("Path isn't meaningful for this database type");
209 Xapian::Database
210 BackendManager::do_get_database(const vector<string> & files)
212 return Xapian::Database(do_get_database_path(files));
215 Xapian::Database
216 BackendManager::get_database(const vector<string> & files)
218 return do_get_database(files);
221 Xapian::Database
222 BackendManager::get_database(const string & file)
224 return do_get_database(vector<string>(1, file));
227 Xapian::Database
228 BackendManager::get_database(const std::string &dbname,
229 void (*gen)(Xapian::WritableDatabase&,
230 const std::string &),
231 const std::string &arg)
233 string dbleaf = "db__";
234 dbleaf += dbname;
235 const string & path = get_writable_database_path(dbleaf);
236 try {
237 return Xapian::Database(path);
238 } catch (const Xapian::DatabaseOpeningError &) {
240 rm_rf(path);
242 string tmp_dbleaf(dbleaf);
243 tmp_dbleaf += '~';
244 string tmp_path(path);
245 tmp_path += '~';
248 Xapian::WritableDatabase wdb = get_writable_database(tmp_dbleaf,
249 string());
250 gen(wdb, arg);
252 rename(tmp_path.c_str(), path.c_str());
254 return Xapian::Database(path);
257 std::string
258 BackendManager::get_database_path(const std::string &dbname,
259 void (*gen)(Xapian::WritableDatabase&,
260 const std::string &),
261 const std::string &arg)
263 string dbleaf = "db__";
264 dbleaf += dbname;
265 const string & path = get_writable_database_path(dbleaf);
266 try {
267 (void)Xapian::Database(path);
268 return path;
269 } catch (const Xapian::DatabaseOpeningError &) {
271 rm_rf(path);
273 string tmp_dbleaf(dbleaf);
274 tmp_dbleaf += '~';
275 string tmp_path(path);
276 tmp_path += '~';
279 Xapian::WritableDatabase wdb = get_writable_database(tmp_dbleaf,
280 string());
281 gen(wdb, arg);
283 rename(tmp_path.c_str(), path.c_str());
285 return path;
288 string
289 BackendManager::get_database_path(const vector<string> & files)
291 return do_get_database_path(files);
294 string
295 BackendManager::get_database_path(const string & file)
297 return do_get_database_path(vector<string>(1, file));
300 Xapian::WritableDatabase
301 BackendManager::get_writable_database(const string &, const string &)
303 throw Xapian::InvalidArgumentError("Attempted to open a disabled database");
306 string
307 BackendManager::get_writable_database_path(const std::string &)
309 throw Xapian::InvalidArgumentError("Path isn't meaningful for this database type");
312 Xapian::Database
313 BackendManager::get_remote_database(const vector<string> &, unsigned int)
315 string msg = "BackendManager::get_remote_database() called for non-remote database (type is ";
316 msg += get_dbtype();
317 msg += ')';
318 throw Xapian::InvalidOperationError(msg);
321 Xapian::Database
322 BackendManager::get_writable_database_as_database()
324 string msg = "Backend ";
325 msg += get_dbtype();
326 msg += " doesn't support get_writable_database_as_database()";
327 throw Xapian::InvalidOperationError(msg);
330 Xapian::WritableDatabase
331 BackendManager::get_writable_database_again()
333 string msg = "Backend ";
334 msg += get_dbtype();
335 msg += " doesn't support get_writable_database_again()";
336 throw Xapian::InvalidOperationError(msg);
339 void
340 BackendManager::clean_up()
344 const char *
345 BackendManager::get_xapian_progsrv_command()
347 #ifdef HAVE_VALGRIND
348 if (RUNNING_ON_VALGRIND) {
349 return "./runsrv " XAPIAN_PROGSRV;
351 #endif
352 return XAPIAN_PROGSRV;