diff --git a/src/navigate/entrypath.cpp b/src/navigate/entrypath.cpp index 9c473a7..ca69d32 100644 --- a/src/navigate/entrypath.cpp +++ b/src/navigate/entrypath.cpp @@ -115,7 +115,7 @@ namespace din { } uint16_t EntryPath::level() const { - return static_cast(m_stack.size()); + return (m_stack.empty() ? static_cast(0) : static_cast(m_stack.size() - 1)); } const std::string& EntryPath::operator[] (std::size_t parIndex) const { @@ -146,4 +146,8 @@ namespace din { boost::copy(boost::make_iterator_range(m_stack.begin() + 1, m_stack.end()), infix_ostream_iterator(oss, "/")); return oss.str(); } + + bool EntryPath::points_to_group(void) const { + return m_stack.empty(); + } } //namespace din diff --git a/src/navigate/entrypath.hpp b/src/navigate/entrypath.hpp index 0e6aaf5..1011330 100644 --- a/src/navigate/entrypath.hpp +++ b/src/navigate/entrypath.hpp @@ -32,6 +32,7 @@ namespace din { uint16_t level ( void ) const; const std::string& operator[] ( std::size_t parIndex ) const; + bool points_to_group ( void ) const; uint32_t group_id ( void ) const; const std::string& group_id_as_string ( void ) const; std::string file_path ( void ) const; diff --git a/src/navigate/listdircontent.cpp b/src/navigate/listdircontent.cpp index 0b6a23c..70dec90 100644 --- a/src/navigate/listdircontent.cpp +++ b/src/navigate/listdircontent.cpp @@ -84,7 +84,7 @@ namespace din { } //Requested item is not cached, so we need to query the db now - if ("/" == curr_path) { + if (parDir.points_to_group()) { auto sets_ids = m_db->sets(); auto sets_info = m_db->set_details(sets_ids); m_cache.push_back(std::make_pair(curr_path, db_result_to_vec(sets_info))); @@ -92,12 +92,10 @@ namespace din { else { auto path_prefix = parDir.file_path(); const auto set_id = parDir.group_id(); - auto files_info = m_db->file_details(set_id, parDir.level(), path_prefix); + auto files_info = m_db->file_details(set_id, parDir.level() + 1, path_prefix); m_cache.push_back(std::make_pair(curr_path, db_result_to_vec(files_info))); } - assert(not m_cache.empty()); - assert(m_cache.back().first == curr_path); - return m_cache.back().second; + return last_cached_item(curr_path); } auto ListDirContent::ls ( EntryPath parDir, const std::string& parStartWith ) const -> const ListType& { @@ -110,18 +108,24 @@ namespace din { return *cached_item; } - if ("/" == curr_path) { + if (parDir.points_to_group()) { assert(false); //not implemented } else { - assert(parDir.level() > 0); const auto set_id = parDir.group_id(); const auto path_prefix = parDir.file_path(); - auto file_list = m_db->paths_starting_by(set_id, parDir.level() - 1, path_prefix); + auto file_list = m_db->paths_starting_by(set_id, parDir.level(), path_prefix); m_cache.push_back(std::make_pair(curr_path, file_list)); } - assert(not m_cache.empty()); - assert(m_cache.back().first == curr_path); - return m_cache.back().second; + return last_cached_item(curr_path); } + + auto ListDirContent::last_cached_item (const std::string& parCurrPath) const -> const ListType& { + assert(not m_cache.empty()); + assert(m_cache.back().first == parCurrPath); +#ifdef NDEBUG + (void)parCurrPath; +#endif + return m_cache.back().second; + }; } //namespace din diff --git a/src/navigate/listdircontent.hpp b/src/navigate/listdircontent.hpp index 5cdaf4f..9a02e40 100644 --- a/src/navigate/listdircontent.hpp +++ b/src/navigate/listdircontent.hpp @@ -38,6 +38,8 @@ namespace din { const ListType& ls ( EntryPath parDir, const std::string& parStartWith ) const; private: + const ListType& last_cached_item ( const std::string& parCurrPath ) const; + mutable boost::circular_buffer m_cache; DBSource* m_db; };