1 /* database.cc: Database::Internal base class.
3 * Copyright 1999,2000,2001 BrightStation PLC
4 * Copyright 2002 Ananova Ltd
5 * Copyright 2002,2003,2004,2005,2006,2007,2008,2009,2011,2014,2016 Olly Betts
6 * Copyright 2008 Lemur Consulting Ltd
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of the
11 * License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
28 #include "xapian/error.h"
30 #include "api/leafpostlist.h"
32 #include "slowvaluelist.h"
38 using Xapian::Internal::intrusive_ptr
;
42 Database::Internal::~Internal()
47 Database::Internal::keep_alive()
49 // For the normal case of local databases, nothing needs to be done.
54 Database::Internal::readahead_for_query(const Xapian::Query
&)
59 Database::Internal::get_doclength_lower_bound() const
61 // A zero-length document can't contain any terms, so we ignore such
62 // documents for the purposes of this lower bound.
67 Database::Internal::get_doclength_upper_bound() const
69 // Not a very tight bound in general, but this is only a fall-back for
70 // backends which don't store these stats.
71 return min(get_total_length(), Xapian::totallength(Xapian::termcount(-1)));
75 Database::Internal::get_wdf_upper_bound(const string
& term
) const
77 // Not a very tight bound in general, but this is only a fall-back for
78 // backends which don't store these stats.
80 get_freqs(term
, NULL
, &cf
);
84 // Discard any exceptions - we're called from the destructors of derived
85 // classes so we can't safely throw.
87 Database::Internal::dtor_called()
90 if (transaction_active()) {
92 } else if (transaction_state
== TRANSACTION_NONE
) {
96 // We can't safely throw exceptions from a destructor in case an
97 // exception is already active and causing us to be destroyed.
102 Database::Internal::commit()
104 // Writable databases should override this method.
109 Database::Internal::cancel()
111 // Writable databases should override this method.
116 Database::Internal::begin_transaction(bool flushed
)
118 if (transaction_state
!= TRANSACTION_NONE
) {
119 if (transaction_state
== TRANSACTION_UNIMPLEMENTED
)
120 throw Xapian::UnimplementedError("This backend doesn't implement transactions");
121 throw InvalidOperationError("Cannot begin transaction - transaction already in progress");
124 // N.B. Call commit() before we set transaction_state since commit()
125 // isn't allowing during a transaction.
127 transaction_state
= TRANSACTION_FLUSHED
;
129 transaction_state
= TRANSACTION_UNFLUSHED
;
134 Database::Internal::commit_transaction()
136 if (!transaction_active()) {
137 if (transaction_state
== TRANSACTION_UNIMPLEMENTED
)
138 throw Xapian::UnimplementedError("This backend doesn't implement transactions");
139 throw InvalidOperationError("Cannot commit transaction - no transaction currently in progress");
141 bool flushed
= (transaction_state
== TRANSACTION_FLUSHED
);
142 transaction_state
= TRANSACTION_NONE
;
143 // N.B. Call commit() after we clear transaction_state since commit()
144 // isn't allowing during a transaction.
145 if (flushed
) commit();
149 Database::Internal::cancel_transaction()
151 if (!transaction_active()) {
152 if (transaction_state
== TRANSACTION_UNIMPLEMENTED
)
153 throw Xapian::UnimplementedError("This backend doesn't implement transactions");
154 throw InvalidOperationError("Cannot cancel transaction - no transaction currently in progress");
156 transaction_state
= TRANSACTION_NONE
;
161 Database::Internal::add_document(const Xapian::Document
&)
163 // Writable databases should override this method.
169 Database::Internal::delete_document(Xapian::docid
)
171 // Writable databases should override this method.
176 Database::Internal::delete_document(const string
& unique_term
)
178 // Default implementation - overridden for remote databases
179 intrusive_ptr
<LeafPostList
> pl(open_post_list(unique_term
));
180 while (pl
->next(), !pl
->at_end()) {
181 delete_document(pl
->get_docid());
186 Database::Internal::replace_document(Xapian::docid
, const Xapian::Document
&)
188 // Writable databases should override this method.
193 Database::Internal::replace_document(const string
& unique_term
,
194 const Xapian::Document
& document
)
196 // Default implementation - overridden for remote databases
197 intrusive_ptr
<LeafPostList
> pl(open_post_list(unique_term
));
200 return add_document(document
);
202 Xapian::docid did
= pl
->get_docid();
203 replace_document(did
, document
);
204 while (pl
->next(), !pl
->at_end()) {
205 delete_document(pl
->get_docid());
211 Database::Internal::open_value_list(Xapian::valueno slot
) const
213 return new SlowValueList(this, slot
);
217 Database::Internal::open_spelling_termlist(const string
&) const
219 // Only implemented for some database backends - others will just not
220 // suggest spelling corrections (or not contribute to them in a multiple
221 // database situation).
226 Database::Internal::open_spelling_wordlist() const
228 // Only implemented for some database backends - others will just not
229 // suggest spelling corrections (or not contribute to them in a multiple
230 // database situation).
235 Database::Internal::get_spelling_frequency(const string
&) const
237 // Only implemented for some database backends - others will just not
238 // suggest spelling corrections (or not contribute to them in a multiple
239 // database situation).
244 Database::Internal::add_spelling(const string
&, Xapian::termcount
) const
246 throw Xapian::UnimplementedError("This backend doesn't implement spelling correction");
250 Database::Internal::remove_spelling(const string
&, Xapian::termcount
) const
252 throw Xapian::UnimplementedError("This backend doesn't implement spelling correction");
256 Database::Internal::open_synonym_termlist(const string
&) const
258 // Only implemented for some database backends - others will just not
259 // expand synonyms (or not contribute to them in a multiple database
265 Database::Internal::open_synonym_keylist(const string
&) const
267 // Only implemented for some database backends - others will just not
268 // expand synonyms (or not contribute to them in a multiple database
274 Database::Internal::add_synonym(const string
&, const string
&) const
276 throw Xapian::UnimplementedError("This backend doesn't implement synonyms");
280 Database::Internal::remove_synonym(const string
&, const string
&) const
282 throw Xapian::UnimplementedError("This backend doesn't implement synonyms");
286 Database::Internal::clear_synonyms(const string
&) const
288 throw Xapian::UnimplementedError("This backend doesn't implement synonyms");
292 Database::Internal::get_metadata(const string
&) const
298 Database::Internal::open_metadata_keylist(const string
&) const
300 // Only implemented for some database backends - others will simply report
301 // there being no metadata keys.
306 Database::Internal::set_metadata(const string
&, const string
&)
308 throw Xapian::UnimplementedError("This backend doesn't implement metadata");
312 Database::Internal::reopen()
314 // Database backends which don't support simultaneous update and reading
315 // probably don't need to do anything here. And since we didn't do
316 // anything we should return false to indicate that nothing has changed.
321 Database::Internal::request_document(Xapian::docid
/*did*/) const
325 Xapian::Document::Internal
*
326 Database::Internal::collect_document(Xapian::docid did
) const
328 // Open the document lazily - collect document is only called by
329 // Enquire::Internal::read_doc() for a given MSetItem, so we know that the
330 // document already exists.
331 return open_document(did
, true);
335 Database::Internal::write_changesets_to_fd(int, const string
&, bool, ReplicationInfo
*)
337 throw Xapian::UnimplementedError("This backend doesn't provide changesets");
341 Database::Internal::get_revision_info() const
343 throw Xapian::UnimplementedError("This backend doesn't provide access to revision information");
347 Database::Internal::get_uuid() const
353 Database::Internal::invalidate_doc_object(Xapian::Document::Internal
*) const
355 // Do nothing, by default.
359 Database::Internal::get_used_docid_range(Xapian::docid
&,
360 Xapian::docid
&) const
362 throw Xapian::UnimplementedError("This backend doesn't implement get_used_docid_range()");
366 Database::Internal::locked() const