| 35 | |
| 36 | template <typename Container> |
| 37 | class KeyValueStoreMemory final : public IKeyValueStore, NonCopyable { |
| 38 | public: |
| 39 | KeyValueStoreMemory(IDiskQueue* log, |
| 40 | Reference<AsyncVar<ServerDBInfo> const> db, |
| 41 | UID id, |
| 42 | int64_t memoryLimit, |
| 43 | KeyValueStoreType storeType, |
| 44 | bool disableSnapshot, |
| 45 | bool replaceContent, |
| 46 | bool exactRecovery, |
| 47 | bool enableEncryption); |
| 48 | |
| 49 | // IClosable |
| 50 | Future<Void> getError() const override { return log->getError(); } |
| 51 | Future<Void> onClosed() const override { return log->onClosed(); } |
| 52 | void dispose() override { |
| 53 | recovering.cancel(); |
| 54 | log->dispose(); |
| 55 | if (reserved_buffer != nullptr) { |
| 56 | delete[] reserved_buffer; |
| 57 | reserved_buffer = nullptr; |
| 58 | } |
| 59 | delete this; |
| 60 | } |
| 61 | void close() override { |
| 62 | recovering.cancel(); |
| 63 | log->close(); |
| 64 | if (reserved_buffer != nullptr) { |
| 65 | delete[] reserved_buffer; |
| 66 | reserved_buffer = nullptr; |
| 67 | } |
| 68 | delete this; |
| 69 | } |
| 70 | |
| 71 | // IKeyValueStore |
| 72 | KeyValueStoreType getType() const override { return type; } |
| 73 | |
| 74 | std::tuple<size_t, size_t, size_t> getSize() const override { return data.size(); } |
| 75 | |
| 76 | int64_t getAvailableSize() const { |
| 77 | int64_t residentSize = data.sumTo(data.end()) + queue.totalSize() + // doesn't account for overhead in queue |
| 78 | transactionSize; |
| 79 | |
| 80 | return memoryLimit - residentSize; |
| 81 | } |
| 82 | |
| 83 | StorageBytes getStorageBytes() const override { |
| 84 | StorageBytes diskQueueBytes = log->getStorageBytes(); |
| 85 | |
| 86 | // Try to bound how many in-memory bytes we might need to write to disk if we commit() now |
| 87 | int64_t uncommittedBytes = queue.totalSize() + transactionSize; |
| 88 | |
| 89 | // Check that we have enough space in memory and on disk |
| 90 | int64_t freeSize = std::min(getAvailableSize(), diskQueueBytes.free / 4 - uncommittedBytes); |
| 91 | int64_t availableSize = std::min(getAvailableSize(), diskQueueBytes.available / 4 - uncommittedBytes); |
| 92 | int64_t totalSize = std::min(memoryLimit, diskQueueBytes.total / 4 - uncommittedBytes); |
| 93 | |
| 94 | return StorageBytes(std::max((int64_t)0, freeSize), |