xapian-core  1.4.26
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, 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_, "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,
59  if (rename(tmp_path.c_str(), db_path.c_str()) < 0) {
60  throw Xapian::DatabaseError("rename failed", errno);
61  }
62  }
63 
64  return db_path;
65 }
66 
69 {
70  // Create generated database inside sub_manager's cache.
71  return sub_manager->get_generated_database(name);
72 }
73 
74 void
76 {
78 
79  // path to the temporary generated db
80  string generated_db_path = sub_manager->get_generated_database_path(name);
81 
82  // path to final singlefile db
83  string path = cachedir + "/" + name;
84 
85  // path to tmpfile
86  string tmpfile = path + ".tmp";
87 
88  // Convert to singlefile db.
89  {
90  Xapian::Database db(generated_db_path);
91  db.compact(tmpfile,
94  }
95 
96  if (rename(tmpfile.c_str(), path.c_str()) < 0) {
97  throw Xapian::DatabaseError("rename failed", errno);
98  }
99 }
100 
102 BackendManagerSingleFile::get_writable_database(const string &, const string &)
103 {
104  throw Xapian::UnimplementedError("Single-file databases don't support writing");
105 }
106 
107 string
109 {
110  return cachedir + "/" + name;
111 }
112 
113 string
115 {
116  return cachedir + "/" + name;
117 }
void finalise_generated_database(const std::string &name)
Finalise generated database.
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.
virtual std::string get_generated_database_path(const std::string &name)
Get the path to use for generating a database, if supported.
Xapian::WritableDatabase get_generated_database(const std::string &name)
Get generated database.
virtual Xapian::WritableDatabase get_generated_database(const std::string &name)
Get a generated writable database instance.
C++ function versions of useful Unix commands.
STL namespace.
Utility functions for testing files.
const int DBCOMPACT_NO_RENUMBER
Use the same document ids in the output as in the input(s).
Definition: constants.h:256
bool create_dir_if_needed(const std::string &dirname)
Create the directory dirname if needed.
BackendManagerSingleFile(const BackendManagerSingleFile &)
Don&#39;t allow copying.
This class provides read/write access to a database.
Definition: database.h:789
std::string get_dbtype()
Definition: apitest.cc:42
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 compact(const std::string &output, unsigned flags=0, int block_size=0)
Produce a compact version of this database.
Definition: database.h:627
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
DatabaseError indicates some sort of database related error.
Definition: error.h:367
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