smollerGraph 0.20.2
A small graph database, from scratch
Loading...
Searching...
No Matches
token_store.h
Go to the documentation of this file.
1#ifndef SMOLLERGRAPH_TOKEN_STORE_H_
2#define SMOLLERGRAPH_TOKEN_STORE_H_
3
4#include <cstddef>
5#include <cstdint>
6#include <functional>
7#include <map>
8#include <optional>
9#include <string>
10#include <string_view>
11#include <vector>
12
13#include "absl/status/status.h"
15
16namespace smollergraph {
17
18// A number that stands for a name many records share, such as the label
19// Species, so a record keeps four bytes in place of the name.
20using TokenId = uint32_t;
21
22// The token for no name, such as the label of a node that hasn't got one.
23inline constexpr TokenId kNoToken = UINT32_MAX;
24
25// A token's name on disk. Its id in the file is the token.
27 char name[32];
28};
29
30// Names and the tokens that stand for them, kept in a file of records, and in
31// memory both ways, as there are only a few of them. Neo4j keeps its labels
32// and relationship types in stores like this one.
34 public:
35 // The longest name a token can stand for.
36 static constexpr size_t kMaxName = sizeof(TokenRecord::name) - 1;
37
38 // Opens the store in the file at `path`, creating it when it isn't there.
39 absl::Status Open(const std::string& path);
40 absl::Status OpenTemporary();
41 void Flush() { file_.Flush(); }
42 uint64_t page_reads() const { return file_.page_reads(); }
43 uint64_t page_writes() const { return file_.page_writes(); }
44
45 // Returns the token for `name`, adding it the first time. The name has to be
46 // at most kMaxName bytes long.
47 TokenId Add(std::string_view name);
48 // Returns the token for `name`, if the store has one.
49 std::optional<TokenId> Find(std::string_view name) const;
50 // Returns the name `token` stands for, or nothing for kNoToken.
51 std::string Name(TokenId token) const {
52 return token == kNoToken ? "" : names_[token];
53 }
54
55 private:
56 // Reads the file's names into memory, once it's open.
57 void Load();
58
60 // Each token's name, and each name's token.
61 std::vector<std::string> names_;
62 std::map<std::string, TokenId, std::less<>> tokens_;
63};
64
65} // namespace smollergraph
66
67#endif // SMOLLERGRAPH_TOKEN_STORE_H_
A file of fixed-size records, packed into pages after a header page, which holds how many records the...
Definition record_file.h:22
Names and the tokens that stand for them, kept in a file of records, and in memory both ways,...
Definition token_store.h:33
std::string Name(TokenId token) const
Returns the name token stands for, or nothing for kNoToken.
Definition token_store.h:51
uint64_t page_reads() const
Definition token_store.h:42
uint64_t page_writes() const
Definition token_store.h:43
TokenId Add(std::string_view name)
Returns the token for name, adding it the first time.
static constexpr size_t kMaxName
The longest name a token can stand for.
Definition token_store.h:36
absl::Status OpenTemporary()
std::optional< TokenId > Find(std::string_view name) const
Returns the token for name, if the store has one.
absl::Status Open(const std::string &path)
Opens the store in the file at path, creating it when it isn't there.
constexpr TokenId kNoToken
The token for no name, such as the label of a node that hasn't got one.
Definition token_store.h:23
uint32_t TokenId
A number that stands for a name many records share, such as the label Species, so a record keeps four...
Definition token_store.h:20
Path path
Definition plan.cc:115
A token's name on disk. Its id in the file is the token.
Definition token_store.h:26