Skip to content

Commit 810e58b

Browse files
committed
Core: Integrated unified recursive statistics for directory file count, size, and uploads
1 parent 7f30c7f commit 810e58b

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
@@ -23,6 +23,7 @@
2323
******************************************************************************/
2424
#include <sstream>
2525
#include <algorithm>
26+
#include <functional>
2627

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

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

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

677678
uint64_t local_cumulative_size = 0;
678679
uint32_t local_cumulative_files = 0;
680+
uint64_t local_cumulative_uploads = 0;
679681

680682
for(uint32_t i=0;i<d.subfiles.size();++i)
681683
if(mNodes[d.subfiles[i]]) { // normally not needed, but an extra-security
682-
local_cumulative_size += static_cast<FileEntry*>(mNodes[d.subfiles[i]])->file_size;
684+
FileEntry *f = static_cast<FileEntry*>(mNodes[d.subfiles[i]]);
685+
local_cumulative_size += f->file_size;
683686
local_cumulative_files++;
687+
688+
if(get_uploads)
689+
local_cumulative_uploads += get_uploads(f->file_hash);
684690
}
685691

686692
for(uint32_t i=0;i<d.subdirs.size();++i) {
687-
local_cumulative_size += recursUpdateCumulatedSize(d.subdirs[i]);
693+
local_cumulative_size += recursUpdateCumulatedSize(d.subdirs[i], get_uploads);
688694
local_cumulative_files += static_cast<DirEntry*>(mNodes[d.subdirs[i]])->dir_cumulated_files;
695+
local_cumulative_uploads += static_cast<DirEntry*>(mNodes[d.subdirs[i]])->dir_cumulated_uploads;
689696
}
690697

691698
d.dir_cumulated_size = local_cumulative_size;
692699
d.dir_cumulated_files = local_cumulative_files;
700+
d.dir_cumulated_uploads = local_cumulative_uploads;
701+
702+
703+
693704
return local_cumulative_size;
694705
}
695706
// 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
@@ -250,6 +250,7 @@ bool DirectoryStorage::extractData(const EntryIndex& indx,DirDetails& d)
250250
d.hash.clear() ;
251251
d.size = dir_entry->dir_cumulated_size;//dir_entry->subdirs.size() + dir_entry->subfiles.size();
252252
d.count = dir_entry->dir_cumulated_files;
253+
d.uploads = dir_entry->dir_cumulated_uploads;
253254
d.max_mtime = dir_entry->dir_most_recent_time ;
254255
d.mtime = dir_entry->dir_modtime ;
255256
d.name = dir_entry->dir_name;
@@ -261,6 +262,8 @@ bool DirectoryStorage::extractData(const EntryIndex& indx,DirDetails& d)
261262
d.type = DIR_TYPE_PERSON ;
262263
d.name = mPeerId.toStdString();
263264
}
265+
266+
264267
}
265268
else if(type == InternalFileHierarchyStorage::FileStorageNode::TYPE_FILE)
266269
{
@@ -300,13 +303,13 @@ bool DirectoryStorage::getIndexFromDirHash(const RsFileHash& hash,EntryIndex& in
300303
return mFileHierarchy->getIndexFromDirHash(hash,index) ;
301304
}
302305

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

307310
if(mChanged && mLastSavedTime + MIN_INTERVAL_BETWEEN_REMOTE_DIRECTORY_SAVE < now)
308311
{
309-
mFileHierarchy->recursUpdateCumulatedSize(mFileHierarchy->mRoot);
312+
mFileHierarchy->recursUpdateCumulatedSize(mFileHierarchy->mRoot, get_uploads);
310313

311314
{
312315
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
@@ -272,11 +272,11 @@ int p3FileDatabase::tick()
272272
mRemoteDirectories[i]->lastSweepTime() = now ;
273273
}
274274

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

278278
mLastRemoteDirSweepTS = now;
279-
mLocalSharedDirs->checkSave() ;
279+
mLocalSharedDirs->checkSave([this](const RsFileHash& h){ return this->locked_getCumulativeUpload(h); }) ;
280280

281281
// 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,
282282
// 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
@@ -1094,6 +1094,11 @@ int p3FileDatabase::getSharedDirStatistics(const RsPeerId& pid,SharedDirStats& s
10941094
uint64_t p3FileDatabase::getCumulativeUpload(const RsFileHash& hash) const
10951095
{
10961096
RS_STACK_MUTEX(mFLSMtx);
1097+
return locked_getCumulativeUpload(hash);
1098+
}
1099+
1100+
uint64_t p3FileDatabase::locked_getCumulativeUpload(const RsFileHash& hash) const
1101+
{
10971102
auto it = mCumulativeUploaded.find(hash);
10981103
if (it != mCumulativeUploaded.end())
10991104
return it->second.total_bytes;
@@ -1440,6 +1445,13 @@ int p3FileDatabase::RequestDirDetails(
14401445
}
14411446

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

14441456
#ifdef DEBUG_FILE_HIERARCHY
14451457
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
@@ -281,7 +281,7 @@ struct DirStub : RsSerializable
281281
struct DirDetails : RsSerializable
282282
{
283283
DirDetails() : parent(nullptr), prow(0), ref(nullptr),
284-
type(DIR_TYPE_UNKNOWN), size(0), mtime(0), max_mtime(0), count(0) {}
284+
type(DIR_TYPE_UNKNOWN), size(0), mtime(0), max_mtime(0), count(0), uploads(0) {}
285285

286286

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

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

0 commit comments

Comments
 (0)