| 29 | }; |
| 30 | |
| 31 | class KeyValueStore { |
| 32 | public: |
| 33 | KeyValueStore(); |
| 34 | ~KeyValueStore(); |
| 35 | |
| 36 | Result<BytesView> serialize(); |
| 37 | Result<Void> populate(const BytesView& data); |
| 38 | |
| 39 | void store(const StringBox& key, const BytesView& blob, uint64_t ttlSeconds, uint64_t weight); |
| 40 | |
| 41 | std::optional<BytesView> fetch(const StringBox& key); |
| 42 | std::vector<std::pair<StringBox, KeyValueStoreEntry>> fetchAll(); |
| 43 | |
| 44 | bool exists(const StringBox& key); |
| 45 | bool remove(const StringBox& key); |
| 46 | void removeAll(); |
| 47 | |
| 48 | uint64_t getMaxWeight() const; |
| 49 | void setMaxWeight(uint64_t maxWeight); |
| 50 | |
| 51 | uint64_t getMutationId() const; |
| 52 | |
| 53 | // For unit tests |
| 54 | void setCurrentTimeSeconds(uint64_t timeSeconds); |
| 55 | |
| 56 | private: |
| 57 | uint64_t _currentTimeSeconds = 0; |
| 58 | uint64_t _mutationId = 0; |
| 59 | uint64_t _maxWeight; |
| 60 | FlatMap<StringBox, KeyValueStoreEntry> _entries; |
| 61 | |
| 62 | void buildManifest(const std::vector<std::pair<StringBox, KeyValueStoreEntry>>& entries, ByteBuffer& output) const; |
| 63 | std::vector<std::pair<StringBox, KeyValueStoreEntry>> collectEntries(); |
| 64 | void evictEntriesIfNeeded(std::vector<std::pair<StringBox, KeyValueStoreEntry>>& collectedEntries); |
| 65 | |
| 66 | bool isEntryExpired(const KeyValueStoreEntry& entry) const; |
| 67 | |
| 68 | uint64_t currentTimeSeconds() const; |
| 69 | |
| 70 | std::optional<BytesView> fetch(const StringBox& key, bool updateSequence); |
| 71 | }; |
| 72 | |
| 73 | } // namespace Valdi |