Split PostList and PostingIterator::Internal
[xapian.git] / xapian-core / backends / databaseinternal.cc
blob9e54c10be3fe435439af45f870b63e6b0244542b
1 /** @file databaseinternal.cc
2 * @brief Virtual base class for Database internals
3 */
4 /* Copyright 2003,2004,2006,2007,2008,2009,2011,2014,2015,2017 Olly Betts
5 * Copyright 2008 Lemur Consulting Ltd
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.
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.
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
22 #include <config.h>
24 #include "databaseinternal.h"
26 #include "api/leafpostlist.h"
27 #include "omassert.h"
28 #include "slowvaluelist.h"
29 #include "xapian/error.h"
31 #include <algorithm>
32 #include <memory>
33 #include <string>
35 using namespace std;
36 using Xapian::Internal::intrusive_ptr;
38 namespace Xapian {
40 [[noreturn]]
41 static void invalid_operation(const char* msg)
43 throw InvalidOperationError(msg);
46 Database::Internal::size_type
47 Database::Internal::size() const
49 return 1;
52 void
53 Database::Internal::keep_alive()
55 // No-op except for remote databases.
58 void
59 Database::Internal::readahead_for_query(const Xapian::Query &) const
63 Xapian::doccount
64 Database::Internal::get_value_freq(Xapian::valueno) const
66 throw Xapian::UnimplementedError("This backend doesn't support get_value_freq");
69 string
70 Database::Internal::get_value_lower_bound(Xapian::valueno) const
72 return string();
75 string
76 Database::Internal::get_value_upper_bound(Xapian::valueno) const
78 throw Xapian::UnimplementedError("This backend doesn't support get_value_upper_bound");
81 Xapian::termcount
82 Database::Internal::get_doclength_lower_bound() const
84 // A zero-length document can't contain any terms, so we ignore such
85 // documents for the purposes of this lower bound.
86 return 1;
89 Xapian::termcount
90 Database::Internal::get_doclength_upper_bound() const
92 // Not a very tight bound in general, but this is only a fall-back for
93 // backends which don't store these stats.
94 return min(get_total_length(), Xapian::totallength(Xapian::termcount(-1)));
97 Xapian::termcount
98 Database::Internal::get_wdf_upper_bound(const string & term) const
100 // Not a very tight bound in general, but this is only a fall-back for
101 // backends which don't store these stats.
102 Xapian::termcount cf;
103 get_freqs(term, NULL, &cf);
104 return cf;
107 // Discard any exceptions - we're called from the destructors of derived
108 // classes so we can't safely throw.
109 void
110 Database::Internal::dtor_called_()
112 try {
113 if (transaction_active()) {
114 end_transaction(false);
115 } else {
116 // TRANSACTION_READONLY and TRANSACTION_UNIMPLEMENTED should be
117 // handled by the inlined dtor_called() wrapper.
118 AssertEq(state, TRANSACTION_NONE);
119 commit();
121 } catch (...) {
122 // We can't safely throw exceptions from a destructor in case an
123 // exception is already active and causing us to be destroyed.
127 void
128 Database::Internal::commit()
130 // Writable databases should override this method, but this can get called
131 // if a read-only shard gets added to a WritableDatabase.
132 invalid_operation("WritableDatabase::commit() called with a read-only shard");
135 void
136 Database::Internal::cancel()
138 // Writable databases should override this method, but this can get called
139 // if a read-only shard gets added to a WritableDatabase.
140 invalid_operation("WritableDatabase::cancel() called with a read-only shard");
143 void
144 Database::Internal::begin_transaction(bool flushed)
146 if (state != TRANSACTION_NONE) {
147 if (transaction_active()) {
148 invalid_operation("WritableDatabase::begin_transaction(): already "
149 "in a transaction");
151 if (is_read_only()) {
152 invalid_operation("WritableDatabase::begin_transaction(): called "
153 "with a read-only shard");
155 throw UnimplementedError("This backend doesn't implement transactions");
157 if (flushed) {
158 // N.B. Call commit() before we set state since commit() isn't allowed
159 // during a transaction.
160 commit();
161 state = TRANSACTION_FLUSHED;
162 } else {
163 state = TRANSACTION_UNFLUSHED;
167 void
168 Database::Internal::end_transaction(bool do_commit)
170 if (!transaction_active()) {
171 if (state != TRANSACTION_NONE) {
172 if (is_read_only()) {
173 invalid_operation(do_commit ?
174 "WritableDatabase::commit_transaction(): "
175 "called with a read-only shard" :
176 "WritableDatabase::cancel_transaction(): "
177 "called with a read-only shard");
179 throw UnimplementedError("This backend doesn't implement transactions");
181 invalid_operation(do_commit ?
182 "WritableDatabase::commit_transaction(): not in a "
183 "transaction" :
184 "WritableDatabase::cancel_transaction(): not in a "
185 "transaction");
188 auto old_state = state;
189 state = TRANSACTION_NONE;
190 if (!do_commit) {
191 cancel();
192 } else if (old_state == TRANSACTION_FLUSHED) {
193 // N.B. Call commit() after we clear state since commit() isn't
194 // allowed during a transaction.
195 commit();
199 Xapian::docid
200 Database::Internal::add_document(const Xapian::Document &)
202 // Writable databases should override this method, but this can get called
203 // if a read-only shard gets added to a WritableDatabase.
204 invalid_operation("WritableDatabase::add_document() called with a "
205 "read-only shard");
208 void
209 Database::Internal::delete_document(Xapian::docid)
211 // Writable databases should override this method, but this can get called
212 // if a read-only shard gets added to a WritableDatabase.
213 invalid_operation("WritableDatabase::delete_document() called with a "
214 "read-only shard");
217 void
218 Database::Internal::delete_document(const string& unique_term)
220 // Default implementation - overridden for remote databases
222 if (is_read_only()) {
223 // This can happen if a read-only shard gets added to a
224 // WritableDatabase.
225 invalid_operation("WritableDatabase::delete_document() called with a "
226 "read-only shard");
229 unique_ptr<PostList> pl(open_post_list(unique_term));
231 // We want this operation to be atomic if possible, so if we aren't in a
232 // transaction and the backend supports transactions, temporarily enter an
233 // unflushed transaction.
234 auto old_state = state;
235 if (state != TRANSACTION_UNIMPLEMENTED)
236 state = TRANSACTION_UNFLUSHED;
237 try {
238 while (pl->next(), !pl->at_end()) {
239 delete_document(pl->get_docid());
241 } catch (...) {
242 state = old_state;
243 throw;
245 state = old_state;
248 void
249 Database::Internal::replace_document(Xapian::docid, const Xapian::Document &)
251 // Writable databases should override this method, but this can get called
252 // if a read-only shard gets added to a WritableDatabase.
253 invalid_operation("WritableDatabase::replace_document() called with a "
254 "read-only shard");
257 Xapian::docid
258 Database::Internal::replace_document(const string & unique_term,
259 const Xapian::Document & document)
261 // Default implementation - overridden for remote databases
263 if (is_read_only()) {
264 // This can happen if a read-only shard gets added to a
265 // WritableDatabase.
266 invalid_operation("WritableDatabase::replace_document() called with a "
267 "read-only shard");
270 unique_ptr<PostList> pl(open_post_list(unique_term));
271 pl->next();
272 if (pl->at_end()) {
273 return add_document(document);
275 Xapian::docid did = pl->get_docid();
277 // We want this operation to be atomic if possible, so if we aren't in a
278 // transaction and the backend supports transactions, temporarily enter an
279 // unflushed transaction.
280 auto old_state = state;
281 if (state != TRANSACTION_UNIMPLEMENTED)
282 state = TRANSACTION_UNFLUSHED;
283 try {
284 replace_document(did, document);
285 while (pl->next(), !pl->at_end()) {
286 delete_document(pl->get_docid());
288 } catch (...) {
289 state = old_state;
290 throw;
292 state = old_state;
293 return did;
296 ValueList *
297 Database::Internal::open_value_list(Xapian::valueno slot) const
299 return new SlowValueList(this, slot);
302 TermList *
303 Database::Internal::open_spelling_termlist(const string &) const
305 // Only implemented for some database backends - others will just not
306 // suggest spelling corrections (or not contribute to them in a multiple
307 // database situation).
308 return NULL;
311 TermList *
312 Database::Internal::open_spelling_wordlist() const
314 // Only implemented for some database backends - others will just not
315 // suggest spelling corrections (or not contribute to them in a multiple
316 // database situation).
317 return NULL;
320 Xapian::doccount
321 Database::Internal::get_spelling_frequency(const string &) const
323 // Only implemented for some database backends - others will just not
324 // suggest spelling corrections (or not contribute to them in a multiple
325 // database situation).
326 return 0;
329 void
330 Database::Internal::add_spelling(const string &, Xapian::termcount) const
332 throw Xapian::UnimplementedError("This backend doesn't implement spelling correction");
335 Xapian::termcount
336 Database::Internal::remove_spelling(const string &, Xapian::termcount) const
338 throw Xapian::UnimplementedError("This backend doesn't implement spelling correction");
341 TermList *
342 Database::Internal::open_synonym_termlist(const string &) const
344 // Only implemented for some database backends - others will just not
345 // expand synonyms (or not contribute to them in a multiple database
346 // situation).
347 return NULL;
350 TermList *
351 Database::Internal::open_synonym_keylist(const string &) const
353 // Only implemented for some database backends - others will just not
354 // expand synonyms (or not contribute to them in a multiple database
355 // situation).
356 return NULL;
359 void
360 Database::Internal::add_synonym(const string &, const string &) const
362 throw Xapian::UnimplementedError("This backend doesn't implement synonyms");
365 void
366 Database::Internal::remove_synonym(const string &, const string &) const
368 throw Xapian::UnimplementedError("This backend doesn't implement synonyms");
371 void
372 Database::Internal::clear_synonyms(const string &) const
374 throw Xapian::UnimplementedError("This backend doesn't implement synonyms");
377 string
378 Database::Internal::get_metadata(const string &) const
380 return string();
383 TermList*
384 Database::Internal::open_metadata_keylist(const string&) const
386 // Only implemented for some database backends - others will simply report
387 // there being no metadata keys.
388 return NULL;
391 void
392 Database::Internal::set_metadata(const string&, const string&)
394 throw Xapian::UnimplementedError("This backend doesn't implement metadata");
397 bool
398 Database::Internal::reopen()
400 // Database backends which don't support simultaneous update and reading
401 // probably don't need to do anything here. And since we didn't do
402 // anything we should return false to indicate that nothing has changed.
403 return false;
406 void
407 Database::Internal::request_document(Xapian::docid) const
411 void
412 Database::Internal::write_changesets_to_fd(int, const string&, bool, ReplicationInfo*)
414 throw Xapian::UnimplementedError("This backend doesn't provide changesets");
417 string
418 Database::Internal::get_revision_info() const
420 throw Xapian::UnimplementedError("This backend doesn't provide access to revision information");
423 string
424 Database::Internal::get_uuid() const
426 return string();
429 void
430 Database::Internal::invalidate_doc_object(Xapian::Document::Internal*) const
432 // Do nothing, by default.
435 void
436 Database::Internal::get_used_docid_range(Xapian::docid &,
437 Xapian::docid &) const
439 throw Xapian::UnimplementedError("This backend doesn't implement get_used_docid_range()");
442 bool
443 Database::Internal::locked() const
445 return false;