1919#include " llvm/CAS/OnDiskKeyValueDB.h"
2020#include " llvm/CAS/UnifiedOnDiskCache.h"
2121#include " llvm/Config/llvm-config.h"
22- #include " llvm/Support/Alignment.h"
2322#include " llvm/Support/BLAKE3.h"
2423#include " llvm/Support/Compiler.h"
24+ #include " llvm/Support/Errc.h"
2525#include " llvm/Support/Path.h"
2626
2727#define DEBUG_TYPE " cas-action-caches"
@@ -67,6 +67,7 @@ class InMemoryActionCache final : public ActionCache {
6767 InMemoryCacheT Cache;
6868};
6969
70+ // / Builtin basic OnDiskActionCache that uses one underlying OnDiskKeyValueDB.
7071class OnDiskActionCache final : public ActionCache {
7172public:
7273 Error putImpl (ArrayRef<uint8_t > ActionKey, const CASID &Result,
@@ -87,6 +88,8 @@ class OnDiskActionCache final : public ActionCache {
8788 using DataT = CacheEntry<sizeof (HashType)>;
8889};
8990
91+ // / Builtin unified ActionCache that wraps around UnifiedOnDiskCache to privode
92+ // / access to its ActionCache.
9093class UnifiedOnDiskActionCache final : public ActionCache {
9194public:
9295 Error putImpl (ArrayRef<uint8_t > ActionKey, const CASID &Result,
@@ -118,7 +121,8 @@ static Error createResultCachePoisonedError(ArrayRef<uint8_t> KeyHash,
118121}
119122
120123Expected<std::optional<CASID>>
121- InMemoryActionCache::getImpl (ArrayRef<uint8_t > Key, bool /* CanBeDistributed*/ ) const {
124+ InMemoryActionCache::getImpl (ArrayRef<uint8_t > Key,
125+ bool /* CanBeDistributed*/ ) const {
122126 auto Result = Cache.find (Key);
123127 if (!Result)
124128 return std::nullopt ;
@@ -169,17 +173,18 @@ OnDiskActionCache::create(StringRef AbsPath) {
169173 ondisk::OnDiskCASLogger::openIfEnabled (AbsPath).moveInto (Logger))
170174 return std::move (E);
171175 std::unique_ptr<ondisk::OnDiskKeyValueDB> DB;
172- if (Error E = ondisk::OnDiskKeyValueDB::open (AbsPath, getHashName (),
173- sizeof (HashType), getHashName (),
174- sizeof (DataT), std::move (Logger))
176+ if (Error E = ondisk::OnDiskKeyValueDB::open (
177+ AbsPath, getHashName (), sizeof (HashType), getHashName (),
178+ sizeof (DataT), /* UnifiedCache= */ nullptr , std::move (Logger))
175179 .moveInto (DB))
176180 return std::move (E);
177181 return std::unique_ptr<OnDiskActionCache>(
178182 new OnDiskActionCache (std::move (DB)));
179183}
180184
181185Expected<std::optional<CASID>>
182- OnDiskActionCache::getImpl (ArrayRef<uint8_t > Key, bool /* CanBeDistributed*/ ) const {
186+ OnDiskActionCache::getImpl (ArrayRef<uint8_t > Key,
187+ bool /* CanBeDistributed*/ ) const {
183188 std::optional<ArrayRef<char >> Val;
184189 if (Error E = DB->get (Key).moveInto (Val))
185190 return std::move (E);
@@ -218,13 +223,14 @@ UnifiedOnDiskActionCache::UnifiedOnDiskActionCache(
218223Expected<std::optional<CASID>>
219224UnifiedOnDiskActionCache::getImpl (ArrayRef<uint8_t > Key,
220225 bool /* CanBeDistributed*/ ) const {
221- std::optional<ondisk::ObjectID > Val;
222- if (Error E = UniDB->KVGet (Key).moveInto (Val))
226+ std::optional<ArrayRef< char > > Val;
227+ if (Error E = UniDB->getKeyValueDB (). get (Key).moveInto (Val))
223228 return std::move (E);
224229 if (!Val)
225230 return std::nullopt ;
231+ auto ID = ondisk::UnifiedOnDiskCache::getObjectIDFromValue (*Val);
226232 return CASID::create (&getContext (),
227- toStringRef (UniDB->getGraphDB ().getDigest (*Val )));
233+ toStringRef (UniDB->getGraphDB ().getDigest (ID )));
228234}
229235
230236Error UnifiedOnDiskActionCache::putImpl (ArrayRef<uint8_t > Key,
@@ -233,20 +239,35 @@ Error UnifiedOnDiskActionCache::putImpl(ArrayRef<uint8_t> Key,
233239 auto Expected = UniDB->getGraphDB ().getReference (Result.getHash ());
234240 if (LLVM_UNLIKELY (!Expected))
235241 return Expected.takeError ();
236- std::optional<ondisk::ObjectID> Observed;
237- if (Error E = UniDB->KVPut (Key, *Expected).moveInto (Observed))
242+
243+ auto Value = ondisk::UnifiedOnDiskCache::getValueFromObjectID (*Expected);
244+ std::optional<ArrayRef<char >> Observed;
245+ if (Error E = UniDB->getKeyValueDB ().put (Key, Value).moveInto (Observed))
238246 return E;
239247
240- if (*Expected == Observed)
248+ auto ObservedID = ondisk::UnifiedOnDiskCache::getObjectIDFromValue (*Observed);
249+ if (*Expected == ObservedID)
241250 return Error::success ();
242251
243252 return createResultCachePoisonedError (
244- Key, getContext (), Result,
245- UniDB->getGraphDB ().getDigest (*Observed));
253+ Key, getContext (), Result, UniDB->getGraphDB ().getDigest (ObservedID));
246254}
247255
248256Error UnifiedOnDiskActionCache::validate () const {
249- return UniDB->validateActionCache ();
257+ auto ValidateRef = [](FileOffset Offset, ArrayRef<char > Value) -> Error {
258+ auto ID = ondisk::UnifiedOnDiskCache::getObjectIDFromValue (Value);
259+ auto formatError = [&](Twine Msg) {
260+ return createStringError (
261+ llvm::errc::illegal_byte_sequence,
262+ " bad record at 0x" +
263+ utohexstr ((unsigned )Offset.get (), /* LowerCase=*/ true ) + " : " +
264+ Msg.str ());
265+ };
266+ if (ID.getOpaqueData () == 0 )
267+ return formatError (" zero is not a valid ref" );
268+ return Error::success ();
269+ };
270+ return UniDB->getKeyValueDB ().validate (ValidateRef);
250271}
251272
252273Expected<std::unique_ptr<ActionCache>>
0 commit comments