aboutsummaryrefslogtreecommitdiff
path: root/zenserver/cache/cachekey.h
blob: c32f7ed876c0c6636a91ad862009e685b32f584e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// Copyright Epic Games, Inc. All Rights Reserved.

#pragma once

#include <zencore/iohash.h>
#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 : uint8_t
{
	None			= 0,
	QueryLocal		= 1 << 0,
	QueryRemote		= 1 << 1,
	Query			= QueryLocal | QueryRemote,
	StoreLocal		= 1 << 2,
	StoreRemote		= 1 << 3,
	Store			= StoreLocal | StoreRemote,
	SkipMeta		= 1 << 4,
	SkipValue		= 1 << 5,
	SkipAttachments = 1 << 6,
	SkipData		= SkipMeta | SkipValue | SkipAttachments,
	SkipLocalCopy	= 1 << 7,
	Local			= QueryLocal | StoreLocal,
	Remote			= QueryRemote | StoreRemote,
	Default			= Query | Store,
	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;

	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;
};

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