Drop special handling for Compaq C++
[xapian.git] / xapian-letor / tests / harness / backendmanager_singlefile.cc
blob2e9be79caf4a8ecc21c8f296c1ea1d6541424521
1 /** @file backendmanager_singlefile.cc
2 * @brief BackendManager subclass for singlefile databases.
3 */
4 /* Copyright (C) 2007,2008,2009,2011,2012,2013,2015 Olly Betts
5 * Copyright (C) 2008 Lemur Consulting Ltd
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 USA
22 #include <config.h>
24 #include "backendmanager_singlefile.h"
26 #include "filetests.h"
27 #include "index_utils.h"
28 #include "unixcmds.h"
30 #include <cerrno>
31 #include <cstdio> // For rename().
32 #include <cstring>
34 #ifdef XAPIAN_HAS_GLASS_BACKEND
36 using namespace std;
38 BackendManagerSingleFile::BackendManagerSingleFile(const std::string & subtype_)
39 : subtype(subtype_)
41 #ifdef XAPIAN_HAS_GLASS_BACKEND
42 if (subtype == "glass") return;
43 #endif
44 throw ("Unknown backend type \"" + subtype + "\" specified for singlefile database subdatabases");
47 std::string
48 BackendManagerSingleFile::get_dbtype() const
50 return "singlefile_" + subtype;
53 #define NUMBER_OF_SUB_DBS 2
55 string
56 BackendManagerSingleFile::createdb_singlefile(const vector<string> & files)
58 string dbdir = ".singlefile" + subtype;
59 create_dir_if_needed(dbdir);
61 string dbname = "db";
62 vector<string>::const_iterator i;
63 for (i = files.begin(); i != files.end(); ++i) {
64 dbname += "__";
65 dbname += *i;
67 string dbpath = dbdir + "/" + dbname;
69 if (file_exists(dbpath)) return dbpath;
71 string db_source = dbpath + ".src";
72 int flags = Xapian::DB_CREATE_OR_OVERWRITE;
73 if (subtype == "glass") {
74 flags |= Xapian::DB_BACKEND_GLASS;
75 } else {
76 string msg = "Unknown singlefiledb subtype: ";
77 msg += subtype;
78 throw msg;
81 string tmpfile = dbpath;
82 tmpfile += ".tmp";
84 Xapian::WritableDatabase db(db_source, flags);
85 FileIndexer(get_datadir(), files).index_to(db);
86 db.commit();
87 db.compact(tmpfile, Xapian::DBCOMPACT_SINGLE_FILE);
88 db.close();
90 rm_rf(db_source);
92 rename(tmpfile.c_str(), dbpath.c_str());
94 return dbpath;
97 string
98 BackendManagerSingleFile::do_get_database_path(const vector<string> & files)
100 return createdb_singlefile(files);
103 Xapian::WritableDatabase
104 BackendManagerSingleFile::get_writable_database(const string &, const string &)
106 throw Xapian::UnimplementedError("Single-file databases don't support writing");
109 #endif // XAPIAN_HAS_GLASS_BACKEND