index: SkipList-based index
Change-Id: Ic04ab803eff1ec4e3ddcd04d0653e7192fc8e308
Refs: #1737
diff --git a/src/storage/index.hpp b/src/storage/index.hpp
new file mode 100644
index 0000000..90f7436
--- /dev/null
+++ b/src/storage/index.hpp
@@ -0,0 +1,247 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2014, Regents of the University of California.
+ *
+ * This file is part of NDN repo-ng (Next generation of NDN repository).
+ * See AUTHORS.md for complete list of repo-ng authors and contributors.
+ *
+ * repo-ng is free software: you can redistribute it and/or modify it under the terms
+ * of the GNU General Public License as published by the Free Software Foundation,
+ * either version 3 of the License, or (at your option) any later version.
+ *
+ * repo-ng is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
+ * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+ * PURPOSE. See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * repo-ng, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef REPO_STORAGE_INDEX_HPP
+#define REPO_STORAGE_INDEX_HPP
+
+#include "common.hpp"
+#include "skiplist.hpp"
+#include <queue>
+
+namespace repo {
+
+class Index : noncopyable
+{
+public:
+ class Error : public std::runtime_error
+ {
+ public:
+ explicit
+ Error(const std::string& what)
+ : std::runtime_error(what)
+ {
+ }
+ };
+
+ class Entry
+ {
+ public:
+ class Error : public std::runtime_error
+ {
+ public:
+ explicit
+ Error(const std::string& what)
+ : std::runtime_error(what)
+ {
+ }
+ };
+
+ public:
+
+ /**
+ * @brief used by skiplist to construct node
+ */
+ Entry()
+ {
+ };
+
+ /**
+ * @brief construct Entry by data and id number
+ */
+ Entry(const Data& data, const int64_t id);
+
+ /**
+ * @brief construct Entry by fullName, keyLocator and id number
+ */
+ Entry(const Name& fullName, const KeyLocator& keyLocator, const int64_t id);
+
+ /**
+ * @brief construct Entry by fullName, keyLocatorHash and id number
+ * @param fullName full name with digest computed from data
+ * @param keyLocatorHash keyLocator hashed by sha256
+ * @param id record ID from database
+ */
+ Entry(const Name& fullName, const ndn::ConstBufferPtr& keyLocatorHash, const int64_t id);
+
+ /**
+ * @brief implicit construct Entry by full name
+ *
+ * Allow implicit conversion from Name for SkipList lookups by Name
+ */
+ Entry(const Name& name);
+
+ /**
+ * @brief get the name of entry
+ */
+ const Name&
+ getName() const
+ {
+ return m_name;
+ }
+
+ /**
+ * @brief get the keyLocator hash value of the entry
+ */
+ const ndn::ConstBufferPtr&
+ getKeyLocatorHash() const
+ {
+ return m_keyLocatorHash;
+ }
+
+ /**
+ * @brief get record ID from database
+ */
+ const int64_t
+ getId() const
+ {
+ return m_id;
+ }
+
+ bool
+ operator>(const Entry& entry) const
+ {
+ return m_name > entry.getName();
+ }
+
+ bool
+ operator<(const Entry& entry) const
+ {
+ return m_name < entry.getName();
+ }
+
+ bool
+ operator==(const Entry& entry) const
+ {
+ return m_name == entry.getName();
+ }
+
+ bool
+ operator!=(const Entry& entry) const
+ {
+ return m_name != entry.getName();
+ }
+
+ private:
+ Name m_name;
+ ndn::ConstBufferPtr m_keyLocatorHash;
+ int64_t m_id;
+ };
+
+private:
+
+ typedef SkipList<Entry> IndexSkipList;
+
+public:
+ explicit
+ Index(const size_t nMaxPackets);
+
+ /**
+ * @brief insert entries into index
+ * @param data used to construct entries
+ * @param id obtained from database
+ */
+ bool
+ insert(const Data& data, const int64_t id);
+
+ /**
+ * @brief insert entries into index
+ * @param data used to construct entries
+ * @param id obtained from database
+ */
+ bool
+ insert(const Name& fullName, const int64_t id,
+ const ndn::ConstBufferPtr& keyLocatorHash);
+
+ /**
+ * @brief erase the entry in index by its fullname
+ */
+ bool
+ erase(const Name& fullName);
+
+ /** @brief find the Entry for best match of an Interest
+ * @return ID and fullName of the Entry, or (0,ignored) if not found
+ */
+ std::pair<int64_t, Name>
+ find(const Interest& interest) const;
+
+ /** @brief find the first Entry under a Name prefix
+ * @return ID and fullName of the Entry, or (0,ignored) if not found
+ */
+ std::pair<int64_t, Name>
+ find(const Name& name) const;
+
+ /**
+ * @brief determine whether same Data is already in the index
+ * @return true if identical Data exists, false otherwise
+ */
+ bool
+ hasData(const Data& data) const;
+
+ /**
+ * @brief compute the hash value of keyLocator
+ */
+ static const ndn::ConstBufferPtr
+ computeKeyLocatorHash(const KeyLocator& keyLocator);
+
+ const size_t
+ size() const
+ {
+ return m_size;
+ }
+
+private:
+ /**
+ * @brief select entries which satisfy the selectors in interest and return their name
+ * @param interest used to select entries by comparing the name and checking selectors
+ * @param idName save the id and name of found entries
+ * @param startingPoint the entry whose name is equal or larger than the interest name
+ */
+ std::pair<int64_t, Name>
+ selectChild(const Interest& interest,
+ IndexSkipList::const_iterator startingPoint) const;
+
+ /**
+ * @brief check whether the index is full
+ */
+ bool
+ isFull() const
+ {
+ return m_size >= m_maxPackets;
+ }
+
+ /**
+ * @brief find the first entry with the prefix
+ * @param prefix used to request the entries
+ * @param startingPoint the entry whose name is equal or larger than the interest name
+ * @return int64_t the id number of found entry
+ * @return Name the name of found entry
+ */
+ std::pair<int64_t, Name>
+ findFirstEntry(const Name& prefix,
+ IndexSkipList::const_iterator startingPoint) const;
+
+private:
+ IndexSkipList m_skipList;
+ size_t m_maxPackets;
+ size_t m_size;
+};
+
+} // namespace repo
+
+#endif // REPO_STORAGE_INDEX_HPP