| 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 | /// \file |
| 10 | /// Implements \c ObjectStore and \c ActionCache on top of a dynamically loaded |
| 11 | /// plugin that provides the C API in \c "llvm-c/CAS/PluginAPI_functions.h". |
| 12 | /// |
| 13 | /// The asynchronous entry points of the plugin API are not called yet; they |
| 14 | /// will be wired up once \c ObjectStore and \c ActionCache grow asynchronous |
| 15 | /// interfaces. |
| 16 | /// |
| 17 | //===----------------------------------------------------------------------===// |
| 18 | |
| 19 | #include "PluginAPI.h" |
| 20 | #include "llvm/ADT/ScopeExit.h" |
| 21 | #include "llvm/CAS/ActionCache.h" |
| 22 | #include "llvm/CAS/ObjectStore.h" |
| 23 | #include "llvm/Support/DynamicLibrary.h" |
| 24 | #include "llvm/Support/Error.h" |
| 25 | #include "llvm/Support/MemoryBuffer.h" |
| 26 | |
| 27 | using namespace llvm; |
| 28 | using namespace llvm::cas; |
| 29 | |
| 30 | namespace { |
| 31 | |
| 32 | class PluginCASContext : public CASContext { |
| 33 | public: |
| 34 | void printIDImpl(raw_ostream &OS, const CASID &ID) const final; |
| 35 | |
| 36 | StringRef getHashSchemaIdentifier() const final { return SchemaName; } |
| 37 | |
| 38 | static Expected<std::shared_ptr<PluginCASContext>> |
| 39 | create(StringRef PluginPath, StringRef OnDiskPath, |
| 40 | ArrayRef<std::pair<std::string, std::string>> PluginArgs); |
| 41 | |
| 42 | ~PluginCASContext() { Functions.cas_dispose(c_cas); } |
| 43 | |
| 44 | llcas_functions_t Functions{}; |
| 45 | llcas_cas_t c_cas = nullptr; |
| 46 | std::string SchemaName; |
| 47 | |
| 48 | static Error errorAndDispose(char *c_err, const llcas_functions_t &Funcs) { |
| 49 | Error E = createStringError(EC: inconvertibleErrorCode(), S: c_err); |
| 50 | Funcs.string_dispose(c_err); |
| 51 | return E; |
| 52 | } |
| 53 | |
| 54 | Error errorAndDispose(char *c_err) const { |
| 55 | return errorAndDispose(c_err, Funcs: Functions); |
| 56 | } |
| 57 | }; |
| 58 | |
| 59 | } // anonymous namespace |
| 60 | |
| 61 | void PluginCASContext::printIDImpl(raw_ostream &OS, const CASID &ID) const { |
| 62 | ArrayRef<uint8_t> Hash = ID.getHash(); |
| 63 | char *c_printed_id = nullptr; |
| 64 | char *c_err = nullptr; |
| 65 | if (Functions.digest_print(c_cas, llcas_digest_t{.data: Hash.data(), .size: Hash.size()}, |
| 66 | &c_printed_id, &c_err)) |
| 67 | report_fatal_error(Err: errorAndDispose(c_err)); |
| 68 | OS << c_printed_id; |
| 69 | Functions.string_dispose(c_printed_id); |
| 70 | } |
| 71 | |
| 72 | Expected<std::shared_ptr<PluginCASContext>> PluginCASContext::create( |
| 73 | StringRef PluginPath, StringRef OnDiskPath, |
| 74 | ArrayRef<std::pair<std::string, std::string>> PluginArgs) { |
| 75 | auto reportError = [PluginPath](const Twine &Description) -> Error { |
| 76 | std::error_code EC = inconvertibleErrorCode(); |
| 77 | return createStringError(EC, S: "error loading '" + PluginPath + |
| 78 | "': " + Description); |
| 79 | }; |
| 80 | |
| 81 | SmallString<256> PathBuf = PluginPath; |
| 82 | std::string ErrMsg; |
| 83 | sys::DynamicLibrary Lib = |
| 84 | sys::DynamicLibrary::getPermanentLibrary(filename: PathBuf.c_str(), errMsg: &ErrMsg); |
| 85 | if (!Lib.isValid()) |
| 86 | return reportError(ErrMsg); |
| 87 | |
| 88 | llcas_functions_t Functions{}; |
| 89 | |
| 90 | #define CASPLUGINAPI_FUNCTION(name, required) \ |
| 91 | if (!(Functions.name = (decltype(llcas_functions_t::name)) \ |
| 92 | Lib.getAddressOfSymbol("llcas_" #name))) { \ |
| 93 | if (required) \ |
| 94 | return reportError("failed symbol 'llcas_" #name "' lookup"); \ |
| 95 | } |
| 96 | #include "PluginAPI_functions.def" |
| 97 | #undef CASPLUGINAPI_FUNCTION |
| 98 | |
| 99 | llcas_cas_options_t c_opts = Functions.cas_options_create(); |
| 100 | scope_exit DisposeOptions([&]() { Functions.cas_options_dispose(c_opts); }); |
| 101 | |
| 102 | Functions.cas_options_set_client_version(c_opts, LLCAS_VERSION_MAJOR, |
| 103 | LLCAS_VERSION_MINOR); |
| 104 | SmallString<256> OnDiskPathBuf = OnDiskPath; |
| 105 | Functions.cas_options_set_ondisk_path(c_opts, OnDiskPathBuf.c_str()); |
| 106 | for (const auto &Pair : PluginArgs) { |
| 107 | char *c_err = nullptr; |
| 108 | if (Functions.cas_options_set_option(c_opts, Pair.first.c_str(), |
| 109 | Pair.second.c_str(), &c_err)) |
| 110 | return errorAndDispose(c_err, Funcs: Functions); |
| 111 | } |
| 112 | |
| 113 | char *c_err = nullptr; |
| 114 | llcas_cas_t c_cas = Functions.cas_create(c_opts, &c_err); |
| 115 | if (!c_cas) |
| 116 | return errorAndDispose(c_err, Funcs: Functions); |
| 117 | |
| 118 | char *c_schema = Functions.cas_get_hash_schema_name(c_cas); |
| 119 | std::string SchemaName = c_schema; |
| 120 | Functions.string_dispose(c_schema); |
| 121 | |
| 122 | auto Ctx = std::make_shared<PluginCASContext>(); |
| 123 | Ctx->Functions = Functions; |
| 124 | Ctx->c_cas = c_cas; |
| 125 | Ctx->SchemaName = std::move(SchemaName); |
| 126 | return Ctx; |
| 127 | } |
| 128 | |
| 129 | //===----------------------------------------------------------------------===// |
| 130 | // ObjectStore API |
| 131 | //===----------------------------------------------------------------------===// |
| 132 | |
| 133 | namespace { |
| 134 | |
| 135 | class PluginObjectStore : public ObjectStore { |
| 136 | public: |
| 137 | Expected<CASID> parseID(StringRef ID) final; |
| 138 | Expected<ObjectRef> store(ArrayRef<ObjectRef> Refs, |
| 139 | ArrayRef<char> Data) final; |
| 140 | Expected<ObjectRef> storeFromFile(StringRef Path) final; |
| 141 | Error exportDataToFile(ObjectHandle Node, StringRef Path) const final; |
| 142 | CASID getID(ObjectRef Ref) const final; |
| 143 | std::optional<ObjectRef> getReference(const CASID &ID) const final; |
| 144 | Expected<bool> isMaterialized(ObjectRef Ref) const final; |
| 145 | Expected<std::optional<ObjectHandle>> loadIfExists(ObjectRef Ref) final; |
| 146 | uint64_t getDataSize(ObjectHandle Node) const final; |
| 147 | Error forEachRef(ObjectHandle Node, |
| 148 | function_ref<Error(ObjectRef)> Callback) const final; |
| 149 | ObjectRef readRef(ObjectHandle Node, size_t I) const final; |
| 150 | size_t getNumRefs(ObjectHandle Node) const final; |
| 151 | ArrayRef<char> getData(ObjectHandle Node, |
| 152 | bool RequiresNullTerminator = false) const final; |
| 153 | std::unique_ptr<MemoryBuffer> |
| 154 | getStandaloneMemoryBufferImpl(ObjectHandle Node, StringRef Name, |
| 155 | bool RequiresNullTerminator) final; |
| 156 | Error validateObject(const CASID &ID) final { |
| 157 | // Not supported yet. Always return success. |
| 158 | return Error::success(); |
| 159 | } |
| 160 | |
| 161 | Error validate(bool CheckHash) const final; |
| 162 | |
| 163 | Error setSizeLimit(std::optional<uint64_t> SizeLimit) final; |
| 164 | Expected<std::optional<uint64_t>> getStorageSize() const final; |
| 165 | Error pruneStorageData() final; |
| 166 | |
| 167 | PluginObjectStore(std::shared_ptr<PluginCASContext>); |
| 168 | |
| 169 | /// Exposes \c makeObjectRef to the file-local helpers below. |
| 170 | ObjectRef makeRef(uint64_t InternalRef) const { |
| 171 | return makeObjectRef(InternalRef); |
| 172 | } |
| 173 | |
| 174 | std::shared_ptr<PluginCASContext> Ctx; |
| 175 | }; |
| 176 | |
| 177 | } // anonymous namespace |
| 178 | |
| 179 | Expected<CASID> PluginObjectStore::parseID(StringRef ID) { |
| 180 | // Use big enough stack so that we don't have to allocate in the heap. |
| 181 | SmallString<148> IDBuf(ID); |
| 182 | SmallVector<uint8_t, 68> BytesBuf(68); |
| 183 | |
| 184 | auto parseDigest = [&]() -> Expected<unsigned> { |
| 185 | char *c_err = nullptr; |
| 186 | unsigned NumBytes = Ctx->Functions.digest_parse( |
| 187 | Ctx->c_cas, IDBuf.c_str(), BytesBuf.data(), BytesBuf.size(), &c_err); |
| 188 | if (NumBytes == 0) |
| 189 | return Ctx->errorAndDispose(c_err); |
| 190 | return NumBytes; |
| 191 | }; |
| 192 | |
| 193 | Expected<unsigned> NumBytes = parseDigest(); |
| 194 | if (!NumBytes) |
| 195 | return NumBytes.takeError(); |
| 196 | |
| 197 | if (*NumBytes > BytesBuf.size()) { |
| 198 | BytesBuf.resize(N: *NumBytes); |
| 199 | NumBytes = parseDigest(); |
| 200 | if (!NumBytes) |
| 201 | return NumBytes.takeError(); |
| 202 | assert(*NumBytes == BytesBuf.size()); |
| 203 | } else { |
| 204 | BytesBuf.truncate(N: *NumBytes); |
| 205 | } |
| 206 | |
| 207 | return CASID::create(Context: Ctx.get(), Hash: toStringRef(Input: BytesBuf)); |
| 208 | } |
| 209 | |
| 210 | Expected<ObjectRef> PluginObjectStore::store(ArrayRef<ObjectRef> Refs, |
| 211 | ArrayRef<char> Data) { |
| 212 | SmallVector<llcas_objectid_t, 64> c_ids; |
| 213 | c_ids.reserve(N: Refs.size()); |
| 214 | for (ObjectRef Ref : Refs) { |
| 215 | c_ids.push_back(Elt: llcas_objectid_t{.opaque: Ref.getInternalRef(ExpectedCAS: *this)}); |
| 216 | } |
| 217 | |
| 218 | llcas_objectid_t c_stored_id; |
| 219 | char *c_err = nullptr; |
| 220 | if (Ctx->Functions.cas_store_object( |
| 221 | Ctx->c_cas, llcas_data_t{.data: Data.data(), .size: Data.size()}, c_ids.data(), |
| 222 | c_ids.size(), &c_stored_id, &c_err)) |
| 223 | return Ctx->errorAndDispose(c_err); |
| 224 | |
| 225 | return makeObjectRef(InternalRef: c_stored_id.opaque); |
| 226 | } |
| 227 | |
| 228 | Expected<ObjectRef> PluginObjectStore::storeFromFile(StringRef Path) { |
| 229 | if (!Ctx->Functions.cas_store_from_filepath) |
| 230 | return ObjectStore::storeFromFile(Path); |
| 231 | |
| 232 | llcas_objectid_t c_stored_id; |
| 233 | char *c_err = nullptr; |
| 234 | std::string PathStr = Path.str(); |
| 235 | if (Ctx->Functions.cas_store_from_filepath(Ctx->c_cas, PathStr.c_str(), |
| 236 | &c_stored_id, &c_err)) |
| 237 | return Ctx->errorAndDispose(c_err); |
| 238 | |
| 239 | return makeObjectRef(InternalRef: c_stored_id.opaque); |
| 240 | } |
| 241 | |
| 242 | Error PluginObjectStore::exportDataToFile(ObjectHandle Node, |
| 243 | StringRef Path) const { |
| 244 | if (!Ctx->Functions.loaded_object_export_data_to_filepath) |
| 245 | return ObjectStore::exportDataToFile(Node, Path); |
| 246 | |
| 247 | char *c_err = nullptr; |
| 248 | std::string PathStr = Path.str(); |
| 249 | if (Ctx->Functions.loaded_object_export_data_to_filepath( |
| 250 | Ctx->c_cas, llcas_loaded_object_t{.opaque: Node.getInternalRef(ExpectedCAS: *this)}, |
| 251 | PathStr.c_str(), &c_err)) |
| 252 | return Ctx->errorAndDispose(c_err); |
| 253 | |
| 254 | return Error::success(); |
| 255 | } |
| 256 | |
| 257 | static StringRef toStringRef(llcas_digest_t c_digest) { |
| 258 | return StringRef((const char *)c_digest.data, c_digest.size); |
| 259 | } |
| 260 | |
| 261 | CASID PluginObjectStore::getID(ObjectRef Ref) const { |
| 262 | llcas_objectid_t c_id{.opaque: Ref.getInternalRef(ExpectedCAS: *this)}; |
| 263 | llcas_digest_t c_digest = |
| 264 | Ctx->Functions.objectid_get_digest(Ctx->c_cas, c_id); |
| 265 | return CASID::create(Context: Ctx.get(), Hash: toStringRef(c_digest)); |
| 266 | } |
| 267 | |
| 268 | std::optional<ObjectRef> |
| 269 | PluginObjectStore::getReference(const CASID &ID) const { |
| 270 | ArrayRef<uint8_t> Hash = ID.getHash(); |
| 271 | llcas_objectid_t c_id; |
| 272 | char *c_err = nullptr; |
| 273 | if (Ctx->Functions.cas_get_objectid( |
| 274 | Ctx->c_cas, llcas_digest_t{.data: Hash.data(), .size: Hash.size()}, &c_id, &c_err)) |
| 275 | report_fatal_error(Err: Ctx->errorAndDispose(c_err)); |
| 276 | |
| 277 | return makeObjectRef(InternalRef: c_id.opaque); |
| 278 | } |
| 279 | |
| 280 | Expected<bool> PluginObjectStore::isMaterialized(ObjectRef Ref) const { |
| 281 | llcas_objectid_t c_id{.opaque: Ref.getInternalRef(ExpectedCAS: *this)}; |
| 282 | char *c_err = nullptr; |
| 283 | llcas_lookup_result_t c_result = Ctx->Functions.cas_contains_object( |
| 284 | Ctx->c_cas, c_id, /*globally=*/false, &c_err); |
| 285 | switch (c_result) { |
| 286 | case LLCAS_LOOKUP_RESULT_SUCCESS: |
| 287 | return true; |
| 288 | case LLCAS_LOOKUP_RESULT_NOTFOUND: |
| 289 | return false; |
| 290 | case LLCAS_LOOKUP_RESULT_ERROR: |
| 291 | return Ctx->errorAndDispose(c_err); |
| 292 | } |
| 293 | llvm_unreachable("unknown llcas_lookup_result_t value" ); |
| 294 | } |
| 295 | |
| 296 | Expected<std::optional<ObjectHandle>> |
| 297 | PluginObjectStore::loadIfExists(ObjectRef Ref) { |
| 298 | llcas_objectid_t c_id{.opaque: Ref.getInternalRef(ExpectedCAS: *this)}; |
| 299 | llcas_loaded_object_t c_obj; |
| 300 | char *c_err = nullptr; |
| 301 | llcas_lookup_result_t c_result = |
| 302 | Ctx->Functions.cas_load_object(Ctx->c_cas, c_id, &c_obj, &c_err); |
| 303 | switch (c_result) { |
| 304 | case LLCAS_LOOKUP_RESULT_SUCCESS: |
| 305 | return makeObjectHandle(InternalRef: c_obj.opaque); |
| 306 | case LLCAS_LOOKUP_RESULT_NOTFOUND: |
| 307 | return std::nullopt; |
| 308 | case LLCAS_LOOKUP_RESULT_ERROR: |
| 309 | return Ctx->errorAndDispose(c_err); |
| 310 | } |
| 311 | llvm_unreachable("unknown llcas_lookup_result_t value" ); |
| 312 | } |
| 313 | |
| 314 | namespace { |
| 315 | |
| 316 | class ObjectRefsWrapper { |
| 317 | public: |
| 318 | ObjectRefsWrapper(const ObjectHandle &Node, const PluginObjectStore &Store) |
| 319 | : Store(Store), Ctx(*Store.Ctx) { |
| 320 | llcas_loaded_object_t c_obj{.opaque: Node.getInternalRef(ExpectedCAS: Store)}; |
| 321 | this->c_refs = Ctx.Functions.loaded_object_get_refs(Ctx.c_cas, c_obj); |
| 322 | } |
| 323 | |
| 324 | size_t size() const { |
| 325 | return Ctx.Functions.object_refs_get_count(Ctx.c_cas, c_refs); |
| 326 | } |
| 327 | |
| 328 | ObjectRef operator[](size_t I) const { |
| 329 | llcas_objectid_t c_id = |
| 330 | Ctx.Functions.object_refs_get_id(Ctx.c_cas, c_refs, I); |
| 331 | return Store.makeRef(InternalRef: c_id.opaque); |
| 332 | } |
| 333 | |
| 334 | private: |
| 335 | const PluginObjectStore &Store; |
| 336 | PluginCASContext &Ctx; |
| 337 | llcas_object_refs_t c_refs; |
| 338 | }; |
| 339 | |
| 340 | } // namespace |
| 341 | |
| 342 | // FIXME: Replace forEachRef/readRef/getNumRefs APIs with an iterator interface. |
| 343 | Error PluginObjectStore::forEachRef( |
| 344 | ObjectHandle Node, function_ref<Error(ObjectRef)> Callback) const { |
| 345 | ObjectRefsWrapper Refs(Node, *this); |
| 346 | for (unsigned I = 0, E = Refs.size(); I != E; ++I) { |
| 347 | if (Error E = Callback(Refs[I])) |
| 348 | return E; |
| 349 | } |
| 350 | return Error::success(); |
| 351 | } |
| 352 | |
| 353 | ObjectRef PluginObjectStore::readRef(ObjectHandle Node, size_t I) const { |
| 354 | ObjectRefsWrapper Refs(Node, *this); |
| 355 | return Refs[I]; |
| 356 | } |
| 357 | |
| 358 | size_t PluginObjectStore::getNumRefs(ObjectHandle Node) const { |
| 359 | ObjectRefsWrapper Refs(Node, *this); |
| 360 | return Refs.size(); |
| 361 | } |
| 362 | |
| 363 | // FIXME: Remove getDataSize(ObjectHandle) from API requirement, |
| 364 | // \c getData(ObjectHandle) should be enough. |
| 365 | uint64_t PluginObjectStore::getDataSize(ObjectHandle Node) const { |
| 366 | ArrayRef<char> Data = getData(Node); |
| 367 | return Data.size(); |
| 368 | } |
| 369 | |
| 370 | ArrayRef<char> PluginObjectStore::getData(ObjectHandle Node, |
| 371 | bool RequiresNullTerminator) const { |
| 372 | // FIXME: Remove RequiresNullTerminator from ObjectStore API requirement? |
| 373 | // It is a requirement for the plugin API. |
| 374 | llcas_data_t c_data = Ctx->Functions.loaded_object_get_data( |
| 375 | Ctx->c_cas, llcas_loaded_object_t{.opaque: Node.getInternalRef(ExpectedCAS: *this)}); |
| 376 | return ArrayRef((const char *)c_data.data, c_data.size); |
| 377 | } |
| 378 | |
| 379 | namespace { |
| 380 | /// A MemoryBuffer over a plugin's standalone buffer, which it releases when |
| 381 | /// destroyed. It holds the dispose function directly rather than the |
| 382 | /// \c llcas_cas_t, since the point of the buffer is to outlive that. |
| 383 | class PluginStandaloneMemoryBuffer final : public MemoryBuffer { |
| 384 | public: |
| 385 | using DisposeFn = void (*)(llcas_data_t); |
| 386 | |
| 387 | PluginStandaloneMemoryBuffer(llcas_data_t Data, StringRef Name, |
| 388 | DisposeFn Dispose) |
| 389 | : Data(Data), Name(Name.str()), Dispose(Dispose) { |
| 390 | const char *Start = static_cast<const char *>(Data.data); |
| 391 | init(BufStart: Start, BufEnd: Start + Data.size, /*RequiresNullTerminator=*/true); |
| 392 | } |
| 393 | |
| 394 | ~PluginStandaloneMemoryBuffer() override { Dispose(Data); } |
| 395 | |
| 396 | StringRef getBufferIdentifier() const final { return Name; } |
| 397 | |
| 398 | BufferKind getBufferKind() const final { return MemoryBuffer_Malloc; } |
| 399 | |
| 400 | private: |
| 401 | llcas_data_t Data; |
| 402 | std::string Name; |
| 403 | DisposeFn Dispose; |
| 404 | }; |
| 405 | } // namespace |
| 406 | |
| 407 | std::unique_ptr<MemoryBuffer> PluginObjectStore::getStandaloneMemoryBufferImpl( |
| 408 | ObjectHandle Node, StringRef Name, bool RequiresNullTerminator) { |
| 409 | // Both halves are needed: without the disposer there is no way to release |
| 410 | // what the getter hands out. |
| 411 | if (!Ctx->Functions.loaded_object_get_standalone_data || |
| 412 | !Ctx->Functions.standalone_data_dispose) |
| 413 | return ObjectStore::getStandaloneMemoryBufferImpl(Node, Name, |
| 414 | RequiresNullTerminator); |
| 415 | |
| 416 | llcas_data_t c_data = Ctx->Functions.loaded_object_get_standalone_data( |
| 417 | Ctx->c_cas, llcas_loaded_object_t{.opaque: Node.getInternalRef(ExpectedCAS: *this)}); |
| 418 | if (!c_data.data) |
| 419 | return ObjectStore::getStandaloneMemoryBufferImpl(Node, Name, |
| 420 | RequiresNullTerminator); |
| 421 | |
| 422 | return std::make_unique<PluginStandaloneMemoryBuffer>( |
| 423 | args&: c_data, args&: Name, args&: Ctx->Functions.standalone_data_dispose); |
| 424 | } |
| 425 | |
| 426 | Error PluginObjectStore::setSizeLimit(std::optional<uint64_t> SizeLimit) { |
| 427 | if (Ctx->Functions.cas_set_ondisk_size_limit) { |
| 428 | char *c_err = nullptr; |
| 429 | if (Ctx->Functions.cas_set_ondisk_size_limit(Ctx->c_cas, |
| 430 | SizeLimit.value_or(u: 0), &c_err)) |
| 431 | return Ctx->errorAndDispose(c_err); |
| 432 | } |
| 433 | return Error::success(); |
| 434 | } |
| 435 | |
| 436 | Expected<std::optional<uint64_t>> PluginObjectStore::getStorageSize() const { |
| 437 | if (!Ctx->Functions.cas_get_ondisk_size) |
| 438 | return std::nullopt; |
| 439 | char *c_err = nullptr; |
| 440 | int64_t ret = Ctx->Functions.cas_get_ondisk_size(Ctx->c_cas, &c_err); |
| 441 | switch (ret) { |
| 442 | case -1: |
| 443 | return std::nullopt; |
| 444 | case -2: |
| 445 | return Ctx->errorAndDispose(c_err); |
| 446 | default: |
| 447 | return ret; |
| 448 | } |
| 449 | } |
| 450 | |
| 451 | Error PluginObjectStore::pruneStorageData() { |
| 452 | if (Ctx->Functions.cas_prune_ondisk_data) { |
| 453 | char *c_err = nullptr; |
| 454 | if (Ctx->Functions.cas_prune_ondisk_data(Ctx->c_cas, &c_err)) |
| 455 | return Ctx->errorAndDispose(c_err); |
| 456 | } |
| 457 | return Error::success(); |
| 458 | } |
| 459 | |
| 460 | Error PluginObjectStore::validate(bool CheckHash) const { |
| 461 | if (Ctx->Functions.cas_validate) { |
| 462 | char *c_err = nullptr; |
| 463 | if (Ctx->Functions.cas_validate(Ctx->c_cas, CheckHash, &c_err)) |
| 464 | return Ctx->errorAndDispose(c_err); |
| 465 | return Error::success(); |
| 466 | } |
| 467 | return createStringError(Fmt: "plugin cas doesn't support validation" ); |
| 468 | } |
| 469 | |
| 470 | PluginObjectStore::PluginObjectStore(std::shared_ptr<PluginCASContext> CASCtx) |
| 471 | : ObjectStore(*CASCtx), Ctx(std::move(CASCtx)) {} |
| 472 | |
| 473 | //===----------------------------------------------------------------------===// |
| 474 | // ActionCache API |
| 475 | //===----------------------------------------------------------------------===// |
| 476 | |
| 477 | namespace { |
| 478 | |
| 479 | class PluginActionCache : public ActionCache { |
| 480 | public: |
| 481 | Expected<std::optional<CASID>> getImpl(ArrayRef<uint8_t> ResolvedKey, |
| 482 | bool CanBeDistributed) const final; |
| 483 | |
| 484 | Error putImpl(ArrayRef<uint8_t> ResolvedKey, const CASID &Result, |
| 485 | bool CanBeDistributed) final; |
| 486 | |
| 487 | PluginActionCache(std::shared_ptr<PluginCASContext>); |
| 488 | |
| 489 | Error validate() const final; |
| 490 | |
| 491 | private: |
| 492 | std::shared_ptr<PluginCASContext> Ctx; |
| 493 | }; |
| 494 | |
| 495 | } // anonymous namespace |
| 496 | |
| 497 | Expected<std::optional<CASID>> |
| 498 | PluginActionCache::getImpl(ArrayRef<uint8_t> ResolvedKey, |
| 499 | bool CanBeDistributed) const { |
| 500 | llcas_objectid_t c_value; |
| 501 | char *c_err = nullptr; |
| 502 | llcas_lookup_result_t c_result = Ctx->Functions.actioncache_get_for_digest( |
| 503 | Ctx->c_cas, llcas_digest_t{.data: ResolvedKey.data(), .size: ResolvedKey.size()}, |
| 504 | &c_value, CanBeDistributed, &c_err); |
| 505 | switch (c_result) { |
| 506 | case LLCAS_LOOKUP_RESULT_SUCCESS: { |
| 507 | llcas_digest_t c_digest = |
| 508 | Ctx->Functions.objectid_get_digest(Ctx->c_cas, c_value); |
| 509 | return CASID::create(Context: Ctx.get(), Hash: toStringRef(c_digest)); |
| 510 | } |
| 511 | case LLCAS_LOOKUP_RESULT_NOTFOUND: |
| 512 | return std::nullopt; |
| 513 | case LLCAS_LOOKUP_RESULT_ERROR: |
| 514 | return Ctx->errorAndDispose(c_err); |
| 515 | } |
| 516 | llvm_unreachable("unknown llcas_lookup_result_t value" ); |
| 517 | } |
| 518 | |
| 519 | Error PluginActionCache::putImpl(ArrayRef<uint8_t> ResolvedKey, |
| 520 | const CASID &Result, bool CanBeDistributed) { |
| 521 | ArrayRef<uint8_t> Hash = Result.getHash(); |
| 522 | llcas_objectid_t c_value; |
| 523 | char *c_err = nullptr; |
| 524 | if (Ctx->Functions.cas_get_objectid(Ctx->c_cas, |
| 525 | llcas_digest_t{.data: Hash.data(), .size: Hash.size()}, |
| 526 | &c_value, &c_err)) |
| 527 | return Ctx->errorAndDispose(c_err); |
| 528 | |
| 529 | if (Ctx->Functions.actioncache_put_for_digest( |
| 530 | Ctx->c_cas, llcas_digest_t{.data: ResolvedKey.data(), .size: ResolvedKey.size()}, |
| 531 | c_value, CanBeDistributed, &c_err)) |
| 532 | return Ctx->errorAndDispose(c_err); |
| 533 | |
| 534 | return Error::success(); |
| 535 | } |
| 536 | |
| 537 | PluginActionCache::PluginActionCache(std::shared_ptr<PluginCASContext> CASCtx) |
| 538 | : ActionCache(*CASCtx), Ctx(std::move(CASCtx)) {} |
| 539 | |
| 540 | Error PluginActionCache::validate() const { |
| 541 | if (Ctx->Functions.actioncache_validate) { |
| 542 | char *c_err = nullptr; |
| 543 | if (Ctx->Functions.actioncache_validate(Ctx->c_cas, &c_err)) |
| 544 | return Ctx->errorAndDispose(c_err); |
| 545 | return Error::success(); |
| 546 | } |
| 547 | return createStringError(Fmt: "plugin action cache doesn't support validation" ); |
| 548 | } |
| 549 | |
| 550 | //===----------------------------------------------------------------------===// |
| 551 | // createPluginCASDatabases API |
| 552 | //===----------------------------------------------------------------------===// |
| 553 | |
| 554 | Expected<std::pair<std::shared_ptr<ObjectStore>, std::shared_ptr<ActionCache>>> |
| 555 | cas::createPluginCASDatabases( |
| 556 | StringRef PluginPath, StringRef OnDiskPath, |
| 557 | ArrayRef<std::pair<std::string, std::string>> PluginArgs) { |
| 558 | std::shared_ptr<PluginCASContext> Ctx; |
| 559 | if (Error E = PluginCASContext::create(PluginPath, OnDiskPath, PluginArgs) |
| 560 | .moveInto(Value&: Ctx)) |
| 561 | return std::move(E); |
| 562 | auto CAS = std::make_shared<PluginObjectStore>(args&: Ctx); |
| 563 | auto AC = std::make_shared<PluginActionCache>(args: std::move(Ctx)); |
| 564 | return std::make_pair(x: std::move(CAS), y: std::move(AC)); |
| 565 | } |
| 566 | |