# Xapian::WritableDatabase Class Reference

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

Inheritance diagram for Xapian::WritableDatabase:

[legend]

List of all members.

## Public Member Functions

virtual | ~WritableDatabase ()
| Destroy this handle on the database.
| WritableDatabase ()
| Create an empty WritableDatabase.
| WritableDatabase (const std::string &path, int action)
| Open a database for update, automatically determining the database backend to use.
| WritableDatabase (const WritableDatabase &other)
| Copying is allowed.
void | operator= (const WritableDatabase &other)
| Assignment is allowed.
void | flush ()
| Commit any pending modifications made to the database.
void | begin_transaction (bool flushed=true)
| Begin a transaction.
void | commit_transaction ()
| Complete the transaction currently in progress.
void | cancel_transaction ()
| Abort the transaction currently in progress, discarding the pending modifications made to the database.
Xapian::docid | add_document (const Xapian::Document &document)
| Add a new document to the database.
void | delete_document (Xapian::docid did)
| Delete a document from the database.
void | delete_document (const std::string &unique_term)
| Delete any documents indexed by a term from the database.
void | replace_document (Xapian::docid did, const Xapian::Document &document)
| Replace a given document in the database.
Xapian::docid | replace_document (const std::string &unique_term, const Xapian::Document &document)
| Replace any documents matching a term.
void | add_spelling (const std::string &word, Xapian::termcount freqinc=1) const
| Add a word to the spelling dictionary.
void | remove_spelling (const std::string &word, Xapian::termcount freqdec=1) const
| Remove a word from the spelling dictionary.
void | add_synonym (const std::string &term, const std::string &synonym) const
| Add a synonym for a term.
void | remove_synonym (const std::string &term, const std::string &synonym) const
| Remove a synonym for a term.
void | clear_synonyms (const std::string &term) const
| Remove all synonyms for a term.
void | set_metadata (const std::string &key, const std::string &value)
| Set the user-specified metadata associated with a given key.
std::string | get_description () const
| Return a string describing this object.

## Detailed Description

This class provides read/write access to a database.

## Constructor & Destructor Documentation

virtual Xapian::WritableDatabase::~WritableDatabase | ( |  | ) | `[virtual]`

Destroy this handle on the database.

If there are no copies of this object remaining, the database will be closed. If there are any transactions in progress these will be aborted as if cancel_transaction had been called.

Xapian::WritableDatabase::WritableDatabase | ( |  | ) |

Create an empty WritableDatabase.

Xapian::WritableDatabase::WritableDatabase | ( | const std::string & | path,
|  | int | action |
| ) |  |  |

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:

  | path | directory that the database is stored in.

  action

  one of:
  - Xapian::DB_CREATE_OR_OPEN open for read/write; create if no db exists
  - Xapian::DB_CREATE create new database; fail if db exists
  - Xapian::DB_CREATE_OR_OVERWRITE overwrite existing db; create if none exists
  - Xapian::DB_OPEN open for read/write; fail if no db exists

Exceptions:

  | Xapian::DatabaseCorruptError | will be thrown if the database is in a corrupt state.
  | Xapian::DatabaseLockError | will be thrown if a lock couldn't be acquired on the database.

Xapian::WritableDatabase::WritableDatabase | ( | const WritableDatabase & | other | ) |

Copying is allowed.

The internals are reference counted, so copying is cheap.

## Member Function Documentation

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 flush() 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 flush() is called or the database is closed).

Parameters:

  | document | The new document to be added.

Returns:
  The document ID of the newly added document.

Exceptions:

  | Xapian::DatabaseError | will be thrown if a problem occurs while writing to the database.
  | Xapian::DatabaseCorruptError | will be thrown if the database is in a corrupt state.

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:

  | word | The word to add.
  | freqinc | How much to increase its frequency by (default 1).

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

Add a synonym for a term.

If synonym is already a synonym for term, then no action is taken.

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).

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 flush() before and after so that the modifications stand and fall without affecting modifications before or after.

