From 566a01ce7f122c2ce2691b24745e72a50cd272ee Mon Sep 17 00:00:00 2001 From: Chris Li Date: Mon, 6 Jun 2016 16:47:50 -0400 Subject: [PATCH] optional progress callback + progress calculation fix --- src/common/kiwix/indexer.cpp | 36 ++++++++++++++++++++++++------------ src/common/kiwix/indexer.h | 7 +++++-- 2 files changed, 29 insertions(+), 14 deletions(-) mode change 100644 => 100755 src/common/kiwix/indexer.cpp mode change 100644 => 100755 src/common/kiwix/indexer.h diff --git a/src/common/kiwix/indexer.cpp b/src/common/kiwix/indexer.cpp old mode 100644 new mode 100755 index 61fdadddd..53aa0236f --- a/src/common/kiwix/indexer.cpp +++ b/src/common/kiwix/indexer.cpp @@ -80,7 +80,7 @@ namespace kiwix { /* Get the number of article to index and the ZIM id */ kiwix::Reader reader(self->getZimPath()); - unsigned int articleCount = reader.getGlobalCount(); + unsigned int articleCount = reader.getArticleCount(); self->setArticleCount(articleCount); string zimId = reader.getId(); self->setZimId(zimId); @@ -101,25 +101,33 @@ namespace kiwix { zim::Article currentArticle; while (currentOffset < lastOffset) { + if (self->getVerboseFlag()) { + std::cout << "currentOffset:" << currentOffset << " lastOffset:" << lastOffset + << " readArticleCount:" << readArticleCount << " totalArticleCount:" << articleCount <getArticle(currentOffset); if (!currentArticle.isRedirect()) { - /* Add articles to the queue */ - indexerToken token; - token.title = currentArticle.getTitle(); - token.url = currentArticle.getLongUrl(); - token.content = string(currentArticle.getData().data(), currentArticle.getData().size()); - self->pushToParseQueue(token); + /* Add articles to the queue */ + indexerToken token; + token.title = currentArticle.getTitle(); + token.url = currentArticle.getLongUrl(); + token.content = string(currentArticle.getData().data(), currentArticle.getData().size()); + self->pushToParseQueue(token); + readArticleCount += 1; + if (self->progressCallback) { + self->progressCallback(readArticleCount, articleCount); + } } - - readArticleCount += 1; + currentOffset += 1; /* Update the progression counter (in percent) */ tmpProgression = (unsigned int)((float)readArticleCount/(float)articleCount*100 - 1); if (tmpProgression > currentProgression) { - currentProgression = tmpProgression; - self->setProgression(currentProgression); + currentProgression = tmpProgression; + self->setProgression(currentProgression); } /* Test if the thread should be cancelled */ @@ -414,10 +422,14 @@ namespace kiwix { } /* Manage */ - bool Indexer::start(const string zimPath, const string indexPath) { + bool Indexer::start(const string zimPath, const string indexPath, ProgressCallback callback) { if (this->getVerboseFlag()) { std::cout << "Indexing of '" << zimPath << "' starting..." <progressCallback = callback; + } this->setArticleCount(0); this->setProgression(0); diff --git a/src/common/kiwix/indexer.h b/src/common/kiwix/indexer.h old mode 100644 new mode 100755 index 0a6100020..8ba671c14 --- a/src/common/kiwix/indexer.h +++ b/src/common/kiwix/indexer.h @@ -54,12 +54,14 @@ namespace kiwix { }; class Indexer { - + + typedef void (* ProgressCallback)(const unsigned int processedArticleCount, const unsigned int totalArticleCount); + public: Indexer(); virtual ~Indexer(); - bool start(const string zimPath, const string indexPath); + bool start(const string zimPath, const string indexPath, ProgressCallback callback = NULL); bool stop(); bool isRunning(); unsigned int getProgression(); @@ -97,6 +99,7 @@ namespace kiwix { bool verboseFlag; private: + ProgressCallback progressCallback; pthread_mutex_t threadIdsMutex; /* Article extraction */