1 /** @file backendmanager_singlefile.cc
2 * @brief BackendManager subclass for singlefile databases.
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
24 #include "backendmanager_singlefile.h"
26 #include "filetests.h"
27 #include "index_utils.h"
31 #include <cstdio> // For rename().
34 #ifdef XAPIAN_HAS_GLASS_BACKEND
38 BackendManagerSingleFile::BackendManagerSingleFile(const std::string
& subtype_
)
41 #ifdef XAPIAN_HAS_GLASS_BACKEND
42 if (subtype
== "glass") return;
44 throw ("Unknown backend type \"" + subtype
+ "\" specified for singlefile database subdatabases");
48 BackendManagerSingleFile::get_dbtype() const
50 return "singlefile_" + subtype
;
53 #define NUMBER_OF_SUB_DBS 2
56 BackendManagerSingleFile::createdb_singlefile(const vector
<string
> & files
)
58 string dbdir
= ".singlefile" + subtype
;
59 create_dir_if_needed(dbdir
);
62 vector
<string
>::const_iterator i
;
63 for (i
= files
.begin(); i
!= files
.end(); ++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
;
76 string msg
= "Unknown singlefiledb subtype: ";
81 string tmpfile
= dbpath
;
84 Xapian::WritableDatabase
db(db_source
, flags
);
85 FileIndexer(get_datadir(), files
).index_to(db
);
87 db
.compact(tmpfile
, Xapian::DBCOMPACT_SINGLE_FILE
);
92 rename(tmpfile
.c_str(), dbpath
.c_str());
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