aboutsummaryrefslogtreecommitdiff
path: root/src/zenserver-test/cacherequests.h
blob: 5b7a533906af0f88143ac03c5e9222d3e1aadc78 (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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
// Copyright Epic Games, Inc. All Rights Reserved.

#pragma once

#include <zencore/compress.h>

#include <zenstore/cache/cachekey.h>
#include <zenstore/cache/cachepolicy.h>

#include <functional>

namespace zen {

class CbPackage;
class CbObjectWriter;
class CbObjectView;

namespace cacherequests {
	// I'd really like to get rid of std::optional<CacheRecordPolicy> (or really the class CacheRecordPolicy)
	//
	// CacheRecordPolicy has a record level policy but it can also contain policies for individual
	// values inside the record.
	//
	// However, when we do a "PutCacheRecords" we already list the individual Values with their Id
	// so we can just as well use an optional plain CachePolicy for each value.
	//
	// In "GetCacheRecords" we do not currently as for the individual values but you can add
	// a policy on a per-value level in the std::optional<CacheRecordPolicy> Policy for each record.
	//
	// But as we already need to know the Ids of the values we want to set the policy for
	// it would be simpler to add an array of requested values which each has an optional policy.
	//
	// We could add:
	// struct GetCacheRecordValueRequest
	// {
	//     Oid						  Id;
	//     std::optional<CachePolicy> Policy;
	// };
	//
	// and change GetCacheRecordRequest to
	// struct GetCacheRecordRequest
	// {
	// 	CacheKey				                Key = CacheKey::Empty;
	//  std::vector<GetCacheRecordValueRequest> ValueRequests;
	// 	std::optional<CachePolicy>              Policy;
	// };
	//
	// This way we don't need the complex CacheRecordPolicy class and the request becomes
	// more uniform and easier to understand.
	//
	// Would need to decide what the ValueRequests actually mean:
	// Do they dictate which values to fetch or just a change of the policy?
	// If they dictate the values to fetch you need to know all the value ids to set them
	// and that is unlikely what we want - we want to be able to get a cache record with
	// all its values without knowing all the Ids, right?
	//

	//////////////////////////////////////////////////////////////////////////
	// Put 1..n structured cache records with optional attachments

	struct PutCacheRecordRequestValue
	{
		Oid				 Id		 = Oid::Zero;
		IoHash			 RawHash = IoHash::Zero;  // If Body is not set, this must be set and the value must already exist in cache
		CompressedBuffer Body	 = CompressedBuffer::Null;
	};

	struct PutCacheRecordRequest
	{
		CacheKey								Key = CacheKey::Empty;
		std::vector<PutCacheRecordRequestValue> Values;
		std::optional<CacheRecordPolicy>		Policy;
	};

	struct PutCacheRecordsRequest
	{
		uint32_t						   AcceptMagic	 = 0;
		CachePolicy						   DefaultPolicy = CachePolicy::Default;
		std::string						   Namespace;
		std::vector<PutCacheRecordRequest> Requests;

		bool Parse(const CbPackage& Package);
		bool Format(CbPackage& OutPackage) const;
	};

	struct PutCacheRecordsResult
	{
		std::vector<bool>		  Success;
		std::vector<CbObjectView> Details;

		bool Parse(const CbPackage& Package);
		bool Format(CbPackage& OutPackage) const;
	};

	//////////////////////////////////////////////////////////////////////////
	// Get 1..n structured cache records with optional attachments
	// We can get requests for a cache record where we want care about a particular
	// value id which we now of, but we don't know the ids of the other values and
	// we still want them.
	// Not sure if in that case we want different policies for the different attachemnts?

	struct GetCacheRecordRequest
	{
		CacheKey						 Key = CacheKey::Empty;
		std::optional<CacheRecordPolicy> Policy;
	};

	struct GetCacheRecordsRequest
	{
		uint32_t						   AcceptMagic	 = 0;
		uint16_t						   AcceptOptions = 0;
		int32_t							   ProcessPid	 = 0;
		CachePolicy						   DefaultPolicy = CachePolicy::Default;
		std::string						   Namespace;
		std::vector<GetCacheRecordRequest> Requests;

		bool Parse(const CbPackage& RpcRequest);
		bool Parse(const CbObjectView& RpcRequest);
		bool Format(CbPackage& OutPackage, const std::span<const size_t> OptionalRecordFilter = {}) const;
		bool Format(CbObjectWriter& Writer, const std::span<const size_t> OptionalRecordFilter = {}) const;
	};

	struct GetCacheRecordResultValue
	{
		Oid				 Id		 = Oid::Zero;
		IoHash			 RawHash = IoHash::Zero;
		uint64_t		 RawSize = 0;
		CompressedBuffer Body	 = CompressedBuffer::Null;
	};

	struct GetCacheRecordResult
	{
		CacheKey							   Key = CacheKey::Empty;
		std::vector<GetCacheRecordResultValue> Values;
	};

	struct GetCacheRecordsResult
	{
		std::vector<std::optional<GetCacheRecordResult>> Results;

		bool Parse(const CbPackage& Package, const std::span<const size_t> OptionalRecordResultIndexes = {});
		bool Format(CbPackage& OutPackage) const;
	};

	//////////////////////////////////////////////////////////////////////////
	// Put 1..n unstructured cache objects

	struct PutCacheValueRequest
	{
		CacheKey				   Key	   = CacheKey::Empty;
		IoHash					   RawHash = IoHash::Zero;
		CompressedBuffer		   Body	   = CompressedBuffer::Null;  // If not set the value is expected to already exist in cache store
		std::optional<CachePolicy> Policy;
	};

	struct PutCacheValuesRequest
	{
		uint32_t						  AcceptMagic	= 0;
		CachePolicy						  DefaultPolicy = CachePolicy::Default;
		std::string						  Namespace;
		std::vector<PutCacheValueRequest> Requests;

		bool Parse(const CbPackage& Package);
		bool Format(CbPackage& OutPackage) const;
	};

	struct PutCacheValuesResult
	{
		std::vector<bool> Success;

		bool Parse(const CbPackage& Package);
		bool Format(CbPackage& OutPackage) const;
	};

	//////////////////////////////////////////////////////////////////////////
	// Get 1..n unstructured cache objects (stored data may be structured or unstructured)

	struct GetCacheValueRequest
	{
		CacheKey				   Key = CacheKey::Empty;
		std::optional<CachePolicy> Policy;
	};

	struct GetCacheValuesRequest
	{
		uint32_t						  AcceptMagic	= 0;
		uint16_t						  AcceptOptions = 0;
		int32_t							  ProcessPid	= 0;
		CachePolicy						  DefaultPolicy = CachePolicy::Default;
		std::string						  Namespace;
		std::vector<GetCacheValueRequest> Requests;

		bool Parse(const CbObjectView& BatchObject);
		bool Format(CbPackage& OutPackage, const std::span<const size_t> OptionalValueFilter = {}) const;
	};

	struct CacheValueResult
	{
		uint64_t		 RawSize		= 0;
		uint64_t		 FragmentOffset = 0;
		IoHash			 FragmentHash	= IoHash::Zero;
		IoHash			 RawHash		= IoHash::Zero;
		CompressedBuffer Body			= CompressedBuffer::Null;
	};

	struct CacheValuesResult
	{
		std::vector<CacheValueResult> Results;

		bool Parse(const CbPackage& Package, const std::span<const size_t> OptionalValueResultIndexes = {});
		bool Format(CbPackage& OutPackage) const;
	};

	typedef CacheValuesResult GetCacheValuesResult;

	//////////////////////////////////////////////////////////////////////////
	// Get 1..n cache record values (attachments) for 1..n records

	struct GetCacheChunkRequest
	{
		CacheKey				   Key;
		Oid						   ValueId	 = Oid::Zero;  // Set if ChunkId is not known at request time
		IoHash					   ChunkId	 = IoHash::Zero;
		uint64_t				   RawOffset = 0ull;
		uint64_t				   RawSize	 = ~uint64_t(0);
		std::optional<CachePolicy> Policy;
	};

	struct GetCacheChunksRequest
	{
		uint32_t						  AcceptMagic	= 0;
		uint16_t						  AcceptOptions = 0;
		int32_t							  ProcessPid	= 0;
		CachePolicy						  DefaultPolicy = CachePolicy::Default;
		std::string						  Namespace;
		std::vector<GetCacheChunkRequest> Requests;

		bool Parse(const CbObjectView& BatchObject);
		bool Format(CbPackage& OutPackage) const;
	};

	typedef CacheValuesResult GetCacheChunksResult;

	//////////////////////////////////////////////////////////////////////////

	//	struct CacheRecordValue
	//	{
	//		Oid		 Id		 = Oid::Zero;
	//		IoHash	 RawHash = IoHash::Zero;
	//		uint64_t RawSize = 0;
	//	};
	//
	//	struct CacheRecord
	//	{
	//		CacheKey					  Key = CacheKey::Empty;
	//		std::vector<CacheRecordValue> Values;
	//
	//		bool Parse(CbObjectView& Reader);
	//		bool Format(CbObjectWriter& Writer) const;
	//	};

}  // namespace cacherequests

}  // namespace zen