8#include "absl/status/status.h"
13 absl::Status status = file_.Open(
path);
14 if (status.ok()) Load();
19 absl::Status status = file_.OpenTemporary();
20 if (status.ok()) Load();
24void TokenStore::Load() {
27 for (
TokenId token = 0; token < file_.count(); ++token) {
28 names_.emplace_back(file_.Read(token).name);
29 tokens_.emplace(names_.back(), token);
34 if (std::optional<TokenId> found =
Find(name))
return *found;
36 std::memset(record.
name, 0,
sizeof(record.
name));
37 std::memcpy(record.
name, name.data(), std::min(name.size(),
kMaxName));
38 TokenId token = file_.Append(record);
39 names_.emplace_back(record.
name);
40 tokens_.emplace(names_.back(), token);
45 auto found = tokens_.find(name);
46 if (found == tokens_.end())
return std::nullopt;
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.
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.
uint32_t TokenId
A number that stands for a name many records share, such as the label Species, so a record keeps four...
A token's name on disk. Its id in the file is the token.