blob: 0dba20d24ab1b29f432b33564a2d3bb197c376c3 [file] [log] [blame]
spirosmastorakis0e2b1972016-03-27 20:54:41 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3* Copyright (c) 2016 Regents of the University of California.
4*
5* This file is part of the nTorrent codebase.
6*
7* nTorrent 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* nTorrent 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 nTorrent, e.g., in COPYING.md file. If not, see
17* <http://www.gnu.org/licenses/>.
18*
19* See AUTHORS for complete list of nTorrent authors and contributors.
20*/
21
22#include "stats-table-record.hpp"
23
24#include <vector>
25
26namespace ndn {
27namespace ntorrent {
28
29/**
30 * @brief Represents a stats table
31 */
32class StatsTable : boost::noncopyable {
33public:
34 /**
35 * @brief Create an empty stats table
36 */
37 StatsTable() = default;
38
39 /**
40 * @brief Create a stats table for a specific torrent
41 * @param torrentName The name of the torrent
42 */
43 StatsTable(const Name& torrentName);
44
45 ~StatsTable() = default;
46
47 /**
48 * @brief Insert a routable prefix to the stats table
49 * @param prefix The prefix to be inserted
50 */
51 void
52 insert(const Name& prefix);
53
54 /**
55 * @brief Erase a prefix from the statsTable
56 * @param prefix The prefix to be erased
57 * @return True if the prefix was found and erased. Otherwise, false
58 */
59 bool
60 erase(const Name& prefix);
61
62 /**
63 * @brief Clear the stats table
64 */
65 void
66 clear();
67
68 /**
69 * @brief Return the size of the stats table (number of prefixes included)
70 */
71 size_t
72 size() const;
73
74 typedef std::vector<StatsTableRecord>::const_iterator const_iterator;
75 typedef std::vector<StatsTableRecord>::iterator iterator;
76
77 /**
78 * @brief Constant iterator to the beginning of the stats table
79 */
80 const_iterator
81 begin() const;
82
83 /**
84 * @brief Iterator to the beginning of the stats table
85 */
86 iterator
87 begin();
88
89 /**
90 * @brief Constant iterator to the end of the stats table
91 */
92 const_iterator
93 end() const;
94
95 /**
96 * @brief Iterator to the end of the stats table
97 */
98 iterator
99 end();
100
101 /**
102 * @brief Find a prefix on the stats table
103 * @param prefix The name prefix to be searched
104 * @return A constant iterator to the prefix if found. Otherwise, return StatsTable::end()
105 */
106 const_iterator
107 find(const Name& prefix) const;
108
109 /**
110 * @brief Find a prefix on the stats table
111 * @param prefix The name prefix to be searched
112 * @return An iterator to the prefix if found. Otherwise, return StatsTable::end()
113 */
114 iterator
115 find(const Name& prefix);
116
117 /**
118 * @brief Comparator used for sorting the records of the stats table
119 */
120 struct comparator {
121 bool operator() (const StatsTableRecord& left, const StatsTableRecord& right) const
122 {return left.getRecordSuccessRate() >= right.getRecordSuccessRate();}
123 };
124
125 /**
126 * @brief Sort the records of the stats table on desceding success rate
127 * @param comp Optional comparator function to be used for sorting.
128 * The default value is the provided comparator struct
129 *
130 * This method has to be called manually by the application to sort the
131 * stats table.
132 */
133 void
134 sort(std::function<bool(const StatsTableRecord&, const StatsTableRecord&)> comp = comparator());
135
136private:
137 // Set of StatsTableRecords
138 std::vector<StatsTableRecord> m_statsTable;
139 Name m_torrentName;
140};
141
142inline void
143StatsTable::clear()
144{
145 m_statsTable.clear();
146}
147
148inline size_t
149StatsTable::size() const
150{
151 return m_statsTable.size();
152}
153
154inline StatsTable::const_iterator
155StatsTable::begin() const
156{
157 return m_statsTable.begin();
158}
159
160inline StatsTable::iterator
161StatsTable::begin()
162{
163 return m_statsTable.begin();
164}
165
166inline StatsTable::const_iterator
167StatsTable::end() const
168{
169 return m_statsTable.end();
170}
171
172inline StatsTable::iterator
173StatsTable::end()
174{
175 return m_statsTable.end();
176}
177
178inline void
179StatsTable::sort(std::function<bool(const StatsTableRecord&, const StatsTableRecord&)> comp)
180{
181 std::sort(m_statsTable.begin(), m_statsTable.end(), comp);
182}
183
184} // namespace ntorrent
185} // namespace ndn