xapian-core  1.4.26
Public Member Functions | Private Member Functions | List of all members
Xapian::WritableDatabase Class Reference

This class provides read/write access to a database. More...

#include <database.h>

+ Inheritance diagram for Xapian::WritableDatabase:
+ Collaboration diagram for Xapian::WritableDatabase:

Public Member Functions

virtual ~WritableDatabase ()
 Destroy this handle on the database. More...
 
 WritableDatabase ()
 Create a WritableDatabase with no subdatabases. More...
 
 WritableDatabase (const std::string &path, int flags=0, int block_size=0)
 Open a database for update, automatically determining the database backend to use. More...
 
 WritableDatabase (const WritableDatabase &other)
 Copying is allowed. More...
 
void operator= (const WritableDatabase &other)
 Assignment is allowed. More...
 
void add_database (const WritableDatabase &other)
 Add shards from another WritableDatabase. More...
 
void commit ()
 Commit any pending modifications made to the database. More...
 
void flush ()
 Pre-1.1.0 name for commit(). More...
 
void begin_transaction (bool flushed=true)
 Begin a transaction. More...
 
void commit_transaction ()
 Complete the transaction currently in progress. More...
 
void cancel_transaction ()
 Abort the transaction currently in progress, discarding the pending modifications made to the database. More...
 
Xapian::docid add_document (const Xapian::Document &document)
 Add a new document to the database. More...
 
void delete_document (Xapian::docid did)
 Delete a document from the database. More...
 
void delete_document (const std::string &unique_term)
 Delete any documents indexed by a term from the database. More...
 
void replace_document (Xapian::docid did, const Xapian::Document &document)
 Replace a given document in the database. More...
 
Xapian::docid replace_document (const std::string &unique_term, const Xapian::Document &document)
 Replace any documents matching a term. More...
 
void add_spelling (const std::string &word, Xapian::termcount freqinc=1) const
 Add a word to the spelling dictionary. More...
 
void remove_spelling (const std::string &word, Xapian::termcount freqdec=1) const
 Remove a word from the spelling dictionary. More...
 
void add_synonym (const std::string &term, const std::string &synonym) const
 Add a synonym for a term. More...
 
void remove_synonym (const std::string &term, const std::string &synonym) const
 Remove a synonym for a term. More...
 
void clear_synonyms (const std::string &term) const
 Remove all synonyms for a term. More...
 
void set_metadata (const std::string &key, const std::string &metadata)
 Set the user-specified metadata associated with a given key. More...
 
std::string get_description () const
 Return a string describing this object. More...
 
- Public Member Functions inherited from Xapian::Database
void add_database (const Database &database)
 Add an existing database (or group of databases) to those accessed by this object. More...
 
size_t size () const
 Return number of shards in this Database object. More...
 
 Database ()
 Create a Database with no databases in. More...
 
 Database (const std::string &path, int flags=0)
 Open a Database, automatically determining the database backend to use. More...
 
 Database (int fd, int flags=0)
 Open a single-file Database. More...
 
virtual ~Database ()
 Destroy this handle on the database. More...
 
 Database (const Database &other)
 Copying is allowed. More...
 
void operator= (const Database &other)
 Assignment is allowed. More...
 
bool reopen ()
 Re-open the database. More...
 
virtual void close ()
 Close the database. More...
 
PostingIterator postlist_begin (const std::string &tname) const
 An iterator pointing to the start of the postlist for a given term. More...
 
PostingIterator postlist_end (const std::string &) const
 Corresponding end iterator to postlist_begin(). More...
 
TermIterator termlist_begin (Xapian::docid did) const
 An iterator pointing to the start of the termlist for a given document. More...
 
TermIterator termlist_end (Xapian::docid) const
 Corresponding end iterator to termlist_begin(). More...
 
bool has_positions () const
 Does this database have any positional information? More...
 
PositionIterator positionlist_begin (Xapian::docid did, const std::string &tname) const
 An iterator pointing to the start of the position list for a given term in a given document. More...
 
PositionIterator positionlist_end (Xapian::docid, const std::string &) const
 Corresponding end iterator to positionlist_begin(). More...
 
TermIterator allterms_begin (const std::string &prefix=std::string()) const
 An iterator which runs across all terms with a given prefix. More...
 
