Skip to content

Commit 5a11a2d

Browse files
committed
Core: Integrated unified recursive statistics for directory file count, size, and uploads
1 parent cc22c1b commit 5a11a2d

7 files changed

Lines changed: 42 additions & 11 deletions

File tree

src/file_sharing/dir_hierarchy.cc

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
******************************************************************************/
2323
#include <sstream>
2424
#include <algorithm>
25+
#include <functional>
2526

2627
#include "util/rstime.h"
2728
#include "util/rsdir.h"
@@ -669,26 +670,36 @@ bool InternalFileHierarchyStorage::setTS(const DirectoryStorage::EntryIndex& ind
669670

670671
// Do a complete recursive sweep of directory hierarchy and update cumulative size of directories
671672

672-
uint64_t InternalFileHierarchyStorage::recursUpdateCumulatedSize(const DirectoryStorage::EntryIndex& dir_index)
673+
uint64_t InternalFileHierarchyStorage::recursUpdateCumulatedSize(const DirectoryStorage::EntryIndex& dir_index, std::function<uint64_t(const RsFileHash&)> get_uploads)
673674
{
674675
DirEntry& d(*static_cast<DirEntry*>(mNodes[dir_index])) ;
675676

676677
uint64_t local_cumulative_size = 0;
677678
uint32_t local_cumulative_files = 0;
679+
uint64_t local_cumulative_uploads = 0;
678680

679681
for(uint32_t i=0;i<d.subfiles.size();++i)
680682
if(mNodes[d.subfiles[i]]) { // normally not needed, but an extra-security
681-
local_cumulative_size += static_cast<FileEntry*>(mNodes[d.subfiles[i]])->file_size;
683+
FileEntry *f = static_cast<FileEntry*>(mNodes[d.subfiles[i]]);
684+
local_cumulative_size += f->file_size;
682685
local_cumulative_files++;
686+
687+
if(get_uploads)
688+
local_cumulative_uploads += get_uploads(f->file_hash);
683689
}
684690

685691
for(uint32_t i=0;i<d.subdirs.size();++i) {
686-
local_cumulative_size += recursUpdateCumulatedSize(d.subdirs[i]);
692+
local_cumulative_size += recursUpdateCumulatedSize(d.subdirs[i], get_uploads);
687693
local_cumulative_files += static_cast<DirEntry*>(mNodes[d.subdirs[i]])->dir_cumulated_files;
694+
local_cumulative_uploads += static_cast<DirEntry*>(mNodes[d.subdirs[i]])->dir_cumulated_uploads;
688695
}
689696

690697
d.dir_cumulated_size = local_cumulative_size;
691698
d.dir_cumulated_files = local_cumulative_files;
699+
d.dir_cumulated_uploads = local_cumulative_uploads;
700+
701+
702+
692703
return local_cumulative_size;
693704
}
694705
// Do a complete recursive sweep over sub-directories and files, and update the lst modf TS. This could be also performed by a cleanup method.

src/file_sharing/dir_hierarchy.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ class InternalFileHierarchyStorage
6363
class DirEntry: public FileStorageNode
6464
{
6565
public:
66-
explicit DirEntry(const std::string& name) : dir_name(name), dir_cumulated_size(0), dir_cumulated_files(0), dir_modtime(0),dir_most_recent_time(0),dir_update_time(0) {}
66+
explicit DirEntry(const std::string& name) : dir_name(name), dir_cumulated_size(0), dir_cumulated_files(0), dir_cumulated_uploads(0), dir_modtime(0),dir_most_recent_time(0),dir_update_time(0) {}
6767
virtual ~DirEntry() {}
6868

6969
virtual uint32_t type() const { return FileStorageNode::TYPE_DIR ; }
@@ -74,6 +74,7 @@ class InternalFileHierarchyStorage
7474
RsFileHash dir_hash ;
7575
uint64_t dir_cumulated_size;
7676
uint32_t dir_cumulated_files;
77+
uint64_t dir_cumulated_uploads;
7778

7879
std::vector<DirectoryStorage::EntryIndex> subdirs ;
7980
std::vector<DirectoryStorage::EntryIndex> subfiles ;
@@ -112,7 +113,7 @@ class InternalFileHierarchyStorage
112113

113114
// Do a complete recursive sweep over sub-directories and files, and update the cumulative size.
114115

115-
uint64_t recursUpdateCumulatedSize(const DirectoryStorage::EntryIndex& dir_index);
116+
uint64_t recursUpdateCumulatedSize(const DirectoryStorage::EntryIndex& dir_index, std::function<uint64_t(const RsFileHash&)> get_uploads = nullptr);
116117

117118
// hash stuff
118119

src/file_sharing/directory_storage.cc

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,7 @@ bool DirectoryStorage::extractData(const EntryIndex& indx,DirDetails& d)
249249
d.hash.clear() ;
250250
d.size = dir_entry->dir_cumulated_size;//dir_entry->subdirs.size() + dir_entry->subfiles.size();
251251
d.count = dir_entry->dir_cumulated_files;
252+
d.uploads = dir_entry->dir_cumulated_uploads;
252253
d.max_mtime = dir_entry->dir_most_recent_time ;
253254
d.mtime = dir_entry->dir_modtime ;
254255
d.name = dir_entry->dir_name;
@@ -260,6 +261,8 @@ bool DirectoryStorage::extractData(const EntryIndex& indx,DirDetails& d)
260261
d.type = DIR_TYPE_PERSON ;
261262
d.name = mPeerId.toStdString();
262263
}
264+
265+
263266
}
264267
else if(type == InternalFileHierarchyStorage::FileStorageNode::TYPE_FILE)
265268
{
@@ -299,13 +302,13 @@ bool DirectoryStorage::getIndexFromDirHash(const RsFileHash& hash,EntryIndex& in
299302
return mFileHierarchy->getIndexFromDirHash(hash,index) ;
300303
}
301304

302-
void DirectoryStorage::checkSave()
305+
void DirectoryStorage::checkSave(std::function<uint64_t(const RsFileHash&)> get_uploads)
303306
{
304307
rstime_t now = time(NULL);
305308

306309
if(mChanged && mLastSavedTime + MIN_INTERVAL_BETWEEN_REMOTE_DIRECTORY_SAVE < now)
307310
{
308-
mFileHierarchy->recursUpdateCumulatedSize(mFileHierarchy->mRoot);
311+
mFileHierarchy->recursUpdateCumulatedSize(mFileHierarchy->mRoot, get_uploads);
309312

310313
{
311314
RS_STACK_MUTEX(mDirStorageMtx) ;

src/file_sharing/directory_storage.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <string>
2525
#include <stdint.h>
2626
#include <list>
27+
#include <functional>
2728

2829
#include "retroshare/rsids.h"
2930
#include "retroshare/rsfiles.h"
@@ -150,7 +151,7 @@ class DirectoryStorage
150151
* \brief checkSave
151152
* Checks the time of last saving, last modification time, and saves if needed.
152153
*/
153-
void checkSave() ;
154+
void checkSave(std::function<uint64_t(const RsFileHash&)> get_uploads = nullptr) ;
154155

155156
const std::string& filename() const { return mFileName ; }
156157

src/file_sharing/p3filelists.cc

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -271,11 +271,11 @@ int p3FileDatabase::tick()
271271
mRemoteDirectories[i]->lastSweepTime() = now ;
272272
}
273273

274-
mRemoteDirectories[i]->checkSave() ;
274+
mRemoteDirectories[i]->checkSave([this](const RsFileHash& h){ return this->locked_getCumulativeUpload(h); }) ;
275275
}
276276

277277
mLastRemoteDirSweepTS = now;
278-
mLocalSharedDirs->checkSave() ;
278+
mLocalSharedDirs->checkSave([this](const RsFileHash& h){ return this->locked_getCumulativeUpload(h); }) ;
279279

280280
// This is a hack to make loaded directories show up in the GUI, because the GUI generally isn't ready at the time they are actually loaded up,
281281
// so the first notify is ignored, and no other notify will happen next. We only do it if no data was received in the last 5 secs, in order to
@@ -1093,6 +1093,11 @@ int p3FileDatabase::getSharedDirStatistics(const RsPeerId& pid,SharedDirStats& s
10931093
uint64_t p3FileDatabase::getCumulativeUpload(const RsFileHash& hash) const
10941094
{
10951095
RS_STACK_MUTEX(mFLSMtx);
1096+
return locked_getCumulativeUpload(hash);
1097+
}
1098+
1099+
uint64_t p3FileDatabase::locked_getCumulativeUpload(const RsFileHash& hash) const
1100+
{
10961101
auto it = mCumulativeUploaded.find(hash);
10971102
if (it != mCumulativeUploaded.end())
10981103
return it->second.total_bytes;
@@ -1439,6 +1444,13 @@ int p3FileDatabase::RequestDirDetails(
14391444
}
14401445

14411446
d.id = storage->peerId();
1447+
1448+
if (d.type == DIR_TYPE_FILE)
1449+
{
1450+
d.uploads = locked_getCumulativeUpload(d.hash);
1451+
}
1452+
1453+
14421454

14431455
#ifdef DEBUG_FILE_HIERARCHY
14441456
P3FILELISTS_DEBUG() << "ExtractData: ref=" << ref << ", flags=" << flags << " : returning this: " << std::endl;

src/file_sharing/p3filelists.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ class p3FileDatabase: public p3Service, public p3Config, public ftSearch //, pub
203203
bool hashingProcessPaused();
204204

205205
protected:
206+
uint64_t locked_getCumulativeUpload(const RsFileHash& hash) const;
206207
void getExtraFilesDirDetails_locked(void *ref,DirectoryStorage::EntryIndex e,DirDetails& d) const;
207208

208209
int filterResults(const std::list<void*>& firesults,std::list<DirDetails>& results,FileSearchFlags flags,const RsPeerId& peer_id) const;

src/retroshare/rstypes.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ struct DirStub : RsSerializable
280280
struct DirDetails : RsSerializable
281281
{
282282
DirDetails() : parent(nullptr), prow(0), ref(nullptr),
283-
type(DIR_TYPE_UNKNOWN), size(0), mtime(0), max_mtime(0), count(0) {}
283+
type(DIR_TYPE_UNKNOWN), size(0), mtime(0), max_mtime(0), count(0), uploads(0) {}
284284

285285

286286
/* G10h4ck do we still need to keep this as void* instead of uint64_t for
@@ -303,6 +303,7 @@ struct DirDetails : RsSerializable
303303
FileStorageFlags flags;
304304
uint32_t max_mtime ; // maximum modification time of the whole hierarchy below.
305305
uint32_t count; // cumulative number of files in the directory hierarchy.
306+
uint64_t uploads; // cumulative number of bytes uploaded for this directory/file.
306307

307308
std::vector<DirStub> children;
308309
std::list<RsNodeGroupId> parent_groups; // parent groups for the shared directory
@@ -333,6 +334,7 @@ struct DirDetails : RsSerializable
333334
RS_SERIAL_PROCESS(flags);
334335
RS_SERIAL_PROCESS(max_mtime);
335336
RS_SERIAL_PROCESS(count);
337+
RS_SERIAL_PROCESS(uploads);
336338
RS_SERIAL_PROCESS(children);
337339
RS_SERIAL_PROCESS(parent_groups);
338340
}

0 commit comments

Comments
 (0)