diff options
author | Roman Smrž <roman.smrz@seznam.cz> | 2021-01-17 21:59:46 +0100 |
---|---|---|
committer | Roman Smrž <roman.smrz@seznam.cz> | 2021-01-19 21:44:22 +0100 |
commit | 2abd6593c8b047d3fd579aa6cc0058bbebe266f8 (patch) | |
tree | 44af7148f648538846ef64bed0f8560f77de7e5d /src/storage.h | |
parent | 1466751256580d8b0e6eea46a8028dcab9742f6b (diff) |
Server watching local state head
Diffstat (limited to 'src/storage.h')
-rw-r--r-- | src/storage.h | 24 |
1 files changed, 23 insertions, 1 deletions
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 <functional> +#include <mutex> #include <unordered_map> #include <unordered_set> 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<tuple<UUID, Digest>> 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<void(UUID id, const Digest &)> &) = 0; virtual optional<vector<uint8_t>> loadKey(const Digest &) const = 0; virtual void storeKey(const Digest &, const vector<uint8_t> &) = 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<tuple<UUID, Digest>> 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<void(UUID id, const Digest &)> &) override; virtual optional<vector<uint8_t>> loadKey(const Digest &) const override; virtual void storeKey(const Digest &, const vector<uint8_t> &) 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<UUID, function<void(UUID id, const Digest &)>> watchers; + unordered_map<int, UUID> watchMap; }; class MemoryStorage : public StorageBackend @@ -85,6 +102,7 @@ public: virtual vector<tuple<UUID, Digest>> 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<void(UUID id, const Digest &)> &) override; virtual optional<vector<uint8_t>> loadKey(const Digest &) const override; virtual void storeKey(const Digest &, const vector<uint8_t> &) override; @@ -93,6 +111,9 @@ private: unordered_map<Digest, vector<uint8_t>> storage; unordered_map<UUID, vector<tuple<UUID, Digest>>> heads; unordered_map<Digest, vector<uint8_t>> keys; + + mutex watcherLock; + unordered_multimap<UUID, function<void(UUID id, const Digest &)>> watchers; }; class ChainStorage : public StorageBackend @@ -113,6 +134,7 @@ public: virtual vector<tuple<UUID, Digest>> 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<void(UUID id, const Digest &)> &) override; virtual optional<vector<uint8_t>> loadKey(const Digest &) const override; virtual void storeKey(const Digest &, const vector<uint8_t> &) override; |