# Xapian::WritableDatabase Class Reference

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

`#include <database.h>`

Inheritance diagram for Xapian::WritableDatabase:

[legend]
Collaboration 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 (Database::Internal *internal)
| WritableDatabase (const WritableDatabase &other)
| Copying is allowed.
void | operator= (const WritableDatabase &other)
| Assignment is allowed.
void | flush ()
| Flush to disk any modifications made to the database.
void | begin_transaction ()
| Begin a transaction.
void | commit_transaction ()
| End the transaction currently in progress, committing the modifications made to the database.
void | cancel_transaction ()
| End the transaction currently in progress, cancelling the potential 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.
std::string | get_description () const
| Introspection method.

## 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 ended.

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

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.

Note that this does not mean the document will immediately appear in the database; 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::begin_transaction | ( |  | ) |

Begin a transaction.

For the purposes of Xapian, a transaction is a group of modifications to the database which are grouped together such that either all or none of them will succeed. Even in the case of a power failure, this characteristic should be preserved (as long as the filesystem isn't corrupted, etc).

Transactions are only available with certain access methods, and as you might expect will generally have a fairly high performance cost.

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

End the transaction currently in progress, cancelling the potential 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::commit_transaction | ( |  | ) |

End the transaction currently in progress, committing the modifications made to the database.

If this completes successfully, 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.

Whatever occurs, after this method 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.

The intended use is to allow UIDs from another system to easily be mapped to terms in Xapian, although this method probably has other uses.

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 this does not mean the document will immediately disappear from the database; 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 | ( |  | ) |

Flush to disk any 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.

If called within a transaction, this will flush database modifications made before the transaction was begun, but will not flush modifications made since begin_transaction() was called.

Beware of calling flush too frequently: this will have a severe performance cost.

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.

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::DatabaseLockError | will be thrown if a lock couldn't be acquired on the database.

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

Introspection method.

Returns:
  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.

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.

The intended use is to allow UIDs from another system to easily be mapped to terms in Xapian, although this method probably has other uses.

Note that this does not mean the document(s) will immediately change in the database; 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.

Note that this does not mean the document will immediately change in the database; 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.

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

- include/xapian/database.h