blob: 5a24bded5326a3f799f8f0ac1762152eed2cbbd1 [file] [log] [blame]
Junxiao Shi0fcb41e2014-01-24 10:29:43 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shi5640ec82015-01-07 21:51:19 -07003 * Copyright (c) 2014-2015, Regents of the University of California,
4 * Arizona Board of Regents,
5 * Colorado State University,
6 * University Pierre & Marie Curie, Sorbonne University,
7 * Washington University in St. Louis,
8 * Beijing Institute of Technology,
9 * The University of Memphis.
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080010 *
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070011 * This file is part of NFD (Named Data Networking Forwarding Daemon).
12 * See AUTHORS.md for complete list of NFD authors and contributors.
13 *
14 * NFD is free software: you can redistribute it and/or modify it under the terms
15 * of the GNU General Public License as published by the Free Software Foundation,
16 * either version 3 of the License, or (at your option) any later version.
17 *
18 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
19 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
20 * PURPOSE. See the GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along with
23 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
Junxiao Shia9388182014-12-13 23:16:09 -070024 */
25
26/** \file
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070027 *
Junxiao Shia9388182014-12-13 23:16:09 -070028 * \brief implements the ContentStore
29 *
30 * This ContentStore implementation consists of two data structures,
31 * a Table, and a set of cleanup queues.
32 *
33 * The Table is a container (std::set) sorted by full Names of stored Data packets.
34 * Data packets are wrapped in Entry objects.
35 * Each Entry contain the Data packet itself,
36 * and a few addition attributes such as the staleness of the Data packet.
37 *
38 * The cleanup queues are three doubly linked lists which stores Table iterators.
39 * The three queues keep track of unsolicited, stale, and fresh Data packet, respectively.
40 * Table iterator is placed into, removed from, and moved between suitable queues
41 * whenever an Entry is added, removed, or has other attribute changes.
42 * The Table iterator of an Entry should be in exactly one queue at any moment.
43 * Within each queue, the iterators are kept in first-in-first-out order.
44 * Eviction procedure exhausts the first queue before moving onto the next queue,
45 * in the order of unsolicited, stale, and fresh queue.
Junxiao Shi0fcb41e2014-01-24 10:29:43 -070046 */
47
Alexander Afanasyev613e2a92014-04-15 13:36:58 -070048#ifndef NFD_DAEMON_TABLE_CS_HPP
49#define NFD_DAEMON_TABLE_CS_HPP
Alexander Afanasyevb927a3a2014-01-24 10:41:47 -080050
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050051#include "cs-policy.hpp"
52#include "cs-internal.hpp"
Junxiao Shifc206962015-01-16 11:12:22 -070053#include "cs-entry-impl.hpp"
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050054#include <ndn-cxx/util/signal.hpp>
Junxiao Shifc206962015-01-16 11:12:22 -070055#include <boost/iterator/transform_iterator.hpp>
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -040056
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080057namespace nfd {
Junxiao Shia9388182014-12-13 23:16:09 -070058namespace cs {
Junxiao Shi0fcb41e2014-01-24 10:29:43 -070059
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050060unique_ptr<Policy>
61makeDefaultPolicy();
62
Junxiao Shia9388182014-12-13 23:16:09 -070063/** \brief represents the ContentStore
Junxiao Shi0fcb41e2014-01-24 10:29:43 -070064 */
Alexander Afanasyevb927a3a2014-01-24 10:41:47 -080065class Cs : noncopyable
Junxiao Shi0fcb41e2014-01-24 10:29:43 -070066{
67public:
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080068 explicit
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050069 Cs(size_t nMaxPackets = 10, unique_ptr<Policy> policy = makeDefaultPolicy());
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080070
Junxiao Shi0fcb41e2014-01-24 10:29:43 -070071 /** \brief inserts a Data packet
Junxiao Shia9388182014-12-13 23:16:09 -070072 * \return true
Junxiao Shi0fcb41e2014-01-24 10:29:43 -070073 */
74 bool
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080075 insert(const Data& data, bool isUnsolicited = false);
76
mzhang4eab72492015-02-25 11:16:09 -060077 typedef std::function<void(const Interest&, const Data& data)> HitCallback;
78 typedef std::function<void(const Interest&)> MissCallback;
79
Junxiao Shia9388182014-12-13 23:16:09 -070080 /** \brief finds the best matching Data packet
mzhang4eab72492015-02-25 11:16:09 -060081 * \param interest the Interest for lookup
82 * \param hitCallback a callback if a match is found; must not be empty
83 * \param missCallback a callback if there's no match; must not be empty
84 * \note A lookup invokes either callback exactly once.
85 * The callback may be invoked either before or after find() returns
Junxiao Shi0fcb41e2014-01-24 10:29:43 -070086 */
mzhang4eab72492015-02-25 11:16:09 -060087 void
88 find(const Interest& interest,
89 const HitCallback& hitCallback,
90 const MissCallback& missCallback) const;
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080091
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080092 void
Junxiao Shia9388182014-12-13 23:16:09 -070093 erase(const Name& exactName)
94 {
95 BOOST_ASSERT_MSG(false, "not implemented");
96 }
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080097
Junxiao Shia9388182014-12-13 23:16:09 -070098 /** \brief changes capacity (in number of packets)
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080099 */
100 void
101 setLimit(size_t nMaxPackets);
102
Junxiao Shia9388182014-12-13 23:16:09 -0700103 /** \return capacity (in number of packets)
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800104 */
105 size_t
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500106 getLimit() const;
107
108 /** \brief changes cs replacement policy
109 * \pre size() == 0
110 */
111 void
112 setPolicy(unique_ptr<Policy> policy);
113
114 /** \return cs replacement policy
115 */
116 Policy*
117 getPolicy() const
Junxiao Shia9388182014-12-13 23:16:09 -0700118 {
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500119 return m_policy.get();
Junxiao Shia9388182014-12-13 23:16:09 -0700120 }
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800121
Junxiao Shia9388182014-12-13 23:16:09 -0700122 /** \return number of stored packets
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800123 */
124 size_t
Junxiao Shifc206962015-01-16 11:12:22 -0700125 size() const
126 {
127 return m_table.size();
128 }
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800129
Junxiao Shia9388182014-12-13 23:16:09 -0700130PUBLIC_WITH_TESTS_ELSE_PRIVATE:
131 void
132 dump();
133
Junxiao Shifc206962015-01-16 11:12:22 -0700134public: // enumeration
135 struct EntryFromEntryImpl
136 {
137 typedef const Entry& result_type;
138
139 const Entry&
140 operator()(const EntryImpl& entry) const
141 {
142 return entry;
143 }
Junxiao Shia9388182014-12-13 23:16:09 -0700144 };
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800145
Junxiao Shifc206962015-01-16 11:12:22 -0700146 /** \brief ContentStore iterator (public API)
147 */
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500148 typedef boost::transform_iterator<EntryFromEntryImpl, iterator, const Entry&> const_iterator;
Junxiao Shifc206962015-01-16 11:12:22 -0700149
150 const_iterator
151 begin() const
152 {
153 return boost::make_transform_iterator(m_table.begin(), EntryFromEntryImpl());
154 }
155
156 const_iterator
157 end() const
158 {
159 return boost::make_transform_iterator(m_table.end(), EntryFromEntryImpl());
160 }
161
162private: // find
Junxiao Shi5640ec82015-01-07 21:51:19 -0700163 /** \brief find leftmost match in [first,last)
164 * \return the leftmost match, or last if not found
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800165 */
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500166 iterator
167 findLeftmost(const Interest& interest, iterator left, iterator right) const;
Junxiao Shi5640ec82015-01-07 21:51:19 -0700168
169 /** \brief find rightmost match in [first,last)
170 * \return the rightmost match, or last if not found
171 */
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500172 iterator
173 findRightmost(const Interest& interest, iterator first, iterator last) const;
Junxiao Shi5640ec82015-01-07 21:51:19 -0700174
175 /** \brief find rightmost match among entries with exact Names in [first,last)
176 * \return the rightmost match, or last if not found
177 */
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500178 iterator
179 findRightmostAmongExact(const Interest& interest, iterator first, iterator last) const;
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800180
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800181 void
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500182 setPolicyImpl(unique_ptr<Policy>& policy);
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800183
184private:
Junxiao Shia9388182014-12-13 23:16:09 -0700185 Table m_table;
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500186 unique_ptr<Policy> m_policy;
187 ndn::util::signal::ScopedConnection m_beforeEvictConnection;
Junxiao Shi0fcb41e2014-01-24 10:29:43 -0700188};
189
Junxiao Shia9388182014-12-13 23:16:09 -0700190} // namespace cs
191
192using cs::Cs;
193
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800194} // namespace nfd
Alexander Afanasyevb927a3a2014-01-24 10:41:47 -0800195
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700196#endif // NFD_DAEMON_TABLE_CS_HPP