added load/save for hash cache in the proper place

This commit is contained in:
mr-alice 2016-09-02 21:49:43 +02:00
parent 52ef7adfae
commit 7b566f2c55
6 changed files with 51 additions and 21 deletions

View file

@ -6,16 +6,7 @@
#include "file_sharing/hash_cache.h" #include "file_sharing/hash_cache.h"
#include "file_sharing/directory_storage.h" #include "file_sharing/directory_storage.h"
class DirectoryUpdater class LocalDirectoryUpdater: public HashStorageClient, public RsTickingThread
{
public:
DirectoryUpdater() {}
virtual ~DirectoryUpdater(){}
};
#warning: simplify, if we don't keep the remote directory updater
class LocalDirectoryUpdater: public DirectoryUpdater, public HashStorageClient, public RsTickingThread
{ {
public: public:
LocalDirectoryUpdater(HashStorage *hash_cache,LocalDirectoryStorage *lds) ; LocalDirectoryUpdater(HashStorage *hash_cache,LocalDirectoryStorage *lds) ;

View file

@ -7,3 +7,7 @@ static const uint32_t DELAY_BETWEEN_LOCAL_DIRECTORIES_TS_UPDATE = 10 ; // 10 sec
static const std::string HASH_CACHE_DURATION_SS = "HASH_CACHE_DURATION" ; // key string to store hash remembering time static const std::string HASH_CACHE_DURATION_SS = "HASH_CACHE_DURATION" ; // key string to store hash remembering time
static const std::string WATCH_FILE_DURATION_SS = "WATCH_FILES_DELAY" ; // key to store delay before re-checking for new files static const std::string WATCH_FILE_DURATION_SS = "WATCH_FILES_DELAY" ; // key to store delay before re-checking for new files
static const std::string FILE_SHARING_DIR_NAME = "file_sharing" ; // hard-coded directory name to store friend file lists, hash cache, etc.
static const std::string HASH_CACHE_FILE_NAME = "hash_cache.bin" ; // hard-coded directory name to store encrypted hash cache.
static const uint32_t MIN_INTERVAL_BETWEEN_HASH_CACHE_SAVE = 20 ; // never save hash cache more often than every 60 secs.

View file

@ -3,6 +3,7 @@
#include "pqi/authssl.h" #include "pqi/authssl.h"
#include "hash_cache.h" #include "hash_cache.h"
#include "filelist_io.h" #include "filelist_io.h"
#include "file_sharing_defaults.h"
#define HASHSTORAGE_DEBUG 1 #define HASHSTORAGE_DEBUG 1
@ -14,8 +15,12 @@ HashStorage::HashStorage(const std::string& save_file_name)
{ {
mInactivitySleepTime = DEFAULT_INACTIVITY_SLEEP_TIME; mInactivitySleepTime = DEFAULT_INACTIVITY_SLEEP_TIME;
mRunning = false ; mRunning = false ;
mLastSaveTime = 0 ;
load() ; {
RS_STACK_MUTEX(mHashMtx) ;
locked_load() ;
}
} }
void HashStorage::data_tick() void HashStorage::data_tick()
@ -48,10 +53,13 @@ void HashStorage::data_tick()
mInactivitySleepTime = MAX_INACTIVITY_SLEEP_TIME; mInactivitySleepTime = MAX_INACTIVITY_SLEEP_TIME;
std::cerr << "Stopping hashing thread." << std::endl; if(!mChanged) // otherwise it might prevent from saving the hash cache
shutdown(); {
mRunning = false ; std::cerr << "Stopping hashing thread." << std::endl;
std::cerr << "done." << std::endl; shutdown();
mRunning = false ;
std::cerr << "done." << std::endl;
}
RsServer::notify()->notifyHashingInfo(NOTIFY_HASHTYPE_FINISH, "") ; RsServer::notify()->notifyHashingInfo(NOTIFY_HASHTYPE_FINISH, "") ;
} }
@ -92,11 +100,23 @@ void HashStorage::data_tick()
info.modf_stamp = job.ts ; info.modf_stamp = job.ts ;
info.time_stamp = time(NULL); info.time_stamp = time(NULL);
info.hash = hash; info.hash = hash;
mChanged = true ;
} }
// call the client // call the client
if(!hash.isNull()) if(!hash.isNull())
job.client->hash_callback(job.client_param, job.full_path, hash, size); job.client->hash_callback(job.client_param, job.full_path, hash, size);
{
RS_STACK_MUTEX(mHashMtx) ;
if(mChanged && mLastSaveTime + MIN_INTERVAL_BETWEEN_HASH_CACHE_SAVE < time(NULL))
{
locked_save();
mLastSaveTime = time(NULL) ;
mChanged = false ;
}
}
} }
bool HashStorage::requestHash(const std::string& full_path,uint64_t size,time_t mod_time,RsFileHash& known_hash,HashStorageClient *c,uint32_t client_param) bool HashStorage::requestHash(const std::string& full_path,uint64_t size,time_t mod_time,RsFileHash& known_hash,HashStorageClient *c,uint32_t client_param)
@ -180,7 +200,7 @@ void HashStorage::clean()
#endif #endif
} }
void HashStorage::load() void HashStorage::locked_load()
{ {
uint64_t file_size ; uint64_t file_size ;
@ -249,7 +269,7 @@ void HashStorage::load()
#endif #endif
} }
void HashStorage::save() void HashStorage::locked_save()
{ {
#ifdef HASHSTORAGE_DEBUG #ifdef HASHSTORAGE_DEBUG
std::cerr << "Saving Hash Cache to file " << mFilePath << "..." << std::endl ; std::cerr << "Saving Hash Cache to file " << mFilePath << "..." << std::endl ;

View file

@ -55,8 +55,8 @@ public:
private: private:
void clean() ; void clean() ;
void save() ; void locked_save() ;
void load() ; void locked_load() ;
bool readHashStorageInfo(const unsigned char *data,uint32_t total_size,uint32_t& offset,HashStorageInfo& info) const; bool readHashStorageInfo(const unsigned char *data,uint32_t total_size,uint32_t& offset,HashStorageInfo& info) const;
bool writeHashStorageInfo(unsigned char *& data,uint32_t& total_size,uint32_t& offset,const HashStorageInfo& info) const; bool writeHashStorageInfo(unsigned char *& data,uint32_t& total_size,uint32_t& offset,const HashStorageInfo& info) const;
@ -85,5 +85,6 @@ private:
RsMutex mHashMtx ; RsMutex mHashMtx ;
bool mRunning; bool mRunning;
uint32_t mInactivitySleepTime ; uint32_t mInactivitySleepTime ;
time_t mLastSaveTime ;
}; };

