blob: 95ddc758f4967f92e02e31debecef5be0bad6233 [file] [log] [blame]
Yingdi Yu274a2272014-08-26 19:25:16 -07001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
2/*
Alexander Afanasyeve9eda8a2017-03-09 14:40:03 -08003 * Copyright (c) 2012-2017 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
28#include "mi-tag.hpp"
29#include "leaf.hpp"
30
Yingdi Yu274a2272014-08-26 19:25:16 -070031#include <boost/multi_index_container.hpp>
32#include <boost/multi_index/ordered_index.hpp>
33#include <boost/multi_index/hashed_index.hpp>
34#include <boost/multi_index/mem_fun.hpp>
35
Ashlesh Gawande687cf922017-05-30 15:04:16 -050036#include <ndn-cxx/util/sha256.hpp>
Yingdi Yu274a2272014-08-26 19:25:16 -070037
38namespace chronosync {
39
40namespace mi = boost::multi_index;
41
42struct SessionNameHash
43{
44 std::size_t
45 operator()(const Name& prefix) const
46 {
Ashlesh Gawande08784d42017-09-06 23:40:21 -050047 ConstBufferPtr buffer =
Ashlesh Gawande687cf922017-05-30 15:04:16 -050048 ndn::util::Sha256::computeDigest(prefix.wireEncode().wire(), prefix.wireEncode().size());
Yingdi Yu274a2272014-08-26 19:25:16 -070049
50 BOOST_ASSERT(buffer->size() > sizeof(std::size_t));
51
Davide Pesavento5473abe2017-10-09 01:35:33 -040052 return *reinterpret_cast<const std::size_t*>(buffer->data());
Yingdi Yu274a2272014-08-26 19:25:16 -070053 }
54};
55
56struct SessionNameEqual
57{
58 bool
59 operator()(const Name& prefix1, const Name& prefix2) const
60 {
61 return prefix1 == prefix2;
62 }
63};
64
65struct SessionNameCompare : public std::less<Name>
66{
67 bool
68 operator()(const Name& prefix1, const Name& prefix2) const
69 {
70 return prefix1 < prefix2;
71 }
72};
73
74/**
75 * @brief Container for chronosync leaves
76 */
77struct LeafContainer : public mi::multi_index_container<
78 LeafPtr,
79 mi::indexed_by<
80 // For fast access to elements using SessionName
81 mi::hashed_unique<
82 mi::tag<hashed>,
83 mi::const_mem_fun<Leaf, const Name&, &Leaf::getSessionName>,
84 SessionNameHash,
85 SessionNameEqual
86 >,
87
88 mi::ordered_unique<
89 mi::tag<ordered>,
90 mi::const_mem_fun<Leaf, const Name&, &Leaf::getSessionName>,
91 SessionNameCompare
92 >
93 >
94 >
95{
96};
97
98} // namespace chronosync
99
100#endif // CHRONOSYNC_LEAF_CONTAINER_HPP