xapian-core  2.0.0
database.h
Go to the documentation of this file.
1 
4 /* Copyright 2003-2026 Olly Betts
5  * Copyright 2007,2008,2009 Lemur Consulting Ltd
6  *
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.
11  *
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.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, see
19  * <https://www.gnu.org/licenses/>.
20  */
21 
22 #ifndef XAPIAN_INCLUDED_DATABASE_H
23 #define XAPIAN_INCLUDED_DATABASE_H
24 
25 #if !defined XAPIAN_IN_XAPIAN_H && !defined XAPIAN_LIB_BUILD
26 # error Never use <xapian/database.h> directly; include <xapian.h> instead.
27 #endif
28 
29 #include <iosfwd>
30 #include <string>
31 #include <string_view>
32 #include <utility>
33 #include <vector>
34 
35 #include <xapian/attributes.h>
36 #include <xapian/constants.h>
37 #include <xapian/intrusive_ptr.h>
39 #include <xapian/postingiterator.h>
40 #include <xapian/termiterator.h>
41 #include <xapian/types.h>
42 #include <xapian/valueiterator.h>
43 #include <xapian/visibility.h>
44 
45 namespace Xapian {
46 
47 class Compactor;
48 class Document;
49 class WritableDatabase;
50 
77  static size_t check_(const std::string_view* path_ptr,
78  int fd,
79  int opts,
80  std::ostream* out);
81 
83  void compact_(const std::string_view* output_ptr,
84  int fd,
85  unsigned flags,
86  int block_size,
87  Xapian::Compactor* compactor) const;
88 
89  protected:
91  void add_database_(const Database& other, bool read_only);
92 
93  public:
95  class Internal;
98 
109  void add_database(const Database& other) {
110  add_database_(other, true);
111  }
112 
119  size_t size() const;
120 
127  Database();
128 
143  explicit Database(std::string_view path, int flags = 0);
144 
160  explicit Database(int fd, int flags = 0);
161 
164  explicit Database(Internal* internal) XAPIAN_NONNULL();
165 
167  virtual ~Database();
168 
173  Database(const Database& o);
174 
180 
183 
186 
210  bool reopen();
211 
244  void close();
245 
247  virtual std::string get_description() const;
248 
255  PostingIterator postlist_begin(std::string_view term) const;
256 
258  PostingIterator postlist_end(std::string_view) const noexcept {
259  return PostingIterator();
260  }
261 
268  TermIterator termlist_begin(Xapian::docid did) const;
269 
272  return TermIterator();
273  }
274 
276  bool has_positions() const;
277 
288  PositionIterator positionlist_begin(Xapian::docid did,
289  std::string_view term) const;
290 
293  std::string_view) const noexcept {
294  return PositionIterator();
295  }
296 
304  TermIterator allterms_begin(std::string_view prefix = {}) const;
305 
307  TermIterator allterms_end(std::string_view = {}) const noexcept
308  {
309  return TermIterator();
310  }
311 
313  Xapian::doccount get_doccount() const;
314 
316  Xapian::docid get_lastdocid() const;
317 
319  double get_average_length() const;
320 
322  double get_avlength() const { return get_average_length(); }
323 
328  Xapian::totallength get_total_length() const;
329 
338  Xapian::doccount get_termfreq(std::string_view term) const;
339 
350  bool term_exists(std::string_view term) const;
351 
364  Xapian::termcount get_collection_freq(std::string_view term) const;
365 
373  Xapian::doccount get_value_freq(Xapian::valueno slot) const;
374 
382  std::string get_value_lower_bound(Xapian::valueno slot) const;
383 
391  std::string get_value_upper_bound(Xapian::valueno slot) const;
392 
397  Xapian::termcount get_doclength_lower_bound() const;
398 
400  Xapian::termcount get_doclength_upper_bound() const;
401 
403  Xapian::termcount get_wdf_upper_bound(std::string_view term) const;
404 
409  Xapian::termcount get_unique_terms_lower_bound() const;
410 
415  Xapian::termcount get_unique_terms_upper_bound() const;
416 
418  ValueIterator valuestream_begin(Xapian::valueno slot) const;
419 
422  return ValueIterator();
423  }
424 
432  Xapian::termcount get_doclength(Xapian::docid did) const;
433 
440  Xapian::termcount get_unique_terms(Xapian::docid did) const;
441 
448  Xapian::termcount get_wdfdocmax(Xapian::docid did) const;
449 
459  void keep_alive();
460 
478  Xapian::Document get_document(Xapian::docid did,
479  unsigned flags = 0) const;
480 
491  std::string get_spelling_suggestion(std::string_view word,
492  unsigned max_edit_distance = 2) const;
493 
500  Xapian::TermIterator spellings_begin() const;
501 
504  return Xapian::TermIterator();
505  }
506 
511  Xapian::TermIterator synonyms_begin(std::string_view term) const;
512 
514  Xapian::TermIterator synonyms_end(std::string_view) const noexcept {
515  return Xapian::TermIterator();
516  }
517 
522  Xapian::TermIterator synonym_keys_begin(std::string_view prefix = {}) const;
523 
526  synonym_keys_end(std::string_view = {}) const noexcept {
527  return Xapian::TermIterator();
528  }
529 
553  std::string get_metadata(std::string_view key) const;
554 
572  metadata_keys_begin(std::string_view prefix = {}) const;
573 
576  metadata_keys_end(std::string_view = {}) const noexcept {
577  return Xapian::TermIterator();
578  }
579 
594  std::string get_uuid() const;
595 
609  bool locked() const;
610 
631  Xapian::WritableDatabase lock(int flags = 0);
632 
644  Xapian::Database unlock();
645 
661  Xapian::rev get_revision() const;
662 
669  static size_t check(std::string_view path,
670  int opts = 0,
671  std::ostream* out = NULL) {
672  return check_(&path, 0, opts, out);
673  }
674 
685  static size_t check(int fd, int opts = 0, std::ostream* out = NULL) {
686  return check_(NULL, fd, opts, out);
687  }
688 
738  void compact(std::string_view output,
739  unsigned flags = 0,
740  int block_size = 0) {
741  compact_(&output, 0, flags, block_size, NULL);
742  }
743 
793  void compact(int fd,
794  unsigned flags = 0,
795  int block_size = 0) {
796  compact_(NULL, fd, flags, block_size, NULL);
797  }
798 
849  void compact(std::string_view output,
850  unsigned flags,
851  int block_size,
852  Xapian::Compactor& compactor)
853  {
854  compact_(&output, 0, flags, block_size, &compactor);
855  }
856 
911  void compact(int fd,
912  unsigned flags,
913  int block_size,
914  Xapian::Compactor& compactor)
915  {
916  compact_(NULL, fd, flags, block_size, &compactor);
917  }
918 
938  std::string reconstruct_text(Xapian::docid did,
939  size_t length = 0,
940  std::string_view prefix = {},
941  Xapian::termpos start_pos = 0,
942  Xapian::termpos end_pos = 0) const;
943 };
944 
970  void end_transaction_(bool do_commit);
971 
972  public:
979 
990  void add_database(const WritableDatabase& other) {
991  // This method is provided mainly so that adding a Database to a
992  // WritableDatabase is a compile-time error - prior to 2.0.0, it
993  // would essentially act as a "black-hole" shard which discarded
994  // any changes made to it.
995  add_database_(other, false);
996  }
997 
1052  explicit WritableDatabase(std::string_view path,
1053  int flags = 0,
1054  int block_size = 0);
1055 
1059  : Database(internal_) {}
1060 
1066 
1073  return *this;
1074  }
1075 
1078 
1081  Database::operator=(std::move(o));
1082  return *this;
1083  }
1084 
1120  void commit();
1121 
1169  void begin_transaction(bool flushed = true);
1170 
1193  void commit_transaction() { end_transaction_(true); }
1194 
1206  void cancel_transaction() { end_transaction_(false); }
1207 
1226  Xapian::docid add_document(const Xapian::Document& doc);
1227 
1240  void delete_document(Xapian::docid did);
1241 
1259  void delete_document(std::string_view unique_term);
1260 
1281  void replace_document(Xapian::docid did, const Xapian::Document& document);
1282 
1311  Xapian::docid replace_document(std::string_view unique_term,
1312  const Xapian::Document& document);
1313 
1321  void add_spelling(std::string_view word,
1322  Xapian::termcount freqinc = 1) const;
1323 
1336  termcount remove_spelling(std::string_view word,
1337  termcount freqdec = 1) const;
1338 
1345  void add_synonym(std::string_view term,
1346  std::string_view synonym) const;
1347 
1354  void remove_synonym(std::string_view term,
1355  std::string_view synonym) const;
1356 
1362  void clear_synonyms(std::string_view term) const;
1363 
1406  void set_metadata(std::string_view key, std::string_view metadata);
1407 
1409  std::string get_description() const;
1410 };
1411 
1412 }
1413 
1414 #endif // XAPIAN_INCLUDED_DATABASE_H
Compiler attribute macros.
#define XAPIAN_NONNULL(LIST)
Annotate function parameters which should be non-NULL pointers.
Definition: attributes.h:84
Compact a database, or merge and compact several.
Definition: compactor.h:39
Virtual base class for Database internals.
An indexed database of documents.
Definition: database.h:75
static size_t check(int fd, int opts=0, std::ostream *out=NULL)
Check the integrity of a single file database.
Definition: database.h:685
Database(const Database &o)
Copy constructor.
static size_t check(std::string_view path, int opts=0, std::ostream *out=NULL)
Check the integrity of a database or database table.
Definition: database.h:669
PositionIterator positionlist_end(Xapian::docid, std::string_view) const noexcept
End iterator corresponding to positionlist_begin().
Definition: database.h:292
Xapian::TermIterator spellings_end() const noexcept
End iterator corresponding to spellings_begin().
Definition: database.h:503
Database & operator=(Database &&o)
Move assignment operator.
void compact(int fd, unsigned flags, int block_size, Xapian::Compactor &compactor)
Produce a compact version of this database.
Definition: database.h:911
double get_avlength() const
Old name for get_average_length() for backward compatibility.
Definition: database.h:322
Xapian::TermIterator synonyms_end(std::string_view) const noexcept
End iterator corresponding to synonyms_begin(term).
Definition: database.h:514
void add_database(const Database &other)
Add shards from another Database.
Definition: database.h:109
Xapian::TermIterator synonym_keys_end(std::string_view={}) const noexcept
End iterator corresponding to synonym_keys_begin(prefix).
Definition: database.h:526
Xapian::TermIterator metadata_keys_end(std::string_view={}) const noexcept
End iterator corresponding to metadata_keys_begin().
Definition: database.h:576
void compact(int fd, unsigned flags=0, int block_size=0)
Produce a compact version of this database.
Definition: database.h:793
TermIterator allterms_end(std::string_view={}) const noexcept
End iterator corresponding to allterms_begin(prefix).
Definition: database.h:307
Database(Database &&o)
Move constructor.
void compact(std::string_view output, unsigned flags, int block_size, Xapian::Compactor &compactor)
Produce a compact version of this database.
Definition: database.h:849
void compact(std::string_view output, unsigned flags=0, int block_size=0)
Produce a compact version of this database.
Definition: database.h:738
PostingIterator postlist_end(std::string_view) const noexcept
End iterator corresponding to postlist_begin().
Definition: database.h:258
TermIterator termlist_end(Xapian::docid) const noexcept
End iterator corresponding to termlist_begin().
Definition: database.h:271
Database & operator=(const Database &o)
Assignment operator.
ValueIterator valuestream_end(Xapian::valueno) const noexcept
Return end iterator corresponding to valuestream_begin().
Definition: database.h:421
Class representing a document.
Definition: document.h:64
Class for iterating over term positions.
Class for iterating over a list of terms.
Class for iterating over a list of terms.
Definition: termiterator.h:41
Class for iterating over document values.
Definition: valueiterator.h:39
This class provides read/write access to a database.
Definition: database.h:964
void commit_transaction()
Complete the transaction currently in progress.
Definition: database.h:1193
WritableDatabase(Database::Internal *internal_)
Definition: database.h:1058
void cancel_transaction()
Abort the transaction currently in progress.
Definition: database.h:1206
WritableDatabase(const WritableDatabase &o)
Copy constructor.
Definition: database.h:1065
void add_database(const WritableDatabase &other)
Add shards from another WritableDatabase.
Definition: database.h:990
WritableDatabase & operator=(WritableDatabase &&o)
Move assignment operator.
Definition: database.h:1080
WritableDatabase()
Create a WritableDatabase with no subdatabases.
Definition: database.h:978
WritableDatabase & operator=(const WritableDatabase &o)
Assignment operator.
Definition: database.h:1071
WritableDatabase(WritableDatabase &&o)
Move constructor.
Definition: database.h:1077
Constants in the Xapian namespace.
string term
int close(FD &fd)
Definition: fd.h:63
The Xapian namespace contains public interfaces for the Xapian library.
Definition: compactor.cc:82
unsigned XAPIAN_TERMCOUNT_BASE_TYPE termcount
A counts of terms.
Definition: types.h:64
XAPIAN_REVISION_TYPE rev
Revision number of a database.
Definition: types.h:108
unsigned valueno
The number for a value slot in a document.
Definition: types.h:90
unsigned XAPIAN_DOCID_BASE_TYPE doccount
A count of documents.
Definition: types.h:37
unsigned XAPIAN_DOCID_BASE_TYPE docid
A unique identifier for a document.
Definition: types.h:51
unsigned XAPIAN_TERMPOS_BASE_TYPE termpos
A term position within a document or query.
Definition: types.h:75
XAPIAN_TOTALLENGTH_TYPE totallength
The total length of all documents in a database.
Definition: types.h:114
Class for iterating over term positions.
Class for iterating over a list of document ids.
Class for iterating over a list of terms.
typedefs for Xapian
Class for iterating over document values.
Define XAPIAN_VISIBILITY_* macros.
#define XAPIAN_VISIBILITY_DEFAULT
Definition: visibility.h:28
#define XAPIAN_VISIBILITY_INTERNAL
Definition: visibility.h:29
static const char * opts