Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / common / simple_cache.hpp
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- 
2 // vim: ts=8 sw=2 smarttab
3 /*
4  * Ceph - scalable distributed file system
5  *
6  * Copyright (C) 2004-2006 Sage Weil <sage@newdream.net>
7  *
8  * This is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License version 2.1, as published by the Free Software 
11  * Foundation.  See file COPYING.
12  * 
13  */
14
15 #ifndef CEPH_SIMPLECACHE_H
16 #define CEPH_SIMPLECACHE_H
17
18 #include "common/Mutex.h"
19 #include "include/unordered_map.h"
20
21 template <class K, class V, class C = std::less<K>, class H = std::hash<K> >
22 class SimpleLRU {
23   Mutex lock;
24   size_t max_size;
25   ceph::unordered_map<K, typename list<pair<K, V> >::iterator, H> contents;
26   list<pair<K, V> > lru;
27   map<K, V, C> pinned;
28
29   void trim_cache() {
30     while (lru.size() > max_size) {
31       contents.erase(lru.back().first);
32       lru.pop_back();
33     }
34   }
35
36   void _add(K key, V&& value) {
37     lru.emplace_front(key, std::move(value)); // can't move key because we access it below
38     contents[key] = lru.begin();
39     trim_cache();
40   }
41
42 public:
43   SimpleLRU(size_t max_size) : lock("SimpleLRU::lock"), max_size(max_size) {
44     contents.rehash(max_size);
45   }
46
47   void pin(K key, V val) {
48     Mutex::Locker l(lock);
49     pinned.emplace(std::move(key), std::move(val));
50   }
51
52   void clear_pinned(K e) {
53     Mutex::Locker l(lock);
54     for (typename map<K, V, C>::iterator i = pinned.begin();
55          i != pinned.end() && i->first <= e;
56          pinned.erase(i++)) {
57       typename ceph::unordered_map<K, typename list<pair<K, V> >::iterator, H>::iterator iter =
58         contents.find(i->first);
59       if (iter == contents.end())
60         _add(i->first, std::move(i->second));
61       else
62         lru.splice(lru.begin(), lru, iter->second);
63     }
64   }
65
66   void clear(K key) {
67     Mutex::Locker l(lock);
68     typename ceph::unordered_map<K, typename list<pair<K, V> >::iterator, H>::iterator i =
69       contents.find(key);
70     if (i == contents.end())
71       return;
72     lru.erase(i->second);
73     contents.erase(i);
74   }
75
76   void set_size(size_t new_size) {
77     Mutex::Locker l(lock);
78     max_size = new_size;
79     trim_cache();
80   }
81
82   bool lookup(K key, V *out) {
83     Mutex::Locker l(lock);
84     typename ceph::unordered_map<K, typename list<pair<K, V> >::iterator, H>::iterator i =
85       contents.find(key);
86     if (i != contents.end()) {
87       *out = i->second->second;
88       lru.splice(lru.begin(), lru, i->second);
89       return true;
90     }
91     typename map<K, V, C>::iterator i_pinned = pinned.find(key);
92     if (i_pinned != pinned.end()) {
93       *out = i_pinned->second;
94       return true;
95     }
96     return false;
97   }
98
99   void add(K key, V value) {
100     Mutex::Locker l(lock);
101     _add(std::move(key), std::move(value));
102   }
103 };
104
105 #endif