restructured needUpdate()

This commit is contained in:
Jean-Francois Dockes 2014-05-06 08:26:38 +02:00
parent 185c994fa5
commit e1b746bcfd

View File

@ -1700,9 +1700,7 @@ bool Db::needUpdate(const string &udi, const string& sig, bool *existed)
if (m_ndb == 0) if (m_ndb == 0)
return false; return false;
// If we are doing an in place or full reset, no need to // If we are doing an in place or full reset, no need to test.
// test. Note that there is no need to update the existence map
// either, it will be done when updating the index
if (o_inPlaceReset || m_mode == DbTrunc) { if (o_inPlaceReset || m_mode == DbTrunc) {
// For in place reset, pretend the doc existed, to enable subdoc purge // For in place reset, pretend the doc existed, to enable subdoc purge
if (existed) if (existed)
@ -1710,6 +1708,9 @@ bool Db::needUpdate(const string &udi, const string& sig, bool *existed)
return true; return true;
} }
if (existed)
*existed = false;
string uniterm = make_uniterm(udi); string uniterm = make_uniterm(udi);
string ermsg; string ermsg;
@ -1720,28 +1721,37 @@ bool Db::needUpdate(const string &udi, const string& sig, bool *existed)
// anyway // anyway
PTMutexLocker lock(m_ndb->m_mutex); PTMutexLocker lock(m_ndb->m_mutex);
#endif #endif
// We look up the document indexed by the uniterm. This is either
// the actual document file, or, for a multi-document file, the
// pseudo-doc we create to stand for the file itself.
// We try twice in case database needs to be reopened. // Try to find the document indexed by the uniterm.
for (int tries = 0; tries < 2; tries++) { Xapian::PostingIterator docid;
try { XAPTRY(docid = m_ndb->xrdb.postlist_begin(uniterm), m_ndb->xrdb, m_reason);
// Get the doc or pseudo-doc if (!m_reason.empty()) {
Xapian::PostingIterator docid = m_ndb->xrdb.postlist_begin(uniterm); LOGERR(("Db::needUpdate: xapian::postlist_begin failed: %s\n",
m_reason.c_str()));
return false;
}
if (docid == m_ndb->xrdb.postlist_end(uniterm)) { if (docid == m_ndb->xrdb.postlist_end(uniterm)) {
// If no document exist with this path, we do need update // No document exists with this path: we do need update
LOGDEB(("Db::needUpdate:yes (new): [%s]\n", uniterm.c_str())); LOGDEB(("Db::needUpdate:yes (new): [%s]\n", uniterm.c_str()));
if (existed)
*existed = false;
return true; return true;
} }
Xapian::Document doc = m_ndb->xrdb.get_document(*docid); Xapian::Document xdoc;
XAPTRY(xdoc = m_ndb->xrdb.get_document(*docid), m_ndb->xrdb, m_reason);
if (!m_reason.empty()) {
LOGERR(("Db::needUpdate: get_document error: %s\n", m_reason.c_str()));
return true;
}
if (existed) if (existed)
*existed = true; *existed = true;
// Retrieve old file/doc signature from value // Retrieve old file/doc signature from value
string osig = doc.get_value(VALUE_SIG); string osig;
XAPTRY(osig = xdoc.get_value(VALUE_SIG), m_ndb->xrdb, m_reason);
if (!m_reason.empty()) {
LOGERR(("Db::needUpdate: get_value error: %s\n", m_reason.c_str()));
return true;
}
LOGDEB2(("Db::needUpdate: oldsig [%s] new [%s]\n", LOGDEB2(("Db::needUpdate: oldsig [%s] new [%s]\n",
osig.c_str(), sig.c_str())); osig.c_str(), sig.c_str()));
// Compare new/old sig // Compare new/old sig
@ -1752,13 +1762,19 @@ bool Db::needUpdate(const string &udi, const string& sig, bool *existed)
return true; return true;
} }
// Up to date.
LOGDEB(("Db::needUpdate:no: [%s]\n", uniterm.c_str())); LOGDEB(("Db::needUpdate:no: [%s]\n", uniterm.c_str()));
// Up to date.
// Set the uptodate flag for doc / pseudo doc
if (m_mode != DbRO) { if (m_mode != DbRO) {
// Set the up to date flag for the document and its subdocs
if (*docid >= updated.size()) {
LOGERR(("needUpdate: existing docid beyond "
"updated.size(). Udi [%s], docid %u, "
"updated.size() %u\n", udi.c_str(),
unsigned(*docid), (unsigned)updated.size()));
} else {
updated[*docid] = true; updated[*docid] = true;
}
// Set the existence flag for all the subdocs (if any) // Set the existence flag for all the subdocs (if any)
vector<Xapian::docid> docids; vector<Xapian::docid> docids;
@ -1775,17 +1791,6 @@ bool Db::needUpdate(const string &udi, const string& sig, bool *existed)
} }
} }
return false; return false;
} catch (const Xapian::DatabaseModifiedError &e) {
LOGDEB(("Db::needUpdate: got modified error. reopen/retry\n"));
m_reason = e.get_msg();
m_ndb->xrdb.reopen();
continue;
} XCATCHERROR(m_reason);
break;
}
LOGERR(("Db::needUpdate: error while checking existence: %s\n",
m_reason.c_str()));
return true;
} }
// Return existing stem db languages // Return existing stem db languages