xapian-core  1.4.27
api_transdb.cc
Go to the documentation of this file.
1 
4 /* Copyright (C) 2006,2009,2018,2023 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  doc.add_value(42, "answer");
88  db.add_document(doc);
90  TEST_EQUAL(db.get_doccount(), docs + 1);
91  TEST_EQUAL(db.get_termfreq("befuddlement"), 1);
92  TEST_EQUAL(db.get_value_freq(42), 1);
93  TEST_EQUAL(db.get_value_lower_bound(42), "answer");
94  TEST_EQUAL(db.get_value_upper_bound(42), "answer");
95  db.cancel_transaction();
96  TEST_EQUAL(db.get_doccount(), docs);
97  TEST_EQUAL(db.get_termfreq("befuddlement"), 0);
98  TEST_EQUAL(db.get_value_freq(42), 0);
100  TEST_EQUAL(db.get_value_upper_bound(42), "");
101 }
102 
104 // transaction.
105 DEFINE_TESTCASE(canceltransaction2, transactions) {
106  Xapian::WritableDatabase db(get_writable_database("apitest_simpledata"));
107 
108  Xapian::doccount docs = db.get_doccount();
109  Xapian::Document doc0;
110  doc0.set_data("pending");
111  doc0.add_term("pending_update");
112  Xapian::docid docid = db.add_document(doc0);
113 
114  db.begin_transaction();
115  TEST_EQUAL(db.get_doccount(), docs + 1);
116  Xapian::Document doc;
117  doc.set_data("testing");
118  doc.add_term("befuddlement");
119  db.add_document(doc);
120  TEST_EQUAL(db.get_doccount(), docs + 2);
121  db.cancel_transaction();
122 
123  TEST_EQUAL(db.get_doccount(), docs + 1);
124  TEST(db.term_exists("pending_update"));
125  Xapian::Document doc_out = db.get_document(docid);
126  TEST_EQUAL(doc_out.get_data(), "pending");
127 }
128 
130 DEFINE_TESTCASE(canceltransaction3, transactions && path) {
131  {
132  Xapian::WritableDatabase db = get_named_writable_database("canceltransaction3");
133  db.begin_transaction();
134  Xapian::Document doc;
135  doc.add_term("baz");
136  db.add_document(doc);
137  db.cancel_transaction();
138  db.add_document(doc);
139  db.commit();
140  }
141 
142  size_t check_errors =
145  TEST_EQUAL(check_errors, 0);
146 }
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
void add_value(Xapian::valueno slot, const std::string &value)
Add a new value.
Definition: omdocument.cc:107
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:564
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.
std::string get_value_upper_bound(Xapian::valueno slot) const
Get an upper bound on the values stored in the given value slot.
Definition: omdatabase.cc:386
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:789
std::ostringstream tout
The debug printing stream.
Definition: testsuite.cc:104
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
std::string get_value_lower_bound(Xapian::valueno slot) const
Get a lower bound on the values stored in the given value slot.
Definition: omdatabase.cc:368
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
Xapian::doccount get_value_freq(Xapian::valueno slot) const
Return the frequency of a given value slot.
Definition: omdatabase.cc:355
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