libfetchers: Add CachingFilteringInputAccessor

Co-authored-by: Eelco Dolstra <edolstra@gmail.com>
This commit is contained in:
Robert Hensing 2024-01-12 17:16:59 +01:00
parent f68ad5acbb
commit d80c582b78
2 changed files with 23 additions and 0 deletions

View file

@ -80,4 +80,13 @@ ref<AllowListInputAccessor> AllowListInputAccessor::create(
return make_ref<AllowListInputAccessorImpl>(next, std::move(allowedPaths), std::move(makeNotAllowedError));
}
bool CachingFilteringInputAccessor::isAllowed(const CanonPath & path)
{
auto i = cache.find(path);
if (i != cache.end()) return i->second;
auto res = isAllowedUncached(path);
cache.emplace(path, res);
return res;
}
}

View file

@ -71,4 +71,18 @@ struct AllowListInputAccessor : public FilteringInputAccessor
using FilteringInputAccessor::FilteringInputAccessor;
};
/**
* A wrapping `InputAccessor` mix-in where `isAllowed()` caches the result of virtual `isAllowedUncached()`.
*/
struct CachingFilteringInputAccessor : FilteringInputAccessor
{
std::map<CanonPath, bool> cache;
using FilteringInputAccessor::FilteringInputAccessor;
bool isAllowed(const CanonPath & path) override;
virtual bool isAllowedUncached(const CanonPath & path) = 0;
};
}