14 if (absl::Status status = pager_.
Open(
path); !status.ok()) {
18 return absl::OkStatus();
23 if (absl::Status status = pager_.
OpenTemporary(); !status.ok()) {
27 return absl::OkStatus();
31 Node node = Load(root_);
32 while (!node.leaf) node = Load(Child(node, name));
33 uint32_t at = Position(node, name);
34 if (at < node.count && NameOf(node.entries[at]) == name) {
35 return node.entries[at].value;
42 std::memcpy(entry.name, name.data(), std::min(name.size(),
kMaxName));
44 std::optional<Entry> split = InsertUnder(root_, entry);
50 root.entries[0] = *split;
58std::optional<NameIndex::Entry> NameIndex::InsertUnder(
PageId id,
61 if (node.leaf)
return Add(
id, node, Position(node, NameOf(entry)), entry);
62 std::optional<Entry> split = InsertUnder(Child(node, NameOf(entry)), entry);
63 if (!split)
return std::nullopt;
64 return Add(
id, node, Position(node, NameOf(*split)), *split);
67std::optional<NameIndex::Entry> NameIndex::Add(
PageId id, Node& node,
70 std::vector<Entry> entries(node.entries, node.entries + node.count);
71 entries.insert(entries.begin() + at, entry);
72 if (entries.size() <= kEntries) {
73 std::copy(entries.begin(), entries.end(), node.entries);
74 node.count =
static_cast<uint32_t
>(entries.size());
80 size_t half = entries.size() / 2;
82 right.leaf = node.leaf;
83 PageId right_id = pages_++;
84 Entry up = entries[half];
87 std::copy(entries.begin() + half, entries.end(), right.entries);
88 right.count =
static_cast<uint32_t
>(entries.size() - half);
91 right.next = node.next;
96 right.first = entries[half].value;
97 std::copy(entries.begin() + half + 1, entries.end(), right.entries);
98 right.count =
static_cast<uint32_t
>(entries.size() - half - 1);
100 std::copy(entries.begin(), entries.begin() + half, node.entries);
101 node.count =
static_cast<uint32_t
>(half);
103 Store(right_id, right);
108 size_t limit)
const {
109 Node node = Load(root_);
110 while (!node.leaf) node = Load(Child(node, prefix));
111 std::vector<std::string> names;
114 uint32_t at = Position(node, prefix);
115 while (names.size() < limit) {
116 if (at == node.count) {
117 if (node.next == 0)
break;
118 node = Load(node.next);
122 std::string_view name = NameOf(node.entries[at++]);
123 if (!name.starts_with(prefix))
break;
124 names.emplace_back(name);
129std::string_view NameIndex::NameOf(
const Entry& entry) {
130 return std::string_view(entry.name, strnlen(entry.name,
sizeof(entry.name)));
133uint32_t NameIndex::Position(
const Node& node, std::string_view name) {
134 const Entry* end = node.entries + node.count;
135 const Entry* found = std::lower_bound(
136 node.entries, end, name, [](
const Entry& entry, std::string_view name) {
137 return NameOf(entry) < name;
139 return static_cast<uint32_t
>(found - node.entries);
142PageId NameIndex::Child(
const Node& node, std::string_view name) {
145 const Entry* end = node.entries + node.count;
146 const Entry* after = std::upper_bound(
147 node.entries, end, name, [](std::string_view name,
const Entry& entry) {
148 return name < NameOf(entry);
150 return after == node.entries ? node.first : (after - 1)->value;
155void NameIndex::Start() {
160 Store(header.root, root);
161 PageHandle page = pool_.
Fetch(0);
162 std::memcpy(page.write().data(), &header,
sizeof(header));
164 PageHandle page = pool_.
Fetch(0);
165 std::memcpy(&header, page.read().data(),
sizeof(header));
168 pages_ = header.pages;
171void NameIndex::StoreHeader() {
172 Header header{root_, pages_};
173 PageHandle page = pool_.
Fetch(0);
174 std::memcpy(page.write().data(), &header,
sizeof(header));
179NameIndex::Node NameIndex::Load(
PageId id)
const {
180 PageHandle page = pool_.
Fetch(
id);
182 std::memcpy(&node, page.read().data(),
sizeof(node));
186void NameIndex::Store(
PageId id,
const Node& node) {
187 PageHandle page = pool_.
Fetch(
id);
188 std::memcpy(page.write().data(), &node,
sizeof(node));
PageHandle Fetch(PageId id)
Pins page id, reading it from the file when it isn't in a frame.
void Clear()
Writes the changed pages back, and forgets them all, before the pager opens another file.
absl::Status OpenTemporary()
Opens an index in a file with no name, which is deleted once it's closed.
absl::Status Open(const std::string &path)
Opens the index in the file at path, creating it when it isn't there.
std::vector< std::string > NamesStartingWith(std::string_view prefix, size_t limit) const
Returns up to limit names that start with prefix, in order.
void Insert(std::string_view name, uint32_t id)
Keeps id for name, which the index doesn't have yet.
static constexpr size_t kMaxName
The longest name it keeps, as long as a node's.
std::optional< uint32_t > Find(std::string_view name) const
Returns the id kept for name, if there's one.