blob: 0a01bd7602efa51ad65810e538fa82e2fe820c37 [file] [log] [blame]
Yingdi Yu274a2272014-08-26 19:25:16 -07001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
2/*
Davide Pesavento5f5101a2022-03-11 20:05:03 -05003 * Copyright (c) 2012-2022 University of California, Los Angeles
Yingdi Yu274a2272014-08-26 19:25:16 -07004 *
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 Pesavento07684bc2021-02-07 20:09:28 -050028#include "detail/mi-tag.hpp"
Yingdi Yu274a2272014-08-26 19:25:16 -070029#include "leaf.hpp"
30
Yingdi Yu274a2272014-08-26 19:25:16 -070031#include <boost/multi_index_container.hpp>
Yingdi Yu274a2272014-08-26 19:25:16 -070032#include <boost/multi_index/hashed_index.hpp>
Davide Pesavento07684bc2021-02-07 20:09:28 -050033#include <boost/multi_index/ordered_index.hpp>
Yingdi Yu274a2272014-08-26 19:25:16 -070034#include <boost/multi_index/mem_fun.hpp>
Davide Pesavento07684bc2021-02-07 20:09:28 -050035#include <boost/multi_index/tag.hpp>
Yingdi Yu274a2272014-08-26 19:25:16 -070036
Ashlesh Gawande687cf922017-05-30 15:04:16 -050037#include <ndn-cxx/util/sha256.hpp>
Yingdi Yu274a2272014-08-26 19:25:16 -070038
39namespace chronosync {
40
41namespace mi = boost::multi_index;
42
43struct SessionNameHash
44{
Davide Pesavento5f5101a2022-03-11 20:05:03 -050045 static_assert(ndn::util::Sha256::DIGEST_SIZE >= sizeof(std::size_t), "");
46
Yingdi Yu274a2272014-08-26 19:25:16 -070047 std::size_t
48 operator()(const Name& prefix) const
49 {
Davide Pesavento5f5101a2022-03-11 20:05:03 -050050 auto buffer = ndn::util::Sha256::computeDigest(prefix.wireEncode());
Davide Pesavento5473abe2017-10-09 01:35:33 -040051 return *reinterpret_cast<const std::size_t*>(buffer->data());
Yingdi Yu274a2272014-08-26 19:25:16 -070052 }
53};
54
55struct SessionNameEqual
56{
57 bool
58 operator()(const Name& prefix1, const Name& prefix2) const
59 {
60 return prefix1 == prefix2;
61 }
62};
63
64struct 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 */
76struct 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