optional progress callback + progress calculation fix

This commit is contained in:
Chris Li 2016-06-06 16:47:50 -04:00 committed by kelson42
parent 10019e3291
commit 566a01ce7f
2 changed files with 29 additions and 14 deletions

36
src/common/kiwix/indexer.cpp Normal file → Executable file
View File

@ -80,7 +80,7 @@ namespace kiwix {
/* Get the number of article to index and the ZIM id */ /* Get the number of article to index and the ZIM id */
kiwix::Reader reader(self->getZimPath()); kiwix::Reader reader(self->getZimPath());
unsigned int articleCount = reader.getGlobalCount(); unsigned int articleCount = reader.getArticleCount();
self->setArticleCount(articleCount); self->setArticleCount(articleCount);
string zimId = reader.getId(); string zimId = reader.getId();
self->setZimId(zimId); self->setZimId(zimId);
@ -101,25 +101,33 @@ namespace kiwix {
zim::Article currentArticle; zim::Article currentArticle;
while (currentOffset < lastOffset) { while (currentOffset < lastOffset) {
if (self->getVerboseFlag()) {
std::cout << "currentOffset:" << currentOffset << " lastOffset:" << lastOffset
<< " readArticleCount:" << readArticleCount << " totalArticleCount:" << articleCount <<std::endl;
}
currentArticle = zimHandler->getArticle(currentOffset); currentArticle = zimHandler->getArticle(currentOffset);
if (!currentArticle.isRedirect()) { if (!currentArticle.isRedirect()) {
/* Add articles to the queue */ /* Add articles to the queue */
indexerToken token; indexerToken token;
token.title = currentArticle.getTitle(); token.title = currentArticle.getTitle();
token.url = currentArticle.getLongUrl(); token.url = currentArticle.getLongUrl();
token.content = string(currentArticle.getData().data(), currentArticle.getData().size()); token.content = string(currentArticle.getData().data(), currentArticle.getData().size());
self->pushToParseQueue(token); self->pushToParseQueue(token);
readArticleCount += 1;
if (self->progressCallback) {
self->progressCallback(readArticleCount, articleCount);
}
} }
readArticleCount += 1;
currentOffset += 1; currentOffset += 1;
/* Update the progression counter (in percent) */ /* Update the progression counter (in percent) */
tmpProgression = (unsigned int)((float)readArticleCount/(float)articleCount*100 - 1); tmpProgression = (unsigned int)((float)readArticleCount/(float)articleCount*100 - 1);
if (tmpProgression > currentProgression) { if (tmpProgression > currentProgression) {
currentProgression = tmpProgression; currentProgression = tmpProgression;
self->setProgression(currentProgression); self->setProgression(currentProgression);
} }
/* Test if the thread should be cancelled */ /* Test if the thread should be cancelled */
@ -414,10 +422,14 @@ namespace kiwix {
} }
/* Manage */ /* Manage */
bool Indexer::start(const string zimPath, const string indexPath) { bool Indexer::start(const string zimPath, const string indexPath, ProgressCallback callback) {
if (this->getVerboseFlag()) { if (this->getVerboseFlag()) {
std::cout << "Indexing of '" << zimPath << "' starting..." <<std::endl; std::cout << "Indexing of '" << zimPath << "' starting..." <<std::endl;
} }
if (callback) {
this->progressCallback = callback;
}
this->setArticleCount(0); this->setArticleCount(0);
this->setProgression(0); this->setProgression(0);

7
src/common/kiwix/indexer.h Normal file → Executable file
View File

@ -54,12 +54,14 @@ namespace kiwix {
}; };
class Indexer { class Indexer {
typedef void (* ProgressCallback)(const unsigned int processedArticleCount, const unsigned int totalArticleCount);
public: public:
Indexer(); Indexer();
virtual ~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 stop();
bool isRunning(); bool isRunning();
unsigned int getProgression(); unsigned int getProgression();
@ -97,6 +99,7 @@ namespace kiwix {
bool verboseFlag; bool verboseFlag;
private: private:
ProgressCallback progressCallback;
pthread_mutex_t threadIdsMutex; pthread_mutex_t threadIdsMutex;
/* Article extraction */ /* Article extraction */