TermIterator allterms_end (const std::string &=std::string()) const
 Corresponding end iterator to allterms_begin(prefix). More...
 
Xapian::doccount get_doccount () const
 Get the number of documents in the database. More...
 
Xapian::docid get_lastdocid () const
 Get the highest document id which has been used in the database. More...
 
Xapian::doclength get_avlength () const
 Get the average length of the documents in the database. More...
 
double get_average_length () const
 New name for get_avlength(). More...
 
Xapian::totallength get_total_length () const
 Get the total length of all the documents in the database. More...
 
Xapian::doccount get_termfreq (const std::string &tname) const
 Get the number of documents in the database indexed by a given term. More...
 
bool term_exists (const std::string &tname) const
 Check if a given term exists in the database. More...
 
Xapian::termcount get_collection_freq (const std::string &tname) const
 Return the total number of occurrences of the given term. More...
 
Xapian::doccount get_value_freq (Xapian::valueno slot) const
 Return the frequency of a given value slot. More...
 
std::string get_value_lower_bound (Xapian::valueno slot) const
 Get a lower bound on the values stored in the given value slot. More...
 
std::string get_value_upper_bound (Xapian::valueno slot) const
 Get an upper bound on the values stored in the given value slot. More...
 
Xapian::termcount get_doclength_lower_bound () const
 Get a lower bound on the length of a document in this DB. More...
 
Xapian::termcount get_doclength_upper_bound () const
 Get an upper bound on the length of a document in this DB. More...
 
Xapian::termcount get_wdf_upper_bound (const std::string &term) const
 Get an upper bound on the wdf of term term. More...
 
ValueIterator valuestream_begin (Xapian::valueno slot) const
 Return an iterator over the value in slot slot for each document. More...
 
ValueIterator valuestream_end (Xapian::valueno) const
 Return end iterator corresponding to valuestream_begin(). More...
 
Xapian::termcount get_doclength (Xapian::docid did) const
 Get the length of a document. More...
 
Xapian::termcount get_unique_terms (Xapian::docid did) const
 Get the number of unique terms in document. More...
 
void keep_alive ()
 Send a "keep-alive" to remote databases to stop them timing out. More...
 
Xapian::Document get_document (Xapian::docid did) const
 Get a document from the database, given its document id. More...
 
Xapian::Document get_document (Xapian::docid did, unsigned flags) const
 Get a document from the database, given its document id. More...
 
std::string get_spelling_suggestion (const std::string &word, unsigned max_edit_distance=2) const
 Suggest a spelling correction. More...
 
Xapian::TermIterator spellings_begin () const
 An iterator which returns all the spelling correction targets. More...
 
Xapian::TermIterator spellings_end () const
 Corresponding end iterator to spellings_begin(). More...
 
Xapian::TermIterator synonyms_begin (const std::string &term) const
 An iterator which returns all the synonyms for a given term. More...
 
Xapian::TermIterator synonyms_end (const std::string &) const
 Corresponding end iterator to synonyms_begin(term). More...
 
Xapian::TermIterator synonym_keys_begin (const std::string &prefix=std::string()) const
 An iterator which returns all terms which have synonyms. More...
 
Xapian::TermIterator synonym_keys_end (const std::string &=std::string()) const
 Corresponding end iterator to synonym_keys_begin(prefix). More...
 
std::string get_metadata (const std::string &key) const
 Get the user-specified metadata associated with a given key. More...
 
Xapian::TermIterator metadata_keys_begin (const std::string &prefix=std::string()) const
 An iterator which returns all user-specified metadata keys. More...
 
Xapian::TermIterator metadata_keys_end (const std::string &=std::string()) const
 Corresponding end iterator to metadata_keys_begin(). More...
 
std::string get_uuid () const
 Get a UUID for the database. More...
 
bool locked () const
 Test if this database is currently locked for writing. More...
 
Xapian::rev get_revision () const
 Get the revision of the database. More...
 
void compact (const std::string &output, unsigned flags=0, int block_size=0)
 Produce a compact version of this database. More...
 
void compact (int fd, unsigned flags=0, int block_size=0)
 Produce a compact version of this database. More...
 
void compact (const std::string &output, unsigned flags, int block_size, Xapian::Compactor &compactor)
 Produce a compact version of this database. More...
 
void compact (int fd, unsigned flags, int block_size, Xapian::Compactor &compactor)
 Produce a compact version of this database. More...
 

