Junxiao Shi | fc20696 | 2015-01-16 11:12:22 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2014-2015, Regents of the University of California, |
| 4 | * Arizona Board of Regents, |
| 5 | * Colorado State University, |
| 6 | * University Pierre & Marie Curie, Sorbonne University, |
| 7 | * Washington University in St. Louis, |
| 8 | * Beijing Institute of Technology, |
| 9 | * The University of Memphis. |
| 10 | * |
| 11 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 12 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 13 | * |
| 14 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 15 | * of the GNU General Public License as published by the Free Software Foundation, |
| 16 | * either version 3 of the License, or (at your option) any later version. |
| 17 | * |
| 18 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 19 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 20 | * PURPOSE. See the GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License along with |
| 23 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 24 | */ |
| 25 | |
| 26 | #include "cs-entry-impl.hpp" |
| 27 | |
| 28 | namespace nfd { |
| 29 | namespace cs { |
| 30 | |
| 31 | EntryImpl::EntryImpl(const Name& name) |
| 32 | : m_queryName(name) |
| 33 | { |
| 34 | BOOST_ASSERT(this->isQuery()); |
| 35 | } |
| 36 | |
| 37 | EntryImpl::EntryImpl(shared_ptr<const Data> data, bool isUnsolicited) |
Junxiao Shi | fc20696 | 2015-01-16 11:12:22 -0700 | [diff] [blame] | 38 | { |
| 39 | this->setData(data, isUnsolicited); |
| 40 | BOOST_ASSERT(!this->isQuery()); |
| 41 | } |
| 42 | |
| 43 | bool |
| 44 | EntryImpl::isQuery() const |
| 45 | { |
| 46 | return !this->hasData(); |
| 47 | } |
| 48 | |
| 49 | bool |
| 50 | EntryImpl::canStale() const |
| 51 | { |
| 52 | BOOST_ASSERT(!this->isQuery()); |
| 53 | return this->getStaleTime() < time::steady_clock::TimePoint::max(); |
| 54 | } |
| 55 | |
| 56 | void |
| 57 | EntryImpl::unsetUnsolicited() |
| 58 | { |
| 59 | BOOST_ASSERT(!this->isQuery()); |
| 60 | this->setData(this->getData(), false); |
| 61 | } |
| 62 | |
Junxiao Shi | 5af9bd3 | 2015-01-28 19:57:31 -0700 | [diff] [blame] | 63 | int |
| 64 | compareQueryWithData(const Name& queryName, const Data& data) |
| 65 | { |
| 66 | bool queryIsFullName = !queryName.empty() && queryName[-1].isImplicitSha256Digest(); |
| 67 | |
| 68 | int cmp = queryIsFullName ? |
| 69 | queryName.compare(0, queryName.size() - 1, data.getName()) : |
| 70 | queryName.compare(data.getName()); |
| 71 | |
| 72 | if (cmp != 0) { // Name without digest differs |
| 73 | return cmp; |
| 74 | } |
| 75 | |
| 76 | if (queryIsFullName) { // Name without digest equals, compare digest |
| 77 | return queryName[-1].compare(data.getFullName()[-1]); |
| 78 | } |
| 79 | else { // queryName is a proper prefix of Data fullName |
| 80 | return -1; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | int |
| 85 | compareDataWithData(const Data& lhs, const Data& rhs) |
| 86 | { |
| 87 | int cmp = lhs.getName().compare(rhs.getName()); |
| 88 | if (cmp != 0) { |
| 89 | return cmp; |
| 90 | } |
| 91 | |
| 92 | return lhs.getFullName()[-1].compare(rhs.getFullName()[-1]); |
| 93 | } |
| 94 | |
Junxiao Shi | fc20696 | 2015-01-16 11:12:22 -0700 | [diff] [blame] | 95 | bool |
| 96 | EntryImpl::operator<(const EntryImpl& other) const |
| 97 | { |
| 98 | if (this->isQuery()) { |
| 99 | if (other.isQuery()) { |
| 100 | return m_queryName < other.m_queryName; |
| 101 | } |
| 102 | else { |
Junxiao Shi | 5af9bd3 | 2015-01-28 19:57:31 -0700 | [diff] [blame] | 103 | return compareQueryWithData(m_queryName, other.getData()) < 0; |
Junxiao Shi | fc20696 | 2015-01-16 11:12:22 -0700 | [diff] [blame] | 104 | } |
| 105 | } |
| 106 | else { |
| 107 | if (other.isQuery()) { |
Junxiao Shi | 5af9bd3 | 2015-01-28 19:57:31 -0700 | [diff] [blame] | 108 | return compareQueryWithData(other.m_queryName, this->getData()) > 0; |
Junxiao Shi | fc20696 | 2015-01-16 11:12:22 -0700 | [diff] [blame] | 109 | } |
| 110 | else { |
Junxiao Shi | 5af9bd3 | 2015-01-28 19:57:31 -0700 | [diff] [blame] | 111 | return compareDataWithData(this->getData(), other.getData()) < 0; |
Junxiao Shi | fc20696 | 2015-01-16 11:12:22 -0700 | [diff] [blame] | 112 | } |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | } // namespace cs |
| 117 | } // namespace nfd |