aboutsummaryrefslogtreecommitdiff
path: root/zenserver/cache/structuredcache.h
blob: e9796999b8c5d770c2b022a5031d41e63c23e33e (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
// Copyright Epic Games, Inc. All Rights Reserved.

#pragma once

#include <zencore/httpserver.h>
#include <zencore/refcount.h>

#include "structuredcachestore.h"
#include "upstream/jupiter.h"

#include <spdlog/spdlog.h>

namespace zen {

class CloudCacheClient;
class CidStore;
class ZenStructuredCacheClient;

/**
 * Structured cache service. Imposes constraints on keys, supports blobs and
 * structured values
 *
 * Keys are structured as:
 *
 *   {BucketId}/{KeyHash}
 *
 * Where BucketId is an alphanumeric string, and KeyHash is a 40-character hexadecimal
 * sequence. The hash value may be derived in any number of ways, it's up to the
 * application to pick an approach.
 *
 * Values may be structured or unstructured. Structured values are encoded using Unreal
 * Engine's compact binary encoding
 *
 * Additionally, attachments may be addressed as:
 *
 *   {BucketId}/{KeyHash}/{PayloadHash}
 *
 * Where the two initial components are the same as for the main endpoint
 *
 * The storage strategy is as follows:
 *
 *  - Structured values are stored in a dedicated backing store per bucket
 *  - Unstructured values and attachments are stored in the CAS pool
 *
 */

class HttpStructuredCacheService : public zen::HttpService
{
public:
	HttpStructuredCacheService(std::filesystem::path RootPath, zen::CasStore& InStore, zen::CidStore& InCidStore);
	~HttpStructuredCacheService();

	virtual const char* BaseUri() const override;

	virtual void HandleRequest(zen::HttpServerRequest& Request) override;

	void Flush();

private:
	struct CacheRef
	{
		std::string BucketSegment;
		IoHash		HashKey;
		IoHash		PayloadId;
	};

	[[nodiscard]] bool ValidateUri(zen::HttpServerRequest& Request, CacheRef& OutRef);
	void			   HandleCacheRecordRequest(zen::HttpServerRequest& Request, CacheRef& Ref);
	void			   HandleCachePayloadRequest(zen::HttpServerRequest& Request, CacheRef& Ref);
	void			   HandleCacheBucketRequest(zen::HttpServerRequest& Request, std::string_view Bucket);

	spdlog::logger					 m_Log;
	zen::CasStore&					 m_CasStore;
	zen::CidStore&					 m_CidStore;
	ZenCacheStore					 m_CacheStore;
	RefPtr<CloudCacheClient>		 m_Cloud;
	RefPtr<ZenStructuredCacheClient> m_ZenClient;
};

}  // namespace zen