View file

@ -8,6 +8,7 @@
#include "retroshare/rsids.h" #include "retroshare/rsids.h"
#include "retroshare/rspeers.h" #include "retroshare/rspeers.h"
#include "rsserver/rsaccounts.h"
#include "rsserver/p3face.h" #include "rsserver/p3face.h"
@ -26,13 +27,25 @@ static const uint32_t DELAY_BEFORE_DROP_REQUEST = 55 ; // every
p3FileDatabase::p3FileDatabase(p3ServiceControl *mpeers) p3FileDatabase::p3FileDatabase(p3ServiceControl *mpeers)
: mServCtrl(mpeers), mFLSMtx("p3FileLists") : mServCtrl(mpeers), mFLSMtx("p3FileLists")
{ {
// loads existing indexes for friends. Some might be already present here. // make sure the base directory exists
std::string base_dir = rsAccounts->PathAccountDirectory();
if(base_dir.empty())
throw std::runtime_error("Cannot create base directory to store/access file sharing files.") ;
mFileSharingDir = base_dir + "/" + FILE_SHARING_DIR_NAME ;
if(!RsDirUtil::checkCreateDirectory(mFileSharingDir))
throw std::runtime_error("Cannot create base directory to store/access file sharing files.") ;
// loads existing indexes for friends. Some might be already present here.
// //
mRemoteDirectories.clear() ; // we should load them! mRemoteDirectories.clear() ; // we should load them!
mOwnId = mpeers->getOwnId() ; mOwnId = mpeers->getOwnId() ;
mLocalSharedDirs = new LocalDirectoryStorage("local_file_store.bin",mOwnId); mLocalSharedDirs = new LocalDirectoryStorage("local_file_store.bin",mOwnId);
mHashCache = new HashStorage("hash_cache.bin") ; mHashCache = new HashStorage(mFileSharingDir + "/" + HASH_CACHE_FILE_NAME) ;
mLocalDirWatcher = new LocalDirectoryUpdater(mHashCache,mLocalSharedDirs) ; mLocalDirWatcher = new LocalDirectoryUpdater(mHashCache,mLocalSharedDirs) ;

View file

@ -195,5 +195,6 @@ class p3FileDatabase: public p3Service, public p3Config, public ftSearch //, pub
mutable RsMutex mFLSMtx ; mutable RsMutex mFLSMtx ;
uint32_t mUpdateFlags ; uint32_t mUpdateFlags ;
std::string mFileSharingDir ;
}; };