blob: 4eadd40413e07aab46b2eb2e3172c9b72b9ba8c1 [file] [log] [blame]
Junxiao Shi0fcb41e2014-01-24 10:29:43 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Steve DiBenedetto3a4f83d2014-06-02 14:58:54 -06003 * Copyright (c) 2014, 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
51#include "common.hpp"
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -040052
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080053namespace nfd {
Junxiao Shia9388182014-12-13 23:16:09 -070054namespace cs {
Junxiao Shi0fcb41e2014-01-24 10:29:43 -070055
Junxiao Shia9388182014-12-13 23:16:09 -070056class Entry;
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080057
Junxiao Shia9388182014-12-13 23:16:09 -070058/** \brief represents the ContentStore
Junxiao Shi0fcb41e2014-01-24 10:29:43 -070059 */
Alexander Afanasyevb927a3a2014-01-24 10:41:47 -080060class Cs : noncopyable
Junxiao Shi0fcb41e2014-01-24 10:29:43 -070061{
62public:
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080063 explicit
Steve DiBenedetto3a4f83d2014-06-02 14:58:54 -060064 Cs(size_t nMaxPackets = 10);
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080065
Junxiao Shi0fcb41e2014-01-24 10:29:43 -070066 ~Cs();
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080067
Junxiao Shi0fcb41e2014-01-24 10:29:43 -070068 /** \brief inserts a Data packet
Junxiao Shia9388182014-12-13 23:16:09 -070069 * \return true
Junxiao Shi0fcb41e2014-01-24 10:29:43 -070070 */
71 bool
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080072 insert(const Data& data, bool isUnsolicited = false);
73
Junxiao Shia9388182014-12-13 23:16:09 -070074 /** \brief finds the best matching Data packet
Junxiao Shi0fcb41e2014-01-24 10:29:43 -070075 */
Alexander Afanasyevc9765df2014-01-25 19:24:27 -080076 const Data*
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080077 find(const Interest& interest) const;
78
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080079 void
Junxiao Shia9388182014-12-13 23:16:09 -070080 erase(const Name& exactName)
81 {
82 BOOST_ASSERT_MSG(false, "not implemented");
83 }
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080084
Junxiao Shia9388182014-12-13 23:16:09 -070085 /** \brief changes capacity (in number of packets)
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080086 */
87 void
88 setLimit(size_t nMaxPackets);
89
Junxiao Shia9388182014-12-13 23:16:09 -070090 /** \return capacity (in number of packets)
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080091 */
92 size_t
Junxiao Shia9388182014-12-13 23:16:09 -070093 getLimit() const
94 {
95 return m_limit;
96 }
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080097
Junxiao Shia9388182014-12-13 23:16:09 -070098 /** \return number of stored packets
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080099 */
100 size_t
101 size() const;
102
Junxiao Shia9388182014-12-13 23:16:09 -0700103PUBLIC_WITH_TESTS_ELSE_PRIVATE:
104 void
105 dump();
106
107public:
108 typedef std::set<Entry> Table;
109 typedef Table::const_iterator TableIt;
110 typedef std::list<TableIt> Queue;
111 typedef Queue::iterator QueueIt;
112 enum QueueType {
113 QUEUE_UNSOLICITED,
114 QUEUE_STALE,
115 QUEUE_FIFO,
116 QUEUE_UBOUND,
117 QUEUE_NONE = QUEUE_UBOUND
118 };
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800119
120private:
Junxiao Shia9388182014-12-13 23:16:09 -0700121 /** \brief determines whether b should be the rightmost candidate instead of a
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800122 */
Junxiao Shia9388182014-12-13 23:16:09 -0700123 static bool
124 isNewRightmostCandidate(const Name& prefix, TableIt a, TableIt b);
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800125
Junxiao Shia9388182014-12-13 23:16:09 -0700126 /** \brief attaches an entry to a suitable cleanup queue
127 * \note if the entry is already attached to a queue, it's automatically detached
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800128 */
129 void
Junxiao Shia9388182014-12-13 23:16:09 -0700130 attachQueue(TableIt it);
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800131
Junxiao Shia9388182014-12-13 23:16:09 -0700132 /** \brief detaches an entry from its current cleanup queue
133 * \warning if the entry is unattached, this will cause undefined behavior
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800134 */
Junxiao Shia9388182014-12-13 23:16:09 -0700135 void
136 detachQueue(TableIt it);
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800137
Junxiao Shia9388182014-12-13 23:16:09 -0700138 /** \brief moves an entry from FIFO queue to STALE queue
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800139 */
Junxiao Shia9388182014-12-13 23:16:09 -0700140 void
141 moveToStaleQueue(TableIt it);
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800142
Junxiao Shia9388182014-12-13 23:16:09 -0700143 /** \brief picks an entry for eviction
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800144 */
Junxiao Shia9388182014-12-13 23:16:09 -0700145 std::tuple<TableIt, std::string/*reason*/>
146 evictPick();
147
148 /** \brief evicts zero or more entries so that number of stored entries
149 * is not greater than capacity
150 */
151 void
152 evict();
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800153
154private:
Junxiao Shia9388182014-12-13 23:16:09 -0700155 size_t m_limit;
156 Table m_table;
157 Queue m_queues[QUEUE_UBOUND];
Junxiao Shi0fcb41e2014-01-24 10:29:43 -0700158};
159
Junxiao Shia9388182014-12-13 23:16:09 -0700160} // namespace cs
161
162using cs::Cs;
163
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800164} // namespace nfd
Alexander Afanasyevb927a3a2014-01-24 10:41:47 -0800165
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700166#endif // NFD_DAEMON_TABLE_CS_HPP