diff options
| author | Stefan Boberg <[email protected]> | 2021-05-22 11:53:09 +0200 |
|---|---|---|
| committer | Stefan Boberg <[email protected]> | 2021-05-22 11:53:09 +0200 |
| commit | f93d04dd9381eac38be82733c34baa2075efa11c (patch) | |
| tree | b5c4f9a173042580398e4dd96be97702909cc06b /zenserver/cache/structuredcachestore.h | |
| parent | Structured cache changes (diff) | |
| download | zen-f93d04dd9381eac38be82733c34baa2075efa11c.tar.xz zen-f93d04dd9381eac38be82733c34baa2075efa11c.zip | |
Split out structured cache store code into dedicated cpp/h pair
Diffstat (limited to 'zenserver/cache/structuredcachestore.h')
| -rw-r--r-- | zenserver/cache/structuredcachestore.h | 117 |
1 files changed, 117 insertions, 0 deletions
diff --git a/zenserver/cache/structuredcachestore.h b/zenserver/cache/structuredcachestore.h new file mode 100644 index 000000000..7936f9d84 --- /dev/null +++ b/zenserver/cache/structuredcachestore.h @@ -0,0 +1,117 @@ +// Copyright Epic Games, Inc. All Rights Reserved. + +#pragma once + +#include <zencore/IoBuffer.h> +#include <zencore/iohash.h> +#include <zencore/thread.h> +#include <zencore/uid.h> +#include <zenstore/cas.h> +#include <compare> +#include <filesystem> +#include <unordered_map> + +namespace zen { + +class WideStringBuilderBase; +class CasStore; + +} // namespace zen + +/****************************************************************************** + + /$$$$$$$$ /$$$$$$ /$$ + |_____ $$ /$$__ $$ | $$ + /$$/ /$$$$$$ /$$$$$$$ | $$ \__/ /$$$$$$ /$$$$$$| $$$$$$$ /$$$$$$ + /$$/ /$$__ $| $$__ $$ | $$ |____ $$/$$_____| $$__ $$/$$__ $$ + /$$/ | $$$$$$$| $$ \ $$ | $$ /$$$$$$| $$ | $$ \ $| $$$$$$$$ + /$$/ | $$_____| $$ | $$ | $$ $$/$$__ $| $$ | $$ | $| $$_____/ + /$$$$$$$| $$$$$$| $$ | $$ | $$$$$$| $$$$$$| $$$$$$| $$ | $| $$$$$$$ + |________/\_______|__/ |__/ \______/ \_______/\_______|__/ |__/\_______/ + + Cache store for UE5. Restricts keys to "{bucket}/{hash}" pairs where the hash + is 40 (hex) chars in size. Values may be opaque blobs or structured objects + which can in turn contain references to other objects (or blobs). + +******************************************************************************/ + +struct ZenCacheValue +{ + zen::IoBuffer Value; + bool IsCompactBinary = false; +}; + +class ZenCacheMemoryLayer +{ +public: + ZenCacheMemoryLayer(); + ~ZenCacheMemoryLayer(); + + bool Get(std::string_view Bucket, const zen::IoHash& HashKey, ZenCacheValue& OutValue); + void Put(std::string_view Bucket, const zen::IoHash& HashKey, const ZenCacheValue& Value); + +private: + struct CacheBucket + { + zen::RwLock m_bucketLock; + std::unordered_map<zen::IoHash, zen::IoBuffer, zen::IoHash::Hasher> m_cacheMap; + + bool Get(const zen::IoHash& HashKey, ZenCacheValue& OutValue); + void Put(const zen::IoHash& HashKey, const ZenCacheValue& Value); + }; + + zen::RwLock m_Lock; + std::unordered_map<std::string, CacheBucket> m_Buckets; +}; + +class ZenCacheDiskLayer +{ +public: + ZenCacheDiskLayer(zen::CasStore& Cas, const std::filesystem::path& RootDir); + ~ZenCacheDiskLayer(); + + bool Get(std::string_view Bucket, const zen::IoHash& HashKey, ZenCacheValue& OutValue); + void Put(std::string_view Bucket, const zen::IoHash& HashKey, const ZenCacheValue& Value); + + void Flush(); + +private: + /** A cache bucket manages a single directory containing + metadata and data for that bucket + */ + struct CacheBucket; + + zen::CasStore& m_CasStore; + std::filesystem::path m_RootDir; + zen::RwLock m_Lock; + std::unordered_map<std::string, CacheBucket> m_Buckets; // TODO: make this case insensitive +}; + +class ZenCacheStore +{ +public: + ZenCacheStore(zen::CasStore& Cas, const std::filesystem::path& RootDir); + ~ZenCacheStore(); + + bool Get(std::string_view Bucket, const zen::IoHash& HashKey, ZenCacheValue& OutValue); + void Put(std::string_view Bucket, const zen::IoHash& HashKey, const ZenCacheValue& Value); + +private: + std::filesystem::path m_RootDir; + ZenCacheMemoryLayer m_MemLayer; + ZenCacheDiskLayer m_DiskLayer; +}; + +/** Tracks cache entry access, stats and orchestrates cleanup activities + */ +class ZenCacheTracker +{ +public: + ZenCacheTracker(ZenCacheStore& CacheStore); + ~ZenCacheTracker(); + + void TrackAccess(std::string_view Bucket, const zen::IoHash& HashKey); + void Flush(); + +private: +}; |