diff options
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; |