Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow forwarding args to backing store via cache::read() #32844

Merged
merged 1 commit into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion vespalib/src/tests/stllike/cache_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,20 @@ class Map : public std::map<K, V> {
using const_iterator = typename std::map<K, V>::const_iterator;
using M = std::map<K, V>;
public:
bool read(const K & k, V & v) const {
mutable std::string _forwarded_arg;

bool read(const K& k, V& v) const {
const_iterator found = M::find(k);
bool ok(found != this->end());
if (ok) {
v = found->second;
}
return ok;
}
bool read(const K& k, V& v, std::string_view arg) const {
_forwarded_arg = arg;
return read(k, v);
}
void write(const K & k, const V & v) {
(*this)[k] = v;
}
Expand Down Expand Up @@ -196,4 +202,17 @@ TEST("testOnInsertonRemoveCalledWhenFull") {
EXPECT_FALSE(cache.hasKey(3));
}

TEST("Can forward arguments to backing store on cache miss") {
B m;
cache<CacheParam<P, B>> cache(m, -1);
m[123] = "foo";
EXPECT_EQUAL(cache.read(123, "hello cache world"), "foo");
EXPECT_EQUAL(m._forwarded_arg, "hello cache world");

// Already cached; no forwarding.
m._forwarded_arg.clear();
EXPECT_EQUAL(cache.read(123, "goodbye cache moon"), "foo");
EXPECT_EQUAL(m._forwarded_arg, "");
}

TEST_MAIN() { TEST_RUN_ALL(); }
6 changes: 5 additions & 1 deletion vespalib/src/vespa/vespalib/stllike/cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,12 @@ class cache : private lrucache_map<P>
* and the cache will be updated.
* If none exist an empty one will be created.
* Object is then put at head of LRU list.
*
* If any more arguments than `key` are present, they will be forwarded verbatim to the
* backing store in case of a cache miss. They are entirely ignored if there is a cache hit.
*/
V read(const K & key);
template <typename... BackingStoreArgs>
V read(const K& key, BackingStoreArgs&&... backing_store_args);

/**
* Update the cache and write through to backing store.
Expand Down
7 changes: 4 additions & 3 deletions vespalib/src/vespa/vespalib/stllike/cache.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,10 @@ cache<P>::getGuard() const {
return UniqueLock(_hashLock);
}

template< typename P >
template <typename P>
template <typename... BackingStoreArgs>
typename P::Value
cache<P>::read(const K & key)
cache<P>::read(const K& key, BackingStoreArgs&&... backing_store_args)
{
{
std::lock_guard guard(_hashLock);
Expand All @@ -105,7 +106,7 @@ cache<P>::read(const K & key)
}
}
V value;
if (_store.read(key, value)) {
if (_store.read(key, value, std::forward<BackingStoreArgs>(backing_store_args)...)) {
std::lock_guard guard(_hashLock);
Lru::insert(key, value);
_sizeBytes.store(sizeBytes() + calcSize(key, value), std::memory_order_relaxed);
Expand Down