LLVM 23.0.0git
DatabaseFile.cpp
Go to the documentation of this file.
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 This file implements the common abstractions for CAS database file.
10///
11//===----------------------------------------------------------------------===//
12
13#include "DatabaseFile.h"
14
15using namespace llvm;
16using namespace llvm::cas;
17using namespace llvm::cas::ondisk;
18
20 StringRef TableName, const Twine &Msg) {
22 Path + "[" + TableName + "]: " + Msg);
23}
24
25Error ondisk::checkTable(StringRef Label, size_t Expected, size_t Observed,
26 StringRef Path, StringRef TrieName) {
27 if (Expected == Observed)
28 return Error::success();
29 return createTableConfigError(std::errc::invalid_argument, Path, TrieName,
30 "mismatched " + Label +
31 " (expected: " + Twine(Expected) +
32 ", observed: " + Twine(Observed) + ")");
33}
34
36DatabaseFile::create(const Twine &Path, uint64_t Capacity,
37 std::shared_ptr<OnDiskCASLogger> Logger,
38 function_ref<Error(DatabaseFile &)> NewDBConstructor) {
39 // Constructor for if the file doesn't exist.
40 auto NewFileConstructor = [&](MappedFileRegionArena &Alloc) -> Error {
41 if (Alloc.capacity() <
42 sizeof(Header) + sizeof(MappedFileRegionArena::Header))
43 return createTableConfigError(std::errc::argument_out_of_domain,
44 Path.str(), "datafile",
45 "Allocator too small for header");
46 (void)new (Alloc.data()) Header{getMagic(), getVersion(), {0}};
47 DatabaseFile DB(Alloc);
48 return NewDBConstructor(DB);
49 };
50
51 // Get or create the file.
53 if (Error E =
54 MappedFileRegionArena::create(Path, Capacity, sizeof(Header),
55 std::move(Logger), NewFileConstructor)
56 .moveInto(Alloc))
57 return std::move(E);
58
59 return DatabaseFile::get(
60 std::make_unique<MappedFileRegionArena>(std::move(Alloc)));
61}
62
64 assert(Table);
65 assert(&Table.getRegion() == &getRegion());
66 int64_t ExistingRootOffset = 0;
67 const int64_t NewOffset =
68 reinterpret_cast<const char *>(&Table.getHeader()) - getRegion().data();
69 if (H->RootTableOffset.compare_exchange_strong(ExistingRootOffset, NewOffset))
70 return Error::success();
71
72 // Silently ignore attempts to set the root to itself.
73 if (ExistingRootOffset == NewOffset)
74 return Error::success();
75
76 // Return an proper error message.
77 TableHandle Root(getRegion(), ExistingRootOffset);
78 if (Root.getName() == Table.getName())
79 return createStringError(
80 make_error_code(std::errc::not_supported),
81 "collision with existing table of the same name '" + Table.getName() +
82 "'");
83
84 return createStringError(make_error_code(std::errc::not_supported),
85 "cannot add new table '" + Table.getName() +
86 "'"
87 " to existing root '" +
88 Root.getName() + "'");
89}
90
91std::optional<TableHandle> DatabaseFile::findTable(StringRef Name) {
92 int64_t RootTableOffset = H->RootTableOffset.load();
93 if (!RootTableOffset)
94 return std::nullopt;
95
96 TableHandle Root(getRegion(), RootTableOffset);
97 if (Root.getName() == Name)
98 return Root;
99
100 return std::nullopt;
101}
102
103Error DatabaseFile::validate(MappedFileRegion &Region) {
104 if (Region.size() < sizeof(Header))
105 return createStringError(std::errc::invalid_argument,
106 "database: missing header");
107
108 // Check the magic and version.
109 auto *H = reinterpret_cast<Header *>(Region.data());
110 if (H->Magic != getMagic())
111 return createStringError(std::errc::invalid_argument,
112 "database: bad magic");
113 if (H->Version != getVersion())
114 return createStringError(std::errc::invalid_argument,
115 "database: wrong version");
116
117 auto *MFH = reinterpret_cast<MappedFileRegionArena::Header *>(Region.data() +
118 sizeof(Header));
119 // Check the bump-ptr, which should point past the header.
120 if (MFH->BumpPtr.load() < (int64_t)sizeof(Header))
121 return createStringError(std::errc::invalid_argument,
122 "database: corrupt bump-ptr");
123
124 return Error::success();
125}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file declares the common interface for a DatabaseFile that is used to implement OnDiskCAS.
#define H(x, y, z)
Definition MD5.cpp:56
Lightweight error class with error context and mandatory checking.
Definition Error.h:160
static ErrorSuccess success()
Create a success value.
Definition Error.h:337
Tagged union holding either a T or a Error.
Definition Error.h:486
Logging utility - given an ordered specification of features, and assuming a scalar reward,...
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
Allocator for an owned mapped file region that supports thread-safe and process-safe bump pointer all...
static LLVM_ABI_FOR_TEST Expected< MappedFileRegionArena > create(const Twine &Path, uint64_t Capacity, uint64_t HeaderOffset, std::shared_ptr< ondisk::OnDiskCASLogger > Logger, function_ref< Error(MappedFileRegionArena &)> NewFileConstructor)
Create a MappedFileRegionArena.
std::optional< TableHandle > findTable(StringRef Name)
Find a table. May return null.
static constexpr uint32_t getVersion()
static constexpr uint32_t getMagic()
MappedFileRegion & getRegion()
Error addTable(TableHandle Table)
Add a table.
static Expected< DatabaseFile > create(const Twine &Path, uint64_t Capacity, std::shared_ptr< OnDiskCASLogger > Logger, function_ref< Error(DatabaseFile &)> NewDBConstructor)
Create the DatabaseFile at Path with Capacity.
Generic handle for a table.
const Header & getHeader() const
MappedFileRegion & getRegion() const
An efficient, type-erasing, non-owning reference to a callable.
LLVM_ABI char * data() const
Definition Path.cpp:1187
Error createTableConfigError(std::errc ErrC, StringRef Path, StringRef TableName, const Twine &Msg)
MappedFileRegionArena::RegionT MappedFileRegion
Error checkTable(StringRef Label, size_t Expected, size_t Observed, StringRef Path, StringRef TrieName)
This is an optimization pass for GlobalISel generic memory operations.
Definition Types.h:26
std::error_code make_error_code(BitcodeError E)
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
Definition Error.h:1306
Header for MappedFileRegionArena.