Files extra list optimization and cleanup

Improve performances and fix compiler warnings
This commit is contained in:
Gioacchino Mazzurco 2021-06-14 11:53:27 +02:00
parent d1a166df55
commit 476180dc14
No known key found for this signature in database
GPG Key ID: A1FBCA3872E87051
2 changed files with 42 additions and 68 deletions

View File

@ -4,7 +4,8 @@
* libretroshare: retroshare core library * * libretroshare: retroshare core library *
* * * *
* Copyright (C) 2008 Robert Fernie <retroshare@lunamutt.com> * * Copyright (C) 2008 Robert Fernie <retroshare@lunamutt.com> *
* Copyright (C) 2018-2020 Gioacchino Mazzurco <gio@eigenlab.org> * * Copyright (C) 2018-2021 Gioacchino Mazzurco <gio@eigenlab.org> *
* Copyright (C) 2021 Asociación Civil Altermundi <info@altermundi.net> *
* * * *
* This program is free software: you can redistribute it and/or modify * * This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as * * it under the terms of the GNU Lesser General Public License as *
@ -42,49 +43,34 @@
* #define DEBUG_ELIST 1 * #define DEBUG_ELIST 1
*****/ *****/
ftExtraList::ftExtraList() ftExtraList::ftExtraList(): p3Config(), extMutex("p3Config"), mNextCleanupTS(0)
:p3Config(), extMutex("p3Config") {}
{
cleanup = 0;
return;
}
void ftExtraList::threadTick() void ftExtraList::threadTick()
{ {
bool todo = false; bool haveFilesToHash = false;
rstime_t now = time(NULL); rstime_t now = time(nullptr);
{ {
RsStackMutex stack(extMutex); RS_STACK_MUTEX(extMutex);
haveFilesToHash = !mToHash.empty();
}
todo = (mToHash.size() > 0); if (haveFilesToHash)
} {
hashAFile();
std::this_thread::sleep_for(std::chrono::microseconds(10));
}
else
{
if (mNextCleanupTS < now)
{
cleanupOldFiles();
mNextCleanupTS = now + CLEANUP_PERIOD;
}
if (todo) std::this_thread::sleep_for(std::chrono::seconds(1));
{ }
/* Hash a file */
hashAFile();
/* microsleep */
rstime::rs_usleep(10);
}
else
{
/* cleanup */
if (cleanup < now)
{
cleanupOldFiles();
cleanup = now + CLEANUP_PERIOD;
}
/* sleep */
#ifdef WIN32
Sleep(1000);
#else
sleep(1);
#endif
}
} }
@ -98,15 +84,11 @@ void ftExtraList::hashAFile()
/* extract entry from the queue */ /* extract entry from the queue */
FileDetails details; FileDetails details;
{ {
RS_STACK_MUTEX(extMutex); RS_STACK_MUTEX(extMutex);
if (mToHash.empty()) return;
if (mToHash.empty())
return;
details = mToHash.front(); details = mToHash.front();
mToHash.pop_front(); mToHash.pop();
} }
#ifdef DEBUG_ELIST #ifdef DEBUG_ELIST
@ -114,26 +96,24 @@ void ftExtraList::hashAFile()
std::cerr << std::endl; std::cerr << std::endl;
#endif #endif
/* hash it! */ if(RsDirUtil::hashFile(
std::string name, hash; details.info.path, details.info.fname, details.info.hash,
//uint64_t size; details.info.size ))
if (RsDirUtil::hashFile(details.info.path, details.info.fname, details.info.hash, details.info.size))
{ {
RS_STACK_MUTEX(extMutex); RS_STACK_MUTEX(extMutex);
/* stick it in the available queue */ /* stick it in the available queue */
mFiles[details.info.hash] = details; mFiles[details.info.hash] = details;
mHashOfHash[makeEncryptedHash(details.info.hash)] = details.info.hash ; mHashOfHash[makeEncryptedHash(details.info.hash)] = details.info.hash;
/* add to the path->hash map */ /* add to the path->hash map */
mHashedList[details.info.path] = details.info.hash; mHashedList[details.info.path] = details.info.hash;
IndicateConfigChanged(); IndicateConfigChanged();
auto ev = std::make_shared<RsSharedDirectoriesEvent>(); auto ev = std::make_shared<RsSharedDirectoriesEvent>();
ev->mEventCode = RsSharedDirectoriesEventCode::EXTRA_LIST_FILE_ADDED; ev->mEventCode = RsSharedDirectoriesEventCode::EXTRA_LIST_FILE_ADDED;
if(rsEvents) rsEvents->postEvent(ev);
rsEvents->postEvent(ev);
} }
} }
@ -304,7 +284,7 @@ bool ftExtraList::hashExtraFile(
{ {
RS_STACK_MUTEX(extMutex); RS_STACK_MUTEX(extMutex);
mToHash.push_back(details); /* add into queue */ mToHash.push(details); /* add into queue */
} }
return true; return true;
@ -420,8 +400,6 @@ RsSerialiser *ftExtraList::setupSerialiser()
bool ftExtraList::saveList(bool &cleanup, std::list<RsItem *>& sList) bool ftExtraList::saveList(bool &cleanup, std::list<RsItem *>& sList)
{ {
cleanup = true; cleanup = true;
/* called after each item is added */ /* called after each item is added */

View File

@ -3,7 +3,9 @@
* * * *
* libretroshare: retroshare core library * * libretroshare: retroshare core library *
* * * *
* Copyright 2008 by Robert Fernie <retroshare@lunamutt.com> * * Copyright (C) 2008 Robert Fernie <retroshare@lunamutt.com> *
* Copyright (C) 2021 Gioacchino Mazzurco <gio@eigenlab.org> *
* Copyright (C) 2021 Asociación Civil Altermundi <info@altermundi.net> *
* * * *
* This program is free software: you can redistribute it and/or modify * * This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as * * it under the terms of the GNU Lesser General Public License as *
@ -20,8 +22,7 @@
* * * *
*******************************************************************************/ *******************************************************************************/
#ifndef FT_FILE_EXTRA_LIST_HEADER #pragma once
#define FT_FILE_EXTRA_LIST_HEADER
/* /*
* ftFileExtraList * ftFileExtraList
@ -50,7 +51,7 @@
* *
*/ */
#include <list> #include <queue>
#include <map> #include <map>
#include <string> #include <string>
@ -159,7 +160,7 @@ public:
protected: protected:
virtual RsSerialiser *setupSerialiser(); virtual RsSerialiser *setupSerialiser();
virtual bool saveList(bool &cleanup, std::list<RsItem*>&); virtual bool saveList(bool& cleanup, std::list<RsItem*>&);
virtual bool loadList(std::list<RsItem *>& load); virtual bool loadList(std::list<RsItem *>& load);
static RsFileHash makeEncryptedHash(const RsFileHash& hash); static RsFileHash makeEncryptedHash(const RsFileHash& hash);
@ -172,16 +173,11 @@ private:
mutable RsMutex extMutex; mutable RsMutex extMutex;
std::list<FileDetails> mToHash; std::queue<FileDetails> mToHash;
std::map<std::string, RsFileHash> mHashedList; /* path -> hash ( not saved ) */ std::map<std::string, RsFileHash> mHashedList; /* path -> hash ( not saved ) */
std::map<RsFileHash, FileDetails> mFiles; std::map<RsFileHash, FileDetails> mFiles;
std::map<RsFileHash, RsFileHash> mHashOfHash; /* sha1(hash) map so as to answer requests to encrypted transfers */ std::map<RsFileHash, RsFileHash> mHashOfHash; /* sha1(hash) map so as to answer requests to encrypted transfers */
rstime_t cleanup ; rstime_t mNextCleanupTS;
}; };
#endif