aboutsummaryrefslogtreecommitdiff
path: root/zenstore/blockstore.cpp
blob: a897ed90290f981fb970add510edcfb817aac7e4 (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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
// Copyright Epic Games, Inc. All Rights Reserved.

#include "compactcas.h"

#include <zencore/fmtutils.h>
#include <zencore/logging.h>
#include <zencore/scopeguard.h>
#include <zenstore/blockstore.h>

#if ZEN_WITH_TESTS
#	include <zencore/compactbinarybuilder.h>
#	include <zencore/testing.h>
#	include <zencore/testutils.h>
#	include <algorithm>
#	include <random>
#endif

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

namespace zen {

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

BlockStoreFile::BlockStoreFile(const std::filesystem::path& BlockPath) : m_Path(BlockPath)
{
}

BlockStoreFile::~BlockStoreFile()
{
	m_IoBuffer = IoBuffer();
	m_File.Detach();
}

const std::filesystem::path&
BlockStoreFile::GetPath() const
{
	return m_Path;
}

void
BlockStoreFile::Open()
{
	m_File.Open(m_Path, BasicFile::Mode::kDelete);
	void* FileHandle = m_File.Handle();
	m_IoBuffer		 = IoBuffer(IoBuffer::File, FileHandle, 0, m_File.FileSize());
}

void
BlockStoreFile::Create(uint64_t InitialSize)
{
	auto ParentPath = m_Path.parent_path();
	if (!std::filesystem::is_directory(ParentPath))
	{
		CreateDirectories(ParentPath);
	}

	m_File.Open(m_Path, BasicFile::Mode::kTruncateDelete);
	if (InitialSize > 0)
	{
		m_File.SetFileSize(InitialSize);
	}
	void* FileHandle = m_File.Handle();
	m_IoBuffer		 = IoBuffer(IoBuffer::File, FileHandle, 0, InitialSize);
}

uint64_t
BlockStoreFile::FileSize()
{
	return m_File.FileSize();
}

void
BlockStoreFile::MarkAsDeleteOnClose(std::error_code& Ec)
{
	m_File.MarkAsDeleteOnClose(Ec);
}

IoBuffer
BlockStoreFile::GetChunk(uint64_t Offset, uint64_t Size)
{
	return IoBuffer(m_IoBuffer, Offset, Size);
}

void
BlockStoreFile::Read(void* Data, uint64_t Size, uint64_t FileOffset)
{
	m_File.Read(Data, Size, FileOffset);
}

void
BlockStoreFile::Write(const void* Data, uint64_t Size, uint64_t FileOffset)
{
	m_File.Write(Data, Size, FileOffset);
}

void
BlockStoreFile::Truncate(uint64_t Size)
{
	m_File.SetFileSize(Size);
}

void
BlockStoreFile::Flush()
{
	m_File.Flush();
}

void
BlockStoreFile::StreamByteRange(uint64_t FileOffset, uint64_t Size, std::function<void(const void* Data, uint64_t Size)>&& ChunkFun)
{
	m_File.StreamByteRange(FileOffset, Size, std::move(ChunkFun));
}

namespace {
	const char* DataExtension = ".ucas";

	std::filesystem::path GetBlockPath(const std::filesystem::path& BlocksBasePath, const uint32_t BlockIndex)
	{
		ExtendablePathBuilder<256> Path;

		char BlockHexString[9];
		ToHexNumber(BlockIndex, BlockHexString);

		Path.Append(BlocksBasePath);
		Path.AppendSeparator();
		Path.AppendAsciiRange(BlockHexString, BlockHexString + 4);
		Path.AppendSeparator();
		Path.Append(BlockHexString);
		Path.Append(DataExtension);
		return Path.ToPath();
	}
}  // namespace

void
BlockStore::Initialize(const std::filesystem::path&			  BlocksBasePath,
					   uint64_t								  MaxBlockSize,
					   uint64_t								  MaxBlockCount,
					   const std::vector<BlockStoreLocation>& KnownLocations)
{
	ZEN_ASSERT(MaxBlockSize > 0);
	ZEN_ASSERT(MaxBlockCount > 0);
	ZEN_ASSERT(IsPow2(MaxBlockCount));

	m_BlocksBasePath = BlocksBasePath;
	m_MaxBlockSize	 = MaxBlockSize;

	m_TotalSize = 0;
	m_ChunkBlocks.clear();

	std::unordered_set<uint32_t> KnownBlocks;
	for (const auto& Entry : KnownLocations)
	{
		m_TotalSize.fetch_add(Entry.Size, std::memory_order_seq_cst);
		KnownBlocks.insert(Entry.BlockIndex);
	}

	if (std::filesystem::is_directory(m_BlocksBasePath))
	{
		std::vector<std::filesystem::path> FoldersToScan;
		FoldersToScan.push_back(m_BlocksBasePath);
		size_t FolderOffset = 0;
		while (FolderOffset < FoldersToScan.size())
		{
			for (const std::filesystem::directory_entry& Entry : std::filesystem::directory_iterator(FoldersToScan[FolderOffset]))
			{
				if (Entry.is_directory())
				{
					FoldersToScan.push_back(Entry.path());
					continue;
				}
				if (Entry.is_regular_file())
				{
					const std::filesystem::path Path = Entry.path();
					if (Path.extension() != DataExtension)
					{
						continue;
					}
					std::string FileName = Path.stem().string();
					uint32_t	BlockIndex;
					bool		OK = ParseHexNumber(FileName, BlockIndex);
					if (!OK)
					{
						continue;
					}
					if (!KnownBlocks.contains(BlockIndex))
					{
						// Log removing unreferenced block
						// Clear out unused blocks
						ZEN_INFO("removing unused block for '{}' at '{}'", m_BlocksBasePath, Path);
						std::error_code Ec;
						std::filesystem::remove(Path, Ec);
						if (Ec)
						{
							ZEN_WARN("Failed to delete file '{}' reason: '{}'", Path, Ec.message());
						}
						continue;
					}
					Ref<BlockStoreFile> BlockFile = new BlockStoreFile(Path);
					BlockFile->Open();
					m_ChunkBlocks[BlockIndex] = BlockFile;
				}
			}
			++FolderOffset;
		}
	}
	else
	{
		CreateDirectories(m_BlocksBasePath);
	}
}

BlockStoreLocation
BlockStore::WriteChunk(const void* Data, uint64_t Size, uint64_t Alignment)
{
	RwLock::ExclusiveLockScope InsertLock(m_InsertLock);

	uint32_t WriteBlockIndex = m_WriteBlockIndex.load(std::memory_order_acquire);
	bool	 IsWriting		 = m_WriteBlock != nullptr;
	if (!IsWriting || (m_CurrentInsertOffset + Size) > m_MaxBlockSize)
	{
		if (m_WriteBlock)
		{
			m_WriteBlock = nullptr;
		}
		{
			if (m_ChunkBlocks.size() == m_MaxBlockCount)
			{
				throw std::runtime_error(fmt::format("unable to allocate a new block in '{}'", m_BlocksBasePath));
			}
			WriteBlockIndex += IsWriting ? 1 : 0;
			while (m_ChunkBlocks.contains(WriteBlockIndex))
			{
				WriteBlockIndex = (WriteBlockIndex + 1) & (m_MaxBlockCount - 1);
			}
			std::filesystem::path BlockPath = GetBlockPath(m_BlocksBasePath, WriteBlockIndex);
			m_WriteBlock					= new BlockStoreFile(BlockPath);
			m_ChunkBlocks[WriteBlockIndex]	= m_WriteBlock;
			m_WriteBlockIndex.store(WriteBlockIndex, std::memory_order_release);
		}
		m_CurrentInsertOffset = 0;
		m_WriteBlock->Create(m_MaxBlockSize);
	}
	uint64_t InsertOffset		   = m_CurrentInsertOffset;
	m_CurrentInsertOffset		   = RoundUp(InsertOffset + Size, Alignment);
	Ref<BlockStoreFile> WriteBlock = m_WriteBlock;
	InsertLock.ReleaseNow();

	BlockStoreLocation Location{.BlockIndex = WriteBlockIndex, .Offset = InsertOffset, .Size = Size};
	WriteBlock->Write(Data, Size, InsertOffset);

	return Location;
}

/*
IoBuffer
BlockStore::ReadChunk(const BlockStoreLocation& Location)
{
	RwLock::SharedLockScope InsertLock(m_InsertLock);
	Ref<BlockStoreFile>		ChunkBlock = m_ChunkBlocks[Location.BlockIndex];
	InsertLock.ReleaseNow();
	return ChunkBlock->GetChunk(Location.Offset, Location.Size);
}
*/

Ref<BlockStoreFile>
BlockStore::GetChunkBlock(const BlockStoreLocation& Location)
{
	RwLock::SharedLockScope InsertLock(m_InsertLock);
	return m_ChunkBlocks[Location.BlockIndex];
}

void
BlockStore::Flush()
{
	RwLock::ExclusiveLockScope _(m_InsertLock);
	if (m_CurrentInsertOffset > 0)
	{
		uint32_t WriteBlockIndex = m_WriteBlockIndex.load(std::memory_order_acquire);
		WriteBlockIndex			 = (WriteBlockIndex + 1) & (m_MaxBlockCount - 1);
		m_WriteBlock			 = nullptr;
		m_WriteBlockIndex.store(WriteBlockIndex, std::memory_order_release);
		m_CurrentInsertOffset = 0;
	}
}

// TODO: Almost there - some bug remain and API might need tweaking
void
BlockStore::ReclaimSpace(const std::vector<BlockStoreLocation>& ChunkLocations,
						 const std::vector<size_t>&				KeepChunkIndexes,
						 uint64_t								PayloadAlignment,
						 bool									DryRun,
						 const ReclaimCallback&					Callback)
{
	if (ChunkLocations.empty())
	{
		return;
	}
	uint64_t WriteBlockTimeUs		 = 0;
	uint64_t WriteBlockLongestTimeUs = 0;
	uint64_t ReadBlockTimeUs		 = 0;
	uint64_t ReadBlockLongestTimeUs	 = 0;
	uint64_t TotalChunkCount		 = ChunkLocations.size();
	uint64_t DeletedSize			 = 0;
	uint64_t OldTotalSize			 = m_TotalSize.load(std::memory_order::relaxed);

	uint64_t MovedCount	  = 0;
	uint64_t DeletedCount = 0;

	Stopwatch  TotalTimer;
	const auto _ = MakeGuard([this,
							  &TotalTimer,
							  &WriteBlockTimeUs,
							  &WriteBlockLongestTimeUs,
							  &ReadBlockTimeUs,
							  &ReadBlockLongestTimeUs,
							  &TotalChunkCount,
							  &DeletedCount,
							  &MovedCount,
							  &DeletedSize,
							  OldTotalSize] {
		ZEN_INFO(
			"garbage collect for '{}' DONE after {}, write lock: {} ({}), read lock: {} ({}), collected {} bytes, deleted #{} and moved "
			"#{} "
			"of #{} "
			"chunks ({}).",
			m_BlocksBasePath,
			NiceTimeSpanMs(TotalTimer.GetElapsedTimeMs()),
			NiceLatencyNs(WriteBlockTimeUs),
			NiceLatencyNs(WriteBlockLongestTimeUs),
			NiceLatencyNs(ReadBlockTimeUs),
			NiceLatencyNs(ReadBlockLongestTimeUs),
			NiceBytes(DeletedSize),
			DeletedCount,
			MovedCount,
			TotalChunkCount,
			NiceBytes(OldTotalSize));
	});

	size_t	 BlockCount		   = 0;
	uint64_t ExcludeBlockIndex = 0x800000000ull;
	{
		RwLock::ExclusiveLockScope __(m_InsertLock);
		if (m_WriteBlock)
		{
			ExcludeBlockIndex = m_WriteBlockIndex.load(std::memory_order_acquire);
		}
		BlockCount = m_ChunkBlocks.size();
	}

	std::unordered_map<size_t, BlockStoreLocation> LocationLookup;
	LocationLookup.reserve(TotalChunkCount);

	std::unordered_set<size_t> KeepChunkMap;
	KeepChunkMap.reserve(KeepChunkIndexes.size());
	for (size_t KeepChunkIndex : KeepChunkIndexes)
	{
		const BlockStoreLocation& Location = ChunkLocations[KeepChunkIndex];
		if (Location.BlockIndex == ExcludeBlockIndex)
		{
			continue;
		}
		KeepChunkMap.insert(KeepChunkIndex);
	}
	std::unordered_set<size_t> DeleteChunkMap;
	DeleteChunkMap.reserve(ChunkLocations.size() - KeepChunkIndexes.size());

	std::unordered_map<uint32_t, size_t> BlockIndexToChunkMapIndex;
	std::vector<std::vector<size_t>>	 KeepChunks;
	std::vector<std::vector<size_t>>	 DeleteChunks;

	BlockIndexToChunkMapIndex.reserve(BlockCount);
	KeepChunks.reserve(BlockCount);
	DeleteChunks.reserve(BlockCount);
	size_t GuesstimateCountPerBlock = TotalChunkCount / BlockCount / 2;

	size_t	 DeleteCount  = 0;
	uint64_t NewTotalSize = 0;
	for (size_t Index = 0; Index < TotalChunkCount; ++Index)
	{
		const BlockStoreLocation& Location = ChunkLocations[Index];
		LocationLookup[Index]			   = Location;
		if (Location.BlockIndex == ExcludeBlockIndex)
		{
			continue;
		}

		auto   BlockIndexPtr = BlockIndexToChunkMapIndex.find(Location.BlockIndex);
		size_t ChunkMapIndex = 0;
		if (BlockIndexPtr == BlockIndexToChunkMapIndex.end())
		{
			ChunkMapIndex								   = KeepChunks.size();
			BlockIndexToChunkMapIndex[Location.BlockIndex] = ChunkMapIndex;
			KeepChunks.resize(ChunkMapIndex + 1);
			KeepChunks.back().reserve(GuesstimateCountPerBlock);
			DeleteChunks.resize(ChunkMapIndex + 1);
			DeleteChunks.back().reserve(GuesstimateCountPerBlock);
		}
		else
		{
			ChunkMapIndex = BlockIndexPtr->second;
		}

		if (KeepChunkMap.contains(Index))
		{
			std::vector<size_t>& IndexMap = KeepChunks[ChunkMapIndex];
			IndexMap.push_back(Index);
			NewTotalSize += Location.Size;
			continue;
		}
		std::vector<size_t>& IndexMap = DeleteChunks[ChunkMapIndex];
		IndexMap.push_back(Index);
		DeleteCount++;
	}

	std::unordered_set<uint32_t> BlocksToReWrite;
	BlocksToReWrite.reserve(BlockIndexToChunkMapIndex.size());
	for (const auto& Entry : BlockIndexToChunkMapIndex)
	{
		uint32_t				   BlockIndex	 = Entry.first;
		size_t					   ChunkMapIndex = Entry.second;
		const std::vector<size_t>& ChunkMap		 = DeleteChunks[ChunkMapIndex];
		if (ChunkMap.empty())
		{
			continue;
		}
		BlocksToReWrite.insert(BlockIndex);
	}

	if (DryRun)
	{
		uint64_t TotalSize = m_TotalSize.load(std::memory_order_relaxed);
		ZEN_INFO("garbage collect for '{}' DISABLED, found #{} {} chunks of total #{} {}",
				 m_BlocksBasePath,
				 DeleteCount,
				 NiceBytes(TotalSize - NewTotalSize),
				 TotalChunkCount,
				 NiceBytes(TotalSize));
		return;
	}

	std::unordered_map<size_t, BlockStoreLocation> MovedChunks;
	std::vector<size_t>							   RemovedChunks;

	Ref<BlockStoreFile> NewBlockFile;
	uint64_t			WriteOffset	  = 0;
	uint32_t			NewBlockIndex = 0;

	for (uint32_t BlockIndex : BlocksToReWrite)
	{
		const size_t ChunkMapIndex = BlockIndexToChunkMapIndex[BlockIndex];

		Ref<BlockStoreFile> OldBlockFile;
		{
			RwLock::SharedLockScope _i(m_InsertLock);
			OldBlockFile = m_ChunkBlocks[BlockIndex];
			ZEN_ASSERT(OldBlockFile);
		}

		const std::vector<size_t>& KeepMap = KeepChunks[ChunkMapIndex];
		if (KeepMap.empty())
		{
			const std::vector<size_t>& DeleteMap = DeleteChunks[ChunkMapIndex];
			for (size_t DeleteIndex : DeleteMap)
			{
				RemovedChunks.push_back(DeleteIndex);
				DeletedSize += ChunkLocations[DeleteIndex].Size;
				DeletedCount++;
			}
			Callback(MovedChunks, RemovedChunks);
			MovedChunks.clear();
			RemovedChunks.clear();
			{
				RwLock::ExclusiveLockScope _i(m_InsertLock);
				Stopwatch				   Timer;
				const auto				   __ = MakeGuard([&Timer, &WriteBlockTimeUs, &WriteBlockLongestTimeUs] {
					uint64_t ElapsedUs = Timer.GetElapsedTimeUs();
					WriteBlockTimeUs += ElapsedUs;
					WriteBlockLongestTimeUs = std::max(ElapsedUs, WriteBlockLongestTimeUs);
				});
				m_ChunkBlocks[BlockIndex]	  = nullptr;
			}
			ZEN_DEBUG("marking cas store file in '{}' for delete , block #{}, '{}'", m_BlocksBasePath, BlockIndex, OldBlockFile->GetPath());
			std::error_code Ec;
			OldBlockFile->MarkAsDeleteOnClose(Ec);
			if (Ec)
			{
				ZEN_WARN("Failed to flag file '{}' for deletion: '{}'", OldBlockFile->GetPath(), Ec.message());
			}
			continue;
		}

		std::vector<uint8_t> Chunk;
		for (const size_t& ChunkIndex : KeepMap)
		{
			const BlockStoreLocation ChunkLocation = ChunkLocations[ChunkIndex];
			Chunk.resize(ChunkLocation.Size);
			OldBlockFile->Read(Chunk.data(), Chunk.size(), ChunkLocation.Offset);

			if (!NewBlockFile || (WriteOffset + Chunk.size() > m_MaxBlockSize))
			{
				uint32_t NextBlockIndex = m_WriteBlockIndex.load(std::memory_order_relaxed);

				if (NewBlockFile)
				{
					NewBlockFile->Truncate(WriteOffset);
					NewBlockFile->Flush();
				}
				{
					Callback(MovedChunks, RemovedChunks);
					MovedChunks.clear();
					RemovedChunks.clear();
					RwLock::ExclusiveLockScope __(m_InsertLock);
					Stopwatch				   Timer;
					const auto				   ___ = MakeGuard([&Timer, &WriteBlockTimeUs, &WriteBlockLongestTimeUs] {
						uint64_t ElapsedUs = Timer.GetElapsedTimeUs();
						WriteBlockTimeUs += ElapsedUs;
						WriteBlockLongestTimeUs = std::max(ElapsedUs, WriteBlockLongestTimeUs);
					});
					if (m_ChunkBlocks.size() == m_MaxBlockCount)
					{
						ZEN_ERROR("unable to allocate a new block in '{}', count limit {} exeeded",
								  m_BlocksBasePath,
								  static_cast<uint64_t>(std::numeric_limits<uint32_t>::max()) + 1);
						return;
					}
					while (m_ChunkBlocks.contains(NextBlockIndex))
					{
						NextBlockIndex = (NextBlockIndex + 1) & (m_MaxBlockCount - 1);
					}
					std::filesystem::path NewBlockPath = GetBlockPath(m_BlocksBasePath, NextBlockIndex);
					NewBlockFile					   = new BlockStoreFile(NewBlockPath);
					m_ChunkBlocks[NextBlockIndex]	   = NewBlockFile;
				}

				std::error_code Error;
				DiskSpace		Space = DiskSpaceInfo(m_BlocksBasePath, Error);
				if (Error)
				{
					ZEN_ERROR("get disk space in '{}' FAILED, reason: '{}'", m_BlocksBasePath, Error.message());
					return;
				}
				if (Space.Free < m_MaxBlockSize)
				{
					uint64_t ReclaimedSpace = 0;  // GcCtx.ClaimGCReserve();
					if (Space.Free + ReclaimedSpace < m_MaxBlockSize)
					{
						ZEN_WARN("garbage collect for '{}' FAILED, required disk space {}, free {}",
								 m_BlocksBasePath,
								 m_MaxBlockSize,
								 NiceBytes(Space.Free + ReclaimedSpace));
						RwLock::ExclusiveLockScope _l(m_InsertLock);
						Stopwatch				   Timer;
						const auto				   __ = MakeGuard([&Timer, &WriteBlockTimeUs, &WriteBlockLongestTimeUs] {
							uint64_t ElapsedUs = Timer.GetElapsedTimeUs();
							WriteBlockTimeUs += ElapsedUs;
							WriteBlockLongestTimeUs = std::max(ElapsedUs, WriteBlockLongestTimeUs);
						});
						m_ChunkBlocks.erase(NextBlockIndex);
						return;
					}

					ZEN_INFO("using gc reserve for '{}', reclaimed {}, disk free {}",
							 m_BlocksBasePath,
							 ReclaimedSpace,
							 NiceBytes(Space.Free + ReclaimedSpace));
				}
				NewBlockFile->Create(m_MaxBlockSize);
				NewBlockIndex = NextBlockIndex;
				WriteOffset	  = 0;
			}

			NewBlockFile->Write(Chunk.data(), Chunk.size(), WriteOffset);
			MovedChunks[ChunkIndex] = {.BlockIndex = NewBlockIndex, .Offset = WriteOffset, .Size = Chunk.size()};
			WriteOffset				= RoundUp(WriteOffset + Chunk.size(), PayloadAlignment);
			MovedCount++;
		}
		Chunk.clear();
		if (NewBlockFile)
		{
			NewBlockFile->Truncate(WriteOffset);
			NewBlockFile->Flush();
			NewBlockFile = {};
		}

		const std::vector<size_t>& DeleteMap = DeleteChunks[ChunkMapIndex];
		for (size_t DeleteIndex : DeleteMap)
		{
			RemovedChunks.push_back(DeleteIndex);
			DeletedSize += ChunkLocations[DeleteIndex].Size;
			DeletedCount++;
		}

		Callback(MovedChunks, RemovedChunks);
		MovedChunks.clear();
		RemovedChunks.clear();
		{
			RwLock::ExclusiveLockScope __(m_InsertLock);
			Stopwatch				   Timer;
			const auto				   ___ = MakeGuard([&Timer, &ReadBlockTimeUs, &ReadBlockLongestTimeUs] {
				uint64_t ElapsedUs = Timer.GetElapsedTimeUs();
				ReadBlockTimeUs += ElapsedUs;
				ReadBlockLongestTimeUs = std::max(ElapsedUs, ReadBlockLongestTimeUs);
			});
			m_ChunkBlocks[BlockIndex]	   = nullptr;
		}
		ZEN_DEBUG("marking cas store file in '{}' for delete , block #{}, '{}'", m_BlocksBasePath, BlockIndex, OldBlockFile->GetPath());
		std::error_code Ec;
		OldBlockFile->MarkAsDeleteOnClose(Ec);
		if (Ec)
		{
			ZEN_WARN("Failed to flag file '{}' for deletion: '{}'", OldBlockFile->GetPath(), Ec.message());
		}
		OldBlockFile = nullptr;
	}

	return;
}

#if ZEN_WITH_TESTS

static bool
operator==(const BlockStoreLocation& Lhs, const BlockStoreLocation& Rhs)
{
	return Lhs.BlockIndex == Rhs.BlockIndex && Lhs.Offset == Rhs.Offset && Lhs.Size == Rhs.Size;
}

TEST_CASE("blockstore.blockstoredisklocation")
{
	BlockStoreLocation Zero = BlockStoreLocation{.BlockIndex = 0, .Offset = 0, .Size = 0};
	CHECK(Zero == BlockStoreDiskLocation(Zero, 4).Get(4));

	BlockStoreLocation MaxBlockIndex = BlockStoreLocation{.BlockIndex = BlockStoreDiskLocation::MaxBlockIndex, .Offset = 0, .Size = 0};
	CHECK(MaxBlockIndex == BlockStoreDiskLocation(MaxBlockIndex, 4).Get(4));

	BlockStoreLocation MaxOffset = BlockStoreLocation{.BlockIndex = 0, .Offset = BlockStoreDiskLocation::MaxOffset * 4, .Size = 0};
	CHECK(MaxOffset == BlockStoreDiskLocation(MaxOffset, 4).Get(4));

	BlockStoreLocation MaxSize = BlockStoreLocation{.BlockIndex = 0, .Offset = 0, .Size = std::numeric_limits<uint32_t>::max()};
	CHECK(MaxSize == BlockStoreDiskLocation(MaxSize, 4).Get(4));

	BlockStoreLocation MaxBlockIndexAndOffset =
		BlockStoreLocation{.BlockIndex = BlockStoreDiskLocation::MaxBlockIndex, .Offset = BlockStoreDiskLocation::MaxOffset * 4, .Size = 0};
	CHECK(MaxBlockIndexAndOffset == BlockStoreDiskLocation(MaxBlockIndexAndOffset, 4).Get(4));

	BlockStoreLocation MaxAll = BlockStoreLocation{.BlockIndex = BlockStoreDiskLocation::MaxBlockIndex,
												   .Offset	   = BlockStoreDiskLocation::MaxOffset * 4,
												   .Size	   = std::numeric_limits<uint32_t>::max()};
	CHECK(MaxAll == BlockStoreDiskLocation(MaxAll, 4).Get(4));

	BlockStoreLocation MaxAll4096 = BlockStoreLocation{.BlockIndex = BlockStoreDiskLocation::MaxBlockIndex,
													   .Offset	   = BlockStoreDiskLocation::MaxOffset * 4096,
													   .Size	   = std::numeric_limits<uint32_t>::max()};
	CHECK(MaxAll4096 == BlockStoreDiskLocation(MaxAll4096, 4096).Get(4096));

	BlockStoreLocation Middle = BlockStoreLocation{.BlockIndex = (BlockStoreDiskLocation::MaxBlockIndex) / 2,
												   .Offset	   = ((BlockStoreDiskLocation::MaxOffset) / 2) * 4,
												   .Size	   = std::numeric_limits<uint32_t>::max() / 2};
	CHECK(Middle == BlockStoreDiskLocation(Middle, 4).Get(4));
}

TEST_CASE("blockstore.blockfile")
{
	ScopedTemporaryDirectory TempDir;
	auto					 RootDirectory = TempDir.Path() / "blocks";
	CreateDirectories(RootDirectory);

	{
		BlockStoreFile File1(RootDirectory / "1");
		File1.Create(16384);
		CHECK(File1.FileSize() == 16384);
		File1.Write("data", 5, 0);
		IoBuffer DataChunk = File1.GetChunk(0, 5);
		File1.Write("boop", 5, 5);
		IoBuffer	BoopChunk = File1.GetChunk(5, 5);
		const char* Data	  = static_cast<const char*>(DataChunk.GetData());
		CHECK(std::string(Data) == "data");
		const char* Boop = static_cast<const char*>(BoopChunk.GetData());
		CHECK(std::string(Boop) == "boop");
		File1.Flush();
	}
	{
		BlockStoreFile File1(RootDirectory / "1");
		File1.Open();

		char DataRaw[5];
		File1.Read(DataRaw, 5, 0);
		CHECK(std::string(DataRaw) == "data");
		IoBuffer DataChunk = File1.GetChunk(0, 5);

		char BoopRaw[5];
		File1.Read(BoopRaw, 5, 5);
		CHECK(std::string(BoopRaw) == "boop");

		IoBuffer	BoopChunk = File1.GetChunk(5, 5);
		const char* Data	  = static_cast<const char*>(DataChunk.GetData());
		CHECK(std::string(Data) == "data");
		const char* Boop = static_cast<const char*>(BoopChunk.GetData());
		CHECK(std::string(Boop) == "boop");
	}

	{
		IoBuffer DataChunk;
		IoBuffer BoopChunk;

		{
			BlockStoreFile File1(RootDirectory / "1");
			File1.Open();
			DataChunk = File1.GetChunk(0, 5);
			BoopChunk = File1.GetChunk(5, 5);
		}

		CHECK(std::filesystem::exists(RootDirectory / "1"));

		const char* Data = static_cast<const char*>(DataChunk.GetData());
		CHECK(std::string(Data) == "data");
		const char* Boop = static_cast<const char*>(BoopChunk.GetData());
		CHECK(std::string(Boop) == "boop");
	}
	CHECK(std::filesystem::exists(RootDirectory / "1"));

	{
		IoBuffer DataChunk;
		IoBuffer BoopChunk;

		{
			BlockStoreFile File1(RootDirectory / "1");
			File1.Open();
			std::error_code Ec;
			File1.MarkAsDeleteOnClose(Ec);
			CHECK(!Ec);
			DataChunk = File1.GetChunk(0, 5);
			BoopChunk = File1.GetChunk(5, 5);
		}

		const char* Data = static_cast<const char*>(DataChunk.GetData());
		CHECK(std::string(Data) == "data");
		const char* Boop = static_cast<const char*>(BoopChunk.GetData());
		CHECK(std::string(Boop) == "boop");
	}
	CHECK(!std::filesystem::exists(RootDirectory / "1"));
}

#endif

void
blockstore_forcelink()
{
}

}  // namespace zen