diff options
| author | Martin Ridgers <[email protected]> | 2021-11-15 09:10:39 +0100 |
|---|---|---|
| committer | Martin Ridgers <[email protected]> | 2021-11-15 09:10:39 +0100 |
| commit | b258c117aba04c6a672fb87d07d126449d961a73 (patch) | |
| tree | 174ccc6a674a173f417debd31a11d32348f042c6 /zenutil/include | |
| parent | Fixed up FileSystemTranersal visitor to use std::fs::path (diff) | |
| parent | Updated cache policy according to UE. (diff) | |
| download | zen-b258c117aba04c6a672fb87d07d126449d961a73.tar.xz zen-b258c117aba04c6a672fb87d07d126449d961a73.zip | |
Merged main
Diffstat (limited to 'zenutil/include')
| -rw-r--r-- | zenutil/include/zenutil/cache/cache.h | 6 | ||||
| -rw-r--r-- | zenutil/include/zenutil/cache/cachekey.h | 83 | ||||
| -rw-r--r-- | zenutil/include/zenutil/cache/cachepolicy.h | 109 |
3 files changed, 198 insertions, 0 deletions
diff --git a/zenutil/include/zenutil/cache/cache.h b/zenutil/include/zenutil/cache/cache.h new file mode 100644 index 000000000..1a1dd9386 --- /dev/null +++ b/zenutil/include/zenutil/cache/cache.h @@ -0,0 +1,6 @@ +// Copyright Epic Games, Inc. All Rights Reserved. + +#pragma once + +#include <zenutil/cache/cachekey.h> +#include <zenutil/cache/cachepolicy.h> diff --git a/zenutil/include/zenutil/cache/cachekey.h b/zenutil/include/zenutil/cache/cachekey.h new file mode 100644 index 000000000..fb36c7759 --- /dev/null +++ b/zenutil/include/zenutil/cache/cachekey.h @@ -0,0 +1,83 @@ +// Copyright Epic Games, Inc. All Rights Reserved. + +#pragma once + +#include <zencore/iohash.h> +#include <zencore/string.h> +#include <zencore/uid.h> + +#include <zenutil/cache/cachepolicy.h> + +namespace zen { + +struct CacheKey +{ + std::string Bucket; + IoHash Hash; + + static CacheKey Create(std::string_view Bucket, const IoHash& Hash) { return {.Bucket = ToLower(Bucket), .Hash = Hash}; } + + static const CacheKey Empty; +}; + +inline bool +operator==(const CacheKey& A, const CacheKey& B) +{ + return A.Bucket == B.Bucket && A.Hash == B.Hash; +} + +inline bool +operator!=(const CacheKey& A, const CacheKey& B) +{ + return A.Bucket != B.Bucket || A.Hash != B.Hash; +} + +inline bool +operator<(const CacheKey& A, const CacheKey& B) +{ + const std::string& BucketA = A.Bucket; + const std::string& BucketB = B.Bucket; + return BucketA == BucketB ? A.Hash < B.Hash : BucketA < BucketB; +} + +struct CacheChunkRequest +{ + CacheKey Key; + IoHash ChunkId; + Oid PayloadId; + uint64_t RawOffset = 0ull; + uint64_t RawSize = ~uint64_t(0); + CachePolicy Policy = CachePolicy::Default; +}; + +inline bool +operator<(const CacheChunkRequest& A, const CacheChunkRequest& B) +{ + if (A.Key < B.Key) + { + return true; + } + if (B.Key < A.Key) + { + return false; + } + if (A.ChunkId < B.ChunkId) + { + return true; + } + if (B.ChunkId < A.ChunkId) + { + return false; + } + if (A.PayloadId < B.PayloadId) + { + return true; + } + if (B.PayloadId < A.PayloadId) + { + return false; + } + return A.RawOffset < B.RawOffset; +} + +} // namespace zen diff --git a/zenutil/include/zenutil/cache/cachepolicy.h b/zenutil/include/zenutil/cache/cachepolicy.h new file mode 100644 index 000000000..5cf19238e --- /dev/null +++ b/zenutil/include/zenutil/cache/cachepolicy.h @@ -0,0 +1,109 @@ +// Copyright Epic Games, Inc. All Rights Reserved. + +#pragma once + +#include <zencore/string.h> +#include <zencore/uid.h> + +#include <gsl/gsl-lite.hpp> +#include <unordered_map> + +namespace zen { + +class CbObjectView; +class CbWriter; + +enum class CachePolicy : uint32_t +{ + /** A value without any flags set. */ + None = 0, + + /** Allow a cache request to query local caches. */ + QueryLocal = 1 << 0, + /** Allow a cache request to query remote caches. */ + QueryRemote = 1 << 1, + /** Allow a cache request to query any caches. */ + Query = QueryLocal | QueryRemote, + + /** Allow cache records and values to be stored in local caches. */ + StoreLocal = 1 << 2, + /** Allow cache records and values to be stored in remote caches. */ + StoreRemote = 1 << 3, + /** Allow cache records and values to be stored in any caches. */ + Store = StoreLocal | StoreRemote, + + /** Skip fetching the metadata for record requests. */ + SkipMeta = 1 << 4, + /** Skip fetching the value for record, chunk, or value requests. */ + SkipValue = 1 << 5, + /** Skip fetching the attachments for record requests. */ + SkipAttachments = 1 << 6, + /** + * Skip fetching the data for any requests. + * + * Put requests with skip flags may assume that record existence implies payload existence. + */ + SkipData = SkipMeta | SkipValue | SkipAttachments, + + /** + * Keep records in the cache for at least the duration of the session. + * + * This is a hint that the record may be accessed again in this session. This is mainly meant + * to be used when subsequent accesses will not tolerate a cache miss. + */ + KeepAlive = 1 << 7, + + /** + * Partial output will be provided with the error status when a required payload is missing. + * + * This is meant for cases when the missing payloads can be individually recovered or rebuilt + * without rebuilding the whole record. The cache automatically adds this flag when there are + * other cache stores that it may be able to recover missing payloads from. + * + * Requests for records would return records where the missing payloads have a hash and size, + * but no data. Requests for chunks or values would return the hash and size, but no data. + */ + PartialOnError = 1 << 8, + + /** Allow cache requests to query and store records and values in local caches. */ + Local = QueryLocal | StoreLocal, + /** Allow cache requests to query and store records and values in remote caches. */ + Remote = QueryRemote | StoreRemote, + + /** Allow cache requests to query and store records and values in any caches. */ + Default = Query | Store, + + /** Do not allow cache requests to query or store records and values in any caches. */ + Disable = None, +}; + +gsl_DEFINE_ENUM_BITMASK_OPERATORS(CachePolicy); + +CachePolicy ParseQueryCachePolicy(std::string_view QueryPolicy, CachePolicy Default = CachePolicy::Query); + +CachePolicy ParseStoreCachePolicy(std::string_view StorePolicy, CachePolicy Default = CachePolicy::Store); + +CachePolicy ParseSkipCachePolicy(std::string_view SkipPolicy, CachePolicy Default = CachePolicy::None); + +class CacheRecordPolicy +{ +public: + CacheRecordPolicy() = default; + CacheRecordPolicy(const CachePolicy RecordPolicy, const CachePolicy DefaultPayloadPolicy = CachePolicy::Default); + + CachePolicy GetRecordPolicy() const { return m_RecordPolicy; } + CachePolicy GetPayloadPolicy(const Oid& PayloadId) const; + CachePolicy GetDefaultPayloadPolicy() const { return m_DefaultPayloadPolicy; } + + static bool Load(CbObjectView RecordPolicyObject, CacheRecordPolicy& OutRecordPolicy); + static void Save(const CacheRecordPolicy& Policy, CbWriter& Writer); + +private: + using PayloadPolicyMap = std::unordered_map<Oid, CachePolicy, Oid::Hasher>; + + CachePolicy m_RecordPolicy = CachePolicy::Default; + CachePolicy m_DefaultPayloadPolicy = CachePolicy::Default; + PayloadPolicyMap m_PayloadPolicies; +}; + +} // namespace zen |