1313#include " BuiltinCAS.h"
1414#include " llvm/ADT/TrieRawHashMap.h"
1515#include " llvm/CAS/ActionCache.h"
16- #include " llvm/CAS/ObjectStore.h"
1716#include " llvm/CAS/OnDiskCASLogger.h"
18- #include " llvm/CAS/OnDiskGraphDB.h"
1917#include " llvm/CAS/OnDiskKeyValueDB.h"
2018#include " llvm/CAS/UnifiedOnDiskCache.h"
2119#include " llvm/Config/llvm-config.h"
22- #include " llvm/Support/Alignment.h"
2320#include " llvm/Support/BLAKE3.h"
2421#include " llvm/Support/Compiler.h"
22+ #include " llvm/Support/Errc.h"
2523#include " llvm/Support/Path.h"
2624
2725#define DEBUG_TYPE " cas-action-caches"
@@ -67,6 +65,7 @@ class InMemoryActionCache final : public ActionCache {
6765 InMemoryCacheT Cache;
6866};
6967
68+ // / Builtin basic OnDiskActionCache that uses one underlying OnDiskKeyValueDB.
7069class OnDiskActionCache final : public ActionCache {
7170public:
7271 Error putImpl (ArrayRef<uint8_t > ActionKey, const CASID &Result,
@@ -87,6 +86,8 @@ class OnDiskActionCache final : public ActionCache {
8786 using DataT = CacheEntry<sizeof (HashType)>;
8887};
8988
89+ // / Builtin unified ActionCache that wraps around UnifiedOnDiskCache to provide
90+ // / access to its ActionCache.
9091class UnifiedOnDiskActionCache final : public ActionCache {
9192public:
9293 Error putImpl (ArrayRef<uint8_t > ActionKey, const CASID &Result,
@@ -118,7 +119,8 @@ static Error createResultCachePoisonedError(ArrayRef<uint8_t> KeyHash,
118119}
119120
120121Expected<std::optional<CASID>>
121- InMemoryActionCache::getImpl (ArrayRef<uint8_t > Key, bool /* CanBeDistributed*/ ) const {
122+ InMemoryActionCache::getImpl (ArrayRef<uint8_t > Key,
123+ bool /* CanBeDistributed*/ ) const {
122124 auto Result = Cache.find (Key);
123125 if (!Result)
124126 return std::nullopt ;
@@ -169,17 +171,18 @@ OnDiskActionCache::create(StringRef AbsPath) {
169171 ondisk::OnDiskCASLogger::openIfEnabled (AbsPath).moveInto (Logger))
170172 return std::move (E);
171173 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))
174+ if (Error E = ondisk::OnDiskKeyValueDB::open (
175+ AbsPath, getHashName (), sizeof (HashType), getHashName (),
176+ sizeof (DataT), /* UnifiedCache= */ nullptr , std::move (Logger))
175177 .moveInto (DB))
176178 return std::move (E);
177179 return std::unique_ptr<OnDiskActionCache>(
178180 new OnDiskActionCache (std::move (DB)));
179181}
180182
181183Expected<std::optional<CASID>>
182- OnDiskActionCache::getImpl (ArrayRef<uint8_t > Key, bool /* CanBeDistributed*/ ) const {
184+ OnDiskActionCache::getImpl (ArrayRef<uint8_t > Key,
185+ bool /* CanBeDistributed*/ ) const {
183186 std::optional<ArrayRef<char >> Val;
184187 if (Error E = DB->get (Key).moveInto (Val))
185188 return std::move (E);
@@ -218,13 +221,14 @@ UnifiedOnDiskActionCache::UnifiedOnDiskActionCache(
218221Expected<std::optional<CASID>>
219222UnifiedOnDiskActionCache::getImpl (ArrayRef<uint8_t > Key,
220223 bool /* CanBeDistributed*/ ) const {
221- std::optional<ondisk::ObjectID > Val;
222- if (Error E = UniDB->KVGet (Key).moveInto (Val))
224+ std::optional<ArrayRef< char > > Val;
225+ if (Error E = UniDB->getKeyValueDB (). get (Key).moveInto (Val))
223226 return std::move (E);
224227 if (!Val)
225228 return std::nullopt ;
229+ auto ID = ondisk::UnifiedOnDiskCache::getObjectIDFromValue (*Val);
226230 return CASID::create (&getContext (),
227- toStringRef (UniDB->getGraphDB ().getDigest (*Val )));
231+ toStringRef (UniDB->getGraphDB ().getDigest (ID )));
228232}
229233
230234Error UnifiedOnDiskActionCache::putImpl (ArrayRef<uint8_t > Key,
@@ -233,20 +237,35 @@ Error UnifiedOnDiskActionCache::putImpl(ArrayRef<uint8_t> Key,
233237 auto Expected = UniDB->getGraphDB ().getReference (Result.getHash ());
234238 if (LLVM_UNLIKELY (!Expected))
235239 return Expected.takeError ();
236- std::optional<ondisk::ObjectID> Observed;
237- if (Error E = UniDB->KVPut (Key, *Expected).moveInto (Observed))
240+
241+ auto Value = ondisk::UnifiedOnDiskCache::getValueFromObjectID (*Expected);
242+ std::optional<ArrayRef<char >> Observed;
243+ if (Error E = UniDB->getKeyValueDB ().put (Key, Value).moveInto (Observed))
238244 return E;
239245
240- if (*Expected == Observed)
246+ auto ObservedID = ondisk::UnifiedOnDiskCache::getObjectIDFromValue (*Observed);
247+ if (*Expected == ObservedID)
241248 return Error::success ();
242249
243250 return createResultCachePoisonedError (
244- Key, getContext (), Result,
245- UniDB->getGraphDB ().getDigest (*Observed));
251+ Key, getContext (), Result, UniDB->getGraphDB ().getDigest (ObservedID));
246252}
247253
248254Error UnifiedOnDiskActionCache::validate () const {
249- return UniDB->validateActionCache ();
255+ auto ValidateRef = [](FileOffset Offset, ArrayRef<char > Value) -> Error {
256+ auto ID = ondisk::UnifiedOnDiskCache::getObjectIDFromValue (Value);
257+ auto formatError = [&](Twine Msg) {
258+ return createStringError (
259+ llvm::errc::illegal_byte_sequence,
260+ " bad record at 0x" +
261+ utohexstr ((unsigned )Offset.get (), /* LowerCase=*/ true ) + " : " +
262+ Msg.str ());
263+ };
264+ if (ID.getOpaqueData () == 0 )
265+ return formatError (" zero is not a valid ref" );
266+ return Error::success ();
267+ };
268+ return UniDB->getKeyValueDB ().validate (ValidateRef);
250269}
251270
252271Expected<std::unique_ptr<ActionCache>>
0 commit comments