xapian-core  2.0.0
backendmanager_honey.cc
Go to the documentation of this file.
1 
4 /* Copyright (C) 2007,2008,2009,2013,2017,2018 Olly Betts
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation; either version 2 of the
9  * License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, see
18  * <https://www.gnu.org/licenses/>.
19  */
20 
21 #include <config.h>
22 
23 #include "backendmanager_honey.h"
24 
25 #include "filetests.h"
26 #include "index_utils.h"
27 #include "unixcmds.h"
28 
29 #include <cstdio> // For rename().
30 
31 using namespace std;
32 
33 #define CACHE_DIRECTORY ".honey"
34 
35 string
36 BackendManagerHoney::do_get_database_path(const vector<string> & files)
37 {
38  string dbdir = CACHE_DIRECTORY;
39  create_dir_if_needed(dbdir);
40 
41  string dbname = "db";
42  vector<string>::const_iterator i;
43  for (i = files.begin(); i != files.end(); ++i) {
44  dbname += "__";
45  dbname += *i;
46  }
47  string dbpath = dbdir + "/" + dbname;
48 
49  if (dir_exists(dbpath)) return dbpath;
50 
51  string db_source = dbpath + ".src";
53 
54  string tmp_path = dbpath;
55  tmp_path += ".tmp";
56 
57  Xapian::WritableDatabase db(db_source, flags);
58  FileIndexer(get_datadir(), files).index_to(db);
59  db.commit();
60  db.compact(tmp_path,
62  db.close();
63 
64  rm_rf(db_source);
65 
66  if (rename(tmp_path.c_str(), dbpath.c_str()) < 0) {
67  throw Xapian::DatabaseError("rename failed", errno);
68  }
69 
70  return dbpath;
71 }
72 
75 {
76  // Create generated database inside glass cache
77  // to prevent a valid glass db inside honey cache
78  // if testsuite was interrupted.
79  return generated_sub_manager->get_generated_database(name);
80 }
81 
82 void
84 {
85  create_dir_if_needed(CACHE_DIRECTORY);
86 
87  // path to the temporary generated db
88  string generated_db_path =
89  generated_sub_manager->get_generated_database_path(name);
90 
91  // path to final honey db
92  string path = CACHE_DIRECTORY "/" + name;
93 
94  // path to temporary db
95  string tmp_path = path + ".tmp";
96 
97  // Convert glass backend to honey.
98  {
99  Xapian::Database db(generated_db_path);
100  db.compact(tmp_path,
102  }
103 
104  if (rename(tmp_path.c_str(), path.c_str()) < 0) {
105  throw Xapian::DatabaseError("rename failed", errno);
106  }
107 }
108 
110 BackendManagerHoney::get_writable_database(const string &, const string &)
111 {
112  throw Xapian::UnimplementedError("Honey databases don't support writing");
113 }
114 
115 string
117 {
118  return CACHE_DIRECTORY "/" + name;
119 }
120 
121 string
123 {
124  return CACHE_DIRECTORY "/" + name;
125 }
#define CACHE_DIRECTORY
BackendManager subclass for honey databases.
char name[9]
Definition: dbcheck.cc:57
Xapian::WritableDatabase get_generated_database(const std::string &name)
Get generated database for honey.
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 Honey Xapian::Database instance.
Xapian::WritableDatabase get_writable_database(const std::string &name, const std::string &file)
Create a Xapian::WritableDatabase object.
std::string get_generated_database_path(const std::string &name)
Get the path to use for generating a database, if supported.
void finalise_generated_database(const std::string &name)
Finalise generated database.
void index_to(Xapian::WritableDatabase &db)
Definition: index_utils.cc:52
DatabaseError indicates some sort of database related error.
Definition: error.h:355
An indexed database of documents.
Definition: database.h:75
void close()
Close the database.
Definition: database.cc:99
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
void commit()
Commit pending modifications.
Definition: database.cc:543
Utility functions for testing files.
bool dir_exists(const char *path)
Test if a directory exists.
Definition: filetests.h:145
utility functions for indexing testcase data
const int DB_BACKEND_HONEY
Use the honey backend.
Definition: constants.h:197
const int DB_BACKEND_GLASS
Use the glass backend.
Definition: constants.h:157
const int DBCOMPACT_NO_RENUMBER
Use the same document ids in the output as in the input(s).
Definition: constants.h:251
const int DB_CREATE_OR_OVERWRITE
Create database if it doesn't already exist, or overwrite if it does.
Definition: constants.h:37
Definition: header.h:215
void rm_rf(const string &filename)
Remove a directory and contents, just like the Unix "rm -rf" command.
Definition: unixcmds.cc:111
C++ function versions of useful Unix commands.