tests/harness/backendmanager.cc

Go to the documentation of this file.
00001 /* backendmanager.cc: manage backends for testsuite
00002  *
00003  * Copyright 1999,2000,2001 BrightStation PLC
00004  * Copyright 2002 Ananova Ltd
00005  * Copyright 2002,2003,2004,2005,2006,2007 Olly Betts
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License as
00009  * published by the Free Software Foundation; either version 2 of the
00010  * License, or (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
00020  * USA
00021  */
00022 
00023 #include <config.h>
00024 
00025 // We have to use the deprecated Quartz::open() method.
00026 #define XAPIAN_DEPRECATED(D) D
00027 #include <xapian.h>
00028 
00029 #ifdef HAVE_VALGRIND
00030 # include <valgrind/memcheck.h>
00031 #endif
00032 
00033 #include "safeerrno.h"
00034 
00035 #include <fstream>
00036 #include <string>
00037 #include <vector>
00038 
00039 #include <sys/types.h>
00040 #include "safesysstat.h"
00041 
00042 #include "index_utils.h"
00043 #include "backendmanager.h"
00044 #include "unixcmds.h"
00045 #include "utils.h"
00046 
00047 using namespace std;
00048 
00049 void
00050 BackendManager::index_files_to_database(Xapian::WritableDatabase & database,
00051                                         const vector<string> & dbnames)
00052 {
00053     FileIndexer f(datadir, dbnames);
00054     while (f) database.add_document(f.next());
00055 }
00056 
00061 bool
00062 BackendManager::create_dir_if_needed(const string &dirname)
00063 {
00064     // create a directory if not present
00065     struct stat sbuf;
00066     int result = stat(dirname, &sbuf);
00067     if (result < 0) {
00068         if (errno != ENOENT)
00069             throw Xapian::DatabaseOpeningError("Can't stat directory");
00070         if (mkdir(dirname, 0700) < 0)
00071             throw Xapian::DatabaseOpeningError("Can't create directory");
00072         return true; // Successfully created a directory.
00073     }
00074     if (!S_ISDIR(sbuf.st_mode))
00075         throw Xapian::DatabaseOpeningError("Is not a directory.");
00076     return false; // Already a directory.
00077 }
00078 
00079 #ifdef XAPIAN_HAS_INMEMORY_BACKEND
00080 Xapian::WritableDatabase
00081 BackendManager::getwritedb_inmemory(const vector<string> &dbnames)
00082 {
00083     Xapian::WritableDatabase db(Xapian::InMemory::open());
00084     index_files_to_database(db, dbnames);
00085     return db;
00086 }
00087 #endif
00088 
00089 #ifdef XAPIAN_HAS_FLINT_BACKEND
00090 string
00091 BackendManager::createdb_flint(const vector<string> &dbnames)
00092 {
00093     string parent_dir = ".flint";
00094     create_dir_if_needed(parent_dir);
00095 
00096     string dbdir = parent_dir + "/db";
00097     for (vector<string>::const_iterator i = dbnames.begin();
00098          i != dbnames.end(); i++) {
00099         dbdir += '=';
00100         dbdir += *i;
00101     }
00102     // If the database is readonly, we can reuse it if it exists.
00103     if (create_dir_if_needed(dbdir)) {
00104         // Directory was created, so do the indexing.
00105         Xapian::WritableDatabase db(Xapian::Flint::open(dbdir, Xapian::DB_CREATE, 2048));
00106         index_files_to_database(db, dbnames);
00107     }
00108     return dbdir;
00109 }
00110 
00111 Xapian::WritableDatabase
00112 BackendManager::getwritedb_flint(const string & name,
00113                                  const vector<string> & files)
00114 {
00115     string parent_dir = ".flint";
00116     create_dir_if_needed(parent_dir);
00117 
00118     string dbdir = parent_dir;
00119     dbdir += '/';
00120     dbdir += name;
00121 
00122     // For a writable database we need to start afresh each time.
00123     rm_rf(dbdir);
00124     (void)create_dir_if_needed(dbdir);
00125 
00126     // directory was created, so do the indexing.
00127     Xapian::WritableDatabase db(Xapian::Flint::open(dbdir, Xapian::DB_CREATE, 2048));
00128     index_files_to_database(db, files);
00129     return db;
00130 }
00131 #endif
00132 
00133 #ifdef XAPIAN_HAS_QUARTZ_BACKEND
00134 string
00135 BackendManager::createdb_quartz(const vector<string> &dbnames)
00136 {
00137     string parent_dir = ".quartz";
00138     create_dir_if_needed(parent_dir);
00139 
00140     string dbdir = parent_dir + "/db";
00141     for (vector<string>::const_iterator i = dbnames.begin();
00142          i != dbnames.end(); i++) {
00143         dbdir += '=';
00144         dbdir += *i;
00145     }
00146     // If the database is readonly, we can reuse it if it exists.
00147     if (create_dir_if_needed(dbdir)) {
00148         // Directory was created, so do the indexing.
00149         Xapian::WritableDatabase db(Xapian::Quartz::open(dbdir, Xapian::DB_CREATE, 2048));
00150         index_files_to_database(db, dbnames);
00151     }
00152     return dbdir;
00153 }
00154 
00155 Xapian::WritableDatabase
00156 BackendManager::getwritedb_quartz(const string & name,
00157                                   const vector<string> & files)
00158 {
00159     string parent_dir = ".quartz";
00160     create_dir_if_needed(parent_dir);
00161 
00162     string dbdir = parent_dir;
00163     dbdir += '/';
00164     dbdir += name;
00165 
00166     // For a writable database we need to start afresh each time.
00167     rm_rf(dbdir);
00168     (void)create_dir_if_needed(dbdir);
00169     touch(dbdir + "/log");
00170 
00171     // directory was created, so do the indexing.
00172     Xapian::WritableDatabase db(Xapian::Quartz::open(dbdir, Xapian::DB_CREATE, 2048));
00173     index_files_to_database(db, files);
00174     return db;
00175 }
00176 #endif
00177 
00178 Xapian::Database
00179 BackendManager::get_database(const vector<string> &)
00180 {
00181     throw Xapian::InvalidArgumentError("Attempted to open a disabled database");
00182 }
00183 
00184 Xapian::Database
00185 BackendManager::get_database(const string &)
00186 {
00187     throw Xapian::InvalidArgumentError("Attempted to open a disabled database");
00188 }
00189 
00190 Xapian::WritableDatabase
00191 BackendManager::get_writable_database(const string &, const string &)
00192 {
00193     throw Xapian::InvalidArgumentError("Attempted to open a disabled database");
00194 }
00195 
00196 Xapian::Database
00197 BackendManager::get_remote_database(const vector<string> &, unsigned int)
00198 {
00199     string msg = "BackendManager::get_remote_database() called for non-remote database (type is ";
00200     msg += get_dbtype();
00201     msg += ')';
00202     throw Xapian::InvalidOperationError(msg);
00203 }
00204 
00205 Xapian::Database
00206 BackendManager::get_writable_database_as_database()
00207 {
00208     string msg = "Backend ";
00209     msg += get_dbtype();
00210     msg += " doesn't support get_writable_database_as_database()";
00211     throw Xapian::InvalidOperationError(msg);
00212 }
00213 
00214 Xapian::WritableDatabase
00215 BackendManager::get_writable_database_again()
00216 {
00217     string msg = "Backend ";
00218     msg += get_dbtype();
00219     msg += " doesn't support get_writable_database_again()";
00220     throw Xapian::InvalidOperationError(msg);
00221 }
00222 
00223 const char *
00224 BackendManager::get_xapian_progsrv_command()
00225 {
00226 #ifdef HAVE_VALGRIND
00227     if (RUNNING_ON_VALGRIND) {
00228         return "./runsrv "XAPIAN_PROGSRV;
00229     }
00230 #endif
00231     return XAPIAN_PROGSRV;
00232 }

Documentation for Xapian (version 1.0.10).
Generated on 24 Dec 2008 by Doxygen 1.5.2.