xapian-core  1.4.19
backendmanager_glass.cc
Go to the documentation of this file.
1 
4 /* Copyright (C) 2007,2008,2009,2013,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, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #include <config.h>
22 
23 #include "backendmanager_glass.h"
24 
25 #include "filetests.h"
26 #include "unixcmds.h"
27 
28 using namespace std;
29 
30 #define CACHE_DIRECTORY ".glass"
31 
32 // Use minimum block size to try to tickle more bugs.
33 #define BLOCK_SIZE 2048
34 
36  : BackendManager(datadir_)
37 {
38  // Ensure the directory we store cached test databases in exists.
40 }
41 
42 std::string
44 {
45  return "glass";
46 }
47 
48 string
49 BackendManagerGlass::do_get_database_path(const vector<string> & files)
50 {
51  string db_path = CACHE_DIRECTORY "/db";
52  for (const string& file : files) {
53  db_path += "__";
54  db_path += file;
55  }
56 
57  if (!dir_exists(db_path)) {
58  // No cached DB exists. Create at a temporary path and rename
59  // so we don't leave a partial DB in place upon failure.
60  string tmp_path = db_path + ".tmp";
61  // Make sure there's nothing existing at our temporary path.
62  rm_rf(tmp_path);
64  Xapian::WritableDatabase wdb(tmp_path, flags, BLOCK_SIZE);
65  index_files_to_database(wdb, files);
66  wdb.close();
67  if (rename(tmp_path.c_str(), db_path.c_str()) < 0) {
68  throw Xapian::Database("rename failed", errno);
69  }
70  }
71 
72  return db_path;
73 }
74 
77  const string & file)
78 {
80  string db_path = CACHE_DIRECTORY "/" + name;
81 
82  // We can't use a cached version, as it may have been modified by the
83  // testcase.
84  rm_rf(db_path);
85 
87  Xapian::WritableDatabase wdb(db_path, flags, BLOCK_SIZE);
88  index_files_to_database(wdb, vector<string>(1, file));
89 
90  return wdb;
91 }
92 
93 string
95 {
96  return CACHE_DIRECTORY "/" + name;
97 }
98 
99 string
101 {
102  return CACHE_DIRECTORY "/" + name;
103 }
104 
105 string
107 {
109 }
110 
113 {
116 }
117 
118 string
120 {
121  return CACHE_DIRECTORY "/" + last_wdb_name;
122 }
This class is used to access a database, or a group of databases.
Definition: database.h:68
std::string get_compaction_output_path(const std::string &name)
Get a path to compact a database to.
const int DB_CREATE
Create a new database.
Definition: constants.h:44
C++ function versions of useful Unix commands.
STL namespace.
Utility functions for testing files.
std::string do_get_database_path(const std::vector< std::string > &files)
Get the path of Glass Xapian::Database instance.
std::string get_writable_database_path_again()
Get the path of the last opened WritableDatabase.
const int DB_BACKEND_GLASS
Use the glass backend.
Definition: constants.h:158
void rm_rf(const string &filename)
Remove a directory and contents, just like the Unix "rm -rf" command.
Definition: unixcmds.cc:111
bool create_dir_if_needed(const std::string &dirname)
Create the directory dirname if needed.
std::string get_dbtype() const
Return a string representing the current database type.
#define BLOCK_SIZE
Xapian::WritableDatabase get_writable_database(const std::string &name, const std::string &file)
Create a Glass Xapian::WritableDatabase object indexing a single file.
const int DB_OPEN
Open an existing database.
Definition: constants.h:50
This class provides read/write access to a database.
Definition: database.h:785
BackendManagerGlass(const BackendManagerGlass &)
Don&#39;t allow copying.
#define CACHE_DIRECTORY
BackendManager subclass for glass databases.
std::string get_writable_database_path(const std::string &name)
Get the path of Glass Xapian::WritableDatabase instance.
bool dir_exists(const char *path)
Test if a directory exists.
Definition: filetests.h:136
char name[9]
Definition: dbcheck.cc:55
Xapian::WritableDatabase get_writable_database_again()
Create a WritableDatabase object for the last opened WritableDatabase.
std::string last_wdb_name
The path of the last writable database used.
std::string get_generated_database_path(const std::string &name)
Get the path to use for generating a database, if supported.
Definition: header.h:151
virtual void close()
Close the database.
Definition: omdatabase.cc:138
void index_files_to_database(Xapian::WritableDatabase &database, const std::vector< std::string > &files)
Index data from zero or more text files into a database.