Ticket #266: minmem.patch

File minmem.patch, 6.6 KB (added by Richard Boulton, 13 years ago)

Patch implementing this (but with insufficient tests)

  • tests/api_transdb.cc

     
    126126
    127127    return true;
    128128}
     129
     130/// Test minimise_memory inside a simple transaction.
     131DEFINE_TESTCASE(minmemtransaction1, transactions) {
     132    Xapian::WritableDatabase db(get_writable_database("apitest_simpledata"));
     133
     134    Xapian::doccount docs = db.get_doccount();
     135    db.begin_transaction();
     136    Xapian::Document doc;
     137    doc.set_data("testing");
     138    doc.add_term("befuddlement");
     139    db.add_document(doc);
     140    TEST_EXCEPTION(Xapian::InvalidOperationError, db.begin_transaction());
     141    db.minimise_memory();
     142    TEST_EQUAL(db.get_doccount(), docs + 1);
     143    TEST_EQUAL(db.get_termfreq("befuddlement"), 1);
     144    db.commit_transaction();
     145    TEST_EQUAL(db.get_doccount(), docs + 1);
     146    TEST_EQUAL(db.get_termfreq("befuddlement"), 1);
     147
     148    return true;
     149}
     150
     151/// Test cancelling a simple transaction after calling minimise_memory.
     152DEFINE_TESTCASE(minmemcanceltransaction1, transactions) {
     153    Xapian::WritableDatabase db(get_writable_database("apitest_simpledata"));
     154
     155    Xapian::doccount docs = db.get_doccount();
     156    db.begin_transaction();
     157    Xapian::Document doc;
     158    doc.set_data("testing");
     159    doc.add_term("befuddlement");
     160    db.add_document(doc);
     161    TEST_EXCEPTION(Xapian::InvalidOperationError, db.begin_transaction());
     162    TEST_EQUAL(db.get_doccount(), docs + 1);
     163    TEST_EQUAL(db.get_termfreq("befuddlement"), 1);
     164    db.minimise_memory();
     165    db.cancel_transaction();
     166    TEST_EQUAL(db.get_doccount(), docs);
     167    TEST_EQUAL(db.get_termfreq("befuddlement"), 0);
     168
     169    return true;
     170}
  • include/xapian/database.h

     
    55 * Copyright 2002 Ananova Ltd
    66 * Copyright 2002,2003,2004,2005,2006,2007,2008,2009,2011 Olly Betts
    77 * Copyright 2006,2008 Lemur Consulting Ltd
     8 * Copyright 2011 Richard Boulton
    89 *
    910 * This program is free software; you can redistribute it and/or
    1011 * modify it under the terms of the GNU General Public License as
     
    565566         */
    566567        void flush() { commit(); }
    567568
     569        /** Reduce the amount of memory currently holding buffered changes.
     570         *
     571         *  This may be used to control the amount of memory used when
     572         *  indexing, while still allowing large sets of changes to be applied
     573         *  atomically.  Note that it will generally perform a similar amount
     574         *  of work to performing a commit(), so as with commit, calling
     575         *  mimimise_memory() too often will make indexing take much longer.
     576         *
     577         *  This may be called whether a transaction is currently in progress
     578         *  or not.
     579         *
     580         *  @exception Xapian::DatabaseError will be thrown if a problem occurs
     581         *             while modifying the database.
     582         *
     583         *  @exception Xapian::DatabaseCorruptError will be thrown if the
     584         *             database is in a corrupt state.
     585         */
     586        void minimise_memory();
     587
    568588        /** Begin a transaction.
    569589         *
    570590         *  In Xapian a transaction is a group of modifications to the database
  • common/database.h

     
    412412        /** Cancel pending modifications to the database. */
    413413        virtual void cancel();
    414414
     415        /** Reduce the amount of memory currently holding buffered changes.
     416         *
     417         *  See WritableDatabase::minimise_memory() for more information.
     418         */
     419        virtual void minimise_memory();
     420
    415421        /** Begin a transaction.
    416422         *
    417423         *  See WritableDatabase::begin_transaction() for more information.
  • api/omdatabase.cc

     
    785785}
    786786
    787787void
     788WritableDatabase::minimise_memory()
     789{
     790    LOGCALL_VOID(API, "WritableDatabase::minimise_memory", NO_ARGS);
     791    if (internal.size() != 1) only_one_subdatabase_allowed();
     792    internal[0]->minimise_memory();
     793}
     794
     795void
    788796WritableDatabase::begin_transaction(bool flushed)
    789797{
    790798    LOGCALL_VOID(API, "WritableDatabase::begin_transaction", NO_ARGS);
  • backends/database.cc

     
    123123}
    124124
    125125void
     126Database::Internal::minimise_memory()
     127{
     128    // Writable databases may override this method.
     129}
     130
     131void
    126132Database::Internal::begin_transaction(bool flushed)
    127133{
    128134    if (transaction_state != TRANSACTION_NONE) {
  • backends/chert/chert_database.cc

     
    16071607}
    16081608
    16091609void
     1610ChertWritableDatabase::minimise_memory()
     1611{
     1612    flush_postlist_changes();
     1613}
     1614
     1615void
    16101616ChertWritableDatabase::add_spelling(const string & word,
    16111617                                    Xapian::termcount freqinc) const
    16121618{
  • backends/chert/chert_database.h

     
    383383        /** Cancel pending modifications to the database. */
    384384        void cancel();
    385385
     386        /** Minimise buffered changes. */
     387        void minimise_memory();
     388
    386389        Xapian::docid add_document(const Xapian::Document & document);
    387390        Xapian::docid add_document_(Xapian::docid did, const Xapian::Document & document);
    388391        // Stop the default implementation of delete_document(term) and
  • backends/brass/brass_database.cc

     
    15321532}
    15331533
    15341534void
     1535BrassWritableDatabase::minimise_memory()
     1536{
     1537    flush_postlist_changes();
     1538}
     1539
     1540void
    15351541BrassWritableDatabase::add_spelling(const string & word,
    15361542                                    Xapian::termcount freqinc) const
    15371543{
  • backends/brass/brass_database.h

     
    340340        /** Cancel pending modifications to the database. */
    341341        void cancel();
    342342
     343        /** Minimise buffered changes. */
     344        void minimise_memory();
     345
    343346        Xapian::docid add_document(const Xapian::Document & document);
    344347        Xapian::docid add_document_(Xapian::docid did, const Xapian::Document & document);
    345348        // Stop the default implementation of delete_document(term) and