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-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef LLVM_LIB_CAS_BUILTINCAS_H
10#define LLVM_LIB_CAS_BUILTINCAS_H
11
12#include "llvm/ADT/StringRef.h"
13#include "llvm/CAS/BuiltinCASContext.h"
14#include "llvm/CAS/ObjectStore.h"
15
16namespace llvm::cas {
17class ActionCache;
18namespace ondisk {
19class UnifiedOnDiskCache;
20} // namespace ondisk
21namespace builtin {
22
23/// Common base class for builtin CAS implementations using the same CASContext.
24class BuiltinCAS : public ObjectStore {
25public:
26 BuiltinCAS() : ObjectStore(BuiltinCASContext::getDefaultContext()) {}
27
28 Expected<CASID> parseID(StringRef Reference) final;
29
30 Expected<ObjectRef> store(ArrayRef<ObjectRef> Refs,
31 ArrayRef<char> Data) final;
32 virtual Expected<ObjectRef> storeImpl(ArrayRef<uint8_t> ComputedHash,
33 ArrayRef<ObjectRef> Refs,
34 ArrayRef<char> Data) = 0;
35
36 virtual Expected<ObjectRef>
37 storeFromNullTerminatedRegion(ArrayRef<uint8_t> ComputedHash,
38 sys::fs::mapped_file_region Map) {
39 return storeImpl(ComputedHash, Refs: {}, Data: ArrayRef(Map.data(), Map.size()));
40 }
41
42 /// Both builtin CAS implementations provide lifetime for free, so this can
43 /// be const, and readData() and getDataSize() can be implemented on top of
44 /// it.
45 virtual ArrayRef<char> getDataConst(ObjectHandle Node) const = 0;
46
47 ArrayRef<char> getData(ObjectHandle Node,
48 bool RequiresNullTerminator) const final {
49 // BuiltinCAS Objects are always null terminated.
50 return getDataConst(Node);
51 }
52 uint64_t getDataSize(ObjectHandle Node) const final {
53 return getDataConst(Node).size();
54 }
55
56 Error createUnknownObjectError(const CASID &ID) const {
57 return createStringError(EC: std::make_error_code(e: std::errc::invalid_argument),
58 S: "unknown object '" + ID.toString() + "'");
59 }
60
61 Error createCorruptObjectError(const CASID &ID) const {
62 return createStringError(EC: std::make_error_code(e: std::errc::invalid_argument),
63 S: "corrupt object '" + ID.toString() + "'");
64 }
65
66 Error createCorruptStorageError() const {
67 return createStringError(EC: std::make_error_code(e: std::errc::invalid_argument),
68 S: "corrupt storage");
69 }
70
71 Error validateObject(const CASID &ID) final;
72};
73
74/// Create a \p UnifiedOnDiskCache instance that uses \p BLAKE3 hashing.
75Expected<std::unique_ptr<ondisk::UnifiedOnDiskCache>>
76createBuiltinUnifiedOnDiskCache(StringRef Path);
77
78/// \param UniDB A \p UnifiedOnDiskCache instance from \p
79/// createBuiltinUnifiedOnDiskCache.
80std::unique_ptr<ObjectStore> createObjectStoreFromUnifiedOnDiskCache(
81 std::shared_ptr<ondisk::UnifiedOnDiskCache> UniDB);
82
83/// \param UniDB A \p UnifiedOnDiskCache instance from \p
84/// createBuiltinUnifiedOnDiskCache.
85std::unique_ptr<ActionCache> createActionCacheFromUnifiedOnDiskCache(
86 std::shared_ptr<ondisk::UnifiedOnDiskCache> UniDB);
87
88/// Convenience wrapper for \c BuiltinObjectHasher.
89void hashingFunc(ArrayRef<ArrayRef<uint8_t>> Refs, ArrayRef<char> Data,
90 SmallVectorImpl<uint8_t> &Result);
91
92// FIXME: Proxy not portable. Maybe also error-prone?
93constexpr StringLiteral DefaultDirProxy = "/^llvm::cas::builtin::default";
94constexpr StringLiteral DefaultDir = "llvm.cas.builtin.default";
95
96} // end namespace builtin
97} // end namespace llvm::cas
98
99#endif // LLVM_LIB_CAS_BUILTINCAS_H
100