blob: d60bd37fdfeae6fa526f5c28b85bea9839fef8fd [file] [log] [blame]
Weiqi Shi28a90fb2014-07-09 10:28:55 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesaventof43a03e2018-02-21 22:04:21 -05002/*
3 * Copyright (c) 2014-2018, Regents of the University of California.
Weiqi Shi28a90fb2014-07-09 10:28:55 -07004 *
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
Davide Pesaventof43a03e2018-02-21 22:04:21 -050023#include "../common.hpp"
24
WeiqiShia79c7782014-12-26 09:42:10 +080025#include <set>
Weiqi Shi28a90fb2014-07-09 10:28:55 -070026
27namespace repo {
28
29class Index : noncopyable
30{
31public:
32 class Error : public std::runtime_error
33 {
34 public:
35 explicit
36 Error(const std::string& what)
37 : std::runtime_error(what)
38 {
39 }
40 };
41
42 class Entry
43 {
44 public:
45 class Error : public std::runtime_error
46 {
47 public:
48 explicit
49 Error(const std::string& what)
50 : std::runtime_error(what)
51 {
52 }
53 };
54
55 public:
Weiqi Shi28a90fb2014-07-09 10:28:55 -070056 /**
WeiqiShia79c7782014-12-26 09:42:10 +080057 * @brief used by set to construct node
Weiqi Shi28a90fb2014-07-09 10:28:55 -070058 */
Davide Pesaventof43a03e2018-02-21 22:04:21 -050059 Entry() = default;
Weiqi Shi28a90fb2014-07-09 10:28:55 -070060
61 /**
62 * @brief construct Entry by data and id number
63 */
Alexander Afanasyevd352cce2015-11-20 14:15:11 -050064 Entry(const Data& data, int64_t id);
Weiqi Shi28a90fb2014-07-09 10:28:55 -070065
66 /**
67 * @brief construct Entry by fullName, keyLocator and id number
68 */
Alexander Afanasyevd352cce2015-11-20 14:15:11 -050069 Entry(const Name& fullName, const KeyLocator& keyLocator, int64_t id);
Weiqi Shi28a90fb2014-07-09 10:28:55 -070070
71 /**
72 * @brief construct Entry by fullName, keyLocatorHash and id number
73 * @param fullName full name with digest computed from data
74 * @param keyLocatorHash keyLocator hashed by sha256
75 * @param id record ID from database
76 */
Alexander Afanasyevd352cce2015-11-20 14:15:11 -050077 Entry(const Name& fullName, const ndn::ConstBufferPtr& keyLocatorHash, int64_t id);
Weiqi Shi28a90fb2014-07-09 10:28:55 -070078
79 /**
80 * @brief implicit construct Entry by full name
81 *
WeiqiShia79c7782014-12-26 09:42:10 +080082 * Allow implicit conversion from Name for set lookups by Name
Weiqi Shi28a90fb2014-07-09 10:28:55 -070083 */
84 Entry(const Name& name);
85
86 /**
87 * @brief get the name of entry
88 */
89 const Name&
90 getName() const
91 {
92 return m_name;
93 }
94
95 /**
96 * @brief get the keyLocator hash value of the entry
97 */
98 const ndn::ConstBufferPtr&
99 getKeyLocatorHash() const
100 {
101 return m_keyLocatorHash;
102 }
103
104 /**
105 * @brief get record ID from database
106 */
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500107 int64_t
Weiqi Shi28a90fb2014-07-09 10:28:55 -0700108 getId() const
109 {
110 return m_id;
111 }
112
113 bool
114 operator>(const Entry& entry) const
115 {
116 return m_name > entry.getName();
117 }
118
119 bool
120 operator<(const Entry& entry) const
121 {
122 return m_name < entry.getName();
123 }
124
125 bool
126 operator==(const Entry& entry) const
127 {
128 return m_name == entry.getName();
129 }
130
131 bool
132 operator!=(const Entry& entry) const
133 {
134 return m_name != entry.getName();
135 }
136
137 private:
138 Name m_name;
139 ndn::ConstBufferPtr m_keyLocatorHash;
140 int64_t m_id;
141 };
142
143private:
WeiqiShia79c7782014-12-26 09:42:10 +0800144 typedef std::set<Entry> IndexContainer;
Weiqi Shi28a90fb2014-07-09 10:28:55 -0700145
146public:
147 explicit
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500148 Index(size_t nMaxPackets);
Weiqi Shi28a90fb2014-07-09 10:28:55 -0700149
150 /**
151 * @brief insert entries into index
152 * @param data used to construct entries
153 * @param id obtained from database
154 */
155 bool
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500156 insert(const Data& data, int64_t id);
Weiqi Shi28a90fb2014-07-09 10:28:55 -0700157
158 /**
159 * @brief insert entries into index
160 * @param data used to construct entries
161 * @param id obtained from database
162 */
163 bool
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500164 insert(const Name& fullName, int64_t id,
Weiqi Shi28a90fb2014-07-09 10:28:55 -0700165 const ndn::ConstBufferPtr& keyLocatorHash);
166
167 /**
168 * @brief erase the entry in index by its fullname
169 */
170 bool
171 erase(const Name& fullName);
172
173 /** @brief find the Entry for best match of an Interest
174 * @return ID and fullName of the Entry, or (0,ignored) if not found
175 */
176 std::pair<int64_t, Name>
177 find(const Interest& interest) const;
178
179 /** @brief find the first Entry under a Name prefix
180 * @return ID and fullName of the Entry, or (0,ignored) if not found
181 */
182 std::pair<int64_t, Name>
183 find(const Name& name) const;
184
185 /**
186 * @brief determine whether same Data is already in the index
187 * @return true if identical Data exists, false otherwise
188 */
189 bool
190 hasData(const Data& data) const;
191
192 /**
193 * @brief compute the hash value of keyLocator
194 */
195 static const ndn::ConstBufferPtr
196 computeKeyLocatorHash(const KeyLocator& keyLocator);
197
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500198 size_t
Weiqi Shi28a90fb2014-07-09 10:28:55 -0700199 size() const
200 {
201 return m_size;
202 }
203
204private:
205 /**
206 * @brief select entries which satisfy the selectors in interest and return their name
207 * @param interest used to select entries by comparing the name and checking selectors
208 * @param idName save the id and name of found entries
209 * @param startingPoint the entry whose name is equal or larger than the interest name
210 */
211 std::pair<int64_t, Name>
212 selectChild(const Interest& interest,
WeiqiShia79c7782014-12-26 09:42:10 +0800213 IndexContainer::const_iterator startingPoint) const;
Weiqi Shi28a90fb2014-07-09 10:28:55 -0700214
215 /**
216 * @brief check whether the index is full
217 */
218 bool
219 isFull() const
220 {
221 return m_size >= m_maxPackets;
222 }
223
224 /**
225 * @brief find the first entry with the prefix
226 * @param prefix used to request the entries
227 * @param startingPoint the entry whose name is equal or larger than the interest name
228 * @return int64_t the id number of found entry
229 * @return Name the name of found entry
230 */
231 std::pair<int64_t, Name>
232 findFirstEntry(const Name& prefix,
WeiqiShia79c7782014-12-26 09:42:10 +0800233 IndexContainer::const_iterator startingPoint) const;
Weiqi Shi28a90fb2014-07-09 10:28:55 -0700234
235private:
WeiqiShia79c7782014-12-26 09:42:10 +0800236 IndexContainer m_indexContainer;
Weiqi Shi28a90fb2014-07-09 10:28:55 -0700237 size_t m_maxPackets;
238 size_t m_size;
239};
240
241} // namespace repo
242
243#endif // REPO_STORAGE_INDEX_HPP