blob: 51d51b240b7427b851ccc92240d271a405fec786 [file] [log] [blame]
Weiqi Shi28a90fb2014-07-09 10:28:55 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014, Regents of the University of California.
4 *
5 * This file is part of NDN repo-ng (Next generation of NDN repository).
6 * See AUTHORS.md for complete list of repo-ng authors and contributors.
7 *
8 * repo-ng is free software: you can redistribute it and/or modify it under the terms
9 * of the GNU General Public License as published by the Free Software Foundation,
10 * either version 3 of the License, or (at your option) any later version.
11 *
12 * repo-ng is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
13 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along with
17 * repo-ng, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#ifndef REPO_STORAGE_INDEX_HPP
21#define REPO_STORAGE_INDEX_HPP
22
23#include "common.hpp"
WeiqiShia79c7782014-12-26 09:42:10 +080024#include <set>
Weiqi Shi28a90fb2014-07-09 10:28:55 -070025
26namespace repo {
27
28class Index : noncopyable
29{
30public:
31 class Error : public std::runtime_error
32 {
33 public:
34 explicit
35 Error(const std::string& what)
36 : std::runtime_error(what)
37 {
38 }
39 };
40
41 class Entry
42 {
43 public:
44 class Error : public std::runtime_error
45 {
46 public:
47 explicit
48 Error(const std::string& what)
49 : std::runtime_error(what)
50 {
51 }
52 };
53
54 public:
55
56 /**
WeiqiShia79c7782014-12-26 09:42:10 +080057 * @brief used by set to construct node
Weiqi Shi28a90fb2014-07-09 10:28:55 -070058 */
59 Entry()
60 {
61 };
62
63 /**
64 * @brief construct Entry by data and id number
65 */
Alexander Afanasyevd352cce2015-11-20 14:15:11 -050066 Entry(const Data& data, int64_t id);
Weiqi Shi28a90fb2014-07-09 10:28:55 -070067
68 /**
69 * @brief construct Entry by fullName, keyLocator and id number
70 */
Alexander Afanasyevd352cce2015-11-20 14:15:11 -050071 Entry(const Name& fullName, const KeyLocator& keyLocator, int64_t id);
Weiqi Shi28a90fb2014-07-09 10:28:55 -070072
73 /**
74 * @brief construct Entry by fullName, keyLocatorHash and id number
75 * @param fullName full name with digest computed from data
76 * @param keyLocatorHash keyLocator hashed by sha256
77 * @param id record ID from database
78 */
Alexander Afanasyevd352cce2015-11-20 14:15:11 -050079 Entry(const Name& fullName, const ndn::ConstBufferPtr& keyLocatorHash, int64_t id);
Weiqi Shi28a90fb2014-07-09 10:28:55 -070080
81 /**
82 * @brief implicit construct Entry by full name
83 *
WeiqiShia79c7782014-12-26 09:42:10 +080084 * Allow implicit conversion from Name for set lookups by Name
Weiqi Shi28a90fb2014-07-09 10:28:55 -070085 */
86 Entry(const Name& name);
87
88 /**
89 * @brief get the name of entry
90 */
91 const Name&
92 getName() const
93 {
94 return m_name;
95 }
96
97 /**
98 * @brief get the keyLocator hash value of the entry
99 */
100 const ndn::ConstBufferPtr&
101 getKeyLocatorHash() const
102 {
103 return m_keyLocatorHash;
104 }
105
106 /**
107 * @brief get record ID from database
108 */
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500109 int64_t
Weiqi Shi28a90fb2014-07-09 10:28:55 -0700110 getId() const
111 {
112 return m_id;
113 }
114
115 bool
116 operator>(const Entry& entry) const
117 {
118 return m_name > entry.getName();
119 }
120
121 bool
122 operator<(const Entry& entry) const
123 {
124 return m_name < entry.getName();
125 }
126
127 bool
128 operator==(const Entry& entry) const
129 {
130 return m_name == entry.getName();
131 }
132
133 bool
134 operator!=(const Entry& entry) const
135 {
136 return m_name != entry.getName();
137 }
138
139 private:
140 Name m_name;
141 ndn::ConstBufferPtr m_keyLocatorHash;
142 int64_t m_id;
143 };
144
145private:
146
WeiqiShia79c7782014-12-26 09:42:10 +0800147 typedef std::set<Entry> IndexContainer;
Weiqi Shi28a90fb2014-07-09 10:28:55 -0700148
149public:
150 explicit
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500151 Index(size_t nMaxPackets);
Weiqi Shi28a90fb2014-07-09 10:28:55 -0700152
153 /**
154 * @brief insert entries into index
155 * @param data used to construct entries
156 * @param id obtained from database
157 */
158 bool
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500159 insert(const Data& data, int64_t id);
Weiqi Shi28a90fb2014-07-09 10:28:55 -0700160
161 /**
162 * @brief insert entries into index
163 * @param data used to construct entries
164 * @param id obtained from database
165 */
166 bool
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500167 insert(const Name& fullName, int64_t id,
Weiqi Shi28a90fb2014-07-09 10:28:55 -0700168 const ndn::ConstBufferPtr& keyLocatorHash);
169
170 /**
171 * @brief erase the entry in index by its fullname
172 */
173 bool
174 erase(const Name& fullName);
175
176 /** @brief find the Entry for best match of an Interest
177 * @return ID and fullName of the Entry, or (0,ignored) if not found
178 */
179 std::pair<int64_t, Name>
180 find(const Interest& interest) const;
181
182 /** @brief find the first Entry under a Name prefix
183 * @return ID and fullName of the Entry, or (0,ignored) if not found
184 */
185 std::pair<int64_t, Name>
186 find(const Name& name) const;
187
188 /**
189 * @brief determine whether same Data is already in the index
190 * @return true if identical Data exists, false otherwise
191 */
192 bool
193 hasData(const Data& data) const;
194
195 /**
196 * @brief compute the hash value of keyLocator
197 */
198 static const ndn::ConstBufferPtr
199 computeKeyLocatorHash(const KeyLocator& keyLocator);
200
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500201 size_t
Weiqi Shi28a90fb2014-07-09 10:28:55 -0700202 size() const
203 {
204 return m_size;
205 }
206
207private:
208 /**
209 * @brief select entries which satisfy the selectors in interest and return their name
210 * @param interest used to select entries by comparing the name and checking selectors
211 * @param idName save the id and name of found entries
212 * @param startingPoint the entry whose name is equal or larger than the interest name
213 */
214 std::pair<int64_t, Name>
215 selectChild(const Interest& interest,
WeiqiShia79c7782014-12-26 09:42:10 +0800216 IndexContainer::const_iterator startingPoint) const;
Weiqi Shi28a90fb2014-07-09 10:28:55 -0700217
218 /**
219 * @brief check whether the index is full
220 */
221 bool
222 isFull() const
223 {
224 return m_size >= m_maxPackets;
225 }
226
227 /**
228 * @brief find the first entry with the prefix
229 * @param prefix used to request the entries
230 * @param startingPoint the entry whose name is equal or larger than the interest name
231 * @return int64_t the id number of found entry
232 * @return Name the name of found entry
233 */
234 std::pair<int64_t, Name>
235 findFirstEntry(const Name& prefix,
WeiqiShia79c7782014-12-26 09:42:10 +0800236 IndexContainer::const_iterator startingPoint) const;
Weiqi Shi28a90fb2014-07-09 10:28:55 -0700237
238private:
WeiqiShia79c7782014-12-26 09:42:10 +0800239 IndexContainer m_indexContainer;
Weiqi Shi28a90fb2014-07-09 10:28:55 -0700240 size_t m_maxPackets;
241 size_t m_size;
242};
243
244} // namespace repo
245
246#endif // REPO_STORAGE_INDEX_HPP