aboutsummaryrefslogtreecommitdiff
path: root/zenserver/cache/structuredcachestore.cpp
blob: 2b213b9b0e6e075e8b9fd2320d26c56c27926bcc (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
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
// Copyright Epic Games, Inc. All Rights Reserved.

#include "structuredcachestore.h"

#include "cachetracking.h"

#include <zencore/compactbinarybuilder.h>
#include <zencore/compactbinarypackage.h>
#include <zencore/compactbinaryvalidation.h>
#include <zencore/compress.h>
#include <zencore/except.h>
#include <zencore/filesystem.h>
#include <zencore/fmtutils.h>
#include <zencore/logging.h>
#include <zencore/scopeguard.h>
#include <zencore/string.h>
#include <zencore/testing.h>
#include <zencore/testutils.h>
#include <zencore/thread.h>
#include <zencore/windows.h>
#include <zenstore/basicfile.h>
#include <zenstore/caslog.h>
#include <zenstore/cidstore.h>

#include <chrono>
#include <concepts>
#include <memory_resource>
#include <ranges>

ZEN_THIRD_PARTY_INCLUDES_START
#include <fmt/core.h>
#include <gsl/gsl-lite.hpp>
ZEN_THIRD_PARTY_INCLUDES_END

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

namespace zen {

using namespace fmt::literals;

static CbObject
LoadCompactBinaryObject(const std::filesystem::path& Path)
{
	FileContents Result = ReadFile(Path);

	if (!Result.ErrorCode)
	{
		IoBuffer Buffer = Result.Flatten();
		if (CbValidateError Error = ValidateCompactBinary(Buffer, CbValidateMode::All); Error == CbValidateError::None)
		{
			return LoadCompactBinaryObject(Buffer);
		}
	}

	return CbObject();
}

static void
SaveCompactBinaryObject(const std::filesystem::path& Path, const CbObject& Object)
{
	WriteFile(Path, Object.GetBuffer().AsIoBuffer());
}

ZenCacheStore::ZenCacheStore(CasGc& Gc, const std::filesystem::path& RootDir) : GcContributor(Gc), m_DiskLayer(RootDir)
{
	ZEN_INFO("initializing structured cache at '{}'", RootDir);
	CreateDirectories(RootDir);

	m_DiskLayer.DiscoverBuckets();

	m_AccessTracker.reset(new ZenCacheTracker(RootDir));
}

ZenCacheStore::~ZenCacheStore()
{
}

bool
ZenCacheStore::Get(std::string_view InBucket, const IoHash& HashKey, ZenCacheValue& OutValue)
{
	bool Ok = m_MemLayer.Get(InBucket, HashKey, OutValue);

	auto _ = MakeGuard([&] {
		if (!Ok)
			return;

		m_AccessTracker->TrackAccess(InBucket, HashKey);
	});

	if (Ok)
	{
		ZEN_ASSERT(OutValue.Value.Size());

		return true;
	}

	Ok = m_DiskLayer.Get(InBucket, HashKey, OutValue);

	if (Ok)
	{
		ZEN_ASSERT(OutValue.Value.Size());

		if (OutValue.Value.Size() <= m_DiskLayerSizeThreshold)
		{
			m_MemLayer.Put(InBucket, HashKey, OutValue);
		}
	}

	return Ok;
}

void
ZenCacheStore::Put(std::string_view InBucket, const IoHash& HashKey, const ZenCacheValue& Value)
{
	// Store value and index

	ZEN_ASSERT(Value.Value.Size());

	m_DiskLayer.Put(InBucket, HashKey, Value);

#if ZEN_USE_REF_TRACKING
	if (Value.Value.GetContentType() == ZenContentType::kCbObject)
	{
		if (ValidateCompactBinary(Value.Value, CbValidateMode::All) == CbValidateError::None)
		{
			CbObject Object{SharedBuffer(Value.Value)};

			uint8_t								TempBuffer[8 * sizeof(IoHash)];
			std::pmr::monotonic_buffer_resource Linear{TempBuffer, sizeof TempBuffer};
			std::pmr::polymorphic_allocator		Allocator{&Linear};
			std::pmr::vector<IoHash>			CidReferences{Allocator};

			Object.IterateAttachments([&](CbFieldView Field) { CidReferences.push_back(Field.AsAttachment()); });

			m_Gc.OnNewCidReferences(CidReferences);
		}
	}
#endif

	if (Value.Value.Size() <= m_DiskLayerSizeThreshold)
	{
		m_MemLayer.Put(InBucket, HashKey, Value);
	}
}

bool
ZenCacheStore::DropBucket(std::string_view Bucket)
{
	ZEN_INFO("dropping bucket '{}'", Bucket);

	// TODO: should ensure this is done atomically across all layers

	const bool MemDropped  = m_MemLayer.DropBucket(Bucket);
	const bool DiskDropped = m_DiskLayer.DropBucket(Bucket);
	const bool AnyDropped  = MemDropped || DiskDropped;

	ZEN_INFO("bucket '{}' was {}", Bucket, AnyDropped ? "dropped" : "not found");

	return AnyDropped;
}

void
ZenCacheStore::Flush()
{
	m_DiskLayer.Flush();
}

void
ZenCacheStore::Scrub(ScrubContext& Ctx)
{
	if (m_LastScrubTime == Ctx.ScrubTimestamp())
	{
		return;
	}

	m_LastScrubTime = Ctx.ScrubTimestamp();

	m_DiskLayer.Scrub(Ctx);
	m_MemLayer.Scrub(Ctx);
}

void
ZenCacheStore::GatherReferences(GcContext& GcCtx)
{
	m_MemLayer.GatherReferences(GcCtx);
	m_DiskLayer.GatherReferences(GcCtx);
}

ZenCacheSize
ZenCacheStore::TotalSize() const
{
	return {.MemorySize = m_MemLayer.TotalSize(), .DiskSize = m_DiskLayer.TotalSize()};
}

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

ZenCacheMemoryLayer::ZenCacheMemoryLayer()
{
}

ZenCacheMemoryLayer::~ZenCacheMemoryLayer()
{
}

bool
ZenCacheMemoryLayer::Get(std::string_view InBucket, const IoHash& HashKey, ZenCacheValue& OutValue)
{
	RwLock::SharedLockScope _(m_Lock);

	auto it = m_Buckets.find(std::string(InBucket));

	if (it == m_Buckets.end())
	{
		return false;
	}

	CacheBucket* Bucket = &it->second;

	_.ReleaseNow();

	// There's a race here. Since the lock is released early to allow
	// inserts, the bucket delete path could end up deleting the
	// underlying data structure

	return Bucket->Get(HashKey, OutValue);
}

void
ZenCacheMemoryLayer::Put(std::string_view InBucket, const IoHash& HashKey, const ZenCacheValue& Value)
{
	CacheBucket* Bucket = nullptr;

	{
		RwLock::SharedLockScope _(m_Lock);

		auto it = m_Buckets.find(std::string(InBucket));

		if (it != m_Buckets.end())
		{
			Bucket = &it->second;
		}
	}

	if (Bucket == nullptr)
	{
		// New bucket

		RwLock::ExclusiveLockScope _(m_Lock);

		Bucket = &m_Buckets[std::string(InBucket)];
	}

	// Note that since the underlying IoBuffer is retained, the content type is also

	Bucket->Put(HashKey, Value);
}

bool
ZenCacheMemoryLayer::DropBucket(std::string_view Bucket)
{
	RwLock::ExclusiveLockScope _(m_Lock);

	return !!m_Buckets.erase(std::string(Bucket));
}

void
ZenCacheMemoryLayer::Scrub(ScrubContext& Ctx)
{
	RwLock::SharedLockScope _(m_Lock);

	for (auto& Kv : m_Buckets)
	{
		Kv.second.Scrub(Ctx);
	}
}

void
ZenCacheMemoryLayer::GatherReferences(GcContext& GcCtx)
{
	RwLock::SharedLockScope _(m_Lock);

	for (auto& Kv : m_Buckets)
	{
		Kv.second.GatherReferences(GcCtx);
	}
}

uint64_t
ZenCacheMemoryLayer::TotalSize() const
{
	uint64_t				TotalSize{};
	RwLock::SharedLockScope _(m_Lock);

	for (auto& Kv : m_Buckets)
	{
		TotalSize += Kv.second.TotalSize();
	}

	return TotalSize;
}

void
ZenCacheMemoryLayer::CacheBucket::Scrub(ScrubContext& Ctx)
{
	RwLock::SharedLockScope _(m_bucketLock);

	std::vector<IoHash> BadHashes;

	for (auto& Kv : m_cacheMap)
	{
		if (Kv.first != IoHash::HashBuffer(Kv.second.Payload))
		{
			BadHashes.push_back(Kv.first);
		}
	}

	if (!BadHashes.empty())
	{
		Ctx.ReportBadCasChunks(BadHashes);
	}
}

void
ZenCacheMemoryLayer::CacheBucket::GatherReferences(GcContext& GcCtx)
{
	// Is it even meaningful to do this? The memory layer shouldn't
	// contain anything which is not already in the disk layer

	RwLock::SharedLockScope _(m_bucketLock);

	std::vector<IoHash> Cids;

	for (const auto& Kv : m_cacheMap)
	{
		const IoBuffer& Payload = Kv.second.Payload;
		if (Payload.GetContentType() != ZenContentType::kCbObject || GcCtx.Expired(Kv.second.LastAccess))
		{
			continue;
		}

		Cids.clear();
		CbObject Obj(SharedBuffer{Payload});
		Obj.IterateAttachments([&Cids](CbFieldView Field) { Cids.push_back(Field.AsAttachment()); });
		GcCtx.ContributeCids(Cids);
	}
}

bool
ZenCacheMemoryLayer::CacheBucket::Get(const IoHash& HashKey, ZenCacheValue& OutValue)
{
	RwLock::SharedLockScope _(m_bucketLock);

	if (auto bucketIt = m_cacheMap.find(HashKey); bucketIt == m_cacheMap.end())
	{
		return false;
	}
	else
	{
		BucketValue& Value = bucketIt.value();
		OutValue.Value	   = Value.Payload;
		Value.LastAccess   = GcClock::TickCount();

		return true;
	}
}

void
ZenCacheMemoryLayer::CacheBucket::Put(const IoHash& HashKey, const ZenCacheValue& Value)
{
	{
		RwLock::ExclusiveLockScope _(m_bucketLock);
		m_cacheMap.insert_or_assign(HashKey, BucketValue{.LastAccess = GcClock::TickCount(), .Payload = Value.Value});
	}

	m_TotalSize.fetch_add(Value.Value.GetSize());
}

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

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

struct DiskLocation
{
	inline DiskLocation() = default;

	inline DiskLocation(uint64_t Offset, uint64_t ValueSize, uint32_t IndexSize, uint64_t Flags)
	: OffsetAndFlags(CombineOffsetAndFlags(Offset, Flags))
	, LowerSize(ValueSize & 0xFFFFffff)
	, IndexDataSize(IndexSize)
	{
	}

	static const uint64_t kOffsetMask	  = 0x0000'ffFF'ffFF'ffFFull;
	static const uint64_t kSizeMask		  = 0x00FF'0000'0000'0000ull;
	static const uint64_t kFlagsMask	  = 0xff00'0000'0000'0000ull;
	static const uint64_t kStandaloneFile = 0x8000'0000'0000'0000ull;
	static const uint64_t kStructured	  = 0x4000'0000'0000'0000ull;
	static const uint64_t kTombStone	  = 0x2000'0000'0000'0000ull;

	static uint64_t CombineOffsetAndFlags(uint64_t Offset, uint64_t Flags) { return Offset | Flags; }

	inline uint64_t		  Offset() const { return OffsetAndFlags & kOffsetMask; }
	inline uint64_t		  Size() const { return LowerSize; }
	inline uint64_t		  IsFlagSet(uint64_t Flag) const { return OffsetAndFlags & Flag; }
	inline ZenContentType GetContentType() const
	{
		ZenContentType ContentType = ZenContentType::kBinary;

		if (IsFlagSet(DiskLocation::kStructured))
		{
			ContentType = ZenContentType::kCbObject;
		}

		return ContentType;
	}

private:
	uint64_t OffsetAndFlags = 0;
	uint32_t LowerSize		= 0;
	uint32_t IndexDataSize	= 0;
};

struct DiskIndexEntry
{
	IoHash		 Key;
	DiskLocation Location;
};

#pragma pack(pop)

static_assert(sizeof(DiskIndexEntry) == 36);

struct ZenCacheDiskLayer::CacheBucket
{
	CacheBucket();
	~CacheBucket();

	void		OpenOrCreate(std::filesystem::path BucketDir, bool AllowCreate = true);
	static bool Delete(std::filesystem::path BucketDir);
	bool		Get(const IoHash& HashKey, ZenCacheValue& OutValue);
	void		Put(const IoHash& HashKey, const ZenCacheValue& Value);
	void		Drop();
	void		Flush();
	void		Scrub(ScrubContext& Ctx);
	void		GatherReferences(GcContext& GcCtx);

	inline bool		IsOk() const { return m_IsOk; }
	inline uint64_t TotalSize() const { return m_TotalSize; }

private:
	std::filesystem::path m_BucketDir;
	Oid					  m_BucketId;
	bool				  m_IsOk				 = false;
	uint64_t			  m_LargeObjectThreshold = 64 * 1024;

	// These files are used to manage storage of small objects for this bucket

	BasicFile					m_SobsFile;
	TCasLogFile<DiskIndexEntry> m_SlogFile;

	struct IndexEntry
	{
		DiskLocation  Location;
		GcClock::Tick LastAccess{};
	};

	RwLock											   m_IndexLock;
	tsl::robin_map<IoHash, IndexEntry, IoHash::Hasher> m_Index;
	uint64_t										   m_WriteCursor = 0;
	std::atomic_uint64_t							   m_TotalSize{};

	void BuildPath(WideStringBuilderBase& Path, const IoHash& HashKey);
	void PutStandaloneCacheValue(const IoHash& HashKey, const ZenCacheValue& Value);
	bool GetStandaloneCacheValue(const DiskLocation& Loc, const IoHash& HashKey, ZenCacheValue& OutValue);
	bool GetInlineCacheValue(const DiskLocation& Loc, ZenCacheValue& OutValue);

	// These locks are here to avoid contention on file creation, therefore it's sufficient
	// that we take the same lock for the same hash
	//
	// These locks are small and should really be spaced out so they don't share cache lines,
	// but we don't currently access them at particularly high frequency so it should not be
	// an issue in practice

	RwLock		   m_ShardedLocks[256];
	inline RwLock& LockForHash(const IoHash& Hash) { return m_ShardedLocks[Hash.Hash[19]]; }
};

ZenCacheDiskLayer::CacheBucket::CacheBucket()
{
}

ZenCacheDiskLayer::CacheBucket::~CacheBucket()
{
}

bool
ZenCacheDiskLayer::CacheBucket::Delete(std::filesystem::path BucketDir)
{
	if (std::filesystem::exists(BucketDir))
	{
		DeleteDirectories(BucketDir);

		return true;
	}

	return false;
}

void
ZenCacheDiskLayer::CacheBucket::OpenOrCreate(std::filesystem::path BucketDir, bool AllowCreate)
{
	using namespace std::literals;

	CreateDirectories(BucketDir);

	m_BucketDir = BucketDir;

	std::filesystem::path ManifestPath{m_BucketDir / "zen_manifest"};
	std::filesystem::path SobsPath{m_BucketDir / "zen.sobs"};
	std::filesystem::path SlogPath{m_BucketDir / "zen.slog"};

	bool IsNew = false;

	CbObject Manifest = LoadCompactBinaryObject(ManifestPath);

	if (Manifest)
	{
		m_BucketId = Manifest["BucketId"].AsObjectId();
		m_IsOk	   = m_BucketId != Oid::Zero;
	}
	else if (AllowCreate)
	{
		m_BucketId.Generate();

		CbObjectWriter Writer;
		Writer << "BucketId"sv << m_BucketId;
		Manifest = Writer.Save();
		SaveCompactBinaryObject(ManifestPath, Manifest);
		IsNew = true;
	}
	else
	{
		return;
	}

	// Initialize small object storage related files

	m_SobsFile.Open(SobsPath, IsNew);

	// Open and replay log

	m_SlogFile.Open(SlogPath, IsNew);

	uint64_t MaxFileOffset	   = 0;
	uint64_t InvalidEntryCount = 0;

	if (RwLock::ExclusiveLockScope _(m_IndexLock); m_Index.empty())
	{
		m_SlogFile.Replay([&](const DiskIndexEntry& Entry) {
			if (Entry.Key == IoHash::Zero)
			{
				++InvalidEntryCount;
			}
			else if (Entry.Location.IsFlagSet(DiskLocation::kTombStone))
			{
				m_TotalSize.fetch_sub(Entry.Location.Size());
			}
			else
			{
				m_Index[Entry.Key] = {.Location = Entry.Location, .LastAccess = GcClock::TickCount()};
				m_TotalSize.fetch_add(Entry.Location.Size());
			}
			MaxFileOffset = std::max<uint64_t>(MaxFileOffset, Entry.Location.Offset() + Entry.Location.Size());
		});

		if (InvalidEntryCount)
		{
			ZEN_WARN("found {} invalid entries in '{}'", InvalidEntryCount, SlogPath);
		}

		m_WriteCursor = (MaxFileOffset + 15) & ~15;
	}

	for (CbFieldView Entry : Manifest["Timestamps"])
	{
		const CbObjectView Obj = Entry.AsObjectView();
		const IoHash	   Key = Obj["Key"sv].AsHash();

		if (auto It = m_Index.find(Key); It != m_Index.end())
		{
			It.value().LastAccess = Obj["LastAccess"sv].AsInt64();
		}
	}

	m_IsOk = true;
}

void
ZenCacheDiskLayer::CacheBucket::BuildPath(WideStringBuilderBase& Path, const IoHash& HashKey)
{
	char HexString[sizeof(HashKey.Hash) * 2];
	ToHexBytes(HashKey.Hash, sizeof HashKey.Hash, HexString);

	Path.Append(m_BucketDir.c_str());
	Path.Append(L"/blob/");
	Path.AppendAsciiRange(HexString, HexString + 3);
	Path.Append(L"/");
	Path.AppendAsciiRange(HexString + 3, HexString + 5);
	Path.Append(L"/");
	Path.AppendAsciiRange(HexString + 5, HexString + sizeof(HexString));
}

bool
ZenCacheDiskLayer::CacheBucket::GetInlineCacheValue(const DiskLocation& Loc, ZenCacheValue& OutValue)
{
	if (Loc.IsFlagSet(DiskLocation::kStandaloneFile))
	{
		return false;
	}

	OutValue.Value = IoBufferBuilder::MakeFromFileHandle(m_SobsFile.Handle(), Loc.Offset(), Loc.Size());
	OutValue.Value.SetContentType(Loc.GetContentType());

	return true;
}

bool
ZenCacheDiskLayer::CacheBucket::GetStandaloneCacheValue(const DiskLocation& Loc, const IoHash& HashKey, ZenCacheValue& OutValue)
{
	WideStringBuilder<128> DataFilePath;
	BuildPath(DataFilePath, HashKey);

	RwLock::SharedLockScope ValueLock(LockForHash(HashKey));

	if (IoBuffer Data = IoBufferBuilder::MakeFromFile(DataFilePath.c_str()))
	{
		OutValue.Value = Data;
		OutValue.Value.SetContentType(Loc.GetContentType());

		return true;
	}

	return false;
}

bool
ZenCacheDiskLayer::CacheBucket::Get(const IoHash& HashKey, ZenCacheValue& OutValue)
{
	if (!m_IsOk)
	{
		return false;
	}

	RwLock::SharedLockScope _(m_IndexLock);

	if (auto It = m_Index.find(HashKey); It != m_Index.end())
	{
		IndexEntry& Entry = It.value();
		Entry.LastAccess  = GcClock::TickCount();

		if (GetInlineCacheValue(Entry.Location, OutValue))
		{
			return true;
		}

		_.ReleaseNow();

		return GetStandaloneCacheValue(Entry.Location, HashKey, OutValue);
	}

	return false;
}

void
ZenCacheDiskLayer::CacheBucket::Put(const IoHash& HashKey, const ZenCacheValue& Value)
{
	if (!m_IsOk)
	{
		return;
	}

	if (Value.Value.Size() >= m_LargeObjectThreshold)
	{
		return PutStandaloneCacheValue(HashKey, Value);
	}
	else
	{
		// Small object put

		uint64_t EntryFlags = 0;

		if (Value.Value.GetContentType() == ZenContentType::kCbObject)
		{
			EntryFlags |= DiskLocation::kStructured;
		}

		RwLock::ExclusiveLockScope _(m_IndexLock);

		DiskLocation Loc(m_WriteCursor, Value.Value.Size(), 0, EntryFlags);

		m_WriteCursor = RoundUp(m_WriteCursor + Loc.Size(), 16);

		if (auto It = m_Index.find(HashKey); It == m_Index.end())
		{
			// Previously unknown object
			m_Index.insert({HashKey, {Loc, GcClock::TickCount()}});
		}
		else
		{
			// TODO: should check if write is idempotent and bail out if it is?
			// this would requiring comparing contents on disk unless we add a
			// content hash to the index entry
			IndexEntry& Entry = It.value();
			Entry.Location	  = Loc;
			Entry.LastAccess  = GcClock::TickCount();
		}

		m_SlogFile.Append({.Key = HashKey, .Location = Loc});
		m_SobsFile.Write(Value.Value.Data(), Loc.Size(), Loc.Offset());
		m_TotalSize.fetch_add(Loc.Size());
	}
}

void
ZenCacheDiskLayer::CacheBucket::Drop()
{
	// TODO: add error handling

	m_SobsFile.Close();
	m_SlogFile.Close();
	DeleteDirectories(m_BucketDir);
}

void
ZenCacheDiskLayer::CacheBucket::Flush()
{
	using namespace std::literals;

	RwLock::SharedLockScope _(m_IndexLock);

	m_SobsFile.Flush();
	m_SlogFile.Flush();

	// Update manifest
	{
		CbObjectWriter Writer;
		Writer << "BucketId"sv << m_BucketId;

		if (!m_Index.empty())
		{
			Writer.BeginArray("Timestamps"sv);
			for (auto& Kv : m_Index)
			{
				const IoHash&	  Key	= Kv.first;
				const IndexEntry& Entry = Kv.second;
				Writer << "Key"sv << Key << "LastAccess"sv << Entry.LastAccess;
			}
			Writer.EndArray();
		}

		SaveCompactBinaryObject(m_BucketDir / "zen_manifest", Writer.Save());
	}
}

void
ZenCacheDiskLayer::CacheBucket::Scrub(ScrubContext& Ctx)
{
	std::vector<IoHash> BadKeys;

	{
		RwLock::SharedLockScope _(m_IndexLock);

		for (auto& Kv : m_Index)
		{
			const IoHash&		HashKey = Kv.first;
			const DiskLocation& Loc		= Kv.second.Location;

			ZenCacheValue Value;

			if (GetInlineCacheValue(Loc, Value))
			{
				// Validate contents
			}
			else if (GetStandaloneCacheValue(Loc, HashKey, Value))
			{
				// Note: we cannot currently validate contents since we don't
				// have a content hash!
			}
			else
			{
				// Value not found
				BadKeys.push_back(HashKey);
			}
		}
	}

	if (BadKeys.empty())
	{
		return;
	}

	if (Ctx.RunRecovery())
	{
		RwLock::ExclusiveLockScope _(m_IndexLock);

		for (const IoHash& BadKey : BadKeys)
		{
			// Log a tombstone and delete the in-memory index for the bad entry

			const auto			It		 = m_Index.find(BadKey);
			const DiskLocation& Location = It->second.Location;
			m_SlogFile.Append(DiskIndexEntry{.Key = BadKey, .Location = {Location.Offset(), Location.Size(), 0, DiskLocation::kTombStone}});
			m_Index.erase(BadKey);
		}
	}
}

void
ZenCacheDiskLayer::CacheBucket::GatherReferences(GcContext& GcCtx)
{
	RwLock::SharedLockScope _(m_IndexLock);

	std::vector<IoHash> Cids;

	for (auto& Kv : m_Index)
	{
		const IoHash&		HashKey = Kv.first;
		const DiskLocation& Loc		= Kv.second.Location;

		if (Loc.IsFlagSet(DiskLocation::kStructured) == false || Loc.IsFlagSet(DiskLocation::kTombStone) ||
			GcCtx.Expired(Kv.second.LastAccess))
		{
			continue;
		}

		ZenCacheValue CacheValue;
		if (!GetInlineCacheValue(Loc, CacheValue))
		{
			GetStandaloneCacheValue(Loc, HashKey, CacheValue);
		}

		if (CacheValue.Value)
		{
			ZEN_ASSERT(CacheValue.Value.GetContentType() == ZenContentType::kCbObject);

			Cids.clear();
			CbObject Obj(SharedBuffer{CacheValue.Value});
			Obj.IterateAttachments([&Cids](CbFieldView Field) { Cids.push_back(Field.AsAttachment()); });
			GcCtx.ContributeCids(Cids);
		}
	}
}

void
ZenCacheDiskLayer::CacheBucket::PutStandaloneCacheValue(const IoHash& HashKey, const ZenCacheValue& Value)
{
	RwLock::ExclusiveLockScope ValueLock(LockForHash(HashKey));

	WideStringBuilder<128> DataFilePath;
	BuildPath(DataFilePath, HashKey);

	TemporaryFile DataFile;

	std::error_code Ec;
	DataFile.CreateTemporary(m_BucketDir.c_str(), Ec);

	if (Ec)
	{
		throw std::system_error(Ec, "Failed to open temporary file for put at '{}'"_format(m_BucketDir));
	}

	DataFile.WriteAll(Value.Value, Ec);

	if (Ec)
	{
		throw std::system_error(Ec, "Failed to write payload ({} bytes) to file"_format(NiceBytes(Value.Value.Size())));
	}

	// Move file into place (atomically)

	std::filesystem::path FsPath{DataFilePath.c_str()};

	DataFile.MoveTemporaryIntoPlace(FsPath, Ec);

	if (Ec)
	{
		int RetryCount = 3;

		do
		{
			std::filesystem::path ParentPath = std::filesystem::path(DataFilePath.c_str()).parent_path();
			CreateDirectories(ParentPath);

			DataFile.MoveTemporaryIntoPlace(FsPath, Ec);

			if (!Ec)
			{
				break;
			}

			std::error_code InnerEc;
			const uint64_t	ExistingFileSize = std::filesystem::file_size(FsPath, InnerEc);

			if (!InnerEc && ExistingFileSize == Value.Value.Size())
			{
				// Concurrent write of same value?
				return;
			}

			// Semi arbitrary back-off
			zen::Sleep(1000 * RetryCount);
		} while (RetryCount--);

		if (Ec)
		{
			throw std::system_error(Ec, "Failed to finalize file '{}'"_format(WideToUtf8(DataFilePath)));
		}
	}

	// Update index

	uint64_t EntryFlags = DiskLocation::kStandaloneFile;

	if (Value.Value.GetContentType() == ZenContentType::kCbObject)
	{
		EntryFlags |= DiskLocation::kStructured;
	}

	RwLock::ExclusiveLockScope _(m_IndexLock);

	DiskLocation Loc(/* Offset */ 0, Value.Value.Size(), 0, EntryFlags);
	IndexEntry	 Entry = IndexEntry{.Location = Loc, .LastAccess = GcClock::TickCount()};

	if (auto It = m_Index.find(HashKey); It == m_Index.end())
	{
		// Previously unknown object
		m_Index.insert({HashKey, Entry});
	}
	else
	{
		// TODO: should check if write is idempotent and bail out if it is?
		It.value() = Entry;
	}

	m_SlogFile.Append({.Key = HashKey, .Location = Loc});
	m_TotalSize.fetch_add(Loc.Size());
}

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

ZenCacheDiskLayer::ZenCacheDiskLayer(const std::filesystem::path& RootDir) : m_RootDir(RootDir)
{
}

ZenCacheDiskLayer::~ZenCacheDiskLayer() = default;

bool
ZenCacheDiskLayer::Get(std::string_view InBucket, const IoHash& HashKey, ZenCacheValue& OutValue)
{
	CacheBucket* Bucket = nullptr;

	{
		RwLock::SharedLockScope _(m_Lock);

		auto it = m_Buckets.find(std::string(InBucket));

		if (it != m_Buckets.end())
		{
			Bucket = &it->second;
		}
	}

	if (Bucket == nullptr)
	{
		// Bucket needs to be opened/created

		RwLock::ExclusiveLockScope _(m_Lock);

		if (auto it = m_Buckets.find(std::string(InBucket)); it != m_Buckets.end())
		{
			Bucket = &it->second;
		}
		else
		{
			auto It = m_Buckets.try_emplace(std::string(InBucket));
			Bucket	= &It.first->second;

			std::filesystem::path BucketPath = m_RootDir;
			BucketPath /= std::string(InBucket);

			Bucket->OpenOrCreate(BucketPath);
		}
	}

	ZEN_ASSERT(Bucket != nullptr);

	return Bucket->Get(HashKey, OutValue);
}

void
ZenCacheDiskLayer::Put(std::string_view InBucket, const IoHash& HashKey, const ZenCacheValue& Value)
{
	CacheBucket* Bucket = nullptr;

	{
		RwLock::SharedLockScope _(m_Lock);

		auto it = m_Buckets.find(std::string(InBucket));

		if (it != m_Buckets.end())
		{
			Bucket = &it->second;
		}
	}

	if (Bucket == nullptr)
	{
		// New bucket needs to be created

		RwLock::ExclusiveLockScope _(m_Lock);

		if (auto it = m_Buckets.find(std::string(InBucket)); it != m_Buckets.end())
		{
			Bucket = &it->second;
		}
		else
		{
			auto It = m_Buckets.try_emplace(std::string(InBucket));
			Bucket	= &It.first->second;

			std::filesystem::path bucketPath = m_RootDir;
			bucketPath /= std::string(InBucket);

			Bucket->OpenOrCreate(bucketPath);
		}
	}

	ZEN_ASSERT(Bucket != nullptr);

	if (Bucket->IsOk())
	{
		Bucket->Put(HashKey, Value);
	}
}

void
ZenCacheDiskLayer::DiscoverBuckets()
{
	FileSystemTraversal Traversal;
	struct Visitor : public FileSystemTraversal::TreeVisitor
	{
		virtual void VisitFile([[maybe_unused]] const std::filesystem::path& Parent,
							   [[maybe_unused]] const path_view&			 File,
							   [[maybe_unused]] uint64_t					 FileSize) override
		{
		}

		virtual bool VisitDirectory([[maybe_unused]] const std::filesystem::path& Parent, const path_view& DirectoryName) override
		{
			Dirs.push_back(std::wstring(DirectoryName));
			return false;
		}

		std::vector<std::wstring> Dirs;
	} Visit;

	Traversal.TraverseFileSystem(m_RootDir, Visit);

	// Initialize buckets

	RwLock::ExclusiveLockScope _(m_Lock);

	for (const std::wstring& BucketName : Visit.Dirs)
	{
		// New bucket needs to be created

		const std::string BucketName8 = ToUtf8(BucketName);

		if (auto It = m_Buckets.find(BucketName8); It != m_Buckets.end())
		{
		}
		else
		{
			auto InsertResult = m_Buckets.try_emplace(BucketName8);

			std::filesystem::path BucketPath = m_RootDir;
			BucketPath /= BucketName8;

			CacheBucket& Bucket = InsertResult.first->second;

			Bucket.OpenOrCreate(BucketPath, /* AllowCreate */ false);

			if (Bucket.IsOk())
			{
				ZEN_INFO("Discovered bucket '{}'", BucketName8);
			}
			else
			{
				ZEN_WARN("Found directory '{}' in our base directory '{}' but it is not a valid bucket", BucketName8, m_RootDir);

				m_Buckets.erase(InsertResult.first);
			}
		}
	}
}

bool
ZenCacheDiskLayer::DropBucket(std::string_view InBucket)
{
	RwLock::ExclusiveLockScope _(m_Lock);

	auto it = m_Buckets.find(std::string(InBucket));

	if (it != m_Buckets.end())
	{
		CacheBucket* Bucket = &it->second;

		Bucket->Drop();

		m_Buckets.erase(it);

		return true;
	}

	std::filesystem::path BucketPath = m_RootDir;
	BucketPath /= std::string(InBucket);

	return CacheBucket::Delete(BucketPath);
}

void
ZenCacheDiskLayer::Flush()
{
	std::vector<CacheBucket*> Buckets;
	Buckets.reserve(m_Buckets.size());

	{
		RwLock::SharedLockScope _(m_Lock);

		for (auto& Kv : m_Buckets)
		{
			Buckets.push_back(&Kv.second);
		}
	}

	for (auto& Bucket : Buckets)
	{
		Bucket->Flush();
	}
}

void
ZenCacheDiskLayer::Scrub(ScrubContext& Ctx)
{
	RwLock::SharedLockScope _(m_Lock);

	for (auto& Kv : m_Buckets)
	{
		Kv.second.Scrub(Ctx);
	}
}

void
ZenCacheDiskLayer::GatherReferences(GcContext& GcCtx)
{
	RwLock::SharedLockScope _(m_Lock);

	for (auto& Kv : m_Buckets)
	{
		Kv.second.GatherReferences(GcCtx);
	}
}

uint64_t
ZenCacheDiskLayer::TotalSize() const
{
	uint64_t				TotalSize{};
	RwLock::SharedLockScope _(m_Lock);

	for (auto& Kv : m_Buckets)
	{
		TotalSize += Kv.second.TotalSize();
	}

	return TotalSize;
}

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

#if ZEN_WITH_TESTS

using namespace std::literals;
using namespace fmt::literals;

namespace testutils {

	IoHash CreateKey(size_t KeyValue) { return IoHash::HashBuffer(&KeyValue, sizeof(size_t)); }

}  // namespace testutils

TEST_CASE("zcache.store")
{
	ScopedTemporaryDirectory TempDir;

	CasGc Gc;

	ZenCacheStore Zcs(Gc, TempDir.Path() / "cache");

	const int kIterationCount = 100;

	for (int i = 0; i < kIterationCount; ++i)
	{
		const IoHash Key = IoHash::HashBuffer(&i, sizeof i);

		CbObjectWriter Cbo;
		Cbo << "hey" << i;
		CbObject Obj = Cbo.Save();

		ZenCacheValue Value;
		Value.Value = Obj.GetBuffer().AsIoBuffer();
		Value.Value.SetContentType(ZenContentType::kCbObject);

		Zcs.Put("test_bucket"sv, Key, Value);
	}

	for (int i = 0; i < kIterationCount; ++i)
	{
		const IoHash Key = IoHash::HashBuffer(&i, sizeof i);

		ZenCacheValue Value;
		Zcs.Get("test_bucket"sv, Key, /* out */ Value);

		REQUIRE(Value.Value);
		CHECK(Value.Value.GetContentType() == ZenContentType::kCbObject);
		CHECK_EQ(ValidateCompactBinary(Value.Value, CbValidateMode::All), CbValidateError::None);
		CbObject Obj = LoadCompactBinaryObject(Value.Value);
		CHECK_EQ(Obj["hey"].AsInt32(), i);
	}
}

TEST_CASE("zcache.size")
{
	const auto CreateCacheValue = [](size_t Size) -> CbObject {
		std::vector<uint8_t> Buf;
		Buf.resize(Size);

		CbObjectWriter Writer;
		Writer.AddBinary("Binary"sv, Buf.data(), Buf.size());
		return Writer.Save();
	};

	SUBCASE("mem/disklayer")
	{
		const size_t			 Count = 16;
		ScopedTemporaryDirectory TempDir;

		ZenCacheSize CacheSize;

		{
			CasGc		  Gc;
			ZenCacheStore Zcs(Gc, TempDir.Path() / "cache");

			CbObject CacheValue = CreateCacheValue(Zcs.DiskLayerThreshold() - 256);

			IoBuffer Buffer = CacheValue.GetBuffer().AsIoBuffer();
			Buffer.SetContentType(ZenContentType::kCbObject);

			for (size_t Key = 0; Key < Count; ++Key)
			{
				const size_t Bucket = Key % 4;
				Zcs.Put("test_bucket-{}"_format(Bucket), IoHash::HashBuffer(&Key, sizeof(uint32_t)), {.Value = Buffer});
			}

			CacheSize = Zcs.TotalSize();
			CHECK_EQ(CacheValue.GetSize() * Count, CacheSize.DiskSize);
			CHECK_EQ(CacheValue.GetSize() * Count, CacheSize.MemorySize);
		}

		{
			CasGc		  Gc;
			ZenCacheStore Zcs(Gc, TempDir.Path() / "cache");

			const ZenCacheSize SerializedSize = Zcs.TotalSize();
			CHECK_EQ(SerializedSize.MemorySize, 0);
			CHECK_EQ(SerializedSize.DiskSize, CacheSize.DiskSize);

			for (size_t Bucket = 0; Bucket < 4; ++Bucket)
			{
				Zcs.DropBucket("test_bucket-{}"_format(Bucket));
			}
			CHECK_EQ(0, Zcs.TotalSize().DiskSize);
		}
	}

	SUBCASE("disklayer")
	{
		const size_t			 Count = 16;
		ScopedTemporaryDirectory TempDir;

		ZenCacheSize CacheSize;

		{
			CasGc		  Gc;
			ZenCacheStore Zcs(Gc, TempDir.Path() / "cache");

			CbObject CacheValue = CreateCacheValue(Zcs.DiskLayerThreshold() + 64);

			IoBuffer Buffer = CacheValue.GetBuffer().AsIoBuffer();
			Buffer.SetContentType(ZenContentType::kCbObject);

			for (size_t Key = 0; Key < Count; ++Key)
			{
				const size_t Bucket = Key % 4;
				Zcs.Put("test_bucket-{}"_format(Bucket), IoHash::HashBuffer(&Key, sizeof(uint32_t)), {.Value = Buffer});
			}

			CacheSize = Zcs.TotalSize();
			CHECK_EQ(CacheValue.GetSize() * Count, CacheSize.DiskSize);
			CHECK_EQ(0, CacheSize.MemorySize);
		}

		{
			CasGc		  Gc;
			ZenCacheStore Zcs(Gc, TempDir.Path() / "cache");

			const ZenCacheSize SerializedSize = Zcs.TotalSize();
			CHECK_EQ(SerializedSize.MemorySize, 0);
			CHECK_EQ(SerializedSize.DiskSize, CacheSize.DiskSize);

			for (size_t Bucket = 0; Bucket < 4; ++Bucket)
			{
				Zcs.DropBucket("test_bucket-{}"_format(Bucket));
			}
			CHECK_EQ(0, Zcs.TotalSize().DiskSize);
		}
	}
}

TEST_CASE("zcache.gc")
{
	using namespace testutils;

	SUBCASE("gather references does NOT add references for expired cache entries")
	{
		ScopedTemporaryDirectory TempDir;
		std::vector<IoHash>		 Cids{CreateKey(1), CreateKey(2), CreateKey(3)};

		const auto CollectAndFilter = [](CasGc&					 Gc,
										 GcClock::TimePoint		 Time,
										 GcClock::Duration		 MaxDuration,
										 std::span<const IoHash> Cids,
										 std::vector<IoHash>&	 OutKeep) {
			GcContext GcCtx(Time);
			GcCtx.MaxCacheDuration(MaxDuration);
			Gc.CollectGarbage(GcCtx);
			OutKeep.clear();
			GcCtx.FilterCids(Cids, [&OutKeep](const IoHash& Hash) { OutKeep.push_back(Hash); });
		};

		{
			CasGc		  Gc;
			ZenCacheStore Zcs(Gc, TempDir.Path() / "cache");
			const auto	  Bucket = "teardrinker"sv;

			// Create a cache record
			const IoHash   Key = CreateKey(42);
			CbObjectWriter Record;
			Record << "Key"sv
				   << "SomeRecord"sv;

			for (size_t Idx = 0; auto& Cid : Cids)
			{
				Record.AddBinaryAttachment("attachment-{}"_format(Idx++), Cid);
			}

			IoBuffer Buffer = Record.Save().GetBuffer().AsIoBuffer();
			Buffer.SetContentType(ZenContentType::kCbObject);

			Zcs.Put(Bucket, Key, {.Value = Buffer});

			std::vector<IoHash> Keep;

			// Collect garbage with 1 hour max cache duration
			{
				CollectAndFilter(Gc, GcClock::Now(), std::chrono::hours(1), Cids, Keep);
				CHECK_EQ(Cids.size(), Keep.size());
			}

			// Move forward in time
			{
				CollectAndFilter(Gc, GcClock::Now() + std::chrono::hours(2), std::chrono::hours(1), Cids, Keep);
				CHECK_EQ(0, Keep.size());
			}
		}

		// Expect timestamps to be serialized
		{
			CasGc				Gc;
			ZenCacheStore		Zcs(Gc, TempDir.Path() / "cache");
			std::vector<IoHash> Keep;

			// Collect garbage with 1 hour max cache duration
			{
				CollectAndFilter(Gc, GcClock::Now(), std::chrono::hours(1), Cids, Keep);
				CHECK_EQ(3, Keep.size());
			}

			// Move forward in time
			{
				CollectAndFilter(Gc, GcClock::Now() + std::chrono::hours(2), std::chrono::hours(1), Cids, Keep);
				CHECK_EQ(0, Keep.size());
			}
		}
	}
}

#endif

void
z$_forcelink()
{
}

}  // namespace zen