-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache_dtable.cpp
More file actions
135 lines (125 loc) · 3.2 KB
/
Copy pathcache_dtable.cpp
File metadata and controls
135 lines (125 loc) · 3.2 KB
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
/* This file is part of the Casa Mia Datastore Project at UBC.It is distributed under the terms of
* version 2 of the GNU GPL. See the file LICENSE for details. */
#include "cache_dtable.h"
dtable::iter * cache_dtable::iterator(ATX_DEF) const
{
/* use the underlying iterator directly; we don't want to kill our cache iterating
* though everything, nor would it be easy to take advantage of our cache anyway */
/* returns base->iterator() */
return iterator_chain_usage(&chain, base, atx);
}
bool cache_dtable::present(const dtype & key, bool * found, ATX_DEF) const
{
if(atx != NO_ABORTABLE_TX)
return base->present(key, found, atx);
cache_map::const_iterator iter = cache.find(key);
if(iter != cache.end())
{
*found = true;
return (*iter).second.value.exists();
}
return base->present(key, found);
}
void cache_dtable::add_cache(const dtype & key, const blob & value, bool found) const
{
/* FIXME: this is a very simple eviction algorithm: evict
* the oldest item, regardless of when it has been used */
assert(!cache.count(key));
if(cache_size)
{
if(cache.size() == cache_size)
{
cache.erase(order.front());
order.pop();
}
assert(cache.size() < cache_size);
}
cache[key] = (entry) {value, found};
order.push(key);
assert(cache.size() == order.size());
}
blob cache_dtable::lookup(const dtype & key, bool * found, ATX_DEF) const
{
if(atx != NO_ABORTABLE_TX)
return base->lookup(key, found, atx);
cache_map::const_iterator iter = cache.find(key);
if(iter != cache.end())
{
*found = (*iter).second.found;
return (*iter).second.value;
}
blob value = base->lookup(key, found);
add_cache(key, value, *found);
return value;
}
int cache_dtable::insert(const dtype & key, const blob & blob, bool append, ATX_DEF)
{
if(atx != NO_ABORTABLE_TX)
return base->insert(key, blob, append, atx);
cache_map::iterator iter;
int value = base->insert(key, blob, append);
if(value < 0)
return value;
iter = cache.find(key);
if(iter != cache.end())
{
(*iter).second.found = true;
(*iter).second.value = blob;
}
else
add_cache(key, blob, true);
return value;
}
int cache_dtable::remove(const dtype & key, ATX_DEF)
{
if(atx != NO_ABORTABLE_TX)
return base->remove(key, atx);
cache_map::iterator iter;
int value = base->remove(key);
if(value < 0)
return value;
iter = cache.find(key);
if(iter != cache.end())
{
(*iter).second.found = false;
(*iter).second.value = blob();
}
else
add_cache(key, blob(), false);
return value;
}
int cache_dtable::init(int dfd, const char * file, const params & config, sys_journal * sysj)
{
int r;
const dtable_factory * factory;
params base_config;
if(base)
deinit();
if(!config.get("cache_size", &r, 0) || r < 0)
return -EINVAL;
cache_size = r;
factory = dtable_factory::lookup(config, "base");
if(!factory)
return -EINVAL;
if(!config.get("base_config", &base_config, params()))
return -EINVAL;
base = factory->open(dfd, file, base_config, sysj);
if(!base)
return -1;
ktype = base->key_type();
cmp_name = base->get_cmp_name();
return 0;
}
void cache_dtable::deinit()
{
if(base)
{
cache.clear();
while(!order.empty())
order.pop();
base->destroy();
base = NULL;
dtable::deinit();
}
}
DEFINE_WRAP_FACTORY(cache_dtable);