smollerGraph 0.20.2
A small graph database, from scratch
Loading...
Searching...
No Matches
name_index.cc
Go to the documentation of this file.
2
3#include <algorithm>
4#include <cstdio>
5#include <cstdlib>
6#include <cstring>
7#include <string>
8#include <vector>
9
10namespace smollergraph {
11
12absl::Status NameIndex::Open(const std::string& path) {
13 pool_.Clear();
14 if (absl::Status status = pager_.Open(path); !status.ok()) {
15 return status;
16 }
17 Start();
18 return absl::OkStatus();
19}
20
22 pool_.Clear();
23 if (absl::Status status = pager_.OpenTemporary(); !status.ok()) {
24 return status;
25 }
26 Start();
27 return absl::OkStatus();
28}
29
30std::optional<uint32_t> NameIndex::Find(std::string_view name) const {
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;
36 }
37 return std::nullopt;
38}
39
40void NameIndex::Insert(std::string_view name, uint32_t id) {
41 Entry entry{};
42 std::memcpy(entry.name, name.data(), std::min(name.size(), kMaxName));
43 entry.value = id;
44 std::optional<Entry> split = InsertUnder(root_, entry);
45 if (split) {
46 // The root split, so a new root goes above its two halves, and the tree
47 // is a level taller.
48 Node root{};
49 root.first = root_;
50 root.entries[0] = *split;
51 root.count = 1;
52 root_ = pages_++;
53 Store(root_, root);
54 }
55 StoreHeader();
56}
57
58std::optional<NameIndex::Entry> NameIndex::InsertUnder(PageId id,
59 const Entry& entry) {
60 Node node = Load(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);
65}
66
67std::optional<NameIndex::Entry> NameIndex::Add(PageId id, Node& node,
68 uint32_t at,
69 const Entry& entry) {
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());
75 Store(id, node);
76 return std::nullopt;
77 }
78 // The page is full, so it keeps the first half, and a new page on its right
79 // takes the rest.
80 size_t half = entries.size() / 2;
81 Node right{};
82 right.leaf = node.leaf;
83 PageId right_id = pages_++;
84 Entry up = entries[half];
85 up.value = right_id;
86 if (node.leaf) {
87 std::copy(entries.begin() + half, entries.end(), right.entries);
88 right.count = static_cast<uint32_t>(entries.size() - half);
89 // The new leaf goes between this one and the next, so a scan can go from
90 // leaf to leaf in name order.
91 right.next = node.next;
92 node.next = right_id;
93 } else {
94 // Above the leaves, the middle entry moves up, and its page becomes the
95 // first of the right half.
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);
99 }
100 std::copy(entries.begin(), entries.begin() + half, node.entries);
101 node.count = static_cast<uint32_t>(half);
102 Store(id, node);
103 Store(right_id, right);
104 return up;
105}
106
107std::vector<std::string> NameIndex::NamesStartingWith(std::string_view prefix,
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;
112 // The names that start with `prefix` sit together, from where `prefix`
113 // would go, and may go on into the leaves after this one.
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);
119 at = 0;
120 continue;
121 }
122 std::string_view name = NameOf(node.entries[at++]);
123 if (!name.starts_with(prefix)) break;
124 names.emplace_back(name);
125 }
126 return names;
127}
128
129std::string_view NameIndex::NameOf(const Entry& entry) {
130 return std::string_view(entry.name, strnlen(entry.name, sizeof(entry.name)));
131}
132
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;
138 });
139 return static_cast<uint32_t>(found - node.entries);
140}
141
142PageId NameIndex::Child(const Node& node, std::string_view name) {
143 // An entry's page holds the names from its name on, so `name` is under the
144 // last entry whose name isn't after it, or under `first`, when they all are.
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);
149 });
150 return after == node.entries ? node.first : (after - 1)->value;
151}
152
153// A new file gets a header and an empty leaf as its root. A file that has them
154// says where its root is.
155void NameIndex::Start() {
156 Header header{1, 2};
157 if (pager_.page_count() == 0) {
158 Node root{};
159 root.leaf = 1;
160 Store(header.root, root);
161 PageHandle page = pool_.Fetch(0);
162 std::memcpy(page.write().data(), &header, sizeof(header));
163 } else {
164 PageHandle page = pool_.Fetch(0);
165 std::memcpy(&header, page.read().data(), sizeof(header));
166 }
167 root_ = header.root;
168 pages_ = header.pages;
169}
170
171void NameIndex::StoreHeader() {
172 Header header{root_, pages_};
173 PageHandle page = pool_.Fetch(0);
174 std::memcpy(page.write().data(), &header, sizeof(header));
175}
176
177// A page is copied in and out of its frame whole, as a Node, which is safer
178// than pointing into the frame's bytes as if they were one.
179NameIndex::Node NameIndex::Load(PageId id) const {
180 PageHandle page = pool_.Fetch(id);
181 Node node;
182 std::memcpy(&node, page.read().data(), sizeof(node));
183 return node;
184}
185
186void NameIndex::Store(PageId id, const Node& node) {
187 PageHandle page = pool_.Fetch(id);
188 std::memcpy(page.write().data(), &node, sizeof(node));
189}
190
191} // namespace smollergraph
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.
Definition name_index.cc:21
absl::Status Open(const std::string &path)
Opens the index in the file at path, creating it when it isn't there.
Definition name_index.cc:12
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.
Definition name_index.cc:40
static constexpr size_t kMaxName
The longest name it keeps, as long as a node's.
Definition name_index.h:23
std::optional< uint32_t > Find(std::string_view name) const
Returns the id kept for name, if there's one.
Definition name_index.cc:30
absl::Status Open(const std::string &path)
Opens the file at path, creating it when it isn't there.
Definition pager.cc:32
absl::Status OpenTemporary()
Opens a file with no name, which is deleted once it's closed.
Definition pager.cc:36
PageId page_count() const
Definition pager.h:35
uint32_t PageId
Definition pager.h:19
Path path
Definition plan.cc:115