From 2abd6593c8b047d3fd579aa6cc0058bbebe266f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roman=20Smr=C5=BE?= Date: Sun, 17 Jan 2021 21:59:46 +0100 Subject: Server watching local state head --- src/storage.h | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) (limited to 'src/storage.h') diff --git a/src/storage.h b/src/storage.h index 18ac1ad..b8d769c 100644 --- a/src/storage.h +++ b/src/storage.h @@ -2,16 +2,21 @@ #include "erebos/storage.h" +#include +#include #include #include namespace fs = std::filesystem; +using std::function; +using std::mutex; using std::optional; using std::shared_future; using std::shared_ptr; using std::unique_ptr; using std::unordered_map; +using std::unordered_multimap; using std::unordered_set; using std::tuple; using std::variant; @@ -34,6 +39,7 @@ public: virtual vector> headRefs(UUID type) const = 0; virtual UUID storeHead(UUID type, const Digest & dgst) = 0; virtual bool replaceHead(UUID type, UUID id, const Digest & old, const Digest & dgst) = 0; + virtual void watchHead(UUID type, const function &) = 0; virtual optional> loadKey(const Digest &) const = 0; virtual void storeKey(const Digest &, const vector &) = 0; @@ -43,7 +49,7 @@ class FilesystemStorage : public StorageBackend { public: FilesystemStorage(const fs::path &); - virtual ~FilesystemStorage() = default; + virtual ~FilesystemStorage(); virtual bool contains(const Digest &) const override; @@ -54,20 +60,31 @@ public: virtual vector> headRefs(UUID type) const override; virtual UUID storeHead(UUID type, const Digest & dgst) override; virtual bool replaceHead(UUID type, UUID id, const Digest & old, const Digest & dgst) override; + virtual void watchHead(UUID type, const function &) override; virtual optional> loadKey(const Digest &) const override; virtual void storeKey(const Digest &, const vector &) override; private: + void inotifyWatch(); + static constexpr size_t CHUNK = 16384; fs::path objectPath(const Digest &) const; + fs::path headPath(UUID id) const; fs::path headPath(UUID id, UUID type) const; fs::path keyPath(const Digest &) const; FILE * openLockFile(const fs::path & path) const; fs::path root; + + mutex watcherLock; + std::thread watcherThread; + int inotify = -1; + int inotifyWakeup = -1; + unordered_multimap> watchers; + unordered_map watchMap; }; class MemoryStorage : public StorageBackend @@ -85,6 +102,7 @@ public: virtual vector> headRefs(UUID type) const override; virtual UUID storeHead(UUID type, const Digest & dgst) override; virtual bool replaceHead(UUID type, UUID id, const Digest & old, const Digest & dgst) override; + virtual void watchHead(UUID type, const function &) override; virtual optional> loadKey(const Digest &) const override; virtual void storeKey(const Digest &, const vector &) override; @@ -93,6 +111,9 @@ private: unordered_map> storage; unordered_map>> heads; unordered_map> keys; + + mutex watcherLock; + unordered_multimap> watchers; }; class ChainStorage : public StorageBackend @@ -113,6 +134,7 @@ public: virtual vector> headRefs(UUID type) const override; virtual UUID storeHead(UUID type, const Digest & dgst) override; virtual bool replaceHead(UUID type, UUID id, const Digest & old, const Digest & dgst) override; + virtual void watchHead(UUID type, const function &) override; virtual optional> loadKey(const Digest &) const override; virtual void storeKey(const Digest &, const vector &) override; -- cgit v1.2.3