Private Member Functions

 WritableDatabase (Database::Internal *internal)
 

Additional Inherited Members

- Static Public Member Functions inherited from Xapian::Database
static size_t check (const std::string &path, int opts=0, std::ostream *out=NULL)
 Check the integrity of a database or database table. More...
 
static size_t check (int fd, int opts=0, std::ostream *out=NULL)
 Check the integrity of a single file database. More...
 

Detailed Description

This class provides read/write access to a database.

Definition at line 789 of file database.h.

Constructor & Destructor Documentation

◆ ~WritableDatabase()

Xapian::WritableDatabase::~WritableDatabase ( )
virtual

Destroy this handle on the database.

If no other handles to this database remain, the database will be closed.

If a transaction is active cancel_transaction() will be implicitly called; if no transaction is active commit() will be implicitly called, but any exception will be swallowed (because throwing exceptions in C++ destructors is problematic). If you aren't using transactions and want to know about any failure to commit changes, call commit() explicitly before the destructor gets called.

Definition at line 851 of file omdatabase.cc.

References LOGCALL_DTOR.

◆ WritableDatabase() [1/4]

Xapian::WritableDatabase::WritableDatabase ( )

Create a WritableDatabase with no subdatabases.

The created object isn't very useful in this state - it's intended as a placeholder value.

Definition at line 827 of file omdatabase.cc.

References LOGCALL_CTOR.

◆ WritableDatabase() [2/4]

Xapian::WritableDatabase::WritableDatabase ( const std::string &  path,
int  flags = 0,
int  block_size = 0 
)
explicit

Open a database for update, automatically determining the database backend to use.

If the database is to be created, Xapian will try to create the directory indicated by path if it doesn't already exist (but only the leaf directory, not recursively).

Parameters
pathdirectory that the database is stored in.
flagsone of:

Additionally, the following flags can be combined with action using bitwise-or (| in C++):

Parameters
block_sizeIf a new database is created, this specifies the block size (in bytes) for backends which have such a concept. For chert and glass, the block size must be a power of 2 between 2048 and 65536 (inclusive), and the default (also used if an invalid value is passed) is 8192 bytes.
Exceptions
Xapian::DatabaseCorruptErrorwill be thrown if the database is in a corrupt state.
Xapian::DatabaseLockErrorwill be thrown if a lock couldn't be acquired on the database.

Definition at line 286 of file dbfactory.cc.

References Xapian::DB_BACKEND_CHERT, Xapian::DB_BACKEND_GLASS, Xapian::DB_BACKEND_INMEMORY, Xapian::DB_BACKEND_STUB, file_exists(), LOGCALL_CTOR, Xapian::open_stub(), rare, S_ISDIR, S_ISREG, and usual.

◆ WritableDatabase() [3/4]

Xapian::WritableDatabase::WritableDatabase ( Database::Internal internal)
explicitprivate

Create an WritableDatabase given its internals.

Definition at line 832 of file omdatabase.cc.

References LOGCALL_CTOR.

◆ WritableDatabase() [4/4]

Xapian::WritableDatabase::WritableDatabase ( const WritableDatabase other)

Copying is allowed.

The internals are reference counted, so copying is cheap.

Parameters
otherThe object to copy.

Definition at line 838 of file omdatabase.cc.

References LOGCALL_CTOR.

Member Function Documentation

◆ add_database()

void Xapian::WritableDatabase::add_database ( const WritableDatabase other)
inline

Add shards from another WritableDatabase.

Any shards in other are added to the list of shards in this object. The shards are reference counted and also remain in other.

Parameters
otherAnother WritableDatabase object to add shards from

Definition at line 895 of file database.h.

References Xapian::Database::add_database().

Referenced by BackendManagerMulti::createdb_multi(), and Xapian::open_stub().

◆ add_document()

Xapian::docid Xapian::WritableDatabase::add_document ( const Xapian::Document document)

Add a new document to the database.

This method adds the specified document to the database, returning a newly allocated document ID. Automatically allocated document IDs come from a per-database monotonically increasing counter, so IDs from deleted documents won't be reused.

If you want to specify the document ID to be used, you should call replace_document() instead.

Note that changes to the database won't be immediately committed to disk; see commit() for more details.

