xapian-core  1.4.31
database.h
Go to the documentation of this file.
1 
4 /* Copyright 1999,2000,2001 BrightStation PLC
5  * Copyright 2002 Ananova Ltd
6  * Copyright 2003-2026 Olly Betts
7  * Copyright 2006,2008 Lemur Consulting Ltd
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License as
11  * published by the Free Software Foundation; either version 2 of the
12  * License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
22  * USA
23  */
24 
25 #ifndef XAPIAN_INCLUDED_DATABASE_H
26 #define XAPIAN_INCLUDED_DATABASE_H
27 
28 #if !defined XAPIAN_IN_XAPIAN_H && !defined XAPIAN_LIB_BUILD
29 # error Never use <xapian/database.h> directly; include <xapian.h> instead.
30 #endif
31 
32 #include <iosfwd>
33 #include <string>
34 #ifdef XAPIAN_MOVE_SEMANTICS
35 # include <utility>
36 #endif
37 #include <vector>
38 
39 #include <xapian/attributes.h>
40 #include <xapian/deprecated.h>
41 #include <xapian/intrusive_ptr.h>
42 #include <xapian/types.h>
44 #include <xapian/postingiterator.h>
45 #include <xapian/termiterator.h>
46 #include <xapian/valueiterator.h>
47 #include <xapian/visibility.h>
48 
49 namespace Xapian {
50 
51 class Compactor;
52 class Document;
53 
70  static size_t check_(const std::string * path_ptr, int fd, int opts,
71  std::ostream *out);
72 
74  void compact_(const std::string * output_ptr,
75  int fd,
76  unsigned flags,
77  int block_size,
78  Xapian::Compactor * compactor) const;
79 
80  public:
81  class Internal;
83  std::vector<Xapian::Internal::intrusive_ptr<Internal> > internal;
84 
90  void add_database(const Database & database);
91 
98  size_t size() const {
99  return internal.size();
100  }
101 
104  Database();
105 
112  explicit Database(const std::string &path, int flags = 0);
113 
125  explicit Database(int fd, int flags = 0);
126 
129  explicit Database(Internal *internal);
130 
136  virtual ~Database();
137 
143  Database(const Database &other);
144 
150  void operator=(const Database &other);
151 
152 #ifdef XAPIAN_MOVE_SEMANTICS
154  Database(Database&& o);
155 
157  Database& operator=(Database&& o);
158 #endif
159 
175  bool reopen();
176 
211  virtual void close();
212 
214  virtual std::string get_description() const;
215 
226  PostingIterator postlist_begin(const std::string &tname) const;
227 
230  PostingIterator XAPIAN_NOTHROW(postlist_end(const std::string &) const) {
231  return PostingIterator();
232  }
233 
241  TermIterator termlist_begin(Xapian::docid did) const;
242 
245  TermIterator XAPIAN_NOTHROW(termlist_end(Xapian::docid) const) {
246  return TermIterator();
247  }
248 
250  bool has_positions() const;
251 
255  PositionIterator positionlist_begin(Xapian::docid did, const std::string &tname) const;
256 
259  PositionIterator XAPIAN_NOTHROW(positionlist_end(Xapian::docid, const std::string &) const) {
260  return PositionIterator();
261  }
262 
270  TermIterator allterms_begin(const std::string & prefix = std::string()) const;
271 
274  TermIterator XAPIAN_NOTHROW(allterms_end(const std::string & = std::string()) const) {
275  return TermIterator();
276  }
277 
279  Xapian::doccount get_doccount() const;
280 
282  Xapian::docid get_lastdocid() const;
283 
285  Xapian::doclength get_avlength() const;
286 
293  double get_average_length() const { return get_avlength(); }
294 
299  Xapian::totallength get_total_length() const;
300 
302  Xapian::doccount get_termfreq(const std::string & tname) const;
303 
312  bool term_exists(const std::string & tname) const;
313 
323  Xapian::termcount get_collection_freq(const std::string & tname) const;
324 
332  Xapian::doccount get_value_freq(Xapian::valueno slot) const;
333 
341  std::string get_value_lower_bound(Xapian::valueno slot) const;
342 
350  std::string get_value_upper_bound(Xapian::valueno slot) const;
351 
356  Xapian::termcount get_doclength_lower_bound() const;
357 
359  Xapian::termcount get_doclength_upper_bound() const;
360 
362  Xapian::termcount get_wdf_upper_bound(const std::string & term) const;
363 
365  ValueIterator valuestream_begin(Xapian::valueno slot) const;
366 
369  return ValueIterator();
370  }
371 
373  Xapian::termcount get_doclength(Xapian::docid did) const;
374 
376  Xapian::termcount get_unique_terms(Xapian::docid did) const;
377 
382  void keep_alive();
383 
399  Xapian::Document get_document(Xapian::docid did) const;
400 
418  Xapian::Document get_document(Xapian::docid did, unsigned flags) const;
419 
428  std::string get_spelling_suggestion(const std::string &word,
429  unsigned max_edit_distance = 2) const;
430 
438  Xapian::TermIterator spellings_begin() const;
439 
441  Xapian::TermIterator XAPIAN_NOTHROW(spellings_end() const) {
442  return Xapian::TermIterator();
443  }
444 
449  Xapian::TermIterator synonyms_begin(const std::string &term) const;
450 
452  Xapian::TermIterator XAPIAN_NOTHROW(synonyms_end(const std::string &) const) {
453  return Xapian::TermIterator();
454  }
455 
461  Xapian::TermIterator synonym_keys_begin(const std::string &prefix = std::string()) const;
462 
464  Xapian::TermIterator XAPIAN_NOTHROW(synonym_keys_end(const std::string & = std::string()) const) {
465  return Xapian::TermIterator();
466  }
467 
492  std::string get_metadata(const std::string & key) const;
493 
512  Xapian::TermIterator metadata_keys_begin(const std::string &prefix = std::string()) const;
513 
515  Xapian::TermIterator XAPIAN_NOTHROW(metadata_keys_end(const std::string & = std::string()) const) {
516  return Xapian::TermIterator();
517  }
518 
533  std::string get_uuid() const;
534 
548  bool locked() const;
549 
561  Xapian::rev get_revision() const;
562 
569  static size_t check(const std::string & path, int opts = 0,
570  std::ostream *out = NULL) {
571  return check_(&path, 0, opts, out);
572  }
573 
584  static size_t check(int fd, int opts = 0, std::ostream *out = NULL) {
585  return check_(NULL, fd, opts, out);
586  }
587 
633  void compact(const std::string & output,
634  unsigned flags = 0,
635  int block_size = 0) {
636  compact_(&output, 0, flags, block_size, NULL);
637  }
638 
681  void compact(int fd,
682  unsigned flags = 0,
683  int block_size = 0) {
684  compact_(NULL, fd, flags, block_size, NULL);
685  }
686 
729  void compact(const std::string & output,
730  unsigned flags,
731  int block_size,
732  Xapian::Compactor & compactor)
733  {
734  compact_(&output, 0, flags, block_size, &compactor);
735  }
736 
784  void compact(int fd,
785  unsigned flags,
786  int block_size,
787  Xapian::Compactor & compactor)
788  {
789  compact_(NULL, fd, flags, block_size, &compactor);
790  }
791 };
792 
796  public:
809  virtual ~WritableDatabase();
810 
817 
856  explicit WritableDatabase(const std::string &path,
857  int flags = 0,
858  int block_size = 0);
859 
862  explicit WritableDatabase(Database::Internal *internal);
863 
869  WritableDatabase(const WritableDatabase &other);
870 
880  void operator=(const WritableDatabase &other);
881 
882 #ifdef XAPIAN_MOVE_SEMANTICS
884  WritableDatabase(WritableDatabase&& o) : Database(std::move(o)) {}
885 
887  WritableDatabase& operator=(WritableDatabase&& o) {
888  Database::operator=(std::move(o));
889  return *this;
890  }
891 #endif
892 
901  void add_database(const WritableDatabase& other) {
902  // This method is provided mainly so that adding a Database to a
903  // WritableDatabase is a compile-time error - prior to 1.4.19, it
904  // would essentially act as a "black-hole" shard which discarded
905  // any changes made to it.
906  Database::add_database(other);
907  }
908 
951  void commit();
952 
957  XAPIAN_DEPRECATED(void flush()) { commit(); }
958 
1009  void begin_transaction(bool flushed = true);
1010 
1040  void commit_transaction();
1041 
1060  void cancel_transaction();
1061 
1090  Xapian::docid add_document(const Xapian::Document & document);
1091 
1113  void delete_document(Xapian::docid did);
1114 
1134  void delete_document(const std::string & unique_term);
1135 
1167  void replace_document(Xapian::docid did,
1168  const Xapian::Document & document);
1169 
1203  Xapian::docid replace_document(const std::string & unique_term,
1204  const Xapian::Document & document);
1205 
1213  void add_spelling(const std::string & word,
1214  Xapian::termcount freqinc = 1) const;
1215 
1224  void remove_spelling(const std::string & word,
1225  Xapian::termcount freqdec = 1) const;
1226 
1233  void add_synonym(const std::string & term,
1234  const std::string & synonym) const;
1235 
1242  void remove_synonym(const std::string & term,
1243  const std::string & synonym) const;
1244 
1250  void clear_synonyms(const std::string & term) const;
1251 
1296  void set_metadata(const std::string & key, const std::string & metadata);
1297 
1299  std::string get_description() const;
1300 };
1301 
1302 }
1303 
1304 #endif /* XAPIAN_INCLUDED_DATABASE_H */
Compiler attribute macros.
Compact a database, or merge and compact several.
Definition: compactor.h:42
Base class for databases.
Definition: database.h:57
This class is used to access a database, or a group of databases.
Definition: database.h:68
void compact(const std::string &output, unsigned flags=0, int block_size=0)
Produce a compact version of this database.
Definition: database.h:633
static size_t check(int fd, int opts=0, std::ostream *out=NULL)
Check the integrity of a single file database.
Definition: database.h:584
Xapian::TermIterator synonyms_end(const std::string &) const
Corresponding end iterator to synonyms_begin(term).
Definition: database.h:452
ValueIterator valuestream_end(Xapian::valueno) const
Return end iterator corresponding to valuestream_begin().
Definition: database.h:368
void add_database(const Database &database)
Add an existing database (or group of databases) to those accessed by this object.
Definition: omdatabase.cc:148
Xapian::TermIterator spellings_end() const
Corresponding end iterator to spellings_begin().
Definition: database.h:441
double get_average_length() const
New name for get_avlength().
Definition: database.h:293
PostingIterator postlist_end(const std::string &) const
Corresponding end iterator to postlist_begin().
Definition: database.h:230
void operator=(const Database &other)
Assignment is allowed.
Definition: omdatabase.cc:113
Xapian::TermIterator metadata_keys_end(const std::string &=std::string()) const
Corresponding end iterator to metadata_keys_begin().
Definition: database.h:515
void compact(const std::string &output, unsigned flags, int block_size, Xapian::Compactor &compactor)
Produce a compact version of this database.
Definition: database.h:729
void compact(int fd, unsigned flags, int block_size, Xapian::Compactor &compactor)
Produce a compact version of this database.
Definition: database.h:784
size_t size() const
Return number of shards in this Database object.
Definition: database.h:98
Xapian::TermIterator synonym_keys_end(const std::string &=std::string()) const
Corresponding end iterator to synonym_keys_begin(prefix).
Definition: database.h:464
void compact(int fd, unsigned flags=0, int block_size=0)
Produce a compact version of this database.
Definition: database.h:681
static size_t check(const std::string &path, int opts=0, std::ostream *out=NULL)
Check the integrity of a database or database table.
Definition: database.h:569
TermIterator allterms_end(const std::string &=std::string()) const
Corresponding end iterator to allterms_begin(prefix).
Definition: database.h:274
TermIterator termlist_end(Xapian::docid) const
Corresponding end iterator to termlist_begin().
Definition: database.h:245
PositionIterator positionlist_end(Xapian::docid, const std::string &) const
Corresponding end iterator to positionlist_begin().
Definition: database.h:259
A handle representing a document in a Xapian database.
Definition: document.h:61
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:40
This class provides read/write access to a database.
Definition: database.h:795
void add_database(const WritableDatabase &other)
Add shards from another WritableDatabase.
Definition: database.h:901
void flush()
Pre-1.1.0 name for commit().
Definition: database.h:957
Define XAPIAN_DEPRECATED() and related macros.
int close(FD &fd)
Definition: fd.h:63
The Xapian namespace contains public interfaces for the Xapian library.
Definition: compactor.cc:80
unsigned XAPIAN_TERMCOUNT_BASE_TYPE termcount
A counts of terms.
Definition: types.h:72
XAPIAN_REVISION_TYPE rev
Revision number of a database.
Definition: types.h:133
double doclength
A normalised document length.
Definition: types.h:59
unsigned valueno
The number for a value slot in a document.
Definition: types.h:108
unsigned XAPIAN_DOCID_BASE_TYPE doccount
A count of documents.
Definition: types.h:38
unsigned XAPIAN_DOCID_BASE_TYPE docid
A unique identifier for a document.
Definition: types.h:52
XAPIAN_TOTALLENGTH_TYPE totallength
The total length of all documents in a database.
Definition: types.h:139
Class for iterating over term positions.
Class for iterating over a list of document ids.
#define XAPIAN_DEPRECATED(X)
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
static const char * opts