1 /** @file databaseinternal.cc
2 * @brief Virtual base class for Database internals
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
24 #include "databaseinternal.h"
26 #include "api/leafpostlist.h"
28 #include "slowvaluelist.h"
29 #include "xapian/error.h"
36 using Xapian::Internal::intrusive_ptr
;
41 static void invalid_operation(const char* msg
)
43 throw InvalidOperationError(msg
);
46 Database::Internal::size_type
47 Database::Internal::size() const
53 Database::Internal::keep_alive()
55 // No-op except for remote databases.
59 Database::Internal::readahead_for_query(const Xapian::Query
&) const
64 Database::Internal::get_value_freq(Xapian::valueno
) const
66 throw Xapian::UnimplementedError("This backend doesn't support get_value_freq");
70 Database::Internal::get_value_lower_bound(Xapian::valueno
) const
76 Database::Internal::get_value_upper_bound(Xapian::valueno
) const
78 throw Xapian::UnimplementedError("This backend doesn't support get_value_upper_bound");
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.
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)));
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
);
107 // Discard any exceptions - we're called from the destructors of derived
108 // classes so we can't safely throw.
110 Database::Internal::dtor_called_()
113 if (transaction_active()) {
114 end_transaction(false);
116 // TRANSACTION_READONLY and TRANSACTION_UNIMPLEMENTED should be
117 // handled by the inlined dtor_called() wrapper.
118 AssertEq(state
, TRANSACTION_NONE
);
122 // We can't safely throw exceptions from a destructor in case an
123 // exception is already active and causing us to be destroyed.
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");
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");
144 Database::Internal::begin_transaction(bool flushed
)
146 if (state
!= TRANSACTION_NONE
) {
147 if (transaction_active()) {
148 invalid_operation("WritableDatabase::begin_transaction(): already "
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");
158 // N.B. Call commit() before we set state since commit() isn't allowed
159 // during a transaction.
161 state
= TRANSACTION_FLUSHED
;
163 state
= TRANSACTION_UNFLUSHED
;
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 "
184 "WritableDatabase::cancel_transaction(): not in a "
188 auto old_state
= state
;
189 state
= TRANSACTION_NONE
;
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.
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 "
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 "
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
225 invalid_operation("WritableDatabase::delete_document() called with a "
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
;
238 while (pl
->next(), !pl
->at_end()) {
239 delete_document(pl
->get_docid());
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 "
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
266 invalid_operation("WritableDatabase::replace_document() called with a "
270 unique_ptr
<PostList
> pl(open_post_list(unique_term
));
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
;
284 replace_document(did
, document
);
285 while (pl
->next(), !pl
->at_end()) {
286 delete_document(pl
->get_docid());
297 Database::Internal::open_value_list(Xapian::valueno slot
) const
299 return new SlowValueList(this, slot
);
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).
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).
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).
330 Database::Internal::add_spelling(const string
&, Xapian::termcount
) const
332 throw Xapian::UnimplementedError("This backend doesn't implement spelling correction");
336 Database::Internal::remove_spelling(const string
&, Xapian::termcount
) const
338 throw Xapian::UnimplementedError("This backend doesn't implement spelling correction");
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
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
360 Database::Internal::add_synonym(const string
&, const string
&) const
362 throw Xapian::UnimplementedError("This backend doesn't implement synonyms");
366 Database::Internal::remove_synonym(const string
&, const string
&) const
368 throw Xapian::UnimplementedError("This backend doesn't implement synonyms");
372 Database::Internal::clear_synonyms(const string
&) const
374 throw Xapian::UnimplementedError("This backend doesn't implement synonyms");
378 Database::Internal::get_metadata(const string
&) const
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.
392 Database::Internal::set_metadata(const string
&, const string
&)
394 throw Xapian::UnimplementedError("This backend doesn't implement metadata");
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.
407 Database::Internal::request_document(Xapian::docid
) const
412 Database::Internal::write_changesets_to_fd(int, const string
&, bool, ReplicationInfo
*)
414 throw Xapian::UnimplementedError("This backend doesn't provide changesets");
418 Database::Internal::get_revision_info() const
420 throw Xapian::UnimplementedError("This backend doesn't provide access to revision information");
424 Database::Internal::get_uuid() const
430 Database::Internal::invalidate_doc_object(Xapian::Document::Internal
*) const
432 // Do nothing, by default.
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()");
443 Database::Internal::locked() const