As with all database modification operations, the effect is atomic: the document will either be fully added, or the document fails to be added and an exception is thrown (possibly at a later time when commit() is called or the database is closed).

Parameters
documentThe new document to be added.
Returns
The document ID of the newly added document.
Exceptions
Xapian::DatabaseErrorwill be thrown if a problem occurs while writing to the database.
Xapian::DatabaseCorruptErrorwill be thrown if the database is in a corrupt state.

Definition at line 902 of file omdatabase.cc.

References Xapian::Database::get_lastdocid(), LOGCALL, no_subdatabases(), rare, RETURN, sub_db(), and sub_docid().

Referenced by bigoaddvalue1_helper(), builddb_coords1(), builddb_queries1(), DEFINE_TESTCASE(), gen_consistency2_db(), gen_decvalwtsource3_db(), gen_decvalwtsource5_db(), gen_lazytablebug1_db(), gen_longpositionlist1_db(), gen_qp_flag_partial1_db(), gen_qp_range3_db(), gen_subdbwithoutpos1_db(), gen_uniqterms_gt_doclen_db(), gen_wdf_eq_doclen_db(), FileIndexer::index_to(), main(), make_all_tables(), make_all_tables2(), make_matchspy2_db(), make_matchtimelimit1_db(), make_missing_tables(), make_msize1_db(), make_msize2_db(), make_multichunk_db(), make_ordecay_db(), make_phrasebug1_db(), make_singularvalue_db(), make_tg_db(), make_topercent7_db(), make_valprefixbounds_db(), make_valuerange5(), make_xordecay1_db(), RemoteServer::msg_adddocument(), and replace_document().

◆ add_spelling()

void Xapian::WritableDatabase::add_spelling ( const std::string &  word,
Xapian::termcount  freqinc = 1 
) const

Add a word to the spelling dictionary.

If the word is already present, its frequency is increased.

Parameters
wordThe word to add.
freqincHow much to increase its frequency by (default 1).

Definition at line 1004 of file omdatabase.cc.

References LOGCALL_VOID, no_subdatabases(), and rare.

Referenced by DEFINE_TESTCASE(), gen_simple_spelling_db(), main(), make_all_tables(), make_all_tables2(), and RemoteServer::msg_addspelling_().

◆ add_synonym()

void Xapian::WritableDatabase::add_synonym ( const std::string &  term,
const std::string &  synonym 
) const

Add a synonym for a term.

Parameters
termThe term to add a synonym for.
synonymThe synonym to add. If this is already a synonym for term, then no action is taken.

Definition at line 1028 of file omdatabase.cc.

References LOGCALL_VOID, no_subdatabases(), and rare.

Referenced by DEFINE_TESTCASE(), gen_lazytablebug1_db(), main(), make_all_tables(), and make_all_tables2().

◆ begin_transaction()

void Xapian::WritableDatabase::begin_transaction ( bool  flushed = true)

Begin a transaction.

In Xapian a transaction is a group of modifications to the database which are linked such that either all will be applied simultaneously or none will be applied at all. Even in the case of a power failure, this characteristic should be preserved (as long as the filesystem isn't corrupted, etc).

However, note that if called on a sharded database, atomicity isn't guaranteed between shards. Within each shard, the transaction will still act atomically.

A transaction is started with begin_transaction() and can either be committed by calling commit_transaction() or aborted by calling cancel_transaction().

By default, a transaction implicitly calls commit() before and after so that the modifications stand and fall without affecting modifications before or after.

The downside of these implicit calls to commit() is that small transactions can harm indexing performance in the same way that explicitly calling commit() frequently can.

If you're applying atomic groups of changes and only wish to ensure that each group is either applied or not applied, then you can prevent the automatic commit() before and after the transaction by starting the transaction with begin_transaction(false). However, if cancel_transaction is called (or if commit_transaction isn't called before the WritableDatabase object is destroyed) then any changes which were pending before the transaction began will also be discarded.

Transactions aren't currently supported by the InMemory backend.

Parameters
flushedIs this a flushed transaction? By default transactions are "flushed", which means that committing a transaction will ensure those changes are permanently written to the database. By contrast, unflushed transactions only ensure that changes within the transaction are either all applied or all aren't.
Exceptions
Xapian::UnimplementedErrorwill be thrown if transactions are not available for this database type.
Xapian::InvalidOperationErrorwill be thrown if this is called at an invalid time, such as when a transaction is already in progress.

