From de6057de814a4dc16654bdda84f697476b2ebef5 Mon Sep 17 00:00:00 2001 From: Dan Engelbrecht Date: Sat, 30 Apr 2022 01:29:24 +0200 Subject: first pass at generic block store with gc --- zenstore/include/zenstore/blockstore.h | 41 ++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) (limited to 'zenstore/include') diff --git a/zenstore/include/zenstore/blockstore.h b/zenstore/include/zenstore/blockstore.h index 424db461a..4dd6e5289 100644 --- a/zenstore/include/zenstore/blockstore.h +++ b/zenstore/include/zenstore/blockstore.h @@ -15,8 +15,14 @@ struct BlockStoreLocation uint32_t BlockIndex; uint64_t Offset; uint64_t Size; + + inline auto operator<=>(const BlockStoreLocation& Rhs) const = default; }; +constexpr BlockStoreLocation InvalidBlockStoreLocation{.BlockIndex = 0xfffffffful, + .Offset = 0xffffffffffffffffull, + .Size = 0xffffffffffffffffull}; + #pragma pack(push) #pragma pack(1) @@ -99,6 +105,41 @@ private: BasicFile m_File; }; +class BlockStore +{ +public: + void Initialize(const std::filesystem::path& BlocksBasePath, + uint64_t MaxBlockSize, + uint64_t MaxBlockCount, + const std::vector& KnownLocations); + BlockStoreLocation WriteChunk(const void* Data, uint64_t Size, uint64_t Alignment); + Ref GetChunkBlock(const BlockStoreLocation& Location); + void Flush(); + + typedef std::function& MovedChunks, const std::vector RemovedChunks)> + ReclaimCallback; + + void ReclaimSpace( + const std::vector& ChunkLocations, + const std::vector& KeepChunkIndexes, + uint64_t PayloadAlignment, + bool DryRun, + const ReclaimCallback& Callback = [](const std::unordered_map&, const std::vector&) {}); + +private: + std::unordered_map> m_ChunkBlocks; + + RwLock m_InsertLock; // used to serialize inserts + Ref m_WriteBlock; + std::uint64_t m_CurrentInsertOffset = 0; + std::atomic_uint32_t m_WriteBlockIndex{}; + + uint64_t m_MaxBlockSize = 1u << 28; + uint64_t m_MaxBlockCount = BlockStoreDiskLocation::MaxBlockIndex + 1; + std::filesystem::path m_BlocksBasePath; + std::atomic_uint64_t m_TotalSize{}; +}; + void blockstore_forcelink(); } // namespace zen -- cgit v1.2.3