smollerGraph 0.20.2
A small graph database, from scratch
Loading...
Searching...
No Matches
name_index.h
Go to the documentation of this file.
1#ifndef SMOLLERGRAPH_NAME_INDEX_H_
2#define SMOLLERGRAPH_NAME_INDEX_H_
3
4#include <cstddef>
5#include <cstdint>
6#include <optional>
7#include <string>
8#include <string_view>
9#include <vector>
10
11#include "absl/status/status.h"
13#include "smollergraph/pager.h"
14
15namespace smollergraph {
16
17// An index from names to ids, kept in a file of pages as a B+ tree: leaves of
18// names in order, each with its id, and pages above them that say which page
19// holds which names. Finding a name reads a page on each level.
20class NameIndex {
21 public:
22 // The longest name it keeps, as long as a node's.
23 static constexpr size_t kMaxName = 47;
24
25 // Opens the index in the file at `path`, creating it when it isn't there.
26 absl::Status Open(const std::string& path);
27 // Opens an index in a file with no name, which is deleted once it's closed.
28 absl::Status OpenTemporary();
29 // Writes the changed pages to the file.
30 void Flush() { pool_.Flush(); }
31 uint64_t page_reads() const { return pager_.reads(); }
32 uint64_t page_writes() const { return pager_.writes(); }
33
34 // Returns the id kept for `name`, if there's one.
35 std::optional<uint32_t> Find(std::string_view name) const;
36 // Keeps `id` for `name`, which the index doesn't have yet.
37 void Insert(std::string_view name, uint32_t id);
38 // Returns up to `limit` names that start with `prefix`, in order.
39 std::vector<std::string> NamesStartingWith(std::string_view prefix,
40 size_t limit) const;
41
42 private:
43 // A name, padded with zeros, and the id kept for it. Above the leaves, the
44 // id is the page that holds the names from that one on.
45 struct Entry {
46 char name[kMaxName + 1];
47 uint32_t value;
48 };
49 static constexpr size_t kHeaderSize = 16;
50 static constexpr uint32_t kEntries =
51 (kPageSize - kHeaderSize) / sizeof(Entry);
52
53 // A page of the tree, as it's laid out in the file: a header of 16 bytes,
54 // then its entries, in name order. Above the leaves, `first` is the page for
55 // names before the first entry's. In a leaf, `next` is the leaf after it, or
56 // 0 for the last one, as page 0 is the header.
57 struct Node {
58 uint32_t leaf;
59 uint32_t count;
60 PageId first;
61 PageId next;
62 Entry entries[kEntries];
63 };
64 static_assert(sizeof(Node) <= kPageSize);
65
66 // Page 0 says which page is the root, and how many pages there are.
67 struct Header {
68 PageId root;
69 PageId pages;
70 };
71
72 // The name in an entry, up to its first zero.
73 static std::string_view NameOf(const Entry& entry);
74 // How many of a page's entries come before `name`, which is where `name` is,
75 // or where it would go.
76 static uint32_t Position(const Node& node, std::string_view name);
77 // The page below `node` that holds `name`, or would.
78 static PageId Child(const Node& node, std::string_view name);
79
80 // Inserts `entry` under page `id`. When a page splits, returns the entry that
81 // the page above needs, for the new page.
82 std::optional<Entry> InsertUnder(PageId id, const Entry& entry);
83 // Puts `entry` in `node`, page `id`, at `at`, splitting the page when it's
84 // full.
85 std::optional<Entry> Add(PageId id, Node& node, uint32_t at,
86 const Entry& entry);
87 void StoreHeader();
88
89 void Start();
90 Node Load(PageId id) const;
91 void Store(PageId id, const Node& node);
92
93 // Up to 16 MiB of the index in memory, about 150,000 names.
94 static constexpr size_t kPoolPages = 4096;
95
96 Pager pager_;
97 mutable BufferPool pool_{pager_, kPoolPages};
98 PageId root_ = 0;
99 PageId pages_ = 0;
100};
101
102} // namespace smollergraph
103
104#endif // SMOLLERGRAPH_NAME_INDEX_H_
void Flush()
Writes the changed pages back to the file, and keeps them.
An index from names to ids, kept in a file of pages as a B+ tree: leaves of names in order,...
Definition name_index.h:20
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.
uint64_t page_reads() const
Definition name_index.h:31
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
uint64_t page_writes() const
Definition name_index.h:32
static constexpr size_t kMaxName
The longest name it keeps, as long as a node's.
Definition name_index.h:23
void Flush()
Writes the changed pages to the file.
Definition name_index.h:30
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
uint64_t writes() const
Definition pager.h:43
uint64_t reads() const
How many pages this pager has read and written since it opened.
Definition pager.h:42
uint32_t PageId
Definition pager.h:19
constexpr size_t kPageSize
What the database reads and writes at a time: 4 KiB, the size of a block on most file systems and dis...
Definition pager.h:17
Path path
Definition plan.cc:115