Definition at line 868 of file omdatabase.cc.

References LOGCALL_VOID, no_subdatabases(), and rare.

Referenced by DEFINE_TESTCASE(), and RemoteServer::msg_cancel_().

◆ cancel_transaction()

void Xapian::WritableDatabase::cancel_transaction ( )

Abort the transaction currently in progress, discarding the pending modifications made to the database.

If an error occurs in this method, an exception will be thrown, but the transaction will be cancelled anyway.

Exceptions
Xapian::DatabaseErrorwill be thrown if a problem occurs while modifying the database.
Xapian::DatabaseCorruptErrorwill be thrown if the database is in a corrupt state.
Xapian::InvalidOperationErrorwill be thrown if a transaction is not currently in progress.
Xapian::UnimplementedErrorwill be thrown if transactions are not available for this database type.

Definition at line 890 of file omdatabase.cc.

References LOGCALL_VOID, no_subdatabases(), and rare.

Referenced by DEFINE_TESTCASE(), and RemoteServer::msg_cancel_().

◆ clear_synonyms()

void Xapian::WritableDatabase::clear_synonyms ( const std::string &  term) const

Remove all synonyms for a term.

Parameters
termThe term to remove all synonyms for. If the term has no synonyms, no action is taken.

Definition at line 1052 of file omdatabase.cc.

References LOGCALL_VOID, no_subdatabases(), and rare.

Referenced by DEFINE_TESTCASE().

◆ commit()

void Xapian::WritableDatabase::commit ( )

Commit any pending modifications made to the database.

For efficiency reasons, when performing multiple updates to a database it is best (indeed, almost essential) to make as many modifications as memory will permit in a single pass through the database. To ensure this, Xapian batches up modifications.

This method may be called at any time to commit any pending modifications to the database.

If any of the modifications fail, an exception will be thrown and the database will be left in a state in which each separate addition, replacement or deletion operation has either been fully performed or not performed at all: it is then up to the application to work out which operations need to be repeated.

However, note that if called on a sharded database, atomicity isn't guaranteed between shards - it's possible for the changes to one shard to be committed but changes to another shard to fail.

It's not valid to call commit() within a transaction.

Beware of calling commit() too frequently: this will make indexing take much longer.

Note that commit() need not be called explicitly: it will be called automatically when the database is closed, or when a sufficient number of modifications have been made. By default, this is every 10000 documents added, deleted, or modified. This value is rather conservative, and if you have a machine with plenty of memory, you can improve indexing throughput dramatically by setting XAPIAN_FLUSH_THRESHOLD in the environment to a larger value.

This method was new in Xapian 1.1.0 - in earlier versions it was called flush().

Exceptions
Xapian::DatabaseErrorwill be thrown if a problem occurs while modifying the database.
Xapian::DatabaseCorruptErrorwill be thrown if the database is in a corrupt state.

Definition at line 857 of file omdatabase.cc.

References LOGCALL_VOID, no_subdatabases(), and rare.

Referenced by bigoaddvalue1_helper(), builddb_queries1(), builddb_valuestest1(), DEFINE_TESTCASE(), gen_lazytablebug1_db(), main(), make_all_tables(), make_all_tables2(), make_missing_tables(), make_multichunk_db(), make_netstats1_db(), make_sparse_db(), make_topercent7_db(), and RemoteServer::msg_commit().

◆ commit_transaction()

void Xapian::WritableDatabase::commit_transaction ( )

Complete the transaction currently in progress.

If this method completes successfully and this is a flushed transaction, all the database modifications made during the transaction will have been committed to the database.

If an error occurs, an exception will be thrown, and none of the modifications made to the database during the transaction will have been applied to the database.

In all cases the transaction will no longer be in progress.

Note that if called on a sharded database, atomicity isn't guaranteed between shards. Within each shard, the transaction will still act atomically.

Exceptions
Xapian::DatabaseErrorwill be thrown if a problem occurs while modifying the database.
Xapian::DatabaseCorruptErrorwill be thrown if the database is in a corrupt state.
Xapian::InvalidOperationErrorwill be thrown if a transaction is not currently in progress.
Xapian::UnimplementedErrorwill be thrown if transactions are not available for this database type.

Definition at line 879 of file omdatabase.cc.

