xapian-core  1.4.19
backendmanager_singlefile.cc
Go to the documentation of this file.
1 
4 /* Copyright (C) 2007,2008,2009,2011,2012,2013,2015,2018 Olly Betts
5  * Copyright (C) 2008 Lemur Consulting Ltd
6  *
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.
11  *
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.
16  *
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
20  */
21 
22 #include <config.h>
23 
25 
26 #include "filetests.h"
27 #include "index_utils.h"
28 #include "unixcmds.h"
29 
30 #include <cstdio> // For rename().
31 
32 using namespace std;
33 
35  BackendManager* sub_manager_)
36  : BackendManager(datadir_),
37  sub_manager(sub_manager_),
38  cachedir(".singlefile" + sub_manager_->get_dbtype())
39 {
40  // Ensure the directory we store cached test databases in exists.
42 }
43 
44 std::string
46 {
47  return "singlefile_" + sub_manager->get_dbtype();
48 }
49 
50 string
52 {
53  string db_path = cachedir + "/db";
54  for (const string& file : files) {
55  db_path += "__";
56  db_path += file;
57  }
58 
59  if (!file_exists(db_path)) {
60  // No cached DB exists. Create at a temporary path and rename
61  // so we don't leave a partial DB in place upon failure.
62  string tmp_path = db_path + ".tmp";
63  sub_manager->get_database(files).compact(tmp_path,
65  if (rename(tmp_path.c_str(), db_path.c_str()) < 0) {
66  throw Xapian::Database("rename failed", errno);
67  }
68  }
69 
70  return db_path;
71 }
72 
74 BackendManagerSingleFile::get_writable_database(const string &, const string &)
75 {
76  throw Xapian::UnimplementedError("Single-file databases don't support writing");
77 }
78 
79 string
81 {
82  return cachedir + "/" + name;
83 }
This class is used to access a database, or a group of databases.
Definition: database.h:68
Xapian::Database get_database(const std::vector< std::string > &files)
Get a database instance of the current type.
C++ function versions of useful Unix commands.
STL namespace.
Utility functions for testing files.
bool create_dir_if_needed(const std::string &dirname)
Create the directory dirname if needed.
BackendManagerSingleFile(const BackendManagerSingleFile &)
Don&#39;t allow copying.
std::string get_dbtype() const
Return a string representing the current database type.
This class provides read/write access to a database.
Definition: database.h:785
std::string get_dbtype()
Definition: apitest.cc:42
Xapian::WritableDatabase get_writable_database(const std::string &name, const std::string &file)
Create a Xapian::WritableDatabase object.
void compact(const std::string &output, unsigned flags=0, int block_size=0)
Produce a compact version of this database.
Definition: database.h:623
virtual std::string get_dbtype() const
Get the database type currently in use.
std::string get_compaction_output_path(const std::string &name)
Get a path to compact a database to.
std::string do_get_database_path(const std::vector< std::string > &files)
Get the path of the Xapian::Database instance.
BackendManager subclass for singlefile databases.
char name[9]
Definition: dbcheck.cc:55
Definition: header.h:151
utility functions for indexing testcase data
const int DBCOMPACT_SINGLE_FILE
Produce a single-file database.
Definition: constants.h:268
bool file_exists(const char *path)
Test if a file exists.
Definition: filetests.h:39
UnimplementedError indicates an attempt to use an unimplemented feature.
Definition: error.h:325