kodi
DirectoryCache.h
1 /*
2  * Copyright (C) 2005-2018 Team Kodi
3  * This file is part of Kodi - https://kodi.tv
4  *
5  * SPDX-License-Identifier: GPL-2.0-or-later
6  * See LICENSES/README.md for more information.
7  */
8 
9 #pragma once
10 
11 #include "IDirectory.h"
12 #include "threads/CriticalSection.h"
13 
14 #include <map>
15 #include <memory>
16 #include <set>
17 
18 class CFileItem;
19 
20 namespace XFILE
21 {
23  {
24  class CDir
25  {
26  public:
27  explicit CDir(DIR_CACHE_TYPE cacheType);
28  CDir(CDir&& dir) = default;
29  CDir& operator=(CDir&& dir) = default;
30  virtual ~CDir();
31 
32  void SetLastAccess(unsigned int &accessCounter);
33  unsigned int GetLastAccess() const { return m_lastAccess; }
34 
35  std::unique_ptr<CFileItemList> m_Items;
36  DIR_CACHE_TYPE m_cacheType;
37  private:
38  CDir(const CDir&) = delete;
39  CDir& operator=(const CDir&) = delete;
40  unsigned int m_lastAccess;
41  };
42  public:
43  CDirectoryCache(void);
44  virtual ~CDirectoryCache(void);
45  bool GetDirectory(const std::string& strPath, CFileItemList &items, bool retrieveAll = false);
46  void SetDirectory(const std::string& strPath, const CFileItemList &items, DIR_CACHE_TYPE cacheType);
47  void ClearDirectory(const std::string& strPath);
48  void ClearFile(const std::string& strFile);
49  void ClearSubPaths(const std::string& strPath);
50  void Clear();
51  void AddFile(const std::string& strFile);
52  bool FileExists(const std::string& strPath, bool& bInCache);
53 #ifdef _DEBUG
54  void PrintStats() const;
55 #endif
56  protected:
57  void InitCache(const std::set<std::string>& dirs);
58  void ClearCache(std::set<std::string>& dirs);
59  void CheckIfFull();
60 
61  std::map<std::string, CDir> m_cache;
62 
63  mutable CCriticalSection m_cs;
64 
65  unsigned int m_accessCounter;
66 
67 #ifdef _DEBUG
68  unsigned int m_cacheHits;
69  unsigned int m_cacheMisses;
70 #endif
71  };
72 }
73 extern XFILE::CDirectoryCache g_directoryCache;
Definition: Scraper.h:41
Represents a list of files.
Definition: FileItem.h:702
Represents a file on a share.
Definition: FileItem.h:102
Definition: DirectoryCache.h:22