5252#include " llvm/ADT/DenseMap.h"
5353#include " llvm/ADT/StringExtras.h"
5454#include " llvm/Support/Alignment.h"
55+ #include " llvm/Support/Compiler.h"
5556#include " llvm/Support/Errc.h"
57+ #include " llvm/Support/Error.h"
5658#include " llvm/Support/MemoryBuffer.h"
5759#include " llvm/Support/Path.h"
5860#include " llvm/Support/Process.h"
61+ #include < optional>
5962
6063#define DEBUG_TYPE " on-disk-cas"
6164
@@ -915,17 +918,21 @@ void OnDiskGraphDB::print(raw_ostream &OS) const {
915918 }
916919}
917920
918- OnDiskGraphDB::IndexProxy OnDiskGraphDB::indexHash (ArrayRef<uint8_t > Hash) {
919- OnDiskHashMappedTrie::pointer P = Index.insertLazy (
921+ Expected<OnDiskGraphDB::IndexProxy>
922+ OnDiskGraphDB::indexHash (ArrayRef<uint8_t > Hash) {
923+ auto P = Index.insertLazy (
920924 Hash, [](FileOffset TentativeOffset,
921925 OnDiskHashMappedTrie::ValueProxy TentativeValue) {
922926 assert (TentativeValue.Data .size () == sizeof (TrieRecord));
923927 assert (
924928 isAddrAligned (Align::Of<TrieRecord>(), TentativeValue.Data .data ()));
925929 new (TentativeValue.Data .data ()) TrieRecord ();
926930 });
927- assert (P && " Expected insertion" );
928- return getIndexProxyFromPointer (P);
931+ if (LLVM_UNLIKELY (!P))
932+ return P.takeError ();
933+
934+ assert (*P && " Expected insertion" );
935+ return getIndexProxyFromPointer (*P);
929936}
930937
931938OnDiskGraphDB::IndexProxy OnDiskGraphDB::getIndexProxyFromPointer (
@@ -937,9 +944,11 @@ OnDiskGraphDB::IndexProxy OnDiskGraphDB::getIndexProxyFromPointer(
937944 reinterpret_cast <const TrieRecord *>(P->Data .data ()))};
938945}
939946
940- ObjectID OnDiskGraphDB::getReference (ArrayRef<uint8_t > Hash) {
941- IndexProxy I = indexHash (Hash);
942- return getExternalReference (I);
947+ Expected<ObjectID> OnDiskGraphDB::getReference (ArrayRef<uint8_t > Hash) {
948+ auto I = indexHash (Hash);
949+ if (LLVM_UNLIKELY (!I))
950+ return I.takeError ();
951+ return getExternalReference (*I);
943952}
944953
945954ObjectID OnDiskGraphDB::getExternalReference (const IndexProxy &I) {
@@ -954,10 +963,13 @@ OnDiskGraphDB::getExistingReference(ArrayRef<uint8_t> Digest) {
954963 return std::nullopt ;
955964 std::optional<ObjectID> UpstreamID =
956965 UpstreamDB->getExistingReference (Digest);
957- if (!UpstreamID)
966+ if (LLVM_UNLIKELY (!UpstreamID))
967+ return std::nullopt ;
968+ auto Ref = expectedToOptional (indexHash (Digest));
969+ if (!Ref)
958970 return std::nullopt ;
959971 if (!I)
960- I.emplace (indexHash (Digest) );
972+ I.emplace (*Ref );
961973 return getExternalReference (*I);
962974 };
963975
@@ -1246,14 +1258,16 @@ Error OnDiskGraphDB::store(ObjectID ID, ArrayRef<ObjectID> Refs,
12461258 auto Alloc = [&](size_t Size) -> Expected<char *> {
12471259 if (Size <= TrieRecord::MaxEmbeddedSize) {
12481260 SK = TrieRecord::StorageKind::DataPool;
1249- OnDiskDataAllocator::pointer P = DataPool.allocate (Size);
1250- PoolOffset = P.getOffset ();
1261+ auto P = DataPool.allocate (Size);
1262+ if (LLVM_UNLIKELY (!P))
1263+ return P.takeError ();
1264+ PoolOffset = P->getOffset ();
12511265 LLVM_DEBUG ({
12521266 dbgs () << " pool-alloc addr=" << (void *)PoolOffset.get ()
12531267 << " size=" << Size
12541268 << " end=" << (void *)(PoolOffset.get () + Size) << " \n " ;
12551269 });
1256- return P ->data ();
1270+ return (*P) ->data ();
12571271 }
12581272
12591273 SK = TrieRecord::StorageKind::Standalone;
@@ -1465,19 +1479,21 @@ Error OnDiskGraphDB::importFullTree(ObjectID PrimaryID,
14651479 }
14661480
14671481 ObjectID UpstreamID = *(Cur.RefI ++);
1468- ObjectID PrimaryID = getReference (UpstreamDB->getDigest (UpstreamID));
1469- if (containsObject (PrimaryID, /* CheckUpstream=*/ false )) {
1482+ auto PrimaryID = getReference (UpstreamDB->getDigest (UpstreamID));
1483+ if (LLVM_UNLIKELY (!PrimaryID))
1484+ return PrimaryID.takeError ();
1485+ if (containsObject (*PrimaryID, /* CheckUpstream=*/ false )) {
14701486 // This \p ObjectID already exists in the primary. Either it was imported
14711487 // via \p importFullTree or the client created it, in which case the
14721488 // client takes responsibility for how it was formed.
1473- enqueueNode (PrimaryID, std::nullopt );
1489+ enqueueNode (* PrimaryID, std::nullopt );
14741490 continue ;
14751491 }
14761492 Expected<std::optional<ObjectHandle>> UpstreamNode =
14771493 UpstreamDB->load (UpstreamID);
14781494 if (!UpstreamNode)
14791495 return UpstreamNode.takeError ();
1480- enqueueNode (PrimaryID, *UpstreamNode);
1496+ enqueueNode (* PrimaryID, *UpstreamNode);
14811497 }
14821498
14831499 assert (PrimaryNodesStack.size () == 1 );
@@ -1497,8 +1513,12 @@ Error OnDiskGraphDB::importSingleNode(ObjectID PrimaryID,
14971513 auto UpstreamRefs = UpstreamDB->getObjectRefs (UpstreamNode);
14981514 SmallVector<ObjectID, 64 > Refs;
14991515 Refs.reserve (std::distance (UpstreamRefs.begin (), UpstreamRefs.end ()));
1500- for (ObjectID UpstreamRef : UpstreamRefs)
1501- Refs.push_back (getReference (UpstreamDB->getDigest (UpstreamRef)));
1516+ for (ObjectID UpstreamRef : UpstreamRefs) {
1517+ auto Ref = getReference (UpstreamDB->getDigest (UpstreamRef));
1518+ if (LLVM_UNLIKELY (!Ref))
1519+ return Ref.takeError ();
1520+ Refs.push_back (*Ref);
1521+ }
15021522
15031523 return store (PrimaryID, Refs, Data);
15041524}
@@ -1507,9 +1527,12 @@ Expected<std::optional<ObjectHandle>>
15071527OnDiskGraphDB::faultInFromUpstream (ObjectID PrimaryID) {
15081528 assert (UpstreamDB);
15091529
1510- ObjectID UpstreamID = UpstreamDB->getReference (getDigest (PrimaryID));
1530+ auto UpstreamID = UpstreamDB->getReference (getDigest (PrimaryID));
1531+ if (LLVM_UNLIKELY (!UpstreamID))
1532+ return UpstreamID.takeError ();
1533+
15111534 Expected<std::optional<ObjectHandle>> UpstreamNode =
1512- UpstreamDB->load (UpstreamID);
1535+ UpstreamDB->load (* UpstreamID);
15131536 if (!UpstreamNode)
15141537 return UpstreamNode.takeError ();
15151538 if (!*UpstreamNode)
0 commit comments