Yingdi Yu | 77627ab | 2015-07-21 16:13:49 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Yingdi Yu | 0a312e5 | 2015-07-22 13:14:53 -0700 | [diff] [blame] | 3 | * Copyright (c) 2014-2015, Regents of the University of California. |
Yingdi Yu | 77627ab | 2015-07-21 16:13:49 -0700 | [diff] [blame] | 4 | * |
Yingdi Yu | 0a312e5 | 2015-07-22 13:14:53 -0700 | [diff] [blame] | 5 | * This file is part of ndn-tools (Named Data Networking Essential Tools). |
| 6 | * See AUTHORS.md for complete list of ndn-tools authors and contributors. |
Yingdi Yu | 77627ab | 2015-07-21 16:13:49 -0700 | [diff] [blame] | 7 | * |
Yingdi Yu | 0a312e5 | 2015-07-22 13:14:53 -0700 | [diff] [blame] | 8 | * ndn-tools 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. |
Yingdi Yu | 77627ab | 2015-07-21 16:13:49 -0700 | [diff] [blame] | 11 | * |
Yingdi Yu | 0a312e5 | 2015-07-22 13:14:53 -0700 | [diff] [blame] | 12 | * ndn-tools 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. |
Yingdi Yu | 77627ab | 2015-07-21 16:13:49 -0700 | [diff] [blame] | 15 | * |
Yingdi Yu | 0a312e5 | 2015-07-22 13:14:53 -0700 | [diff] [blame] | 16 | * You should have received a copy of the GNU General Public License along with |
| 17 | * ndn-tools, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
Yingdi Yu | 77627ab | 2015-07-21 16:13:49 -0700 | [diff] [blame] | 18 | * |
Yingdi Yu | 0a312e5 | 2015-07-22 13:14:53 -0700 | [diff] [blame] | 19 | * @author Yingdi Yu <yingdi@cs.ucla.edu> |
Yingdi Yu | 77627ab | 2015-07-21 16:13:49 -0700 | [diff] [blame] | 20 | */ |
| 21 | |
| 22 | #include "key-cache.hpp" |
| 23 | |
| 24 | namespace ndn { |
| 25 | namespace pib { |
| 26 | |
| 27 | KeyCacheEntry::KeyCacheEntry(const Name& name, shared_ptr<PublicKey> key) |
| 28 | : name(name) |
| 29 | , key(key) |
| 30 | { |
| 31 | BOOST_ASSERT(static_cast<bool>(key)); |
| 32 | } |
| 33 | |
| 34 | KeyCache::KeyCache(size_t capacity) |
| 35 | : m_capacity(capacity) |
| 36 | { |
| 37 | } |
| 38 | |
| 39 | void |
| 40 | KeyCache::insert(const Name& name, shared_ptr<PublicKey> key) |
| 41 | { |
| 42 | // check if key exist |
| 43 | KeyContainer::index<byName>::type::iterator it = m_keys.get<byName>().find(name); |
| 44 | if (it != m_keys.get<byName>().end()) { |
| 45 | adjustLru(it); |
| 46 | return; |
| 47 | } |
| 48 | |
| 49 | // evict key when capacity is reached |
| 50 | if (size() >= m_capacity) |
| 51 | evictKey(); |
| 52 | |
| 53 | // insert entry |
| 54 | m_keys.insert(KeyCacheEntry(name, key)); |
| 55 | } |
| 56 | |
| 57 | shared_ptr<PublicKey> |
| 58 | KeyCache::find(const Name& name) const |
| 59 | { |
| 60 | // check if key exist |
| 61 | KeyContainer::index<byName>::type::iterator it = m_keys.get<byName>().find(name); |
| 62 | if (it == m_keys.get<byName>().end()) |
| 63 | return shared_ptr<PublicKey>(); |
| 64 | else { |
| 65 | // adjust lru |
| 66 | shared_ptr<PublicKey> key = it->key; |
| 67 | adjustLru(it); |
| 68 | return key; |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | void |
| 73 | KeyCache::erase(const Name& name) |
| 74 | { |
| 75 | // check if key exist |
| 76 | KeyContainer::index<byName>::type::iterator it = m_keys.get<byName>().find(name); |
| 77 | if (it != m_keys.get<byName>().end()) { |
| 78 | m_keys.erase(it); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | size_t |
| 83 | KeyCache::size() const |
| 84 | { |
| 85 | return m_keys.size(); |
| 86 | } |
| 87 | |
| 88 | void |
| 89 | KeyCache::evictKey() |
| 90 | { |
| 91 | if (!m_keys.get<byUsedTime>().empty()) { |
| 92 | KeyContainer::index<byUsedTime>::type::iterator it = m_keys.get<byUsedTime>().begin(); |
| 93 | m_keys.erase(m_keys.project<0>(it)); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | void |
| 98 | KeyCache::adjustLru(KeyContainer::iterator it) const |
| 99 | { |
| 100 | KeyCacheEntry entry = std::move(*it); |
| 101 | m_keys.erase(it); |
| 102 | m_keys.insert(entry); |
| 103 | } |
| 104 | |
| 105 | } // namespace pib |
| 106 | } // namespace ndn |