in glean/rts/factset.cpp [105:159]
std::unique_ptr<FactIterator> FactSet::seek(
Pid type,
folly::ByteRange start,
size_t prefix_size) {
struct SeekIterator : FactIterator {
explicit SeekIterator(Index::map_t::iterator b, Index::map_t::iterator e)
: current(b)
, end(e)
{}
void next() override {
assert(current != end);
++current;
}
Fact::Ref get(Demand) override {
return current != end ? current->second->ref() : Fact::Ref::invalid();
}
Index::map_t::const_iterator current;
const Index::map_t::const_iterator end;
};
assert(prefix_size <= start.size());
if (const auto p = keys.lookup(type)) {
auto& entry = index[type];
// Check if the entry is up to date (i.e., has the same number of items as
// the key hashmap). If it doesn't, fill it.
if (!entry.withRLock([&](auto& map) { return map.size() == p->size(); })) {
entry.withWLock([&](auto& map) {
if (map.size() != p->size()) {
map.clear();
for (const Fact *fact : *p) {
map.insert({fact->key(), fact});
}
}
});
}
// The map for a pid is only created once so this is safe. We are *not*
// thread safe with respect to concurrent modifications of the FactSet
// as per spec.
auto& map = entry.unsafeGetUnlocked();
const auto next =
binary::lexicographicallyNext({start.data(), prefix_size});
return std::make_unique<SeekIterator>(
map.lower_bound(start),
next.empty()
? map.end()
: map.lower_bound(binary::byteRange(next)));
} else {
return std::make_unique<EmptyIterator>();
}
}