summaryrefslogtreecommitdiffstats
path: root/staging_vespalib/src/vespa/vespalib/stllike/cache.hpp
blob: 1b608578e8d219f1b6712c7e740186a3931f6384 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#pragma once

#include "cache.h"
#include "lrucache_map.hpp"

namespace vespalib {

template< typename P >
cache<P> &
cache<P>::maxElements(size_t elems) {
    Lru::maxElements(elems);
    return *this;
}

template< typename P >
cache<P> &
cache<P>::reserveElements(size_t elems) {
    Lru::reserve(elems);
    return *this;
}

template< typename P >
void
cache<P>::invalidate(const K & key) {
    vespalib::LockGuard guard(_hashLock);
    invalidate(guard, key);
}

template< typename P >
bool
cache<P>::hasKey(const K & key) const {
    vespalib::LockGuard guard(_hashLock);
    return hasKey(guard, key);
}

template< typename P >
bool
cache<P>::hasLock() const {
    return TryLock(_hashLock).hasLock();
}

template< typename P >
cache<P>::~cache() { }

template< typename P >
cache<P>::cache(BackingStore & b, size_t maxBytes) :
    Lru(Lru::UNLIMITED),
    _maxBytes(maxBytes),
    _sizeBytes(0),
    _hit(0),
    _miss(0),
    _noneExisting(0),
    _race(0),
    _insert(0),
    _write(0),
    _erase(0),
    _invalidate(0),
    _lookup(0),
    _store(b)
{ }

template< typename P >
bool
cache<P>::removeOldest(const value_type & v) {
    bool remove(Lru::removeOldest(v) || (sizeBytes() >= capacityBytes()));
    if (remove) {
        _sizeBytes -= calcSize(v.first, v.second._value);
    }
    return remove;
}

template< typename P >
vespalib::LockGuard
cache<P>::getGuard() {
    return vespalib::LockGuard(_hashLock);
}

template< typename P >
typename P::Value
cache<P>::read(const K & key)
{
    {
        vespalib::LockGuard guard(_hashLock);
        if (Lru::hasKey(key)) {
            _hit++;
            return (*this)[key];
        } else {
            _miss++;
        }
    }

    vespalib::LockGuard storeGuard(getLock(key));
    {
        vespalib::LockGuard guard(_hashLock);
        if (Lru::hasKey(key)) {
            // Somebody else just fetched it ahead of me.
            _race++;
            return (*this)[key];
        }
    }
    V value;
    if (_store.read(key, value)) {
        vespalib::LockGuard guard(_hashLock);
        Lru::insert(key, value);
        _sizeBytes += calcSize(key, value);
        _insert++;
    } else {
        vespalib::Atomic::postInc(&_noneExisting);
    }
    return value;
}

template< typename P >
void
cache<P>::write(const K & key, const V & value)
{
    vespalib::LockGuard storeGuard(getLock(key));
    {
        vespalib::LockGuard guard(_hashLock);
        (*this)[key] = value;
        _sizeBytes += calcSize(key, value);
        _write++;
    }
    _store.write(key, value);
}

template< typename P >
void
cache<P>::erase(const K & key)
{
    vespalib::LockGuard storeGuard(getLock(key));
    invalidate(key);
    _store.erase(key);
}

template< typename P >
void
cache<P>::invalidate(const vespalib::LockGuard & guard, const K & key)
{
    assert(guard.locks(_hashLock));
    (void) guard;
    if (Lru::hasKey(key)) {
        _sizeBytes -= calcSize(key, (*this)[key]);
        _invalidate++;
        Lru::erase(key);
    }
}

template< typename P >
bool
cache<P>::hasKey(const vespalib::LockGuard & guard, const K & key) const
{
    (void) guard;
    assert(guard.locks(_hashLock));
    _lookup++;
    return Lru::hasKey(key);
}

}