blob: d630cad7c5b0c925a98ea2aec72e885b7914a618 [file] [log] [blame]
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/**
* Copyright (c) 2014-2016, Regents of the University of California,
* Arizona Board of Regents,
* Colorado State University,
* University Pierre & Marie Curie, Sorbonne University,
* Washington University in St. Louis,
* Beijing Institute of Technology,
* The University of Memphis.
*
* This file is part of NFD (Named Data Networking Forwarding Daemon).
* See AUTHORS.md for complete list of NFD authors and contributors.
*
* NFD 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.
*
* NFD 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
* NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef NFD_DAEMON_TABLE_NAME_TREE_HPP
#define NFD_DAEMON_TABLE_NAME_TREE_HPP
#include "name-tree-iterator.hpp"
namespace nfd {
namespace name_tree {
/** \brief a common index structure for FIB, PIT, StrategyChoice, and Measurements
*/
class NameTree : noncopyable
{
public:
typedef Iterator const_iterator;
explicit
NameTree(size_t nBuckets = 1024);
public: // information
/** \return number of name tree entries
*/
size_t
size() const
{
return m_ht.size();
}
/** \return number of hashtable buckets
*/
size_t
getNBuckets() const
{
return m_ht.getNBuckets();
}
/** \return entry on which a table entry is attached
*/
template<typename ENTRY>
shared_ptr<Entry>
getEntry(const ENTRY& tableEntry) const
{
return tableEntry.m_nameTreeEntry.lock();
}
public: // mutation
/** \brief find or insert an entry with specified name
* \param name a name prefix
* \return an entry with \p name
* \post an entry with \p name and all ancestors are created
* \note Existing iterators are unaffected.
*/
shared_ptr<Entry>
lookup(const Name& name);
/** \brief equivalent to .lookup(fibEntry.getPrefix())
* \note This overload is more efficient than .lookup(const Name&) in common cases.
*/
shared_ptr<Entry>
lookup(const fib::Entry& fibEntry) const;
/** \brief equivalent to .lookup(pitEntry.getName()).
* \note This overload is more efficient than .lookup(const Name&) in common cases.
*/
shared_ptr<Entry>
lookup(const pit::Entry& pitEntry);
/** \brief equivalent to .lookup(measurementsEntry.getName())
* \note This overload is more efficient than .lookup(const Name&) in common cases.
*/
shared_ptr<Entry>
lookup(const measurements::Entry& measurementsEntry) const;
/** \brief equivalent to .lookup(strategyChoiceEntry.getName())
* \note This overload is more efficient than .lookup(const Name&) in common cases.
*/
shared_ptr<Entry>
lookup(const strategy_choice::Entry& strategyChoiceEntry) const;
/** \brief delete the entry if it is empty
* \param entry a valid entry
* \return number of deleted entries
* \sa Entry::isEmpty()
* \post If the entry is empty, it's deleted.
* Ancestors of the entry are also deleted if they become empty.
* \note This function must be called after detaching a table entry from a name tree entry,
* \note Existing iterators, except those pointing to deleted entries, are unaffected.
*/
size_t
eraseIfEmpty(Entry* entry);
public: // matching
/** \brief exact match lookup
* \return entry with \p name, or nullptr if it does not exist
*/
shared_ptr<Entry>
findExactMatch(const Name& name) const;
/** \brief longest prefix matching
* \return entry whose name is a prefix of \p name and passes \p entrySelector,
* where no other entry with a longer name satisfies those requirements;
* or nullptr if no entry satisfying those requirements exists
*/
shared_ptr<Entry>
findLongestPrefixMatch(const Name& name,
const EntrySelector& entrySelector = AnyEntry()) const;
/** \brief equivalent to .findLongestPrefixMatch(entry->getName(), entrySelector)
* \note This overload is more efficient than .findLongestPrefixMatch(const Name&)
* in common cases.
*/
shared_ptr<Entry>
findLongestPrefixMatch(shared_ptr<Entry> entry,
const EntrySelector& entrySelector = AnyEntry()) const;
/** \brief equivalent to .findLongestPrefixMatch(pitEntry.getName(), AnyEntry())
* \note This overload is more efficient than .findLongestPrefixMatch(const Name&)
* in common cases.
*/
shared_ptr<Entry>
findLongestPrefixMatch(const pit::Entry& pitEntry) const;
/** \brief all-prefixes match lookup
* \return an unspecified type that have .begin() and .end() methods
* and is usable with range-based for.
* Every entry in the range has a name that is a prefix of \p name ,
* and matches \p entrySelector.
*
* Example:
* \code
* Name name("/A/B/C");
* auto&& allMatches = nt.findAllMatches(name);
* for (const Entry& nte : allMatches) {
* BOOST_ASSERT(nte.getName().isPrefixOf(name));
* ...
* }
* \endcode
* \note Iteration order is implementation-specific and is undefined.
* \warning If a name tree entry whose name is a prefix of \p name is inserted
* during the enumeration, it may or may not be visited.
* If a name tree entry whose name is a prefix of \p name is deleted
* during the enumeration, undefined behavior may occur.
*/
boost::iterator_range<const_iterator>
findAllMatches(const Name& name,
const EntrySelector& entrySelector = AnyEntry()) const;
public: // enumeration
/** \brief enumerate all entries
* \return an unspecified type that have .begin() and .end() methods
* and is usable with range-based for.
* Every entry in the range matches \p entrySelector.
*
* Example:
* \code
* auto&& enumerable = nt.fullEnumerate();
* for (const Entry& nte : enumerable) {
* ...
* }
* \endcode
* \note Iteration order is implementation-specific and is undefined.
* \warning If a name tree entry is inserted or deleted during the enumeration,
* it may cause the enumeration to skip entries or visit some entries twice.
*/
boost::iterator_range<const_iterator>
fullEnumerate(const EntrySelector& entrySelector = AnyEntry()) const;
/** \brief enumerate all entries under a prefix
* \return an unspecified type that have .begin() and .end() methods
* and is usable with range-based for.
* Every entry in the range has a name that starts with \p prefix,
* and matches \p entrySubTreeSelector.
*
* Example:
* \code
* Name name("/A/B/C");
* auto&& enumerable = nt.partialEnumerate(name);
* for (const Entry& nte : enumerable) {
* BOOST_ASSERT(name.isPrefixOf(nte.getName()));
* ...
* }
* \endcode
* \note Iteration order is implementation-specific and is undefined.
* \warning If a name tree entry under \p prefix is inserted or deleted during the enumeration,
* it may cause the enumeration to skip entries or visit some entries twice.
*/
boost::iterator_range<const_iterator>
partialEnumerate(const Name& prefix,
const EntrySubTreeSelector& entrySubTreeSelector = AnyEntrySubTree()) const;
/** \return an iterator to enumerate all entries
* \sa fullEnumerate
*/
const_iterator
begin() const
{
return fullEnumerate().begin();
}
/** \return a past-the-end iterator
*/
const_iterator
end() const
{
return Iterator();
}
private:
Hashtable m_ht;
friend class EnumerationImpl;
};
} // namespace name_tree
using name_tree::NameTree;
} // namespace nfd
#endif // NFD_DAEMON_TABLE_NAME_TREE_HPP