summaryrefslogtreecommitdiffstats
path: root/staging_vespalib
diff options
context:
space:
mode:
authorHenning Baldersheim <balder@yahoo-inc.com>2016-08-29 16:04:25 +0200
committerGitHub <noreply@github.com>2016-08-29 16:04:25 +0200
commitddeaaa0e30fcd6a5bb927b4eea8fa32d6f283686 (patch)
tree70d7e0eef431b6383e5aef2990cb2dafc2fcd001 /staging_vespalib
parent7a7c125cd9aacaf34425dcfe7c83333c5fc67073 (diff)
parentdfb249c85e812b5a72589d23b503aa2352ee977d (diff)
Merge pull request #482 from yahoo/balder/add-visit-cache-2
Balder/add visit cache 2
Diffstat (limited to 'staging_vespalib')
-rw-r--r--staging_vespalib/src/vespa/vespalib/stllike/cache.h33
-rw-r--r--staging_vespalib/src/vespa/vespalib/stllike/lrucache_map.h13
2 files changed, 37 insertions, 9 deletions
diff --git a/staging_vespalib/src/vespa/vespalib/stllike/cache.h b/staging_vespalib/src/vespa/vespalib/stllike/cache.h
index ed4eb4350c0..02795510870 100644
--- a/staging_vespalib/src/vespa/vespalib/stllike/cache.h
+++ b/staging_vespalib/src/vespa/vespalib/stllike/cache.h
@@ -42,8 +42,8 @@ struct CacheParam : public P
template< typename P >
class cache : private lrucache_map<P>
{
-private:
typedef lrucache_map<P> Lru;
+protected:
typedef typename P::BackingStore BackingStore;
typedef typename P::Hash Hash;
typedef typename P::Key K;
@@ -91,7 +91,10 @@ public:
/**
* This simply erases the object from the cache.
*/
- void invalidate(const K & key);
+ void invalidate(const K & key) {
+ vespalib::LockGuard guard(_hashLock);
+ invalidate(guard, key);
+ }
/**
* Return the object with the given key. If it does not exist, the backing store will be consulted.
@@ -111,7 +114,10 @@ public:
* Tell if an object with given key exists in the cache.
* Does not alter the LRU list.
*/
- bool hasKey(const K & key) const;
+ bool hasKey(const K & key) const {
+ vespalib::LockGuard guard(_hashLock);
+ return hasKey(guard, key);
+ }
size_t getHit() const { return _hit; }
size_t getMiss() const { return _miss; }
@@ -123,6 +129,11 @@ public:
size_t getInvalidate() const { return _invalidate; }
size_t getlookup() const { return _lookup; }
+protected:
+ vespalib::LockGuard getGuard();
+ void invalidate(const vespalib::LockGuard & guard, const K & key);
+ bool hasKey(const vespalib::LockGuard & guard, const K & key) const;
+ bool hasLock() const { return TryLock(_hashLock).hasLock(); }
private:
/**
* Called when an object is inserted, to see if the LRU should be removed.
@@ -130,7 +141,7 @@ private:
* The obvious extension is when you are storing pointers and want to cap
* on the real size of the object pointed to.
*/
- virtual bool removeOldest(const value_type & v);
+ bool removeOldest(const value_type & v) override;
size_t calcSize(const K & k, const V & v) const { return sizeof(value_type) + _sizeK(k) + _sizeV(v); }
vespalib::Lock & getLock(const K & k) {
size_t h(_hasher(k));
@@ -184,6 +195,12 @@ cache<P>::removeOldest(const value_type & v) {
}
template< typename P >
+vespalib::LockGuard
+cache<P>::getGuard() {
+ return vespalib::LockGuard(_hashLock);
+}
+
+template< typename P >
typename P::Value
cache<P>::read(const K & key)
{
@@ -243,9 +260,9 @@ cache<P>::erase(const K & key)
template< typename P >
void
-cache<P>::invalidate(const K & key)
+cache<P>::invalidate(const vespalib::LockGuard & guard, const K & key)
{
- vespalib::LockGuard guard(_hashLock);
+ assert(guard.locks(_hashLock));
if (Lru::hasKey(key)) {
_sizeBytes -= calcSize(key, (*this)[key]);
_invalidate++;
@@ -255,9 +272,9 @@ cache<P>::invalidate(const K & key)
template< typename P >
bool
-cache<P>::hasKey(const K & key) const
+cache<P>::hasKey(const vespalib::LockGuard & guard, const K & key) const
{
- vespalib::LockGuard guard(_hashLock);
+ assert(guard.locks(_hashLock));
_lookup++;
return Lru::hasKey(key);
}
diff --git a/staging_vespalib/src/vespa/vespalib/stllike/lrucache_map.h b/staging_vespalib/src/vespa/vespalib/stllike/lrucache_map.h
index 993163a82cd..d0fb4259b30 100644
--- a/staging_vespalib/src/vespa/vespalib/stllike/lrucache_map.h
+++ b/staging_vespalib/src/vespa/vespalib/stllike/lrucache_map.h
@@ -120,7 +120,11 @@ public:
* Object is then put at head of LRU list.
*/
insert_result insert(const K & key, const V & value) {
- return insert(value_type(key, LV(value)));
+ insert_result res = insert(value_type(key, LV(value)));
+ if (res.second) {
+ onInsert(key);
+ }
+ return res;
}
/**
@@ -155,6 +159,12 @@ public:
(void) v;
return (size() > capacity());
}
+ virtual void onRemove(const K & key) {
+ (void) key;
+ }
+ virtual void onInsert(const K & key) {
+ (void) key;
+ }
/**
* Method for testing that internal consitency is good.
@@ -255,6 +265,7 @@ void
lrucache_map<P>::erase(const K & key) {
internal_iterator it = HashTable::find(key);
if (it != HashTable::end()) {
+ onRemove(key);
LV & v = it->second;
if (v._prev != LinkedValueBase::npos) {
HashTable::getByInternalIndex(v._prev).second._next = v._next;