Commit 52bc00a5 authored by Hugo Beauzée-Luyssen's avatar Hugo Beauzée-Luyssen

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
parent 01fdddd0
...@@ -100,7 +100,7 @@ std::shared_ptr<Folder> Folder::create( DBConnection connection, const std::stri ...@@ -100,7 +100,7 @@ std::shared_ptr<Folder> Folder::create( DBConnection connection, const std::stri
return nullptr; return nullptr;
self->m_dbConection = connection; self->m_dbConection = connection;
self->m_deviceMountpoint = deviceFs.mountpoint(); self->m_deviceMountpoint = deviceFs.mountpoint();
self->computeFullPath(); self->m_fullPath = self->m_deviceMountpoint.get() + path;
return self; return self;
} }
...@@ -146,7 +146,7 @@ std::shared_ptr<Folder> Folder::fromPath( DBConnection conn, const std::string& ...@@ -146,7 +146,7 @@ std::shared_ptr<Folder> Folder::fromPath( DBConnection conn, const std::string&
if ( folder == nullptr ) if ( folder == nullptr )
return nullptr; return nullptr;
folder->m_deviceMountpoint = deviceFs->mountpoint(); folder->m_deviceMountpoint = deviceFs->mountpoint();
folder->computeFullPath(); folder->m_fullPath = folder->m_deviceMountpoint.get() + path;
return folder; return folder;
} }
...@@ -157,6 +157,14 @@ unsigned int Folder::id() const ...@@ -157,6 +157,14 @@ unsigned int Folder::id() const
const std::string& Folder::path() 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; return m_fullPath;
} }
...@@ -202,42 +210,18 @@ bool Folder::isPresent() const ...@@ -202,42 +210,18 @@ bool Folder::isPresent() const
return m_isPresent; return m_isPresent;
} }
void Folder::computeFullPath()
{
if ( ( *m_deviceMountpoint.crbegin() ) != '/' )
m_deviceMountpoint += '/';
m_fullPath = m_deviceMountpoint + m_path;
}
std::vector<std::shared_ptr<Folder>> Folder::fetchAll( DBConnection dbConn, unsigned int parentFolderId ) std::vector<std::shared_ptr<Folder>> Folder::fetchAll( DBConnection dbConn, unsigned int parentFolderId )
{ {
std::vector<std::shared_ptr<Folder>> res;
if ( parentFolderId == 0 ) if ( parentFolderId == 0 )
{ {
static const std::string req = "SELECT * FROM " + policy::FolderTable::Name static const std::string req = "SELECT * FROM " + policy::FolderTable::Name
+ " WHERE id_parent IS NULL AND is_blacklisted is NULL"; + " WHERE id_parent IS NULL AND is_blacklisted is NULL";
res = DatabaseHelpers::fetchAll<Folder>( dbConn, req ); return DatabaseHelpers::fetchAll<Folder>( dbConn, req );
} }
else else
{ {
static const std::string req = "SELECT * FROM " + policy::FolderTable::Name static const std::string req = "SELECT * FROM " + policy::FolderTable::Name
+ " WHERE id_parent = ? AND is_blacklisted is NULL"; + " WHERE id_parent = ? AND is_blacklisted is NULL";
res = DatabaseHelpers::fetchAll<Folder>( dbConn, req, parentFolderId ); return DatabaseHelpers::fetchAll<Folder>( dbConn, req, parentFolderId );
}
std::unordered_map<unsigned int, std::string> 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 res;
} }
...@@ -24,6 +24,7 @@ ...@@ -24,6 +24,7 @@
#include "database/DatabaseHelpers.h" #include "database/DatabaseHelpers.h"
#include "factory/IFileSystem.h" #include "factory/IFileSystem.h"
#include "utils/Cache.h"
#include <sqlite3.h> #include <sqlite3.h>
...@@ -79,8 +80,6 @@ public: ...@@ -79,8 +80,6 @@ public:
bool isPresent() const; bool isPresent() const;
private: private:
void computeFullPath();
static std::shared_ptr<factory::IFileSystem> FsFactory; static std::shared_ptr<factory::IFileSystem> FsFactory;
private: private:
...@@ -95,9 +94,9 @@ private: ...@@ -95,9 +94,9 @@ private:
unsigned int m_deviceId; unsigned int m_deviceId;
bool m_isPresent; bool m_isPresent;
std::string m_deviceMountpoint; mutable Cache<std::string> m_deviceMountpoint;
// This contains the full path, including device mountpoint. // This contains the full path, including device mountpoint.
std::string m_fullPath; mutable std::string m_fullPath;
friend struct policy::FolderTable; friend struct policy::FolderTable;
}; };
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment