blob: f02079e0bdb8224bb6a7daba74736630ef4c7ce6 [file] [log] [blame]
Jiewen Tan99135962014-09-20 02:18:53 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Yingdi Yu404eafd2016-03-06 14:54:25 -08003 * Copyright (c) 2013-2016 Regents of the University of California.
Jiewen Tan99135962014-09-20 02:18:53 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6 *
7 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
20 */
21
22#ifndef NDN_UTIL_IN_MEMORY_STORAGE_LFU_HPP
23#define NDN_UTIL_IN_MEMORY_STORAGE_LFU_HPP
24
25#include "in-memory-storage.hpp"
26
27#include <boost/multi_index/member.hpp>
28#include <boost/multi_index_container.hpp>
29#include <boost/multi_index/ordered_index.hpp>
30#include <boost/multi_index/hashed_index.hpp>
31#include <boost/multi_index/identity.hpp>
32
33namespace ndn {
34namespace util {
35
36/** @brief Provides an in-memory storage with Least Frequently Used (LFU) replacement policy.
37 * @note The frequency right now is usage count.
38 * @sa https://en.wikipedia.org/w/index.php?title=Least_frequently_used&oldid=604542656
39 */
40class InMemoryStorageLfu : public InMemoryStorage
41{
42public:
43 explicit
44 InMemoryStorageLfu(size_t limit = 10);
45
Yingdi Yu404eafd2016-03-06 14:54:25 -080046 explicit
47 InMemoryStorageLfu(boost::asio::io_service& ioService, size_t limit = 10);
48
Jiewen Tan99135962014-09-20 02:18:53 -070049 virtual
50 ~InMemoryStorageLfu();
51
Junxiao Shi99848502014-10-13 19:22:22 -070052NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PROTECTED:
Jiewen Tan99135962014-09-20 02:18:53 -070053 /** @brief Removes one Data packet from in-memory storage based on LFU, i.e. evict the least
54 * frequently accessed Data packet
55 * @return{ whether the Data was removed }
56 */
57 virtual bool
58 evictItem();
59
60 /** @brief Update the entry when the entry is returned by the find() function,
61 * increment the frequency according to LFU
62 */
63 virtual void
64 afterAccess(InMemoryStorageEntry* entry);
65
66 /** @brief Update the entry after a entry is successfully inserted, add it to the cleanupIndex
67 */
68 virtual void
69 afterInsert(InMemoryStorageEntry* entry);
70
71 /** @brief Update the entry or other data structures before a entry is successfully erased,
72 * erase it from the cleanupIndex
73 */
74 virtual void
75 beforeErase(InMemoryStorageEntry* entry);
76
77private:
78 //binds frequency and entry together
79 struct CleanupEntry
80 {
81 InMemoryStorageEntry* entry;
82 uint64_t frequency;//could potentially be overflowed
83 };
84
85 /** @brief Function to increment frequency of the entry in the CleanupEntry
86 */
87 static inline void
88 incrementFrequency(CleanupEntry& cleanupEntry)
89 {
90 ++cleanupEntry.frequency;
91 }
92
93private:
94 //multi_index_container to implement LFU
95 class byFrequency;
96 class byEntity;
97
98 typedef boost::multi_index_container<
99 CleanupEntry,
100 boost::multi_index::indexed_by<
101
102 // by Entry itself
103 boost::multi_index::hashed_unique<
104 boost::multi_index::tag<byEntity>,
105 boost::multi_index::member<CleanupEntry, InMemoryStorageEntry*, &CleanupEntry::entry>
106 >,
107
108 // by frequency (LFU)
109 boost::multi_index::ordered_non_unique<
110 boost::multi_index::tag<byFrequency>,
111 boost::multi_index::member<CleanupEntry, uint64_t, &CleanupEntry::frequency>,
112 std::less<uint64_t>
113 >
114
115 >
116 > CleanupIndex;
117
118 CleanupIndex m_cleanupIndex;
119};
120
121} // namespace util
122} // namespace ndn
123
124#endif // NDN_UTIL_IN_MEMORY_STORAGE_LFU_HPP