xapian-core  2.0.0
backendmanager_singlefile.cc
Go to the documentation of this file.
1 
4 /* Copyright (C) 2007,2008,2009,2011,2012,2013,2015,2018,2023 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, see
19  * <https://www.gnu.org/licenses/>.
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_, "singlefile_" + sub_manager_->get_dbtype()),
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 string
46 {
47  string db_path = cachedir + "/db";
48  for (const string& file : files) {
49  db_path += "__";
50  db_path += file;
51  }
52 
53  if (!file_exists(db_path)) {
54  // No cached DB exists. Create at a temporary path and rename
55  // so we don't leave a partial DB in place upon failure.
56  string tmp_path = db_path + ".tmp";
57  sub_manager->get_database(files).compact(tmp_path,
60  if (rename(tmp_path.c_str(), db_path.c_str()) < 0) {
61  throw Xapian::DatabaseError("rename failed", errno);
62  }
63  }
64 
65  return db_path;
66 }
67 
70 {
71  // Create generated database inside sub_manager's cache.
73 }
74 
75 void
77 {
79 
80  // path to the temporary generated db
81  string generated_db_path = sub_manager->get_generated_database_path(name);
82 
83  // path to final singlefile db
84  string path = cachedir + "/" + name;
85 
86  // path to tmpfile
87  string tmpfile = path + ".tmp";
88 
89  // Convert to singlefile db.
90  {
91  Xapian::Database db(generated_db_path);
92  db.compact(tmpfile,
95  }
96 
97  if (rename(tmpfile.c_str(), path.c_str()) < 0) {
98  throw Xapian::DatabaseError("rename failed", errno);
99  }
100 }
101 
103 BackendManagerSingleFile::get_writable_database(const string &, const string &)
104 {
105  throw Xapian::UnimplementedError("Single-file databases don't support writing");
106 }
107 
108 string
110 {
111  return cachedir + "/" + name;
112 }
113 
114 string
116 {
117  return cachedir + "/" + name;
118 }
std::string get_dbtype()
Definition: apitest.cc:41
BackendManager subclass for singlefile databases.
char name[9]
Definition: dbcheck.cc:57
BackendManagerSingleFile(const BackendManagerSingleFile &)
Don't allow copying.
std::string get_generated_database_path(const std::string &name)
Get the path to use for generating a database, if supported.
Xapian::WritableDatabase get_writable_database(const std::string &name, const std::string &file)
Create a Xapian::WritableDatabase object.
void finalise_generated_database(const std::string &name)
Finalise generated database.
std::string get_compaction_output_path(const std::string &name)
Get a path to compact a database to.
Xapian::WritableDatabase get_generated_database(const std::string &name)
Get generated database.
std::string do_get_database_path(const std::vector< std::string > &files)
Get the path of the Xapian::Database instance.
Xapian::Database get_database(const std::vector< std::string > &files)
Get a database instance of the current type.
virtual Xapian::WritableDatabase get_generated_database(const std::string &name)
Get a generated writable database instance.
bool create_dir_if_needed(const std::string &dirname)
Create the directory dirname if needed.
virtual std::string get_generated_database_path(const std::string &name)
Get the path to use for generating a database, if supported.
DatabaseError indicates some sort of database related error.
Definition: error.h:355
An indexed database of documents.
Definition: database.h:75
void compact(std::string_view output, unsigned flags=0, int block_size=0)
Produce a compact version of this database.
Definition: database.h:738
UnimplementedError indicates an attempt to use an unimplemented feature.
Definition: error.h:313
This class provides read/write access to a database.
Definition: database.h:964
Utility functions for testing files.
bool file_exists(const char *path)
Test if a file exists.
Definition: filetests.h:40
utility functions for indexing testcase data
const int DBCOMPACT_NO_RENUMBER
Use the same document ids in the output as in the input(s).
Definition: constants.h:251
const int DBCOMPACT_SINGLE_FILE
Produce a single-file database.
Definition: constants.h:263
Definition: header.h:215
C++ function versions of useful Unix commands.