diff options
author | Greg Farnum <greg@inktank.com> | 2014-01-30 23:21:52 +0100 |
---|---|---|
committer | Somnath Roy <somnath.roy@sandisk.com> | 2014-08-14 23:11:12 +0200 |
commit | 4c2828ed1492beb0fa2c4ec2997dd6b282f2a0e9 (patch) | |
tree | 4cdaf7d488204a1d53f34957947c704e61702ca2 /src/common/shared_cache.hpp | |
parent | shared_cache: use a single lookup for lookup() too (diff) | |
download | ceph-4c2828ed1492beb0fa2c4ec2997dd6b282f2a0e9.tar.xz ceph-4c2828ed1492beb0fa2c4ec2997dd6b282f2a0e9.zip |
shared_cache: expose prior existence when inserting an element
The LRU now handles you attempting to insert multiple values for the
same key, by telling you that you've done so and returning the
existing value before it manages to muck up existing data.
The param 'existed' is not mandatory, default value is NULL.
Signed-off-by: Greg Farnum <greg@inktank.com>
Signed-off-by: Somnath Roy <somnath.roy@sandisk.com>
Diffstat (limited to 'src/common/shared_cache.hpp')
-rw-r--r-- | src/common/shared_cache.hpp | 27 |
1 files changed, 25 insertions, 2 deletions
diff --git a/src/common/shared_cache.hpp b/src/common/shared_cache.hpp index 2f5f05a0d64..6e05a6880a3 100644 --- a/src/common/shared_cache.hpp +++ b/src/common/shared_cache.hpp @@ -169,12 +169,35 @@ public: return val; } - VPtr add(K key, V *value) { + /*** + * Inserts a key if not present, or bumps it to the front of the LRU if + * it is, and then gives you a reference to the value. If the key already + * existed, you are responsible for deleting the new value you tried to + * insert. + * + * @param key The key to insert + * @param value The value that goes with the key + * @param existed Set to true if the value was already in the + * map, false otherwise + * @return A reference to the map's value for the given key + */ + VPtr add(K key, V *value, bool *existed = NULL) { VPtr val(value, Cleanup(this, key)); list<VPtr> to_release; { Mutex::Locker l(lock); - weak_refs.insert(make_pair(key, val)); + typename map<K, WeakVPtr>::iterator actual = weak_refs.lower_bound(key); + if (actual != weak_refs.end() && actual->first == key) { + if (existed) + *existed = true; + + return actual->second.lock(); + } + + if (existed) + *existed = false; + + weak_refs.insert(actual, make_pair(key, val)); lru_add(key, val, &to_release); } return val; |