xapian-core  1.4.22
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 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 
234  TermIterator termlist_begin(Xapian::docid did) const;
235 
238  TermIterator XAPIAN_NOTHROW(termlist_end(Xapian::docid) const) {
239  return TermIterator();
240  }
241 
243  bool has_positions() const;
244 
248  PositionIterator positionlist_begin(Xapian::docid did, const std::string &tname) const;
249 
252  PositionIterator XAPIAN_NOTHROW(positionlist_end(Xapian::docid, const std::string &) const) {
253  return PositionIterator();
254  }
255 
261  TermIterator allterms_begin(const std::string & prefix = std::string()) const;
262 
265  TermIterator XAPIAN_NOTHROW(allterms_end(const std::string & = std::string()) const) {
266  return TermIterator();
267  }
268 
270  Xapian::doccount get_doccount() const;
271 
273  Xapian::docid get_lastdocid() const;
274 
276  Xapian::doclength get_avlength() const;
277 
284  double get_average_length() const { return get_avlength(); }
285 
290  Xapian::totallength get_total_length() const;
291 
293  Xapian::doccount get_termfreq(const std::string & tname) const;
294 
303  bool term_exists(const std::string & tname) const;
304 
314  Xapian::termcount get_collection_freq(const std::string & tname) const;
315 
323  Xapian::doccount get_value_freq(Xapian::valueno slot) const;
324 
332  std::string get_value_lower_bound(Xapian::valueno slot) const;
333 
341  std::string get_value_upper_bound(Xapian::valueno slot) const;
342 
347  Xapian::termcount get_doclength_lower_bound() const;
348 
350  Xapian::termcount get_doclength_upper_bound() const;
351 
353  Xapian::termcount get_wdf_upper_bound(const std::string & term) const;
354 
356  ValueIterator valuestream_begin(Xapian::valueno slot) const;
357 
360  return ValueIterator();
361  }
362 
364  Xapian::termcount get_doclength(Xapian::docid did) const;
365 
367  Xapian::termcount get_unique_terms(Xapian::docid did) const;
368 
373  void keep_alive();
374 
390  Xapian::Document get_document(Xapian::docid did) const;
391 
409  Xapian::Document get_document(Xapian::docid did, unsigned flags) const;
410 
419  std::string get_spelling_suggestion(const std::string &word,
420  unsigned max_edit_distance = 2) const;
421 
429  Xapian::TermIterator spellings_begin() const;
430 
432  Xapian::TermIterator XAPIAN_NOTHROW(spellings_end() const) {
433  return Xapian::TermIterator();
434  }
435 
440  Xapian::TermIterator synonyms_begin(const std::string &term) const;
441 
443  Xapian::TermIterator XAPIAN_NOTHROW(synonyms_end(const std::string &) const) {
444  return Xapian::TermIterator();
445  }
446 
452  Xapian::TermIterator synonym_keys_begin(const std::string &prefix = std::string()) const;
453 
455  Xapian::TermIterator XAPIAN_NOTHROW(synonym_keys_end(const std::string & = std::string()) const) {
456  return Xapian::TermIterator();
457  }
458 
483  std::string get_metadata(const std::string & key) const;
484 
503  Xapian::TermIterator metadata_keys_begin(const std::string &prefix = std::string()) const;
504 
506  Xapian::TermIterator XAPIAN_NOTHROW(metadata_keys_end(const std::string & = std::string()) const) {
507  return Xapian::TermIterator();
508  }
509 
524  std::string get_uuid() const;
525 
539  bool locked() const;
540 
552  Xapian::rev get_revision() const;
553 
560  static size_t check(const std::string & path, int opts = 0,
561  std::ostream *out = NULL) {
562  return check_(&path, 0, opts, out);
563  }
564 
575  static size_t check(int fd, int opts = 0, std::ostream *out = NULL) {
576  return check_(NULL, fd, opts, out);
577  }
578 
623  void compact(const std::string & output,
624  unsigned flags = 0,
625  int block_size = 0) {
626  compact_(&output, 0, flags, block_size, NULL);
627  }
628 
671  void compact(int fd,
672  unsigned flags = 0,
673  int block_size = 0) {
674  compact_(NULL, fd, flags, block_size, NULL);
675  }
676 
719  void compact(const std::string & output,
720  unsigned flags,
721  int block_size,
722  Xapian::Compactor & compactor)
723  {
724  compact_(&output, 0, flags, block_size, &compactor);
725  }
726 
774  void compact(int fd,
775  unsigned flags,
776  int block_size,
777  Xapian::Compactor & compactor)
778  {
779  compact_(NULL, fd, flags, block_size, &compactor);
780  }
781 };
782 
786  public:
799  virtual ~WritableDatabase();
800 
807 
846  explicit WritableDatabase(const std::string &path,
847  int flags = 0,
848  int block_size = 0);
849 
852  explicit WritableDatabase(Database::Internal *internal);
853 
859  WritableDatabase(const WritableDatabase &other);
860 
870  void operator=(const WritableDatabase &other);
871 
872 #ifdef XAPIAN_MOVE_SEMANTICS
873  WritableDatabase(WritableDatabase&& o) : Database(std::move(o)) {}
875 
877  WritableDatabase& operator=(WritableDatabase&& o) {
878  Database::operator=(std::move(o));
879  return *this;
880  }
881 #endif
882 
891  void add_database(const WritableDatabase& other) {
892  // This method is provided mainly so that adding a Database to a
893  // WritableDatabase is a compile-time error - prior to 1.4.19, it
894  // would essentially act as a "black-hole" shard which discarded
895  // any changes made to it.
896  Database::add_database(other);
897  }
898 
937  void commit();
938 
943  XAPIAN_DEPRECATED(void flush()) { commit(); }
944 
991  void begin_transaction(bool flushed = true);
992 
1018  void commit_transaction();
1019 
1038  void cancel_transaction();
1039 
1068  Xapian::docid add_document(const Xapian::Document & document);
1069 
1091  void delete_document(Xapian::docid did);
1092 
1112  void delete_document(const std::string & unique_term);
1113 
1145  void replace_document(Xapian::docid did,
1146  const Xapian::Document & document);
1147 
1181  Xapian::docid replace_document(const std::string & unique_term,
1182  const Xapian::Document & document);
1183 
1191  void add_spelling(const std::string & word,
1192  Xapian::termcount freqinc = 1) const;
1193 
1202  void remove_spelling(const std::string & word,
1203  Xapian::termcount freqdec = 1) const;
1204 
1211  void add_synonym(const std::string & term,
1212  const std::string & synonym) const;
1213 
1220  void remove_synonym(const std::string & term,
1221  const std::string & synonym) const;
1222 
1228  void clear_synonyms(const std::string & term) const;
1229 
1274  void set_metadata(const std::string & key, const std::string & metadata);
1275 
1277  std::string get_description() const;
1278 };
1279 
1280 }
1281 
1282 #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:671
PositionIterator positionlist_end(Xapian::docid, const std::string &) const
Corresponding end iterator to positionlist_begin().
Definition: database.h:252
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:560
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:575
Base class for databases.
Definition: database.h:56
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:265
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:443
#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:774
ValueIterator valuestream_end(Xapian::valueno) const
Return end iterator corresponding to valuestream_begin().
Definition: database.h:359
This class provides read/write access to a database.
Definition: database.h:785
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:719
Xapian::TermIterator synonym_keys_end(const std::string &=std::string()) const
Corresponding end iterator to synonym_keys_begin(prefix).
Definition: database.h:455
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:238
void compact(const std::string &output, unsigned flags=0, int block_size=0)
Produce a compact version of this database.
Definition: database.h:623
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:284
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:891
void flush()
Pre-1.1.0 name for commit().
Definition: database.h:943
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:432
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:506