xapian-core  1.4.27
database.h
Go to the documentation of this file.
1 
4 /* Copyright 1999,2000,2001 BrightStation PLC
5  * Copyright 2002 Ananova Ltd
6  * Copyright 2002,2003,2004,2005,2006,2007,2008,2009,2011,2012,2013,2014,2015,2016,2017,2019,2023 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 
93  size_t size() const {
94  return internal.size();
95  }
96 
99  Database();
100 
107  explicit Database(const std::string &path, int flags = 0);
108 
120  explicit Database(int fd, int flags = 0);
121 
124  explicit Database(Internal *internal);
125 
131  virtual ~Database();
132 
138  Database(const Database &other);
139 
145  void operator=(const Database &other);
146 
147 #ifdef XAPIAN_MOVE_SEMANTICS
148  Database(Database&& o);
150 
152  Database& operator=(Database&& o);
153 #endif
154 
170  bool reopen();
171 
206  virtual void close();
207 
209  virtual std::string get_description() const;
210 
221  PostingIterator postlist_begin(const std::string &tname) const;
222 
225  PostingIterator XAPIAN_NOTHROW(postlist_end(const std::string &) const) {
226  return PostingIterator();
227  }
228 
236  TermIterator termlist_begin(Xapian::docid did) const;
237 
240  TermIterator XAPIAN_NOTHROW(termlist_end(Xapian::docid) const) {
241  return TermIterator();
242  }
243 
245  bool has_positions() const;
246 
250  PositionIterator positionlist_begin(Xapian::docid did, const std::string &tname) const;
251 
254  PositionIterator XAPIAN_NOTHROW(positionlist_end(Xapian::docid, const std::string &) const) {
255  return PositionIterator();
256  }
257 
265  TermIterator allterms_begin(const std::string & prefix = std::string()) const;
266 
269  TermIterator XAPIAN_NOTHROW(allterms_end(const std::string & = std::string()) const) {
270  return TermIterator();
271  }
272 
274  Xapian::doccount get_doccount() const;
275 
277  Xapian::docid get_lastdocid() const;
278 
280  Xapian::doclength get_avlength() const;
281 
288  double get_average_length() const { return get_avlength(); }
289 
294  Xapian::totallength get_total_length() const;
295 
297  Xapian::doccount get_termfreq(const std::string & tname) const;
298 
307  bool term_exists(const std::string & tname) const;
308 
318  Xapian::termcount get_collection_freq(const std::string & tname) const;
319 
327  Xapian::doccount get_value_freq(Xapian::valueno slot) const;
328 
336  std::string get_value_lower_bound(Xapian::valueno slot) const;
337 
345  std::string get_value_upper_bound(Xapian::valueno slot) const;
346 
351  Xapian::termcount get_doclength_lower_bound() const;
352 
354  Xapian::termcount get_doclength_upper_bound() const;
355 
357  Xapian::termcount get_wdf_upper_bound(const std::string & term) const;
358 
360  ValueIterator valuestream_begin(Xapian::valueno slot) const;
361 
364  return ValueIterator();
365  }
366 
368  Xapian::termcount get_doclength(Xapian::docid did) const;
369 
371  Xapian::termcount get_unique_terms(Xapian::docid did) const;
372 
377  void keep_alive();
378 
394  Xapian::Document get_document(Xapian::docid did) const;
395 
413  Xapian::Document get_document(Xapian::docid did, unsigned flags) const;
414 
423  std::string get_spelling_suggestion(const std::string &word,
424  unsigned max_edit_distance = 2) const;
425 
433  Xapian::TermIterator spellings_begin() const;
434 
436  Xapian::TermIterator XAPIAN_NOTHROW(spellings_end() const) {
437  return Xapian::TermIterator();
438  }
439 
444  Xapian::TermIterator synonyms_begin(const std::string &term) const;
445 
447  Xapian::TermIterator XAPIAN_NOTHROW(synonyms_end(const std::string &) const) {
448  return Xapian::TermIterator();
449  }
450 
456  Xapian::TermIterator synonym_keys_begin(const std::string &prefix = std::string()) const;
457 
459  Xapian::TermIterator XAPIAN_NOTHROW(synonym_keys_end(const std::string & = std::string()) const) {
460  return Xapian::TermIterator();
461  }
462 
487  std::string get_metadata(const std::string & key) const;
488 
507  Xapian::TermIterator metadata_keys_begin(const std::string &prefix = std::string()) const;
508 
510  Xapian::TermIterator XAPIAN_NOTHROW(metadata_keys_end(const std::string & = std::string()) const) {
511  return Xapian::TermIterator();
512  }
513 
528  std::string get_uuid() const;
529 
543  bool locked() const;
544 
556  Xapian::rev get_revision() const;
557 
564  static size_t check(const std::string & path, int opts = 0,
565  std::ostream *out = NULL) {
566  return check_(&path, 0, opts, out);
567  }
568 
579  static size_t check(int fd, int opts = 0, std::ostream *out = NULL) {
580  return check_(NULL, fd, opts, out);
581  }
582 
627  void compact(const std::string & output,
628  unsigned flags = 0,
629  int block_size = 0) {
630  compact_(&output, 0, flags, block_size, NULL);
631  }
632 
675  void compact(int fd,
676  unsigned flags = 0,
677  int block_size = 0) {
678  compact_(NULL, fd, flags, block_size, NULL);
679  }
680 
723  void compact(const std::string & output,
724  unsigned flags,
725  int block_size,
726  Xapian::Compactor & compactor)
727  {
728  compact_(&output, 0, flags, block_size, &compactor);
729  }
730 
778  void compact(int fd,
779  unsigned flags,
780  int block_size,
781  Xapian::Compactor & compactor)
782  {
783  compact_(NULL, fd, flags, block_size, &compactor);
784  }
785 };
786 
790  public:
803  virtual ~WritableDatabase();
804 
811 
850  explicit WritableDatabase(const std::string &path,
851  int flags = 0,
852  int block_size = 0);
853 
856  explicit WritableDatabase(Database::Internal *internal);
857 
863  WritableDatabase(const WritableDatabase &other);
864 
874  void operator=(const WritableDatabase &other);
875 
876 #ifdef XAPIAN_MOVE_SEMANTICS
877  WritableDatabase(WritableDatabase&& o) : Database(std::move(o)) {}
879 
881  WritableDatabase& operator=(WritableDatabase&& o) {
882  Database::operator=(std::move(o));
883  return *this;
884  }
885 #endif
886 
895  void add_database(const WritableDatabase& other) {
896  // This method is provided mainly so that adding a Database to a
897  // WritableDatabase is a compile-time error - prior to 1.4.19, it
898  // would essentially act as a "black-hole" shard which discarded
899  // any changes made to it.
900  Database::add_database(other);
901  }
902 
945  void commit();
946 
951  XAPIAN_DEPRECATED(void flush()) { commit(); }
952 
1003  void begin_transaction(bool flushed = true);
1004 
1034  void commit_transaction();
1035 
1054  void cancel_transaction();
1055 
1084  Xapian::docid add_document(const Xapian::Document & document);
1085 
1107  void delete_document(Xapian::docid did);
1108 
1128  void delete_document(const std::string & unique_term);
1129 
1161  void replace_document(Xapian::docid did,
1162  const Xapian::Document & document);
1163 
1197  Xapian::docid replace_document(const std::string & unique_term,
1198  const Xapian::Document & document);
1199 
1207  void add_spelling(const std::string & word,
1208  Xapian::termcount freqinc = 1) const;
1209 
1218  void remove_spelling(const std::string & word,
1219  Xapian::termcount freqdec = 1) const;
1220 
1227  void add_synonym(const std::string & term,
1228  const std::string & synonym) const;
1229 
1236  void remove_synonym(const std::string & term,
1237  const std::string & synonym) const;
1238 
1244  void clear_synonyms(const std::string & term) const;
1245 
1290  void set_metadata(const std::string & key, const std::string & metadata);
1291 
1293  std::string get_description() const;
1294 };
1295 
1296 }
1297 
1298 #endif /* XAPIAN_INCLUDED_DATABASE_H */
The Xapian namespace contains public interfaces for the Xapian library.
Definition: compactor.cc:80
int close(FD &fd)
Definition: fd.h:63
void compact(int fd, unsigned flags=0, int block_size=0)
Produce a compact version of this database.
Definition: database.h:675
PositionIterator positionlist_end(Xapian::docid, const std::string &) const
Corresponding end iterator to positionlist_begin().
Definition: database.h:254
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:564
typedefs for Xapian
XAPIAN_REVISION_TYPE rev
Revision number of a database.
Definition: types.h:133
This class is used to access a database, or a group of databases.
Definition: database.h:68
static size_t check(int fd, int opts=0, std::ostream *out=NULL)
Check the integrity of a single file database.
Definition: database.h:579
Base class for databases.
Definition: database.h:57
XAPIAN_TOTALLENGTH_TYPE totallength
The total length of all documents in a database.
Definition: types.h:139
TermIterator allterms_end(const std::string &=std::string()) const
Corresponding end iterator to allterms_begin(prefix).
Definition: database.h:269
Compiler attribute macros.
static const char * opts
Define XAPIAN_DEPRECATED() and related macros.
Class for iterating over document values.
Definition: valueiterator.h:40
Xapian::TermIterator synonyms_end(const std::string &) const
Corresponding end iterator to synonyms_begin(term).
Definition: database.h:447
#define XAPIAN_DEPRECATED(X)
Class for iterating over term positions.
#define XAPIAN_VISIBILITY_DEFAULT
Definition: visibility.h:28
Class for iterating over a list of terms.
Definition: termiterator.h:41
unsigned XAPIAN_TERMCOUNT_BASE_TYPE termcount
A counts of terms.
Definition: types.h:72
Class for iterating over a list of terms.
void compact(int fd, unsigned flags, int block_size, Xapian::Compactor &compactor)
Produce a compact version of this database.
Definition: database.h:778
ValueIterator valuestream_end(Xapian::valueno) const
Return end iterator corresponding to valuestream_begin().
Definition: database.h:363
This class provides read/write access to a database.
Definition: database.h:789
Define XAPIAN_VISIBILITY_* macros.
double doclength
A normalised document length.
Definition: types.h:59
Compact a database, or merge and compact several.
Definition: compactor.h:42
void compact(const std::string &output, unsigned flags, int block_size, Xapian::Compactor &compactor)
Produce a compact version of this database.
Definition: database.h:723
Xapian::TermIterator synonym_keys_end(const std::string &=std::string()) const
Corresponding end iterator to synonym_keys_begin(prefix).
Definition: database.h:459
Class for iterating over document values.
Class for iterating over term positions.
TermIterator termlist_end(Xapian::docid) const
Corresponding end iterator to termlist_begin().
Definition: database.h:240
void compact(const std::string &output, unsigned flags=0, int block_size=0)
Produce a compact version of this database.
Definition: database.h:627
void add_database(const Database &database)
Add an existing database (or group of databases) to those accessed by this object.
Definition: omdatabase.cc:148
size_t size() const
Return number of shards in this Database object.
Definition: database.h:93
double get_average_length() const
New name for get_avlength().
Definition: database.h:288
unsigned XAPIAN_DOCID_BASE_TYPE doccount
A count of documents.
Definition: types.h:38
void add_database(const WritableDatabase &other)
Add shards from another WritableDatabase.
Definition: database.h:895
void flush()
Pre-1.1.0 name for commit().
Definition: database.h:951
unsigned valueno
The number for a value slot in a document.
Definition: types.h:108
void operator=(const Database &other)
Assignment is allowed.
Definition: omdatabase.cc:113
Class for iterating over a list of document ids.
Xapian::TermIterator spellings_end() const
Corresponding end iterator to spellings_begin().
Definition: database.h:436
unsigned XAPIAN_DOCID_BASE_TYPE docid
A unique identifier for a document.
Definition: types.h:52
PostingIterator postlist_end(const std::string &) const
Corresponding end iterator to postlist_begin().
Definition: database.h:225
Class for iterating over a list of terms.
A handle representing a document in a Xapian database.
Definition: document.h:61
Xapian::TermIterator metadata_keys_end(const std::string &=std::string()) const
Corresponding end iterator to metadata_keys_begin().
Definition: database.h:510