Pioneer
GalaxyCache.h
Go to the documentation of this file.
1 // Copyright © 2008-2023 Pioneer Developers. See AUTHORS.txt for details
2 // Licensed under the terms of the GPL v3. See licenses/GPL-3.txt
3 
4 #ifndef SECTORCACHE_H
5 #define SECTORCACHE_H
6 
7 #include "JobQueue.h"
8 #include "RefCounted.h"
9 #include "galaxy/SystemPath.h"
10 #include <functional>
11 #include <map>
12 #include <memory>
13 #include <set>
14 #include <vector>
15 
16 class GalaxyGenerator;
17 class Galaxy;
18 
19 template <typename T, typename CompareT>
21  friend T;
22 
23 public:
24  static const std::string CACHE_NAME;
25 
27  m_galaxy(galaxy),
28  m_cacheHits(0),
29  m_cacheHitsSlave(0),
30  m_cacheMisses(0) {}
32 
35 
36  void ClearCache(); // Completely clear slave caches
37  bool IsEmpty() { return m_attic.empty(); }
38 
39  void OutputCacheStatistics(bool reset = true);
40 
41  typedef std::vector<SystemPath> PathVector;
42  typedef std::map<SystemPath, RefCountedPtr<T>, CompareT> CacheMap;
43  typedef std::map<SystemPath, T *, CompareT> AtticMap;
44  typedef std::function<void()> CacheFilledCallback;
45 
46  class Slave : public RefCounted {
47  friend class GalaxyObjectCache<T, CompareT>;
48 
49  public:
52  typename CacheMap::const_iterator Begin() const { return m_cache.begin(); }
53  typename CacheMap::const_iterator End() const { return m_cache.end(); }
54 
55  void FillCache(const PathVector &paths, CacheFilledCallback callback = CacheFilledCallback());
56  void Erase(const SystemPath &path);
57  void Erase(const typename CacheMap::const_iterator &it);
58  void ClearCache();
59  bool IsEmpty() { return m_cache.empty(); }
60  ~Slave();
61 
62  private:
63  GalaxyObjectCache *m_master;
64  RefCountedPtr<Galaxy> m_galaxy;
65  CacheMap m_cache;
66  JobSet m_jobs;
67 
68  Slave(GalaxyObjectCache *master, RefCountedPtr<Galaxy> galaxy, JobQueue *jobQueue);
69  void MasterDeleted();
70  void AddToCache(std::vector<RefCountedPtr<T>> &objects);
71  };
72 
74 
75 private:
76  static const unsigned CACHE_JOB_SIZE = 100;
77 
78  void AddToCache(std::vector<RefCountedPtr<T>> &objects);
79  bool HasCached(const SystemPath &path) const;
80  void RemoveFromAttic(const SystemPath &path);
81 
82  // ********************************************************************************
83  // Overloaded Job class to handle generating a collection of sectors
84  // ********************************************************************************
85  class CacheJob : public Job {
86  public:
87  CacheJob(std::unique_ptr<std::vector<SystemPath>> path, Slave *slaveCache, RefCountedPtr<Galaxy> galaxy, CacheFilledCallback callback = CacheFilledCallback());
88 
89  virtual void OnRun(); // RUNS IN ANOTHER THREAD!! MUST BE THREAD SAFE!
90  virtual void OnFinish(); // runs in primary thread of the context
91  virtual void OnCancel() {} // runs in primary thread of the context
92 
93  protected:
94  std::unique_ptr<std::vector<SystemPath>> m_paths;
95  std::vector<RefCountedPtr<T>> m_objects;
96  Slave *m_slaveCache;
97  RefCountedPtr<Galaxy> m_galaxy;
98  RefCountedPtr<GalaxyGenerator> m_galaxyGenerator;
99  CacheFilledCallback m_callback;
100  };
101 
102  Galaxy *m_galaxy;
103  std::set<Slave *> m_slaves;
104  AtticMap m_attic; // Those contains non-refcounted pointers which are kept alive by RefCountedPtrs in slave caches
105  // or elsewhere. The Sector destructor ensures that it is removed from here.
106  // This ensures, that there is only ever one object for each Sector.
107 
108  unsigned long long m_cacheHits;
109  unsigned long long m_cacheHitsSlave;
110  unsigned long long m_cacheMisses;
111 };
112 
113 class Sector;
115 
116 class StarSystem;
118 
119 #endif
GalaxyObjectCache< Sector, SystemPath::LessSectorOnly > SectorCache
Definition: GalaxyCache.h:113
GalaxyObjectCache< StarSystem, SystemPath::LessSystemOnly > StarSystemCache
Definition: GalaxyCache.h:116
Definition: GalaxyGenerator.h:17
Definition: GalaxyCache.h:46
void Erase(const SystemPath &path)
Definition: GalaxyCache.cpp:147
~Slave()
Definition: GalaxyCache.cpp:156
CacheMap::const_iterator End() const
Definition: GalaxyCache.h:53
RefCountedPtr< T > GetCached(const SystemPath &path)
Definition: GalaxyCache.cpp:127
RefCountedPtr< T > GetIfCached(const SystemPath &path)
Definition: GalaxyCache.cpp:118
void FillCache(const PathVector &paths, CacheFilledCallback callback=CacheFilledCallback())
Definition: GalaxyCache.cpp:181
bool IsEmpty()
Definition: GalaxyCache.h:59
void ClearCache()
Definition: GalaxyCache.cpp:153
CacheMap::const_iterator Begin() const
Definition: GalaxyCache.h:52
Definition: GalaxyCache.h:20
GalaxyObjectCache(Galaxy *galaxy)
Definition: GalaxyCache.h:26
std::function< void()> CacheFilledCallback
Definition: GalaxyCache.h:44
std::map< SystemPath, RefCountedPtr< T >, CompareT > CacheMap
Definition: GalaxyCache.h:42
void OutputCacheStatistics(bool reset=true)
Definition: GalaxyCache.cpp:89
RefCountedPtr< T > GetIfCached(const SystemPath &path)
Definition: GalaxyCache.cpp:42
std::map< SystemPath, T *, CompareT > AtticMap
Definition: GalaxyCache.h:43
RefCountedPtr< Slave > NewSlaveCache()
Definition: GalaxyCache.cpp:97
bool IsEmpty()
Definition: GalaxyCache.h:37
static const std::string CACHE_NAME
Definition: GalaxyCache.h:24
std::vector< SystemPath > PathVector
Definition: GalaxyCache.h:41
~GalaxyObjectCache()
Definition: GalaxyCache.cpp:20
RefCountedPtr< T > GetCached(const SystemPath &path)
Definition: GalaxyCache.cpp:54
void ClearCache()
Definition: GalaxyCache.cpp:82
Definition: Galaxy.h:18
Definition: JobQueue.h:108
Definition: JobQueue.h:157
Definition: JobQueue.h:33
Definition: RefCounted.h:36
Definition: RefCounted.h:11
Definition: Sector.h:19
Definition: StarSystem.h:27
Definition: SystemPath.h:13