blob: a9f0cfa08d76a94134e563e2161456af1e37a6d8 [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_HASHTABLE_HPP
#define NFD_DAEMON_TABLE_NAME_TREE_HASHTABLE_HPP
#include "core/common.hpp"
namespace nfd {
namespace name_tree {
class Entry;
/** \brief a single hash value
*/
typedef size_t HashValue;
/** \brief a sequence of hash values
* \sa computeHashes
*/
typedef std::vector<HashValue> HashSequence;
/** \brief computes a single hash value
* \param name base name
* \param prefixLen if non-negative, compute hash value for name.getPrefix(prefixLen);
* if negative, compute hash value for complete name
*/
HashValue
computeHash(const Name& name, ssize_t prefixLen = -1);
/** \brief computes hash values for each prefix of name
* \return a hash sequence, where the i-th hash value equals computeHash(name, i)
*/
HashSequence
computeHashes(const Name& name);
/** \brief a hashtable node
*
* Zero or more nodes can be added to a hashtable bucket. They are organized as
* a doubly linked list through prev and next pointers.
*/
class Node
{
public:
/** \post entry != nullptr && entry->getName() == name
*/
Node(HashValue h, const Name& name);
/** \pre prev == nullptr
* \pre next == nullptr
*/
~Node();
public:
HashValue hash;
Node* prev;
Node* next;
shared_ptr<Entry> entry; /// \todo #3687 make Node sole owner of Entry
};
/** \return node associated with entry
* \note This function is for NameTree internal use.
*/
Node*
getNode(const Entry& entry);
/** \brief invoke a function for each node in a doubly linked list
* \tparam N either Node or const Node
* \tparam F a functor with signature void F(N*)
* \note It's safe to delete the node in the function.
*/
template<typename N, typename F>
void
foreachNode(N* head, const F& func)
{
N* node = head;
while (node != nullptr) {
N* next = node->next;
func(node);
node = next;
}
}
/** \brief provides options for Hashtable
*/
class HashtableOptions
{
public:
/** \brief constructor
* \post initialSize == size
* \post minSize == size
*/
explicit
HashtableOptions(size_t size = 16);
public:
/** \brief initial number of buckets
*/
size_t initialSize;
/** \brief minimal number of buckets
*/
size_t minSize;
/** \brief if hashtable has more than nBuckets*expandLoadFactor nodes, it will be expanded
*/
float expandLoadFactor = 0.5;
/** \brief when hashtable is expanded, its new size is nBuckets*expandFactor
*/
float expandFactor = 2.0;
/** \brief if hashtable has less than nBuckets*shrinkLoadFactor nodes, it will be shrunk
*/
float shrinkLoadFactor = 0.1;
/** \brief when hashtable is shrunk, its new size is max(nBuckets*shrinkFactor, minSize)
*/
float shrinkFactor = 0.5;
};
/** \brief a hashtable for fast exact name lookup
*
* The Hashtable contains a number of buckets.
* Each node is placed into a bucket determined by a hash value computed from its name.
* Hash collision is resolved through a doubly linked list in each bucket.
* The number of buckets is adjusted according to how many nodes are stored.
*/
class Hashtable
{
public:
typedef HashtableOptions Options;
explicit
Hashtable(const Options& options);
/** \brief deallocates all nodes
*/
~Hashtable();
/** \return number of nodes
*/
size_t
size() const
{
return m_size;
}
/** \return number of buckets
*/
size_t
getNBuckets() const
{
return m_buckets.size();
}
/** \return bucket index for hash value h
*/
size_t
computeBucketIndex(HashValue h) const
{
return h % this->getNBuckets();
}
/** \return i-th bucket
* \pre bucket < getNBuckets()
*/
const Node*
getBucket(size_t bucket) const
{
BOOST_ASSERT(bucket < this->getNBuckets());
return m_buckets[bucket]; // don't use m_bucket.at() for better performance
}
/** \brief find node for name.getPrefix(prefixLen)
* \pre name.size() > prefixLen
*/
const Node*
find(const Name& name, size_t prefixLen) const;
/** \brief find node for name.getPrefix(prefixLen)
* \pre name.size() > prefixLen
* \pre hashes == computeHashes(name)
*/
const Node*
find(const Name& name, size_t prefixLen, const HashSequence& hashes) const;
/** \brief find or insert node for name.getPrefix(prefixLen)
* \pre name.size() > prefixLen
* \pre hashes == computeHashes(name)
*/
std::pair<const Node*, bool>
insert(const Name& name, size_t prefixLen, const HashSequence& hashes);
/** \brief delete node
* \pre node exists in this hashtable
*/
void
erase(Node* node);
private:
/** \brief attach node to bucket
*/
void
attach(size_t bucket, Node* node);
/** \brief detach node from bucket
*/
void
detach(size_t bucket, Node* node);
std::pair<const Node*, bool>
findOrInsert(const Name& name, size_t prefixLen, HashValue h, bool allowInsert);
void
computeThresholds();
void
resize(size_t newNBuckets);
private:
std::vector<Node*> m_buckets;
Options m_options;
size_t m_size;
size_t m_expandThreshold;
size_t m_shrinkThreshold;
};
} // namespace name_tree
} // namespace nfd
#endif // NFD_DAEMON_TABLE_NAME_TREE_HASHTABLE_HPP