References LOGCALL_VOID, no_subdatabases(), and rare.

Referenced by DEFINE_TESTCASE().

◆ delete_document() [1/2]

void Xapian::WritableDatabase::delete_document ( Xapian::docid  did)

Delete a document from the database.

This method removes the document with the specified document ID from the database.

Note that changes to the database won't be immediately committed to disk; see commit() for more details.

As with all database modification operations, the effect is atomic: the document will either be fully removed, or the document fails to be removed and an exception is thrown (possibly at a later time when commit() is called or the database is closed).

Parameters
didThe document ID of the document to be removed.
Exceptions
Xapian::DatabaseErrorwill be thrown if a problem occurs while writing to the database.
Xapian::DatabaseCorruptErrorwill be thrown if the database is in a corrupt state.

Definition at line 925 of file omdatabase.cc.

References docid_zero_invalid(), LOGCALL_VOID, no_subdatabases(), rare, sub_db(), and sub_docid().

Referenced by DEFINE_TESTCASE(), delete_document(), make_sparse_db(), RemoteServer::msg_deletedocument(), and RemoteServer::msg_deletedocumentterm_().

◆ delete_document() [2/2]

void Xapian::WritableDatabase::delete_document ( const std::string &  unique_term)

Delete any documents indexed by a term from the database.

This method removes any documents indexed by the specified term from the database.

A major use is for convenience when UIDs from another system are mapped to terms in Xapian, although this method has other uses (for example, you could add a "deletion date" term to documents at index time and use this method to delete all documents due for deletion on a particular date).

Parameters
unique_termThe term to remove references to.
Exceptions
Xapian::DatabaseErrorwill be thrown if a problem occurs while writing to the database.
Xapian::DatabaseCorruptErrorwill be thrown if the database is in a corrupt state.

Definition at line 939 of file omdatabase.cc.

References delete_document(), LOGCALL_VOID, no_subdatabases(), and rare.

◆ flush()

void Xapian::WritableDatabase::flush ( )
inline

Pre-1.1.0 name for commit().

Use commit() instead.

Definition at line 951 of file database.h.

Referenced by DEFINE_TESTCASE().

◆ get_description()

string Xapian::WritableDatabase::get_description ( ) const
virtual

Return a string describing this object.

Todo:
display contents of the writable database

Reimplemented from Xapian::Database.

Definition at line 1075 of file omdatabase.cc.

◆ operator=()

void Xapian::WritableDatabase::operator= ( const WritableDatabase other)

Assignment is allowed.

The internals are reference counted, so assignment is cheap.

Note that only an WritableDatabase may be assigned to an WritableDatabase: an attempt to assign a Database is caught at compile-time.

Parameters
otherThe object to copy.

Definition at line 845 of file omdatabase.cc.

References LOGCALL_VOID, and Xapian::Database::operator=().

◆ remove_spelling()

void Xapian::WritableDatabase::remove_spelling ( const std::string &  word,
Xapian::termcount  freqdec = 1 
) const

Remove a word from the spelling dictionary.

The word's frequency is decreased, and if would become zero or less then the word is removed completely.

Parameters
wordThe word to remove.
freqdecHow much to decrease its frequency by (default 1).

Definition at line 1015 of file omdatabase.cc.

References LOGCALL_VOID, no_subdatabases(), and rare.

Referenced by DEFINE_TESTCASE(), and RemoteServer::msg_removespelling().

◆ remove_synonym()

void Xapian::WritableDatabase::remove_synonym ( const std::string &  term,
const std::string &  synonym 
) const

Remove a synonym for a term.

Parameters
termThe term to remove a synonym for.
synonymThe synonym to remove. If this isn't currently a synonym for term, then no action is taken.

Definition at line 1039 of file omdatabase.cc.

References LOGCALL_VOID, no_subdatabases(), and rare.

Referenced by DEFINE_TESTCASE().

◆ replace_document() [1/2]

void Xapian::WritableDatabase::replace_document ( Xapian::docid  did,
const Xapian::Document document 
)

Replace a given document in the database.

This method replaces the document with the specified document ID. If document ID did isn't currently used, the document will be added with document ID did.

The monotonic counter used for automatically allocating document IDs is increased so that the next automatically allocated document ID will be did + 1. Be aware that if you use this method to specify a high document ID for a new document, and also use WritableDatabase::add_document(), Xapian may get to a state where this counter wraps around and will be unable to automatically allocate document IDs!

