98 lines · c
1//===----------------------------------------------------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8//9/// \file Helper functions to test OnDiskCASDatabases.10//11//===----------------------------------------------------------------------===//12 13#include "llvm/CAS/BuiltinObjectHasher.h"14#include "llvm/CAS/OnDiskGraphDB.h"15#include "llvm/CAS/OnDiskKeyValueDB.h"16#include "llvm/CAS/UnifiedOnDiskCache.h"17#include "llvm/Support/BLAKE3.h"18#include "llvm/Testing/Support/Error.h"19 20namespace llvm::unittest::cas {21 22using namespace llvm::cas;23using namespace llvm::cas::ondisk;24 25using HasherT = BLAKE3;26using HashType = decltype(HasherT::hash(std::declval<ArrayRef<uint8_t> &>()));27using ValueType = std::array<char, 20>;28 29inline HashType digest(StringRef Data, ArrayRef<ArrayRef<uint8_t>> RefHashes) {30 return BuiltinObjectHasher<HasherT>::hashObject(31 RefHashes, arrayRefFromStringRef<char>(Data));32}33 34inline ObjectID digest(OnDiskGraphDB &DB, StringRef Data,35 ArrayRef<ObjectID> Refs) {36 SmallVector<ArrayRef<uint8_t>, 8> RefHashes;37 for (ObjectID Ref : Refs)38 RefHashes.push_back(DB.getDigest(Ref));39 HashType Digest = digest(Data, RefHashes);40 std::optional<ObjectID> ID;41 EXPECT_THAT_ERROR(DB.getReference(Digest).moveInto(ID), Succeeded());42 return *ID;43}44 45inline HashType digest(StringRef Data) {46 return HasherT::hash(arrayRefFromStringRef(Data));47}48 49inline ValueType valueFromString(StringRef S) {50 ValueType Val = {};51 llvm::copy(S.substr(0, sizeof(Val)), Val.data());52 return Val;53}54 55inline Expected<ObjectID> store(OnDiskGraphDB &DB, StringRef Data,56 ArrayRef<ObjectID> Refs) {57 ObjectID ID = digest(DB, Data, Refs);58 if (Error E = DB.store(ID, Refs, arrayRefFromStringRef<char>(Data)))59 return std::move(E);60 return ID;61}62 63inline Expected<ObjectID> cachePut(OnDiskKeyValueDB &DB, ArrayRef<uint8_t> Key,64 ObjectID ID) {65 auto Value = UnifiedOnDiskCache::getValueFromObjectID(ID);66 auto Result = DB.put(Key, Value);67 if (!Result)68 return Result.takeError();69 return UnifiedOnDiskCache::getObjectIDFromValue(*Result);70}71 72inline Expected<std::optional<ObjectID>> cacheGet(OnDiskKeyValueDB &DB,73 ArrayRef<uint8_t> Key) {74 auto Result = DB.get(Key);75 if (!Result)76 return Result.takeError();77 if (!*Result)78 return std::nullopt;79 return UnifiedOnDiskCache::getObjectIDFromValue(**Result);80}81 82inline Error printTree(OnDiskGraphDB &DB, ObjectID ID, raw_ostream &OS,83 unsigned Indent = 0) {84 std::optional<ondisk::ObjectHandle> Obj;85 if (Error E = DB.load(ID).moveInto(Obj))86 return E;87 if (!Obj)88 return Error::success();89 OS.indent(Indent) << toStringRef(DB.getObjectData(*Obj)) << '\n';90 for (ObjectID Ref : DB.getObjectRefs(*Obj)) {91 if (Error E = printTree(DB, Ref, OS, Indent + 2))92 return E;93 }94 return Error::success();95}96 97} // namespace llvm::unittest::cas98