| 1 | /*
 | 
|---|
| 2 | 
 | 
|---|
| 3 |   # http://trac.xapian.org/ticket/654
 | 
|---|
| 4 |   # Getting EOF when using get_data() in a remote database
 | 
|---|
| 5 | 
 | 
|---|
| 6 |   # This script triggers an exception when using in both chert and glass databases.
 | 
|---|
| 7 |   g++ -I/usr/local/include/xapian-1.3 -lxapian-1.3 the_bug.cpp -o the_bug
 | 
|---|
| 8 | 
 | 
|---|
| 9 |   # Chert:
 | 
|---|
| 10 |   unset XAPIAN_PREFER_GLASS
 | 
|---|
| 11 |   rm -rf db_the_bug
 | 
|---|
| 12 |   xapian-tcpsrv-1.3 --timeout=0 --port=8890 --writable db_the_bug &
 | 
|---|
| 13 |   ./the_bug
 | 
|---|
| 14 | 
 | 
|---|
| 15 |   # Glass:
 | 
|---|
| 16 |   export XAPIAN_PREFER_GLASS=1
 | 
|---|
| 17 |   rm -rf db_the_bug
 | 
|---|
| 18 |   xapian-tcpsrv-1.3 --timeout=0 --port=8890 --writable db_the_bug &
 | 
|---|
| 19 |   ./the_bug
 | 
|---|
| 20 | 
 | 
|---|
| 21 | */
 | 
|---|
| 22 | 
 | 
|---|
| 23 | #include <xapian.h>
 | 
|---|
| 24 | #include <stdio.h>
 | 
|---|
| 25 | #include <string>
 | 
|---|
| 26 | #include <string.h>
 | 
|---|
| 27 | 
 | 
|---|
| 28 | 
 | 
|---|
| 29 | #define ITEMS 10
 | 
|---|
| 30 | #define DATABASE "db_the_bug"
 | 
|---|
| 31 | 
 | 
|---|
| 32 | 
 | 
|---|
| 33 | int process_all(const Xapian::Database &db, int offset, int limit, bool datas, bool values) {
 | 
|---|
| 34 |         Xapian::Enquire enq(db);
 | 
|---|
| 35 |         enq.set_query(Xapian::Query(""));
 | 
|---|
| 36 |         Xapian::MSet m = enq.get_mset(offset, limit);
 | 
|---|
| 37 |         Xapian::MSetIterator mi(m.begin());
 | 
|---|
| 38 |         int i = 1;
 | 
|---|
| 39 |         for ( ; mi != m.end(); mi++, i++) {
 | 
|---|
| 40 |                 Xapian::docid docid = *mi;
 | 
|---|
| 41 |                 Xapian::Document document = mi.get_document();
 | 
|---|
| 42 |                 std::string data, value;
 | 
|---|
| 43 |                 if (datas) {
 | 
|---|
| 44 |                         data = document.get_data();
 | 
|---|
| 45 |                 }
 | 
|---|
| 46 |                 if (values) {
 | 
|---|
| 47 |                         value = document.get_value(0);
 | 
|---|
| 48 |                 }
 | 
|---|
| 49 |                 printf("docid: %u, value: %s, data: %s\n", docid, value.c_str(), data.c_str());
 | 
|---|
| 50 |         }
 | 
|---|
| 51 |         return i;
 | 
|---|
| 52 | }
 | 
|---|
| 53 | 
 | 
|---|
| 54 | 
 | 
|---|
| 55 | void index(Xapian::WritableDatabase &wdb, Xapian::doccount next_id) {
 | 
|---|
| 56 |         Xapian::Document doc;
 | 
|---|
| 57 |         std::string id = std::to_string(next_id);
 | 
|---|
| 58 |         std::string doc_id = "Q" + id;
 | 
|---|
| 59 |         doc.add_value(0, id);
 | 
|---|
| 60 |         doc.set_data(doc_id);
 | 
|---|
| 61 |         doc.add_boolean_term(doc_id);
 | 
|---|
| 62 |         wdb.replace_document(doc_id, doc);
 | 
|---|
| 63 |         printf("indexed: %s\n", doc_id.c_str());
 | 
|---|
| 64 | }
 | 
|---|
| 65 | 
 | 
|---|
| 66 | 
 | 
|---|
| 67 | void run() {
 | 
|---|
| 68 |         Xapian::WritableDatabase wdb = Xapian::Remote::open_writable("localhost", 8890, 0);
 | 
|---|
| 69 |         Xapian::Database db = Xapian::Remote::open("localhost", 8890, 0);
 | 
|---|
| 70 | 
 | 
|---|
| 71 |         Xapian::doccount free_id = db.get_doccount();
 | 
|---|
| 72 | 
 | 
|---|
| 73 |         int offset = free_id > (ITEMS * 2) ? free_id - (ITEMS * 2) : 0;
 | 
|---|
| 74 |         int limit = offset + (ITEMS * 2);
 | 
|---|
| 75 | 
 | 
|---|
| 76 |         /* If the next line is commented, the exception isn't thrown; a read is needed before the reopen() */
 | 
|---|
| 77 |         process_all(db, offset, limit, true, false);
 | 
|---|
| 78 | 
 | 
|---|
| 79 |         for (Xapian::doccount next_id = free_id; next_id <= free_id + ITEMS; next_id++) {
 | 
|---|
| 80 |                 index(wdb, next_id);
 | 
|---|
| 81 |         }
 | 
|---|
| 82 |         wdb.commit();
 | 
|---|
| 83 | 
 | 
|---|
| 84 |         db.reopen();
 | 
|---|
| 85 | 
 | 
|---|
| 86 |         /* The next line will throw the exception on get_value()! */
 | 
|---|
| 87 |         process_all(db, offset, limit, true, false);
 | 
|---|
| 88 | }
 | 
|---|
| 89 | 
 | 
|---|
| 90 | int main() {
 | 
|---|
| 91 |         for (int i = 0; i < 100; i++) {
 | 
|---|
| 92 |                 run();
 | 
|---|
| 93 |         }
 | 
|---|
| 94 | }
 | 
|---|