aboutsummaryrefslogtreecommitdiff
path: root/zenstore/compactcas.h
blob: 101e6b1b777f9eb7246ea034734544d969d65229 (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
// Copyright Epic Games, Inc. All Rights Reserved.

#pragma once

#include <zencore/zencore.h>

#include <zencore/iobuffer.h>
#include <zencore/iohash.h>
#include <zencore/string.h>
#include <zencore/thread.h>
#include <zencore/uid.h>
#include <zencore/windows.h>
#include <zenstore/basicfile.h>
#include <zenstore/cas.h>
#include <zenstore/caslog.h>

namespace zen {

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

#pragma pack(push)
#pragma pack(1)

struct CasDiskLocation
{
	uint64_t Offset;
	// If we wanted to be able to store larger chunks using this storage mechanism then
	// we could make this more like the IoStore index so we can store larger chunks.
	// I.e use five bytes for size and seven for offset
	uint32_t Size;
};

struct CasDiskIndexEntry
{
	IoHash			Key;
	CasDiskLocation Location;
};

#pragma pack(pop)

static_assert(sizeof(CasDiskIndexEntry) == 32);

/** This implements a storage strategy for small CAS values
 *
 * New chunks are simply appended to a small object file, and an index is
 * maintained to allow chunks to be looked up within the active small object
 * files
 *
 */

struct CasContainerStrategy
{
	CasContainerStrategy(const CasStoreConfiguration& Config);
	~CasContainerStrategy();

	CasStore::InsertResult InsertChunk(const void* ChunkData, size_t ChunkSize, const IoHash& ChunkHash);
	CasStore::InsertResult InsertChunk(IoBuffer Chunk, const IoHash& chunkHash);
	IoBuffer			   FindChunk(const IoHash& ChunkHash);
	bool				   HaveChunk(const IoHash& ChunkHash);
	void				   FilterChunks(CasChunkSet& InOutChunks);
	void				   Initialize(const std::string_view ContainerBaseName, uint64_t Alignment, bool IsNewStore);
	void				   Flush();
	void				   Scrub(ScrubContext& Ctx);

private:
	const CasStoreConfiguration&   m_Config;
	uint64_t					   m_PayloadAlignment = 1 << 4;
	bool						   m_IsInitialized	  = false;
	BasicFile					   m_SmallObjectFile;
	BasicFile					   m_SmallObjectIndex;
	TCasLogFile<CasDiskIndexEntry> m_CasLog;

	RwLock														m_LocationMapLock;
	std::unordered_map<IoHash, CasDiskLocation, IoHash::Hasher> m_LocationMap;
	RwLock														m_InsertLock;  // used to serialize inserts
	std::atomic<uint64_t>										m_CurrentInsertOffset = 0;
	std::atomic<uint64_t>										m_CurrentIndexOffset  = 0;

	void MakeSnapshot();
};

}  // namespace zen