aboutsummaryrefslogtreecommitdiff
path: root/zenstore/CAS.cpp
blob: 36cf8554910c1a0dd9f407c3581b866db384aa5f (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
// Copyright Epic Games, Inc. All Rights Reserved.

#include <zenstore/cas.h>

#include "compactcas.h"
#include "filecas.h"

#include <doctest/doctest.h>
#include <zencore/except.h>
#include <zencore/fmtutils.h>
#include <zencore/memory.h>
#include <zencore/string.h>
#include <zencore/thread.h>
#include <zencore/uid.h>

#include <spdlog/spdlog.h>

#include <gsl/gsl-lite.hpp>

#include <filesystem>
#include <functional>
#include <unordered_map>

struct IUnknown;  // Workaround for "combaseapi.h(229): error C2187: syntax error: 'identifier' was unexpected here" when using /permissive-
#include <atlfile.h>

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

namespace zen {

/**
 * Slightly less naive CAS store
 */
class CasImpl : public CasStore
{
public:
	CasImpl();
	virtual ~CasImpl();

	virtual void				   Initialize(const CasStoreConfiguration& InConfig) override;
	virtual CasStore::InsertResult InsertChunk(IoBuffer Chunk, const IoHash& ChunkHash) override;
	virtual IoBuffer			   FindChunk(const IoHash& ChunkHash) override;
	virtual void				   Flush() override;

private:
	void PickDefaultDirectory();

	CasContainerStrategy m_TinyStrategy;
	CasContainerStrategy m_SmallStrategy;
	FileCasStrategy		 m_LargeStrategy;
};

CasImpl::CasImpl() : m_TinyStrategy(m_Config, m_Stats), m_SmallStrategy(m_Config, m_Stats), m_LargeStrategy(m_Config, m_Stats)
{
}

CasImpl::~CasImpl()
{
}

void
CasImpl::Initialize(const CasStoreConfiguration& InConfig)
{
	m_Config = InConfig;

	spdlog::info("initializing CAS pool at {}", m_Config.RootDirectory);

	// Ensure root directory exists - create if it doesn't exist already

	std::filesystem::create_directories(m_Config.RootDirectory);

	// Open or create manifest

	bool IsNewStore = false;

	{
		std::filesystem::path ManifestPath = m_Config.RootDirectory;
		ManifestPath /= ".ucas_root";

		CAtlFile marker;
		HRESULT	 hRes = marker.Create(ManifestPath.c_str(), GENERIC_READ, 0, OPEN_EXISTING);

		if (FAILED(hRes))
		{
			IsNewStore = true;

			ExtendableStringBuilder<128> manifest;
			manifest.Append("#CAS_ROOT\n");	 // TODO: should write something meaningful here
			manifest.Append("ID=");
			zen::Oid id = zen::Oid::NewOid();
			id.ToString(manifest);

			hRes = marker.Create(ManifestPath.c_str(), GENERIC_WRITE, 0, CREATE_ALWAYS);

			if (SUCCEEDED(hRes))
				marker.Write(manifest.c_str(), (DWORD)manifest.Size());
		}
	}

	// Initialize payload storage

	m_TinyStrategy.Initialize("tobs", 16, IsNewStore);
	m_SmallStrategy.Initialize("sobs", 4096, IsNewStore);
}

CasStore::InsertResult
CasImpl::InsertChunk(IoBuffer Chunk, const IoHash& ChunkHash)
{
	const uint64_t ChunkSize = Chunk.Size();

	if (ChunkSize < m_Config.TinyValueThreshold)
	{
		ZEN_ASSERT(ChunkSize);

		return m_TinyStrategy.InsertChunk(Chunk, ChunkHash);
	}
	else if (ChunkSize < m_Config.HugeValueThreshold)
	{
		return m_SmallStrategy.InsertChunk(Chunk, ChunkHash);
	}

	return m_LargeStrategy.InsertChunk(Chunk, ChunkHash);
}

IoBuffer
CasImpl::FindChunk(const IoHash& ChunkHash)
{
	if (IoBuffer Found = m_SmallStrategy.FindChunk(ChunkHash))
	{
		return Found;
	}

	if (IoBuffer Found = m_TinyStrategy.FindChunk(ChunkHash))
	{
		return Found;
	}

	if (IoBuffer Found = m_LargeStrategy.FindChunk(ChunkHash))
	{
		return Found;
	}

	// Not found
	return IoBuffer{};
}

void
CasImpl::Flush()
{
	m_SmallStrategy.Flush();
	m_TinyStrategy.Flush();
	m_LargeStrategy.Flush();
}

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

CasStore*
CreateCasStore()
{
	return new CasImpl();
}

//////////////////////////////////////////////////////////////////////////
//
// Testing related code follows...
//

void
CAS_forcelink()
{
}

TEST_CASE("CasStore")
{
	zen::CasStoreConfiguration config;
	config.RootDirectory = "c:\\temp\\test";

	std::unique_ptr<zen::CasStore> store{CreateCasStore()};
	store->Initialize(config);
}

}  // namespace zen