From 52bc00a5ade94268037d209d673ec03e0349d49b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20Beauz=C3=A9e-Luyssen?= Date: Mon, 28 Dec 2015 16:08:46 +0100 Subject: [PATCH] Folder: Simplify full path reconstruction Relying on the fact that we only use a specific set of getters is not future proof at all. Also, this is using lazy loading instead of a fairly heavy overhead each time we fetch all the folders --- src/Folder.cpp | 40 ++++++++++++---------------------------- src/Folder.h | 7 +++---- 2 files changed, 15 insertions(+), 32 deletions(-) diff --git a/src/Folder.cpp b/src/Folder.cpp index 859f4336..7b311faa 100644 --- a/src/Folder.cpp +++ b/src/Folder.cpp @@ -100,7 +100,7 @@ std::shared_ptr Folder::create( DBConnection connection, const std::stri return nullptr; self->m_dbConection = connection; self->m_deviceMountpoint = deviceFs.mountpoint(); - self->computeFullPath(); + self->m_fullPath = self->m_deviceMountpoint.get() + path; return self; } @@ -146,7 +146,7 @@ std::shared_ptr Folder::fromPath( DBConnection conn, const std::string& if ( folder == nullptr ) return nullptr; folder->m_deviceMountpoint = deviceFs->mountpoint(); - folder->computeFullPath(); + folder->m_fullPath = folder->m_deviceMountpoint.get() + path; return folder; } @@ -157,6 +157,14 @@ unsigned int Folder::id() const const std::string& Folder::path() const { + auto lock = m_deviceMountpoint.lock(); + if ( m_deviceMountpoint.isCached() == true ) + return m_fullPath; + + auto device = Device::fetch( m_dbConection, m_deviceId ); + auto deviceFs = FsFactory->createDevice( device->uuid() ); + m_deviceMountpoint = deviceFs->mountpoint(); + m_fullPath = m_deviceMountpoint.get() + m_path; return m_fullPath; } @@ -202,42 +210,18 @@ bool Folder::isPresent() const return m_isPresent; } -void Folder::computeFullPath() -{ - if ( ( *m_deviceMountpoint.crbegin() ) != '/' ) - m_deviceMountpoint += '/'; - m_fullPath = m_deviceMountpoint + m_path; -} - std::vector> Folder::fetchAll( DBConnection dbConn, unsigned int parentFolderId ) { - std::vector> res; if ( parentFolderId == 0 ) { static const std::string req = "SELECT * FROM " + policy::FolderTable::Name + " WHERE id_parent IS NULL AND is_blacklisted is NULL"; - res = DatabaseHelpers::fetchAll( dbConn, req ); + return DatabaseHelpers::fetchAll( dbConn, req ); } else { static const std::string req = "SELECT * FROM " + policy::FolderTable::Name + " WHERE id_parent = ? AND is_blacklisted is NULL"; - res = DatabaseHelpers::fetchAll( dbConn, req, parentFolderId ); - } - std::unordered_map deviceMountpoints; - for ( auto& f : res ) - { - auto it = deviceMountpoints.find( f->deviceId() ); - if ( it == end( deviceMountpoints ) ) - { - auto device = Device::fetch( dbConn, f->deviceId() ); - auto deviceFs = FsFactory->createDevice( device->uuid() ); - deviceMountpoints[f->deviceId()] = deviceFs->mountpoint(); - f->m_deviceMountpoint = deviceFs->mountpoint(); - } - else - f->m_deviceMountpoint = it->second; - f->computeFullPath(); + return DatabaseHelpers::fetchAll( dbConn, req, parentFolderId ); } - return res; } diff --git a/src/Folder.h b/src/Folder.h index 44ca3400..13156bc7 100644 --- a/src/Folder.h +++ b/src/Folder.h @@ -24,6 +24,7 @@ #include "database/DatabaseHelpers.h" #include "factory/IFileSystem.h" +#include "utils/Cache.h" #include @@ -79,8 +80,6 @@ public: bool isPresent() const; private: - void computeFullPath(); - static std::shared_ptr FsFactory; private: @@ -95,9 +94,9 @@ private: unsigned int m_deviceId; bool m_isPresent; - std::string m_deviceMountpoint; + mutable Cache m_deviceMountpoint; // This contains the full path, including device mountpoint. - std::string m_fullPath; + mutable std::string m_fullPath; friend struct policy::FolderTable; }; -- 2.22.0