index: SkipList-based index
Change-Id: Ic04ab803eff1ec4e3ddcd04d0653e7192fc8e308
Refs: #1737
diff --git a/src/storage/index.cpp b/src/storage/index.cpp
new file mode 100644
index 0000000..d2472fb
--- /dev/null
+++ b/src/storage/index.cpp
@@ -0,0 +1,258 @@
+/* -*- 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/>.
+ */
+
+#include "index.hpp"
+#include "skiplist.hpp"
+
+#include <ndn-cxx/util/crypto.hpp>
+#include "ndn-cxx/security/signature-sha256-with-rsa.hpp"
+
+namespace repo {
+
+/** @brief determines if entry can satisfy interest
+ * @param hash SHA256 hash of PublisherPublicKeyLocator if exists in interest, otherwise ignored
+ */
+static bool
+matchesSimpleSelectors(const Interest& interest, ndn::ConstBufferPtr& hash,
+ const Index::Entry& entry)
+{
+ const Name& fullName = entry.getName();
+
+ if (!interest.getName().isPrefixOf(fullName))
+ return false;
+
+ size_t nSuffixComponents = fullName.size() - interest.getName().size();
+ if (interest.getMinSuffixComponents() >= 0 &&
+ nSuffixComponents < static_cast<size_t>(interest.getMinSuffixComponents()))
+ return false;
+ if (interest.getMaxSuffixComponents() >= 0 &&
+ nSuffixComponents > static_cast<size_t>(interest.getMaxSuffixComponents()))
+ return false;
+
+ if (!interest.getExclude().empty() &&
+ entry.getName().size() > interest.getName().size() &&
+ interest.getExclude().isExcluded(entry.getName()[interest.getName().size()]))
+ return false;
+ if (!interest.getPublisherPublicKeyLocator().empty())
+ {
+ if (*entry.getKeyLocatorHash() != *hash)
+ return false;
+ }
+ return true;
+}
+
+Index::Index(const size_t nMaxPackets)
+ : m_maxPackets(nMaxPackets)
+ , m_size(0)
+{
+}
+
+
+bool
+Index::insert(const Data& data, const int64_t id)
+{
+ if (isFull())
+ throw Error("The Index is Full. Cannot Insert Any Data!");
+ Entry entry(data, id);
+ bool isInserted = m_skipList.insert(entry).second;
+ if (isInserted)
+ ++m_size;
+ return isInserted;
+}
+
+bool
+Index::insert(const Name& fullName, const int64_t id,
+ const ndn::ConstBufferPtr& keyLocatorHash)
+{
+ if (isFull())
+ throw Error("The Index is Full. Cannot Insert Any Data!");
+ Entry entry(fullName, keyLocatorHash, id);
+ bool isInserted = m_skipList.insert(entry).second;
+ if (isInserted)
+ ++m_size;
+ return isInserted;
+}
+
+std::pair<int64_t,Name>
+Index::find(const Interest& interest) const
+{
+ Name name = interest.getName();
+ IndexSkipList::const_iterator result = m_skipList.lower_bound(name);
+ if (result != m_skipList.end())
+ {
+ return selectChild(interest, result);
+ }
+ else
+ {
+ return std::pair<int64_t,Name>();
+ }
+}
+
+std::pair<int64_t,Name>
+Index::find(const Name& name) const
+{
+ IndexSkipList::const_iterator result = m_skipList.lower_bound(name);
+ if (result != m_skipList.end())
+ {
+ return findFirstEntry(name, result);
+ }
+ else
+ {
+ return std::pair<int64_t,Name>();
+ }
+}
+
+bool
+Index::hasData(const Data& data) const
+{
+ Index::Entry entry(data, -1); // the id number is useless
+ IndexSkipList::const_iterator result = m_skipList.find(entry);
+ return result != m_skipList.end();
+
+}
+
+std::pair<int64_t,Name>
+Index::findFirstEntry(const Name& prefix,
+ IndexSkipList::const_iterator startingPoint) const
+{
+ BOOST_ASSERT(startingPoint != m_skipList.end());
+ if (prefix.isPrefixOf(startingPoint->getName()))
+ {
+ return std::make_pair(startingPoint->getId(), startingPoint->getName());
+ }
+ else
+ {
+ return std::pair<int64_t,Name>();
+ }
+}
+
+bool
+Index::erase(const Name& fullName)
+{
+ Entry entry(fullName);
+ IndexSkipList::const_iterator findIterator = m_skipList.find(entry);
+ if (findIterator != m_skipList.end())
+ {
+ m_skipList.erase(findIterator);
+ m_size--;
+ return true;
+ }
+ else
+ return false;
+}
+
+const ndn::ConstBufferPtr
+Index::computeKeyLocatorHash(const KeyLocator& keyLocator)
+{
+ const Block& block = keyLocator.wireEncode();
+ ndn::ConstBufferPtr keyLocatorHash = ndn::crypto::sha256(block.wire(), block.size());
+ return keyLocatorHash;
+}
+
+std::pair<int64_t,Name>
+Index::selectChild(const Interest& interest,
+ IndexSkipList::const_iterator startingPoint) const
+{
+ BOOST_ASSERT(startingPoint != m_skipList.end());
+ bool isLeftmost = (interest.getChildSelector() <= 0);
+ ndn::ConstBufferPtr hash;
+ if (!interest.getPublisherPublicKeyLocator().empty())
+ {
+ KeyLocator keyLocator = interest.getPublisherPublicKeyLocator();
+ const Block& block = keyLocator.wireEncode();
+ hash = ndn::crypto::sha256(block.wire(), block.size());
+ }
+
+ if (isLeftmost)
+ {
+ for (IndexSkipList::const_iterator it = startingPoint;
+ it != m_skipList.end(); ++it)
+ {
+ if (!interest.getName().isPrefixOf(it->getName()))
+ return std::pair<int64_t,Name>();
+ if (matchesSimpleSelectors(interest, hash, (*it)))
+ return std::make_pair(it->getId(), it->getName());
+ }
+ }
+ else
+ {
+ IndexSkipList::const_iterator boundary = m_skipList.lower_bound(interest.getName());
+ if (boundary == m_skipList.end() || !interest.getName().isPrefixOf(boundary->getName()))
+ return std::pair<int64_t,Name>();
+ Name successor = interest.getName().getSuccessor();
+ IndexSkipList::const_iterator last = interest.getName().size() == 0 ?
+ m_skipList.end() : m_skipList.lower_bound(interest.getName().getSuccessor());
+ while (true)
+ {
+ IndexSkipList::const_iterator prev = last;
+ --prev;
+ if (prev == boundary)
+ {
+ bool isMatch = matchesSimpleSelectors(interest, hash, (*prev));
+ if (isMatch)
+ {
+ return std::make_pair(prev->getId(), prev->getName());
+ }
+ else
+ return std::pair<int64_t,Name>();
+ }
+ IndexSkipList::const_iterator first =
+ m_skipList.lower_bound(prev->getName().getPrefix(interest.getName().size() + 1));
+ IndexSkipList::const_iterator match =
+ std::find_if(first, last, bind(&matchesSimpleSelectors, interest, hash, _1));
+ if (match != last)
+ {
+ return std::make_pair(match->getId(), match->getName());
+ }
+ last = first;
+ }
+ }
+ return std::pair<int64_t,Name>();
+}
+
+Index::Entry::Entry(const Data& data, const int64_t id)
+ : m_name(data.getFullName())
+ , m_id(id)
+{
+ const ndn::Signature& signature = data.getSignature();
+ if (signature.hasKeyLocator())
+ m_keyLocatorHash = computeKeyLocatorHash(signature.getKeyLocator());
+}
+
+Index::Entry::Entry(const Name& fullName, const KeyLocator& keyLocator, const int64_t id)
+ : m_name(fullName)
+ , m_keyLocatorHash(computeKeyLocatorHash(keyLocator))
+ , m_id(id)
+{
+}
+
+Index::Entry::Entry(const Name& fullName,
+ const ndn::ConstBufferPtr& keyLocatorHash, const int64_t id)
+ : m_name(fullName)
+ , m_keyLocatorHash(keyLocatorHash)
+ , m_id(id)
+{
+}
+
+Index::Entry::Entry(const Name& name)
+ : m_name(name)
+{
+}
+
+} // namespace repo
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
diff --git a/tests/dataset-fixtures.hpp b/tests/dataset-fixtures.hpp
index 3cc2685..309078c 100644
--- a/tests/dataset-fixtures.hpp
+++ b/tests/dataset-fixtures.hpp
@@ -51,6 +51,11 @@
typedef std::list<std::pair<ndn::Interest, ndn::shared_ptr<ndn::Data> > > InterestContainer;
InterestContainer interests;
+ typedef std::list<std::pair<ndn::Interest, int > > InterestIdContainer;
+ InterestIdContainer interestsSelectors;
+
+ typedef std::list<std::pair<int, ndn::shared_ptr<ndn::Data> > > IdContainer;
+ IdContainer ids, insert;
struct DataSetNameCompare
{
@@ -91,6 +96,7 @@
this->data.push_back(data);
this->interests.push_back(std::make_pair(Interest(name), data));
+ this->ids.push_back(std::make_pair(i+2, data));
}
}
};
@@ -110,19 +116,23 @@
{
this->data.push_back(createData("/a"));
this->interests.push_back(std::make_pair(Interest("/a"), this->data.back()));
+ this->ids.push_back(std::make_pair(2, this->data.back()));
this->data.push_back(createData("/a/b"));
this->interests.push_back(std::make_pair(Interest("/a/b"), this->data.back()));
+ this->ids.push_back(std::make_pair(3, this->data.back()));
this->data.push_back(createData("/a/b/c"));
this->interests.push_back(std::make_pair(Interest("/a/b/c"), this->data.back()));
+ this->ids.push_back(std::make_pair(4, this->data.back()));
this->data.push_back(createData("/a/b/c/d"));
this->interests.push_back(std::make_pair(Interest("/a/b/c/d"), this->data.back()));
+ this->ids.push_back(std::make_pair(5, this->data.back()));
}
};
-
+//Fetch by prefix is useless due to the database is fetched by id
class FetchByPrefixTestFixture : public DatasetBase
{
public:
@@ -179,11 +189,11 @@
this->interests.push_back(std::make_pair(Interest("/a/b/c/d/e/f/g/h/i/j/k/l/m/n/o/p/q/r/s/t/u"),
this->data.back()));
this->interests.push_back(
- std::make_pair(Interest("/a/b/c/d/e/f/g/h/i/j/k/l/m/n/o/p/q/r/s/t/u/v"),
- this->data.back()));
+ std::make_pair(Interest("/a/b/c/d/e/f/g/h/i/j/k/l/m/n/o/p/q/r/s/t/u/v"),
+ this->data.back()));
this->interests.push_back(
- std::make_pair(Interest("/a/b/c/d/e/f/g/h/i/j/k/l/m/n/o/p/q/r/s/t/u/v/w"),
- this->data.back()));
+ std::make_pair(Interest("/a/b/c/d/e/f/g/h/i/j/k/l/m/n/o/p/q/r/s/t/u/v/w"),
+ this->data.back()));
this->interests.push_back(
std::make_pair(Interest("/a/b/c/d/e/f/g/h/i/j/k/l/m/n/o/p/q/r/s/t/u/v/w/x"),
this->data.back()));
@@ -212,29 +222,252 @@
this->data.push_back(createData("/a/1"));
this->data.push_back(createData("/b/1"));
this->interests.push_back(std::make_pair(Interest()
- .setName("/b")
- .setSelectors(Selectors()
- .setChildSelector(0)),
- this->data.back()));
+ .setName("/b")
+ .setSelectors(Selectors()
+ .setChildSelector(0)),
+ this->data.back()));
this->data.push_back(createData("/c/1"));
this->data.push_back(createData("/b/99"));
this->interests.push_back(std::make_pair(Interest()
- .setName("/b")
- .setSelectors(Selectors()
- .setChildSelector(1)),
- this->data.back()));
+ .setName("/b")
+ .setSelectors(Selectors()
+ .setChildSelector(1)),
+ this->data.back()));
this->data.push_back(createData("/b/5"));
this->data.push_back(createData("/b/55"));
}
};
+class IndexTestFixture : public DatasetBase
+{
+public:
+ static const std::string&
+ getName()
+ {
+ static std::string name = "IndexTest";
+ return name;
+ }
+
+ IndexTestFixture()
+ {
+ this->data.push_back(createData("/a/b/c"));
+ this->interests.push_back(std::make_pair(Interest("/a/b/c"), this->data.back()));
+ this->ids.push_back(std::make_pair(2, this->data.back()));
+ this->insert.push_back(std::make_pair(2, this->data.back()));
+
+ this->data.push_back(createData("/a/b/d/1"));
+ this->interests.push_back(std::make_pair(Interest("/a/b/d"), this->data.back()));
+ this->interests.push_back(std::make_pair(Interest("/a/b/d/1"), this->data.back()));
+ this->ids.push_back(std::make_pair(3, this->data.back()));
+ this->ids.push_back(std::make_pair(3, this->data.back()));
+ this->insert.push_back(std::make_pair(3, this->data.back()));
+
+ this->data.push_back(createData("/a/b/d/2"));
+ this->interests.push_back(std::make_pair(Interest("/a/b/d/2"), this->data.back()));
+ this->ids.push_back(std::make_pair(4, this->data.back()));
+ this->insert.push_back(std::make_pair(4, this->data.back()));
+
+ this->data.push_back(createData("/a/b/d/3"));
+ this->interests.push_back(std::make_pair(Interest("/a/b/d/3"), this->data.back()));
+ this->ids.push_back(std::make_pair(5, this->data.back()));
+ this->insert.push_back(std::make_pair(5, this->data.back()));
+
+ this->data.push_back(createData("/a/b/d/4/I"));
+ this->interests.push_back(std::make_pair(Interest("/a/b/d/4/I"), this->data.back()));
+ this->interests.push_back(std::make_pair(Interest("/a/b/d/4"), this->data.back()));
+ this->ids.push_back(std::make_pair(6, this->data.back()));
+ this->ids.push_back(std::make_pair(6, this->data.back()));
+ this->insert.push_back(std::make_pair(6, this->data.back()));
+
+ this->data.push_back(createData("/a/b/d/4"));
+ // this->ids.push_back(std::make_pair(7, this->data.back()));
+ this->insert.push_back(std::make_pair(7, this->data.back()));
+
+ this->data.push_back(createData("/a/b/d"));
+ // this->ids.push_back(std::make_pair(8, this->data.back()));
+ this->insert.push_back(std::make_pair(8, this->data.back()));
+
+ this->data.push_back(createData("/a/b/e/1"));
+ this->interests.push_back(std::make_pair(Interest("/a/b/e"), this->data.back()));
+ this->interests.push_back(std::make_pair(Interest("/a/b/e/1"), this->data.back()));
+ this->ids.push_back(std::make_pair(9, this->data.back()));
+ this->ids.push_back(std::make_pair(9, this->data.back()));
+ this->insert.push_back(std::make_pair(9, this->data.back()));
+
+ Selectors selector_keylocator;
+ ndn::SignatureSha256WithRsa rsaSignature(this->data.back()->getSignature());
+
+ this->data.push_back(createData("/a/b/e"));
+ this->ids.push_back(std::make_pair(10, this->data.back()));
+ this->insert.push_back(std::make_pair(10, this->data.back()));
+
+ Selectors selector;
+ selector.setMinSuffixComponents(3);
+ this->interestsSelectors.push_back(std::make_pair(Interest("/a/b/d").setSelectors(selector),
+ 6));
+
+ selector.setMinSuffixComponents(-1);
+ selector.setChildSelector(0);
+ this->interestsSelectors.push_back(std::make_pair(Interest("/a/b/d").setSelectors(selector),
+ 3));
+
+ selector.setMinSuffixComponents(2);
+ this->interestsSelectors.push_back(std::make_pair(Interest("/a/b/d").setSelectors(selector),
+ 3));
+
+ selector.setChildSelector(1);
+ this->interestsSelectors.push_back(std::make_pair(Interest("/a/b/d").setSelectors(selector),
+ 7));
+
+ selector.setChildSelector(-1);
+ selector.setMaxSuffixComponents(2);
+ this->interestsSelectors.push_back(std::make_pair(Interest("/a/b").setSelectors(selector),
+ 2));
+
+ ndn::name::Component from("3");
+ ndn::name::Component to("4");
+ Exclude exclude;
+ exclude.excludeRange(from, to);
+ selector.setChildSelector(1);
+ selector.setExclude(exclude);
+ this->interestsSelectors.push_back(std::make_pair(Interest("/a/b/d").setSelectors(selector),
+ 4));
+
+
+ KeyLocator keylocate = rsaSignature.getKeyLocator();
+ // std::cout<<"keylocator from data = "<<keylocate.wireEncode()<<std::endl;
+ selector_keylocator.setPublisherPublicKeyLocator(keylocate);
+ this->interestsSelectors.push_back(std::make_pair
+ (Interest("/a/b/e").setSelectors(selector_keylocator), 9));
+ }
+};
+
+class ChildSelectorTestFixture : public DatasetBase
+{
+public:
+ static const std::string&
+ getName()
+ {
+ static std::string name = "storage";
+ return name;
+ }
+
+ ChildSelectorTestFixture()
+ {
+ this->data.push_back(createData("/a/b/1"));
+ this->insert.push_back(std::make_pair(2, this->data.back()));
+ this->data.push_back(createData("/a/c/1"));
+ this->insert.push_back(std::make_pair(3, this->data.back()));
+
+ this->data.push_back(createData("/a/c/2"));
+ this->insert.push_back(std::make_pair(4, this->data.back()));
+
+ this->data.push_back(createData("/b"));
+ this->insert.push_back(std::make_pair(5, this->data.back()));
+
+ Selectors selector;
+ selector.setChildSelector(1);
+ this->interestsSelectors.push_back(std::make_pair(Interest("/a").setSelectors(selector),
+ 3));
+ }
+};
+
+class StorageFixture : public DatasetBase
+{
+public:
+ static const std::string&
+ getName()
+ {
+ static std::string name = "storage";
+ return name;
+ }
+
+ StorageFixture()
+ {
+ this->data.push_back(createData("/a/b/c"));
+ this->interests.push_back(std::make_pair(Interest("/a/b/c"), this->data.back()));
+ this->ids.push_back(std::make_pair(2, this->data.back()));
+ this->insert.push_back(std::make_pair(2, this->data.back()));
+
+ this->data.push_back(createData("/a/b/d/1"));
+ this->interests.push_back(std::make_pair(Interest("/a/b/d/1"), this->data.back()));
+ this->ids.push_back(std::make_pair(3, this->data.back()));
+ this->insert.push_back(std::make_pair(3, this->data.back()));
+
+ this->data.push_back(createData("/a/b/d/2"));
+ this->interests.push_back(std::make_pair(Interest("/a/b/d/2"), this->data.back()));
+ this->ids.push_back(std::make_pair(4, this->data.back()));
+ this->insert.push_back(std::make_pair(4, this->data.back()));
+
+ this->data.push_back(createData("/a/b/d/3"));
+ this->interests.push_back(std::make_pair(Interest("/a/b/d/3"), this->data.back()));
+ this->ids.push_back(std::make_pair(5, this->data.back()));
+ this->insert.push_back(std::make_pair(5, this->data.back()));
+
+ this->data.push_back(createData("/a/b/d/4"));
+ this->ids.push_back(std::make_pair(6, this->data.back()));
+ this->interests.push_back(std::make_pair(Interest("/a/b/d/4"), this->data.back()));
+ this->insert.push_back(std::make_pair(6, this->data.back()));
+
+ this->data.push_back(createData("/a/b/e/1"));
+ this->interests.push_back(std::make_pair(Interest("/a/b/e/1"), this->data.back()));
+ this->ids.push_back(std::make_pair(7, this->data.back()));
+ this->insert.push_back(std::make_pair(7, this->data.back()));
+
+ Selectors selector_keylocator;
+ ndn::SignatureSha256WithRsa rsaSignature(this->data.back()->getSignature());
+
+ this->data.push_back(createData("/a/b/e/2"));
+ this->ids.push_back(std::make_pair(8, this->data.back()));
+ this->interests.push_back(std::make_pair(Interest("/a/b/e/2"), this->data.back()));
+ this->insert.push_back(std::make_pair(8, this->data.back()));
+
+ Selectors selector;
+ selector.setChildSelector(0);
+ this->interestsSelectors.push_back(std::make_pair(Interest("/a/b/d").setSelectors(selector),
+ 3));
+
+
+ selector.setChildSelector(1);
+ this->interestsSelectors.push_back(std::make_pair(Interest("/a/b/d").setSelectors(selector),
+ 6));
+
+ selector.setChildSelector(-1);
+ selector.setMaxSuffixComponents(2);
+ this->interestsSelectors.push_back(std::make_pair(Interest("/a/b").setSelectors(selector),
+ 2));
+ ndn::name::Component from("3");
+ ndn::name::Component to("4");
+ Exclude exclude;
+ exclude.excludeRange(from, to);
+ selector.setChildSelector(1);
+ selector.setExclude(exclude);
+ this->interestsSelectors.push_back(std::make_pair(Interest("/a/b/d").setSelectors(selector),
+ 4));
+
+
+ KeyLocator keylocate = rsaSignature.getKeyLocator();
+ selector_keylocator.setPublisherPublicKeyLocator(keylocate);
+ this->interestsSelectors.push_back(std::make_pair
+ (Interest("/a/b/e").setSelectors(selector_keylocator),
+ 7));
+ }
+};
typedef boost::mpl::vector< BaseTestFixture,
FetchByPrefixTestFixture,
SelectorTestFixture,
BaseSmoketestFixture<1>,
- BaseSmoketestFixture<100> > DatasetFixtures;
+ BaseSmoketestFixture<10> > DatasetFixtures;
+
+typedef boost::mpl::vector< BaseTestFixture,
+ BaseSmoketestFixture<1>,
+ BaseSmoketestFixture<10> > DatasetFixtures_Sqlite;
+
+typedef boost::mpl::vector<IndexTestFixture> DatasetFixtures_Index;
+typedef boost::mpl::vector<StorageFixture> DatasetFixtures_Storage;
+typedef boost::mpl::vector<ChildSelectorTestFixture> DatasetFixtures_Selector;
} // namespace tests
} // namespace repo
diff --git a/tests/unit/index.cpp b/tests/unit/index.cpp
new file mode 100644
index 0000000..1367403
--- /dev/null
+++ b/tests/unit/index.cpp
@@ -0,0 +1,420 @@
+/* -*- 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/>.
+ */
+
+#include "storage/index.hpp"
+
+#include "../sqlite-fixture.hpp"
+#include "../dataset-fixtures.hpp"
+
+#include <boost/test/unit_test.hpp>
+#include <iostream>
+
+namespace repo {
+namespace tests {
+
+BOOST_AUTO_TEST_SUITE(TestIndex)
+
+template<class Dataset>
+class Fixture : public Dataset
+{
+};
+
+BOOST_FIXTURE_TEST_CASE_TEMPLATE(IndexGeneralTest, T, DatasetFixtures_Storage, Fixture<T>)
+{
+ Index index(65535);
+ for (typename T::IdContainer::iterator i = this->insert.begin();
+ i != this->insert.end(); ++i)
+ {
+ BOOST_CHECK_EQUAL(index.insert(*i->second, i->first), true);
+ }
+ BOOST_CHECK_EQUAL(index.size(), 7);
+
+ typename T::IdContainer::iterator id = this->ids.begin();
+ for (typename T::InterestContainer::iterator i = this->interests.begin();
+ i != this->interests.end(); ++i)
+ {
+ vector<std::pair<int, ndn::Name> > id_names;
+ BOOST_CHECK_EQUAL(index.find(i->first.getName()).first, id->first);
+ BOOST_CHECK_EQUAL(index.hasData(*i->second), true);
+ ++id;
+ }
+
+ for (typename T::InterestIdContainer::iterator i = this->interestsSelectors.begin();
+ i != this->interestsSelectors.end(); ++i)
+ {
+ BOOST_CHECK_EQUAL(index.find(i->first).first, i->second);
+ ndn::Name name = index.find(i->first).second;
+ BOOST_CHECK_EQUAL(index.erase(name), true);
+ }
+ BOOST_CHECK_EQUAL(index.size(), 2);
+}
+
+
+BOOST_FIXTURE_TEST_CASE_TEMPLATE(IndexTestSelector, T, DatasetFixtures_Selector, Fixture<T>)
+{
+ Index index(65535);
+ for (typename T::IdContainer::iterator i = this->insert.begin();
+ i != this->insert.end(); ++i)
+ BOOST_CHECK_EQUAL(index.insert(*i->second, i->first), true);
+ for (typename T::InterestIdContainer::iterator i = this->interestsSelectors.begin();
+ i != this->interestsSelectors.end(); ++i)
+ {
+ BOOST_CHECK_EQUAL(index.find(i->first).first, i->second);
+ }
+}
+
+class FindFixture
+{
+protected:
+ FindFixture()
+ : m_index(std::numeric_limits<size_t>::max())
+ {
+ }
+
+ void
+ insert(int id, const Name& name)
+ {
+ shared_ptr<Data> data = make_shared<Data>(name);
+ data->setContent(reinterpret_cast<const uint8_t*>(&id), sizeof(id));
+ m_keyChain.signWithSha256(*data);
+ data->wireEncode();
+ m_index.insert(*data, id);
+ }
+
+ Interest&
+ startInterest(const Name& name)
+ {
+ m_interest = make_shared<Interest>(name);
+ return *m_interest;
+ }
+
+ int
+ find()
+ {
+ std::pair<int,Name> found = m_index.find(*m_interest);
+ return found.first;
+ }
+
+protected:
+ Index m_index;
+ KeyChain m_keyChain;
+ shared_ptr<Interest> m_interest;
+};
+
+BOOST_FIXTURE_TEST_SUITE(Find, FindFixture)
+
+BOOST_AUTO_TEST_CASE(EmptyDataName)
+{
+ insert(1, "ndn:/");
+ startInterest("ndn:/");
+ BOOST_CHECK_EQUAL(find(), 1);
+}
+
+BOOST_AUTO_TEST_CASE(EmptyInterestName)
+{
+ insert(1, "ndn:/A");
+ startInterest("ndn:/");
+ BOOST_CHECK_EQUAL(find(), 1);
+}
+
+BOOST_AUTO_TEST_CASE(Leftmost)
+{
+ insert(1, "ndn:/A");
+ insert(2, "ndn:/B/p/1");
+ insert(3, "ndn:/B/p/2");
+ insert(4, "ndn:/B/q/1");
+ insert(5, "ndn:/B/q/2");
+ insert(6, "ndn:/C");
+
+ startInterest("ndn:/B");
+ BOOST_CHECK_EQUAL(find(), 2);
+}
+
+BOOST_AUTO_TEST_CASE(Rightmost)
+{
+ insert(1, "ndn:/A");
+ insert(2, "ndn:/B/p/1");
+ insert(3, "ndn:/B/p/2");
+ insert(4, "ndn:/B/q/1");
+ insert(5, "ndn:/B/q/2");
+ insert(6, "ndn:/C");
+
+ startInterest("ndn:/B")
+ .setChildSelector(1);
+ BOOST_CHECK_EQUAL(find(), 4);
+}
+
+BOOST_AUTO_TEST_CASE(Leftmost_ExactName1)
+{
+ insert(1, "ndn:/");
+ insert(2, "ndn:/A/B");
+ insert(3, "ndn:/A/C");
+ insert(4, "ndn:/A");
+ insert(5, "ndn:/D");
+
+ // Intuitively you would think Data 4 should be between Data 1 and 2,
+ // but Data 4 has full Name ndn:/A/<32-octet hash>.
+ startInterest("ndn:/A");
+ BOOST_CHECK_EQUAL(find(), 2);
+}
+
+BOOST_AUTO_TEST_CASE(Leftmost_ExactName33)
+{
+ insert(1, "ndn:/");
+ insert(2, "ndn:/A");
+ insert(3, "ndn:/A/BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB"); // 33 'B's
+ insert(4, "ndn:/A/CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC"); // 33 'C's
+ insert(5, "ndn:/D");
+
+ // Data 2 is returned, because <32-octet hash> is less than Data 3.
+ startInterest("ndn:/A");
+ BOOST_CHECK_EQUAL(find(), 2);
+}
+
+BOOST_AUTO_TEST_CASE(MinSuffixComponents)
+{
+ insert(1, "ndn:/A/1/2/3/4");
+ insert(2, "ndn:/B/1/2/3");
+ insert(3, "ndn:/C/1/2");
+ insert(4, "ndn:/D/1");
+ insert(5, "ndn:/E");
+ insert(6, "ndn:/");
+
+ startInterest("ndn:/")
+ .setChildSelector(1)
+ .setMinSuffixComponents(0);
+ BOOST_CHECK_EQUAL(find(), 6);
+
+ startInterest("ndn:/")
+ .setChildSelector(1)
+ .setMinSuffixComponents(1);
+ BOOST_CHECK_EQUAL(find(), 6);
+
+ startInterest("ndn:/")
+ .setChildSelector(1)
+ .setMinSuffixComponents(2);
+ BOOST_CHECK_EQUAL(find(), 5);
+
+ startInterest("ndn:/")
+ .setChildSelector(1)
+ .setMinSuffixComponents(3);
+ BOOST_CHECK_EQUAL(find(), 4);
+
+ startInterest("ndn:/")
+ .setChildSelector(1)
+ .setMinSuffixComponents(4);
+ BOOST_CHECK_EQUAL(find(), 3);
+
+ startInterest("ndn:/")
+ .setChildSelector(1)
+ .setMinSuffixComponents(5);
+ BOOST_CHECK_EQUAL(find(), 2);
+
+ startInterest("ndn:/")
+ .setChildSelector(1)
+ .setMinSuffixComponents(6);
+ BOOST_CHECK_EQUAL(find(), 1);
+
+ startInterest("ndn:/")
+ .setChildSelector(1)
+ .setMinSuffixComponents(7);
+ BOOST_CHECK_EQUAL(find(), 0);
+}
+
+BOOST_AUTO_TEST_CASE(MaxSuffixComponents)
+{
+ insert(1, "ndn:/");
+ insert(2, "ndn:/A");
+ insert(3, "ndn:/A/B");
+ insert(4, "ndn:/A/B/C");
+ insert(5, "ndn:/A/B/C/D");
+ insert(6, "ndn:/A/B/C/D/E");
+ // Order is 6,5,4,3,2,1, because <32-octet hash> is greater than a 1-octet component.
+
+ startInterest("ndn:/")
+ .setMaxSuffixComponents(0);
+ BOOST_CHECK_EQUAL(find(), 0);
+
+ startInterest("ndn:/")
+ .setMaxSuffixComponents(1);
+ BOOST_CHECK_EQUAL(find(), 1);
+
+ startInterest("ndn:/")
+ .setMaxSuffixComponents(2);
+ BOOST_CHECK_EQUAL(find(), 2);
+
+ startInterest("ndn:/")
+ .setMaxSuffixComponents(3);
+ BOOST_CHECK_EQUAL(find(), 3);
+
+ startInterest("ndn:/")
+ .setMaxSuffixComponents(4);
+ BOOST_CHECK_EQUAL(find(), 4);
+
+ startInterest("ndn:/")
+ .setMaxSuffixComponents(5);
+ BOOST_CHECK_EQUAL(find(), 5);
+
+ startInterest("ndn:/")
+ .setMaxSuffixComponents(6);
+ BOOST_CHECK_EQUAL(find(), 6);
+}
+
+BOOST_AUTO_TEST_CASE(DigestOrder)
+{
+ insert(1, "ndn:/A");
+ insert(2, "ndn:/A");
+ // We don't know which comes first, but there must be some order
+
+ startInterest("ndn:/A")
+ .setChildSelector(0);
+ uint32_t leftmost = find();
+
+ startInterest("ndn:/A")
+ .setChildSelector(1);
+ uint32_t rightmost = find();
+
+ BOOST_CHECK_NE(leftmost, rightmost);
+}
+
+BOOST_AUTO_TEST_CASE(DigestExclude)
+{
+ insert(1, "ndn:/A/B");
+ insert(2, "ndn:/A");
+ insert(3, "ndn:/A/CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC"); // 33 'C's
+
+ startInterest("ndn:/A")
+ .setExclude(Exclude().excludeBefore(Name::Component(reinterpret_cast<const uint8_t*>(
+ "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF"
+ "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF"), 31))); // 31 0xFF's
+ BOOST_CHECK_EQUAL(find(), 2);
+
+ startInterest("ndn:/A")
+ .setChildSelector(1)
+ .setExclude(Exclude().excludeAfter(Name::Component(reinterpret_cast<const uint8_t*>(
+ "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
+ "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
+ "\x00"), 33))); // 33 0x00's
+ BOOST_CHECK_EQUAL(find(), 2);
+}
+
+BOOST_AUTO_TEST_CASE(ExactName32)
+{
+ insert(1, "ndn:/A/BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB"); // 32 'B's
+ insert(2, "ndn:/A/CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC"); // 32 'C's
+
+ startInterest("ndn:/A/BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB");
+ BOOST_CHECK_EQUAL(find(), 1);
+}
+
+BOOST_AUTO_TEST_CASE(MinSuffixComponents32)
+{
+ insert(1, "ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/A/1/2/3/4"); // 32 'x's
+ insert(2, "ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/B/1/2/3");
+ insert(3, "ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/C/1/2");
+ insert(4, "ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/D/1");
+ insert(5, "ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/E");
+ insert(6, "ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
+
+ startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
+ .setChildSelector(1)
+ .setMinSuffixComponents(0);
+ BOOST_CHECK_EQUAL(find(), 6);
+
+ startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
+ .setChildSelector(1)
+ .setMinSuffixComponents(1);
+ BOOST_CHECK_EQUAL(find(), 6);
+
+ startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
+ .setChildSelector(1)
+ .setMinSuffixComponents(2);
+ BOOST_CHECK_EQUAL(find(), 5);
+
+ startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
+ .setChildSelector(1)
+ .setMinSuffixComponents(3);
+ BOOST_CHECK_EQUAL(find(), 4);
+
+ startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
+ .setChildSelector(1)
+ .setMinSuffixComponents(4);
+ BOOST_CHECK_EQUAL(find(), 3);
+
+ startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
+ .setChildSelector(1)
+ .setMinSuffixComponents(5);
+ BOOST_CHECK_EQUAL(find(), 2);
+
+ startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
+ .setChildSelector(1)
+ .setMinSuffixComponents(6);
+ BOOST_CHECK_EQUAL(find(), 1);
+
+ startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
+ .setChildSelector(1)
+ .setMinSuffixComponents(7);
+ BOOST_CHECK_EQUAL(find(), 0);
+}
+
+BOOST_AUTO_TEST_CASE(MaxSuffixComponents32)
+{
+ insert(1, "ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/"); // 32 'x's
+ insert(2, "ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/A");
+ insert(3, "ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/A/B");
+ insert(4, "ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/A/B/C");
+ insert(5, "ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/A/B/C/D");
+ insert(6, "ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/A/B/C/D/E");
+ // Order is 6,5,4,3,2,1, because <32-octet hash> is greater than a 1-octet component.
+
+ startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
+ .setMaxSuffixComponents(0);
+ BOOST_CHECK_EQUAL(find(), 0);
+
+ startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
+ .setMaxSuffixComponents(1);
+ BOOST_CHECK_EQUAL(find(), 1);
+
+ startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
+ .setMaxSuffixComponents(2);
+ BOOST_CHECK_EQUAL(find(), 2);
+
+ startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
+ .setMaxSuffixComponents(3);
+ BOOST_CHECK_EQUAL(find(), 3);
+
+ startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
+ .setMaxSuffixComponents(4);
+ BOOST_CHECK_EQUAL(find(), 4);
+
+ startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
+ .setMaxSuffixComponents(5);
+ BOOST_CHECK_EQUAL(find(), 5);
+
+ startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
+ .setMaxSuffixComponents(6);
+ BOOST_CHECK_EQUAL(find(), 6);
+}
+
+BOOST_AUTO_TEST_SUITE_END() // Find
+
+BOOST_AUTO_TEST_SUITE_END()
+
+} // namespace tests
+} // namespace repo
diff --git a/tests/unit/skiplist.cpp b/tests/unit/skiplist.cpp
index 34083d6..c5bc0d5 100644
--- a/tests/unit/skiplist.cpp
+++ b/tests/unit/skiplist.cpp
@@ -36,7 +36,7 @@
{
};
-BOOST_FIXTURE_TEST_CASE_TEMPLATE(NdnNameSkipList, T, DatasetFixtures, Fixture<T>)
+BOOST_FIXTURE_TEST_CASE_TEMPLATE(NdnNameSkipList, T, DatasetFixtures_Index, Fixture<T>)
{
repo::SkipList<ndn::Name, typename T::DataSetNameCompare> skipList;
//Insert
@@ -49,7 +49,7 @@
for (typename T::DataContainer::iterator i = this->data.begin();
i != this->data.end(); ++i) {
typename repo::SkipList<ndn::Name, typename T::DataSetNameCompare>::iterator findIterator =
- skipList.find((*i)->getName());
+ skipList.lower_bound((*i)->getName());
skipList.erase(findIterator);
}