Rose
Cache.h
Go to the documentation of this file.
1 
8 #pragma once
9 
10 #include <memory>
11 #include <string>
12 #include <istream>
13 #include <future>
14 #include <mutex>
15 #include <ostream>
16 #include <optional>
17 #include <utility>
18 #include <filesystem>
19 #include <map>
20 #include <chrono>
21 #include "Constants.h"
22 #include "Signals.h"
23 
24 namespace rose {
25 
26  using CacheURI = std::string;
27  using CacheObjectURI = std::string;
28 
29  class CacheObject;
30 
36  public:
37  CacheLocalStore() = default;
38  virtual ~CacheLocalStore() = default;
39 
45  virtual CacheError create(const CacheObject &object) = 0;
46 
52  virtual bool find(const CacheObject &object) = 0;
53 
60  virtual std::ofstream openWrite(const CacheObject &object, bool temp) = 0;
61 
66  virtual void moveFromTemp(const CacheObject &object) = 0;
67 
74  virtual void
75  setCacheTime(const CacheObject &object, std::chrono::system_clock::duration cacheTimeInc) = 0;
76 
83  virtual void removeTemp(const CacheObject &object) = 0;
84 
90  virtual std::ifstream openRead(const CacheObject &object) = 0;
91 
96  [[nodiscard]] virtual std::optional<std::string> fileSystemName(const CacheObject &object) const { return std::nullopt; }
97  };
98 
103  class CacheObject {
104  protected:
105  std::string mObjectSrcName{};
106  std::string mObjectUsrName{};
107 
108  long mStatusCode;
109 
110  bool mFirstProcess{false};
111 
112  public:
113  CacheObject() = delete;
114  virtual ~CacheObject() = default;
115 
121  explicit CacheObject(std::string srcName, std::string userName = "")
122  : mObjectSrcName(std::move(srcName)), mObjectUsrName(std::move(userName)), mStatusCode(0) {}
123 
128  [[nodiscard]] const std::string& objectSrcName() const { return mObjectSrcName; }
129 
134  [[nodiscard]] const std::string& objectUsrName() const { return mObjectUsrName; }
135 
140  void setStatusCode(long statusCode) { mStatusCode = statusCode; }
141 
146  [[nodiscard]] long getStatusCode() const { return mStatusCode; }
147 
154  void setFirstProcess() { mFirstProcess = true; }
155 
160  [[nodiscard]] bool getFirstProcess() const { return mFirstProcess; }
161  };
162 
167  class CacheSource {
168  protected:
169  std::chrono::system_clock::duration mCacheValidDuration{};
170 
171  public:
172  CacheSource();
173  virtual ~CacheSource() = default;
174 
181  virtual void fetch(CacheObject &cacheObject, std::ostream &ostrm, time_t cacheTime) = 0;
182 
187  [[nodiscard]] auto cacheValidDuration() const { return mCacheValidDuration; }
188  };
189 
194  class Cache : public std::map<uint32_t,CacheObject> {
195  protected:
196  std::unique_ptr<CacheLocalStore> localStore;
197  std::unique_ptr<CacheSource> source;
198 
199  public:
200  Cache() = default;
201  virtual ~Cache() = default;
202 
203  template<typename S>
204  std::optional<uint32_t> findByUserName(S userName) {
205  std::string un{userName};
206  for (auto & it : *this) {
207  if (it.second.objectUsrName() == un)
208  return it.first;
209  }
210  return std::nullopt;
211  }
212  };
213 
219  protected:
220  std::error_code mErrorCode;
221  std::filesystem::path mRootPath;
222 
223  public:
224  CacheFileSystem() = delete;
225  ~CacheFileSystem() override = default;
226 
231  explicit operator bool() const noexcept {
232  return mErrorCode.operator bool();
233  }
234 
239  explicit CacheFileSystem(std::filesystem::path rootPath);
240 
245  explicit CacheFileSystem(const std::string& rootPath) : CacheFileSystem(std::filesystem::path(rootPath)) {}
246 
252  CacheFileSystem(const std::filesystem::path& rootPath, const std::string& cacheName) : CacheFileSystem(rootPath) {
253  mRootPath.append(cacheName);
254  if (!mErrorCode)
255  std::filesystem::create_directory(mRootPath, mErrorCode);
256  }
257 
262  [[nodiscard]] const std::filesystem::path& rootPath() const { return mRootPath; }
263 
269  CacheError create(const CacheObject &object) override;
270 
276  bool find(const CacheObject &object) override;
277 
282  std::ofstream openWrite(const CacheObject &object, bool temp) override;
283 
288  std::ifstream openRead(const CacheObject &object) override;
289 
294  void moveFromTemp(const CacheObject &object) override;
295 
302  void removeTemp(const CacheObject &object) override;
303 
307  void setCacheTime(const CacheObject &object, std::chrono::system_clock::duration cacheTimeInc) override;
308 
313  [[nodiscard]] std::optional<std::string> fileSystemName(const CacheObject &object) const override;
314  };
315 
320  class CacheWebSource : public CacheSource {
321  protected:
322  std::string mSourceURI;
323  long mResponseCode{0};
324 
325  public:
326  CacheWebSource() = delete;
327  ~CacheWebSource() override = default;
328 
335  explicit CacheWebSource(std::string sourceURI);
336 
344  CacheWebSource(std::string sourceURI, std::chrono::system_clock::duration cacheValidityDuration);
345 
352  void fetch(CacheObject &cacheObject, std::ostream &ostrm, time_t cacheTime) override;
353 
358  [[nodiscard]] long responseCode() const { return mResponseCode; }
359 
360  [[nodiscard]] const std::string& sourceURI() const { return mSourceURI; }
361  };
362 
367  class WebFileCache : public Cache {
368  protected:
369  WebFileCache();
370 
371  SignalSerialNumber mSignalSerialNumber{};
372 
373  std::mutex mMutex;
374 
375  std::shared_ptr<Slot<int>> mCheckValidity;
376  std::shared_ptr<Slot<int>> mCheckFutures;
377 
378  std::vector<std::future<uint32_t>> mFutureList;
379 
380  void checkFutures();
381 
389  static uint32_t asyncFetch(WebFileCache *self, uint32_t id, time_t cacheTime);
390 
391  public:
392  ~WebFileCache() override = default;
393 
401  WebFileCache(const std::string& sourceURI,
402  const std::filesystem::path& rootPath, const std::string& cacheName);
403 
412  WebFileCache(const std::string& sourceURI, const std::filesystem::path& rootPath,
413  const std::string& cacheName, std::chrono::system_clock::duration cacheValidityDuration);
414 
420  void connect(Signal<int> &futureCheck, Signal<int> &validityCheck);
421 
422  Signal<uint32_t> itemFetched{};
423 
424  explicit operator bool() const { return localStore.operator bool(); }
425 
430  [[nodiscard]] auto cacheRootPath() const {
431  return dynamic_cast<CacheFileSystem*>(localStore.get())->rootPath();
432  }
433 
437  void fetchAll();
438  };
439 }
std::unique_ptr< CacheLocalStore > localStore
The local store for the Cache.
Definition: Cache.h:196
virtual void setCacheTime(const CacheObject &object, std::chrono::system_clock::duration cacheTimeInc)=0
Modify the object cache time.
std::string CacheObjectURI
Type for a cache object URI value.
Definition: Cache.h:27
virtual void removeTemp(const CacheObject &object)=0
Remove a temporary cache location.
The base class for an object to be cached.
Definition: Cache.h:103
virtual std::optional< std::string > fileSystemName(const CacheObject &object) const
Get a filesystem name locator for the object, if available.
Definition: Cache.h:96
virtual void moveFromTemp(const CacheObject &object)=0
Move object from temporary cache location to permanent cache location.
CacheError
Error values return by the cache.
Definition: Constants.h:110
A collection CacheObject objects associated with.
Definition: Cache.h:194
std::mutex mMutex
Mutex for locking the WebFileCache.
Definition: Cache.h:373
Virtual ase class for data sources to be cached.
Definition: Cache.h:167
const std::filesystem::path & rootPath() const
Accessor for cache root path.
Definition: Cache.h:262
CacheObject(std::string srcName, std::string userName="")
Constructor.
Definition: Cache.h:121
bool getFirstProcess() const
Get the value of the first process flag.
Definition: Cache.h:160
auto cacheValidDuration() const
Access the cache object validity period.
Definition: Cache.h:187
A CacheLocalStore implemented on the local filesystem.
Definition: Cache.h:218
std::vector< std::future< uint32_t > > mFutureList
The list of outstanding futures.
Definition: Cache.h:378
auto cacheRootPath() const
Get the Cache root path.
Definition: Cache.h:430
virtual std::ofstream openWrite(const CacheObject &object, bool temp)=0
Open a cache object for writing.
Constants and Enumerations.
std::shared_ptr< Slot< int > > mCheckFutures
Slot for check futures timeing signal.
Definition: Cache.h:376
virtual bool find(const CacheObject &object)=0
Find an item on a Cache.
A cache source on the World Wide Web.
Definition: Cache.h:320
std::error_code mErrorCode
Store the last error code.
Definition: Cache.h:220
CacheFileSystem(const std::filesystem::path &rootPath, const std::string &cacheName)
Constructor.
Definition: Cache.h:252
long responseCode() const
Get the response code returned by the server.
Definition: Cache.h:358
std::string CacheURI
Type for a cache URI value.
Definition: Cache.h:26
std::string mSourceURI
The URI for the source.
Definition: Cache.h:322
const std::string & objectUsrName() const
Accessor for the object user name.
Definition: Cache.h:134
CacheFileSystem(const std::string &rootPath)
Constructor.
Definition: Cache.h:245
const std::string & objectSrcName() const
Accessor for the object source name.
Definition: Cache.h:128
void setFirstProcess()
Called after the first process.
Definition: Cache.h:154
A cache with a source on the World Wide Web, and local store on the filesystem.
Definition: Cache.h:367
long getStatusCode() const
Get the cache fetch status code.
Definition: Cache.h:146
ToDo: There is an issue that the initial scroll interaction is lost if the click/press lands on a Wid...
Definition: CelestialOverlay.cpp:13
The transmitter portion of a Signal-Slot transmitter receiver pair.
Definition: Signals.h:40
long mStatusCode
The status code for the last fetch.
Definition: Cache.h:108
std::filesystem::path mRootPath
Store the filesystem path to the cache storage.
Definition: Cache.h:221
Establish an intra-application signaling protocol.
void setStatusCode(long statusCode)
Set the cash fetch status code.
Definition: Cache.h:140
std::shared_ptr< Slot< int > > mCheckValidity
Slot for check validity timing signal.
Definition: Cache.h:375
A pure virtual class specifying the interface to a cache store system.
Definition: Cache.h:35
std::unique_ptr< CacheSource > source
The source for cache objects.
Definition: Cache.h:197
virtual std::ifstream openRead(const CacheObject &object)=0
Open a cache object for reading.
virtual CacheError create(const CacheObject &object)=0
Create an item on a Cache.