The downside of these implicit calls to flush() is that small transactions can harm indexing performance in the same way that explicitly calling flush() 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 flush 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.

Exceptions:

  | Xapian::UnimplementedError | will be thrown if transactions are not available for this database type.
  | Xapian::InvalidOperationError | will be thrown if this is called at an invalid time, such as when a transaction is already in progress.

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::DatabaseError | will be thrown if a problem occurs while modifying the database.
  | Xapian::DatabaseCorruptError | will be thrown if the database is in a corrupt state.
  | Xapian::InvalidOperationError | will be thrown if a transaction is not currently in progress.
  | Xapian::UnimplementedError | will be thrown if transactions are not available for this database type.

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

Remove all synonyms for a term.

If term has no synonyms, no action is taken.

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.

Exceptions:

  | Xapian::DatabaseError | will be thrown if a problem occurs while modifying the database.
  | Xapian::DatabaseCorruptError | will be thrown if the database is in a corrupt state.
  | Xapian::InvalidOperationError | will be thrown if a transaction is not currently in progress.
  | Xapian::UnimplementedError | will be thrown if transactions are not available for this database type.

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_term | The term to remove references to.

Exceptions:

  | Xapian::DatabaseError | will be thrown if a problem occurs while writing to the database.
  | Xapian::DatabaseCorruptError | will be thrown if the database is in a corrupt state.

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 flush() 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 flush() is called or the database is closed).

Parameters:

  | did | The document ID of the document to be removed.

Exceptions:

  | Xapian::DatabaseError | will be thrown if a problem occurs while writing to the database.
  | Xapian::DatabaseCorruptError | will be thrown if the database is in a corrupt state.

void Xapian::WritableDatabase::flush | ( |  | ) |

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.

Flush may be called at any time to ensure that the modifications which have been made are written to disk: if the flush succeeds, all the preceding modifications will have been written to disk.

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.

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

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

Note that flush() 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.

Exceptions:

  | Xapian::DatabaseError | will be thrown if a problem occurs while modifying the database.
  | Xapian::DatabaseCorruptError | will be thrown if the database is in a corrupt state.

std::string Xapian::WritableDatabase::get_description | ( |  | ) | const`[virtual]`

Return a string describing this object.

Reimplemented from Xapian::Database.

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.

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:

  | word | The word to remove.
  | freqdec | How much to decrease its frequency by (default 1).

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

Remove a synonym for a term.

If synonym isn't a synonym for term, then no action is taken.

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.

Another possible use is to allow groups of documents to be marked for later deletion - for example, you could add a "deletion date" term to documents at index time and use this method to easily and efficiently delete all documents due for deletion on a particular date.

Note that changes to the database won't be immediately committed to disk; see flush() 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 flush() is called or the database is closed).

Parameters:

  | unique_term | The "unique" term.
  | document | The new document.

Returns:
  The document ID that document was given.

Exceptions:

  | Xapian::DatabaseError | will be thrown if a problem occurs while writing to the database.
  | Xapian::DatabaseCorruptError | will be thrown if the database is in a corrupt state.

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 flush() 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 flush() is called or the database is closed).

Parameters:

  | did | The document ID of the document to be replaced.
  | document | The new document.

Exceptions:

  | Xapian::DatabaseError | will be thrown if a problem occurs while writing to the database.
  | Xapian::DatabaseCorruptError | will be thrown if the database is in a corrupt state.

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

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,tag) 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 depends 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 flush() 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:

  | key | The key of the metadata item to set.
  | value | The value of the metadata item to set.

Exceptions:

  | Xapian::DatabaseError | will be thrown if a problem occurs while writing to the database.
  | Xapian::DatabaseCorruptError | will be thrown if the database is in a corrupt state.
  | Xapian::InvalidArgumentError | will be thrown if the key supplied is empty.
  | Xapian::UnimplementedError | will be thrown if the database backend in use doesn't support user-specified metadata.

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

- xapian/database.h