blob: 4e7a805e00d30fa5c98d6479f1ce87183ef8d6d8 [file] [log] [blame]
Yingdi Yu274a2272014-08-26 19:25:16 -07001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
2/*
Davide Pesavento07684bc2021-02-07 20:09:28 -05003 * Copyright (c) 2012-2021 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{
45 std::size_t
46 operator()(const Name& prefix) const
47 {
Ashlesh Gawande08784d42017-09-06 23:40:21 -050048 ConstBufferPtr buffer =
Ashlesh Gawande687cf922017-05-30 15:04:16 -050049 ndn::util::Sha256::computeDigest(prefix.wireEncode().wire(), prefix.wireEncode().size());
Yingdi Yu274a2272014-08-26 19:25:16 -070050
51 BOOST_ASSERT(buffer->size() > sizeof(std::size_t));
52
Davide Pesavento5473abe2017-10-09 01:35:33 -040053 return *reinterpret_cast<const std::size_t*>(buffer->data());
Yingdi Yu274a2272014-08-26 19:25:16 -070054 }
55};
56
57struct SessionNameEqual
58{
59 bool
60 operator()(const Name& prefix1, const Name& prefix2) const
61 {
62 return prefix1 == prefix2;
63 }
64};
65
66struct SessionNameCompare : public std::less<Name>
67{
68 bool
69 operator()(const Name& prefix1, const Name& prefix2) const
70 {
71 return prefix1 < prefix2;
72 }
73};
74
75/**
76 * @brief Container for chronosync leaves
77 */
78struct LeafContainer : public mi::multi_index_container<
79 LeafPtr,
80 mi::indexed_by<
81 // For fast access to elements using SessionName
82 mi::hashed_unique<
83 mi::tag<hashed>,
84 mi::const_mem_fun<Leaf, const Name&, &Leaf::getSessionName>,
85 SessionNameHash,
86 SessionNameEqual
87 >,
88
89 mi::ordered_unique<
90 mi::tag<ordered>,
91 mi::const_mem_fun<Leaf, const Name&, &Leaf::getSessionName>,
92 SessionNameCompare
93 >
94 >
95 >
96{
97};
98
99} // namespace chronosync
100
101#endif // CHRONOSYNC_LEAF_CONTAINER_HPP