Note that changes to the database won't be immediately committed to disk; see commit() for more details.

As with all database modification operations, the effect is atomic: the document will either be fully replaced, or the document fails to be replaced and an exception is thrown (possibly at a later time when commit() is called or the database is closed).

Parameters
didThe document ID of the document to be replaced.
documentThe new document.
Exceptions
Xapian::DatabaseErrorwill be thrown if a problem occurs while writing to the database.
Xapian::DatabaseCorruptErrorwill be thrown if the database is in a corrupt state.

Definition at line 952 of file omdatabase.cc.

References docid_zero_invalid(), LOGCALL_VOID, no_subdatabases(), rare, sub_db(), and sub_docid().

Referenced by builddb_valuestest1(), DEFINE_TESTCASE(), gen_valueweightsource5_db(), main(), make_netstats1_db(), make_orcheck_db(), make_sparse_db(), RemoteServer::msg_replacedocument_(), RemoteServer::msg_replacedocumentterm(), and replace_document().

◆ replace_document() [2/2]

Xapian::docid Xapian::WritableDatabase::replace_document ( const std::string &  unique_term,
const Xapian::Document document 
)

Replace any documents matching a term.

This method replaces any documents indexed by the specified term with the specified document. If any documents are indexed by the term, the lowest document ID will be used for the document, otherwise a new document ID will be generated as for add_document.

One common use is to allow UIDs from another system to easily be mapped to terms in Xapian. Note that this method doesn't automatically add unique_term as a term, so you'll need to call document.add_term(unique_term) first when using replace_document() in this way.

Note that changes to the database won't be immediately committed to disk; see commit() for more details.

As with all database modification operations, the effect is atomic: the document(s) will either be fully replaced, or the document(s) fail to be replaced and an exception is thrown (possibly at a later time when commit() is called or the database is closed).

Parameters
unique_termThe "unique" term.
documentThe new document.
Returns
The document ID that document was given.
Exceptions
Xapian::DatabaseErrorwill be thrown if a problem occurs while writing to the database.
Xapian::DatabaseCorruptErrorwill be thrown if the database is in a corrupt state.

Definition at line 965 of file omdatabase.cc.

References add_document(), Xapian::Database::get_lastdocid(), LOGCALL, no_subdatabases(), Xapian::Database::postlist_begin(), Xapian::Database::postlist_end(), rare, replace_document(), RETURN, sub_db(), and sub_docid().

◆ set_metadata()

void Xapian::WritableDatabase::set_metadata ( const std::string &  key,
const std::string &  metadata 
)

Set the user-specified metadata associated with a given key.

This method sets the metadata value associated with a given key. If there is already a metadata value stored in the database with the same key, the old value is replaced. If you want to delete an existing item of metadata, just set its value to the empty string.

User-specified metadata allows you to store arbitrary information in the form of (key, value) pairs.

There's no hard limit on the number of metadata items, or the size of the metadata values. Metadata keys have a limited length, which depend on the backend. We recommend limiting them to 200 bytes. Empty keys are not valid, and specifying one will cause an exception.

Metadata modifications are committed to disk in the same way as modifications to the documents in the database are: i.e., modifications are atomic, and won't be committed to disk immediately (see commit() for more details). This allows metadata to be used to link databases with versioned external resources by storing the appropriate version number in a metadata item.

You can also use the metadata to store arbitrary extra information associated with terms, documents, or postings by encoding the termname and/or document id into the metadata key.

Parameters
keyThe key of the metadata item to set.
metadataThe value of the metadata item to set.
Exceptions
Xapian::DatabaseErrorwill be thrown if a problem occurs while writing to the database.
Xapian::DatabaseCorruptErrorwill be thrown if the database is in a corrupt state.
Xapian::InvalidArgumentErrorwill be thrown if the key supplied is empty.
Xapian::UnimplementedErrorwill be thrown if the database backend in use doesn't support user-specified metadata.

Definition at line 1064 of file omdatabase.cc.

References empty_metadata_key(), LOGCALL_VOID, no_subdatabases(), and rare.

Referenced by DEFINE_TESTCASE(), main(), and RemoteServer::msg_setmetadata_().


The documentation for this class was generated from the following files: