Mercurial > hg > orthanc
comparison Core/MultiThreading/CacheIndex.cpp @ 282:915ed24547ea
cache lru policy
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Wed, 12 Dec 2012 13:17:42 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
280:77e526e6fdf8 | 282:915ed24547ea |
---|---|
1 /** | |
2 * Orthanc - A Lightweight, RESTful DICOM Store | |
3 * Copyright (C) 2012 Medical Physics Department, CHU of Liege, | |
4 * Belgium | |
5 * | |
6 * This program is free software: you can redistribute it and/or | |
7 * modify it under the terms of the GNU General Public License as | |
8 * published by the Free Software Foundation, either version 3 of the | |
9 * License, or (at your option) any later version. | |
10 * | |
11 * In addition, as a special exception, the copyright holders of this | |
12 * program give permission to link the code of its release with the | |
13 * OpenSSL project's "OpenSSL" library (or with modified versions of it | |
14 * that use the same license as the "OpenSSL" library), and distribute | |
15 * the linked executables. You must obey the GNU General Public License | |
16 * in all respects for all of the code used other than "OpenSSL". If you | |
17 * modify file(s) with this exception, you may extend this exception to | |
18 * your version of the file(s), but you are not obligated to do so. If | |
19 * you do not wish to do so, delete this exception statement from your | |
20 * version. If you delete this exception statement from all source files | |
21 * in the program, then also delete it here. | |
22 * | |
23 * This program is distributed in the hope that it will be useful, but | |
24 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
26 * General Public License for more details. | |
27 * | |
28 * You should have received a copy of the GNU General Public License | |
29 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
30 **/ | |
31 | |
32 | |
33 #include "CacheIndex.h" | |
34 | |
35 #include <cassert> | |
36 #include <string> | |
37 #include "../OrthancException.h" | |
38 #include "../IDynamicObject.h" | |
39 | |
40 namespace Orthanc | |
41 { | |
42 template <typename T, typename Payload> | |
43 void CacheIndex<T, Payload>::CheckInvariants() const | |
44 { | |
45 #ifndef NDEBUG | |
46 assert(index_.size() == queue_.size()); | |
47 | |
48 for (typename Index::const_iterator | |
49 it = index_.begin(); it != index_.end(); it++) | |
50 { | |
51 assert(it->second != queue_.end()); | |
52 assert(it->second->first == it->first); | |
53 } | |
54 #endif | |
55 } | |
56 | |
57 | |
58 template <typename T, typename Payload> | |
59 void CacheIndex<T, Payload>::Add(T id, Payload payload) | |
60 { | |
61 if (Contains(id)) | |
62 { | |
63 throw OrthancException(ErrorCode_BadSequenceOfCalls); | |
64 } | |
65 | |
66 queue_.push_front(std::make_pair(id, payload)); | |
67 index_[id] = queue_.begin(); | |
68 | |
69 CheckInvariants(); | |
70 } | |
71 | |
72 | |
73 template <typename T, typename Payload> | |
74 void CacheIndex<T, Payload>::TagAsMostRecent(T id) | |
75 { | |
76 if (!Contains(id)) | |
77 { | |
78 throw OrthancException(ErrorCode_InexistentItem); | |
79 } | |
80 | |
81 typename Index::iterator it = index_.find(id); | |
82 assert(it != index_.end()); | |
83 | |
84 std::pair<T, Payload> item = *(it->second); | |
85 | |
86 queue_.erase(it->second); | |
87 queue_.push_front(item); | |
88 index_[id] = queue_.begin(); | |
89 | |
90 CheckInvariants(); | |
91 } | |
92 | |
93 | |
94 template <typename T, typename Payload> | |
95 Payload CacheIndex<T, Payload>::Invalidate(T id) | |
96 { | |
97 if (!Contains(id)) | |
98 { | |
99 throw OrthancException(ErrorCode_InexistentItem); | |
100 } | |
101 | |
102 typename Index::iterator it = index_.find(id); | |
103 assert(it != index_.end()); | |
104 | |
105 Payload payload = it->second->second; | |
106 queue_.erase(it->second); | |
107 index_.erase(it); | |
108 | |
109 CheckInvariants(); | |
110 return payload; | |
111 } | |
112 | |
113 | |
114 template <typename T, typename Payload> | |
115 T CacheIndex<T, Payload>::RemoveOldest(Payload& payload) | |
116 { | |
117 if (IsEmpty()) | |
118 { | |
119 throw OrthancException(ErrorCode_BadSequenceOfCalls); | |
120 } | |
121 | |
122 std::pair<T, Payload> item = queue_.back(); | |
123 T oldest = item.first; | |
124 payload = item.second; | |
125 | |
126 queue_.pop_back(); | |
127 assert(index_.find(oldest) != index_.end()); | |
128 index_.erase(oldest); | |
129 | |
130 CheckInvariants(); | |
131 | |
132 return oldest; | |
133 } | |
134 | |
135 | |
136 // Explicit template instanciation for some data types | |
137 template class CacheIndex<std::string, NullType>; | |
138 template class CacheIndex<std::string, int>; | |
139 template class CacheIndex<const char*, IDynamicObject*>; | |
140 } |