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

#pragma once

#include <zencore/iobuffer.h>
#include <zencore/iohash.h>
#include <zenstore/gc.h>

#include <gsl/gsl-lite.hpp>
#include <unordered_map>

namespace zen {

namespace access_tracking {

	struct KeyAccessTime
	{
		IoHash		  Key;
		GcClock::Tick LastAccess{};
	};

	struct AccessTimes
	{
		std::unordered_map<std::string, std::vector<KeyAccessTime>> Buckets;
	};
};	// namespace access_tracking

struct ZenCacheValue
{
	IoBuffer Value;
	uint64_t RawSize = 0;
	IoHash	 RawHash = IoHash::Zero;
};

struct CacheValueDetails
{
	struct ValueDetails
	{
		uint64_t			Size;
		uint64_t			RawSize;
		IoHash				RawHash;
		GcClock::Tick		LastAccess{};
		std::vector<IoHash> Attachments;
		ZenContentType		ContentType;
	};

	struct BucketDetails
	{
		std::unordered_map<IoHash, ValueDetails, IoHash::Hasher> Values;
	};

	struct NamespaceDetails
	{
		std::unordered_map<std::string, BucketDetails> Buckets;
	};

	std::unordered_map<std::string, NamespaceDetails> Namespaces;
};

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

// This store the access time as seconds since epoch internally in a 32-bit value giving is a range of 136 years since epoch
struct AccessTime
{
	explicit AccessTime(GcClock::Tick Tick) noexcept : SecondsSinceEpoch(ToSeconds(Tick)) {}
	AccessTime& operator=(GcClock::Tick Tick) noexcept
	{
		SecondsSinceEpoch.store(ToSeconds(Tick), std::memory_order_relaxed);
		return *this;
	}
	operator GcClock::Tick() const noexcept
	{
		return std::chrono::duration_cast<GcClock::Duration>(std::chrono::seconds(SecondsSinceEpoch.load(std::memory_order_relaxed)))
			.count();
	}

	AccessTime(AccessTime&& Rhs) noexcept : SecondsSinceEpoch(Rhs.SecondsSinceEpoch.load(std::memory_order_relaxed)) {}
	AccessTime(const AccessTime& Rhs) noexcept : SecondsSinceEpoch(Rhs.SecondsSinceEpoch.load(std::memory_order_relaxed)) {}
	AccessTime& operator=(AccessTime&& Rhs) noexcept
	{
		SecondsSinceEpoch.store(Rhs.SecondsSinceEpoch.load(std::memory_order_relaxed), std::memory_order_relaxed);
		return *this;
	}
	AccessTime& operator=(const AccessTime& Rhs) noexcept
	{
		SecondsSinceEpoch.store(Rhs.SecondsSinceEpoch.load(std::memory_order_relaxed), std::memory_order_relaxed);
		return *this;
	}

private:
	static uint32_t ToSeconds(GcClock::Tick Tick)
	{
		return gsl::narrow<uint32_t>(std::chrono::duration_cast<std::chrono::seconds>(GcClock::Duration(Tick)).count());
	}
	std::atomic_uint32_t SecondsSinceEpoch;
};

}  // namespace zen