summaryrefslogtreecommitdiff
path: root/src/storage.h
diff options
context:
space:
mode:
authorRoman Smrž <roman.smrz@seznam.cz>2020-01-04 21:07:33 +0100
committerRoman Smrž <roman.smrz@seznam.cz>2020-01-04 21:29:00 +0100
commit2059c09154870d704d26d10f0462db1e9b4ea676 (patch)
treec256c807500c498452de3c82b695a59f990b606f /src/storage.h
parentb97b503408911130d24d7f07f9247dca8314a316 (diff)
Partial and memory-backed storage
Diffstat (limited to 'src/storage.h')
-rw-r--r--src/storage.h95
1 files changed, 86 insertions, 9 deletions
diff --git a/src/storage.h b/src/storage.h
index 68002fa..cdb8984 100644
--- a/src/storage.h
+++ b/src/storage.h
@@ -3,35 +3,112 @@
#include "erebos/storage.h"
#include <future>
+#include <unordered_map>
#include <unordered_set>
namespace fs = std::filesystem;
using std::optional;
using std::shared_future;
+using std::shared_ptr;
+using std::unique_ptr;
+using std::unordered_map;
using std::unordered_set;
+using std::variant;
using std::vector;
namespace erebos {
-struct Storage::Priv
+class StorageBackend
{
- static constexpr size_t CHUNK = 16384;
+public:
+ StorageBackend() = default;
+ virtual ~StorageBackend() = default;
- fs::path root;
+ virtual bool contains(const Digest &) const = 0;
+
+ virtual optional<vector<uint8_t>> loadBytes(const Digest &) const = 0;
+ virtual void storeBytes(const Digest &, const vector<uint8_t> &) = 0;
+
+ virtual optional<vector<uint8_t>> loadKey(const Digest &) const = 0;
+ virtual void storeKey(const Digest &, const vector<uint8_t> &) = 0;
+};
+
+class FilesystemStorage : public StorageBackend
+{
+public:
+ FilesystemStorage(const fs::path &);
+ virtual ~FilesystemStorage() = default;
+
+ virtual bool contains(const Digest &) const override;
+
+ virtual optional<vector<uint8_t>> loadBytes(const Digest &) const override;
+ virtual void storeBytes(const Digest &, const vector<uint8_t> &) override;
+
+ virtual optional<vector<uint8_t>> loadKey(const Digest &) const override;
+ virtual void storeKey(const Digest &, const vector<uint8_t> &) override;
+
+private:
+ static constexpr size_t CHUNK = 16384;
fs::path objectPath(const Digest &) const;
fs::path keyPath(const Digest &) const;
- optional<vector<uint8_t>> loadBytes(const Digest &) const;
- void storeBytes(const Digest &, const vector<uint8_t> &) const;
+
+ fs::path root;
};
-struct Ref::Priv
+class MemoryStorage : public StorageBackend
{
- Storage storage;
- Digest digest;
+public:
+ MemoryStorage() = default;
+ virtual ~MemoryStorage() = default;
+
+ virtual bool contains(const Digest &) const override;
+
+ virtual optional<vector<uint8_t>> loadBytes(const Digest &) const override;
+ virtual void storeBytes(const Digest &, const vector<uint8_t> &) override;
- shared_future<Object> object;
+ virtual optional<vector<uint8_t>> loadKey(const Digest &) const override;
+ virtual void storeKey(const Digest &, const vector<uint8_t> &) override;
+
+private:
+ unordered_map<Digest, vector<uint8_t>> storage;
+ unordered_map<Digest, vector<uint8_t>> keys;
+};
+
+class ChainStorage : public StorageBackend
+{
+public:
+ ChainStorage(shared_ptr<StorageBackend> storage):
+ ChainStorage(std::move(storage), nullptr) {}
+ ChainStorage(shared_ptr<StorageBackend> storage, unique_ptr<ChainStorage> parent):
+ storage(std::move(storage)), parent(std::move(parent)) {}
+ virtual ~ChainStorage() = default;
+
+ virtual bool contains(const Digest &) const override;
+
+ virtual optional<vector<uint8_t>> loadBytes(const Digest &) const override;
+ virtual void storeBytes(const Digest &, const vector<uint8_t> &) override;
+
+ virtual optional<vector<uint8_t>> loadKey(const Digest &) const override;
+ virtual void storeKey(const Digest &, const vector<uint8_t> &) override;
+
+private:
+ shared_ptr<StorageBackend> storage;
+ unique_ptr<ChainStorage> parent;
+};
+
+struct Storage::Priv
+{
+ shared_ptr<StorageBackend> backend;
+
+ optional<vector<uint8_t>> loadBytes(const Digest & digest) const;
+};
+
+struct Ref::Priv
+{
+ const unique_ptr<PartialStorage> storage;
+ const Digest digest;
};
vector<Stored<Object>> collectStoredObjects(const Stored<Object> &);