Yingdi Yu | 274a227 | 2014-08-26 19:25:16 -0700 | [diff] [blame] | 1 | /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
Davide Pesavento | 5f5101a | 2022-03-11 20:05:03 -0500 | [diff] [blame^] | 3 | * Copyright (c) 2012-2022 University of California, Los Angeles |
Yingdi Yu | 274a227 | 2014-08-26 19:25:16 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ChronoSync, synchronization library for distributed realtime |
| 6 | * applications for NDN. |
| 7 | * |
| 8 | * ChronoSync 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, either |
| 10 | * version 3 of the License, or (at your option) any later version. |
| 11 | * |
| 12 | * ChronoSync 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. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License along with |
| 17 | * ChronoSync, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * @author Zhenkai Zhu <http://irl.cs.ucla.edu/~zhenkai/> |
| 20 | * @author Chaoyi Bian <bcy@pku.edu.cn> |
| 21 | * @author Alexander Afanasyev <http://lasr.cs.ucla.edu/afanasyev/index.html> |
| 22 | * @author Yingdi Yu <yingdi@cs.ucla.edu> |
| 23 | */ |
| 24 | |
| 25 | #ifndef CHRONOSYNC_LEAF_CONTAINER_HPP |
| 26 | #define CHRONOSYNC_LEAF_CONTAINER_HPP |
| 27 | |
Davide Pesavento | 07684bc | 2021-02-07 20:09:28 -0500 | [diff] [blame] | 28 | #include "detail/mi-tag.hpp" |
Yingdi Yu | 274a227 | 2014-08-26 19:25:16 -0700 | [diff] [blame] | 29 | #include "leaf.hpp" |
| 30 | |
Yingdi Yu | 274a227 | 2014-08-26 19:25:16 -0700 | [diff] [blame] | 31 | #include <boost/multi_index_container.hpp> |
Yingdi Yu | 274a227 | 2014-08-26 19:25:16 -0700 | [diff] [blame] | 32 | #include <boost/multi_index/hashed_index.hpp> |
Davide Pesavento | 07684bc | 2021-02-07 20:09:28 -0500 | [diff] [blame] | 33 | #include <boost/multi_index/ordered_index.hpp> |
Yingdi Yu | 274a227 | 2014-08-26 19:25:16 -0700 | [diff] [blame] | 34 | #include <boost/multi_index/mem_fun.hpp> |
Davide Pesavento | 07684bc | 2021-02-07 20:09:28 -0500 | [diff] [blame] | 35 | #include <boost/multi_index/tag.hpp> |
Yingdi Yu | 274a227 | 2014-08-26 19:25:16 -0700 | [diff] [blame] | 36 | |
Ashlesh Gawande | 687cf92 | 2017-05-30 15:04:16 -0500 | [diff] [blame] | 37 | #include <ndn-cxx/util/sha256.hpp> |
Yingdi Yu | 274a227 | 2014-08-26 19:25:16 -0700 | [diff] [blame] | 38 | |
| 39 | namespace chronosync { |
| 40 | |
| 41 | namespace mi = boost::multi_index; |
| 42 | |
| 43 | struct SessionNameHash |
| 44 | { |
Davide Pesavento | 5f5101a | 2022-03-11 20:05:03 -0500 | [diff] [blame^] | 45 | static_assert(ndn::util::Sha256::DIGEST_SIZE >= sizeof(std::size_t), ""); |
| 46 | |
Yingdi Yu | 274a227 | 2014-08-26 19:25:16 -0700 | [diff] [blame] | 47 | std::size_t |
| 48 | operator()(const Name& prefix) const |
| 49 | { |
Davide Pesavento | 5f5101a | 2022-03-11 20:05:03 -0500 | [diff] [blame^] | 50 | auto buffer = ndn::util::Sha256::computeDigest(prefix.wireEncode()); |
Davide Pesavento | 5473abe | 2017-10-09 01:35:33 -0400 | [diff] [blame] | 51 | return *reinterpret_cast<const std::size_t*>(buffer->data()); |
Yingdi Yu | 274a227 | 2014-08-26 19:25:16 -0700 | [diff] [blame] | 52 | } |
| 53 | }; |
| 54 | |
| 55 | struct SessionNameEqual |
| 56 | { |
| 57 | bool |
| 58 | operator()(const Name& prefix1, const Name& prefix2) const |
| 59 | { |
| 60 | return prefix1 == prefix2; |
| 61 | } |
| 62 | }; |
| 63 | |
| 64 | struct SessionNameCompare : public std::less<Name> |
| 65 | { |
| 66 | bool |
| 67 | operator()(const Name& prefix1, const Name& prefix2) const |
| 68 | { |
| 69 | return prefix1 < prefix2; |
| 70 | } |
| 71 | }; |
| 72 | |
| 73 | /** |
| 74 | * @brief Container for chronosync leaves |
| 75 | */ |
| 76 | struct LeafContainer : public mi::multi_index_container< |
| 77 | LeafPtr, |
| 78 | mi::indexed_by< |
| 79 | // For fast access to elements using SessionName |
| 80 | mi::hashed_unique< |
| 81 | mi::tag<hashed>, |
| 82 | mi::const_mem_fun<Leaf, const Name&, &Leaf::getSessionName>, |
| 83 | SessionNameHash, |
| 84 | SessionNameEqual |
| 85 | >, |
| 86 | |
| 87 | mi::ordered_unique< |
| 88 | mi::tag<ordered>, |
| 89 | mi::const_mem_fun<Leaf, const Name&, &Leaf::getSessionName>, |
| 90 | SessionNameCompare |
| 91 | > |
| 92 | > |
| 93 | > |
| 94 | { |
| 95 | }; |
| 96 | |
| 97 | } // namespace chronosync |
| 98 | |
| 99 | #endif // CHRONOSYNC_LEAF_CONTAINER_HPP |