xapian-core  1.4.21
api_transdb.cc
Go to the documentation of this file.
1 
4 /* Copyright (C) 2006,2009,2018 Olly Betts
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #include <config.h>
22 
23 #include "api_transdb.h"
24 
25 #include <xapian.h>
26 
27 #include "apitest.h"
28 #include "testutils.h"
29 
30 using namespace std;
31 
33 DEFINE_TESTCASE(badtransaction1, transactions) {
34  Xapian::WritableDatabase db(get_writable_database("apitest_simpledata"));
35 
38 
39  db.begin_transaction();
41  db.commit_transaction();
42 
45 
46  db.begin_transaction();
48  db.cancel_transaction();
49 
52 
53  db.begin_transaction();
54  db.commit_transaction();
55 
56  db.begin_transaction();
57  db.cancel_transaction();
58 }
59 
61 DEFINE_TESTCASE(committransaction1, transactions) {
62  Xapian::WritableDatabase db(get_writable_database("apitest_simpledata"));
63 
64  Xapian::doccount docs = db.get_doccount();
65  db.begin_transaction();
66  Xapian::Document doc;
67  doc.set_data("testing");
68  doc.add_term("befuddlement");
69  db.add_document(doc);
71  TEST_EQUAL(db.get_doccount(), docs + 1);
72  TEST_EQUAL(db.get_termfreq("befuddlement"), 1);
73  db.commit_transaction();
74  TEST_EQUAL(db.get_doccount(), docs + 1);
75  TEST_EQUAL(db.get_termfreq("befuddlement"), 1);
76 }
77 
79 DEFINE_TESTCASE(canceltransaction1, transactions) {
80  Xapian::WritableDatabase db(get_writable_database("apitest_simpledata"));
81 
82  Xapian::doccount docs = db.get_doccount();
83  db.begin_transaction();
84  Xapian::Document doc;
85  doc.set_data("testing");
86  doc.add_term("befuddlement");
87  db.add_document(doc);
89  TEST_EQUAL(db.get_doccount(), docs + 1);
90  TEST_EQUAL(db.get_termfreq("befuddlement"), 1);
91  db.cancel_transaction();
92  TEST_EQUAL(db.get_doccount(), docs);
93  TEST_EQUAL(db.get_termfreq("befuddlement"), 0);
94 }
95 
97 // transaction.
98 DEFINE_TESTCASE(canceltransaction2, transactions) {
99  Xapian::WritableDatabase db(get_writable_database("apitest_simpledata"));
100 
101  Xapian::doccount docs = db.get_doccount();
102  Xapian::Document doc0;
103  doc0.set_data("pending");
104  doc0.add_term("pending_update");
105  Xapian::docid docid = db.add_document(doc0);
106 
107  db.begin_transaction();
108  TEST_EQUAL(db.get_doccount(), docs + 1);
109  Xapian::Document doc;
110  doc.set_data("testing");
111  doc.add_term("befuddlement");
112  db.add_document(doc);
113  TEST_EQUAL(db.get_doccount(), docs + 2);
114  db.cancel_transaction();
115 
116  TEST_EQUAL(db.get_doccount(), docs + 1);
117  TEST(db.term_exists("pending_update"));
118  Xapian::Document doc_out = db.get_document(docid);
119  TEST_EQUAL(doc_out.get_data(), "pending");
120 }
121 
123 DEFINE_TESTCASE(canceltransaction3, transactions && path) {
124  {
125  Xapian::WritableDatabase db = get_named_writable_database("canceltransaction3");
126  db.begin_transaction();
127  Xapian::Document doc;
128  doc.add_term("baz");
129  db.add_document(doc);
130  db.cancel_transaction();
131  db.add_document(doc);
132  db.commit();
133  }
134 
135  size_t check_errors =
138  TEST_EQUAL(check_errors, 0);
139 }
Xapian::Document get_document(Xapian::docid did) const
Get a document from the database, given its document id.
Definition: omdatabase.cc:490
Xapian::docid add_document(const Xapian::Document &document)
Add a new document to the database.
Definition: omdatabase.cc:902
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
void cancel_transaction()
Abort the transaction currently in progress, discarding the pending modifications made to the databas...
Definition: omdatabase.cc:890
#define TEST(a)
Test a condition, without an additional explanation for failure.
Definition: testsuite.h:275
DEFINE_TESTCASE(badtransaction1, transactions)
Test incorrect uses of the transaction API lead to errors.
Definition: api_transdb.cc:33
InvalidOperationError indicates the API was used in an invalid way.
Definition: error.h:283
Xapian::WritableDatabase get_writable_database(const string &dbname)
Definition: apitest.cc:87
void begin_transaction(bool flushed=true)
Begin a transaction.
Definition: omdatabase.cc:868
STL namespace.
Xapian::doccount get_doccount() const
Get the number of documents in the database.
Definition: omdatabase.cc:267
Xapian::WritableDatabase get_named_writable_database(const std::string &name, const std::string &source)
Definition: apitest.cc:93
test functionality of the Xapian API
std::string get_named_writable_database_path(const std::string &name)
Definition: apitest.cc:99
This class provides read/write access to a database.
Definition: database.h:785
std::ostringstream tout
The debug printing stream.
Definition: testsuite.cc:103
Public interfaces for the Xapian library.
#define TEST_EXCEPTION(TYPE, CODE)
Check that CODE throws exactly Xapian exception TYPE.
Definition: testutils.h:109
void commit()
Commit any pending modifications made to the database.
Definition: omdatabase.cc:857
bool term_exists(const std::string &tname) const
Check if a given term exists in the database.
Definition: omdatabase.cc:524
unsigned XAPIAN_DOCID_BASE_TYPE doccount
A count of documents.
Definition: types.h:38
Xapian-specific test helper functions and macros.
void commit_transaction()
Complete the transaction currently in progress.
Definition: omdatabase.cc:879
unsigned XAPIAN_DOCID_BASE_TYPE docid
A unique identifier for a document.
Definition: types.h:52
#define TEST_EQUAL(a, b)
Test for equality of two things.
Definition: testsuite.h:278
void set_data(const std::string &data)
Set data stored in the document.
Definition: omdocument.cc:78
Xapian::doccount get_termfreq(const std::string &tname) const
Get the number of documents in the database indexed by a given term.
Definition: omdatabase.cc:323
A handle representing a document in a Xapian database.
Definition: document.h:61
void add_term(const std::string &tname, Xapian::termcount wdfinc=1)
Add a term to the document, without positional information.
Definition: omdocument.cc:140
const int DBCHECK_SHOW_STATS
Show statistics for the B-tree.
Definition: constants.h:228