m2etis  0.4
DirectBroadcastPartition.h
Go to the documentation of this file.
1 /*
2  Copyright (2016) Michael Baer, Daniel Bonrath, All rights reserved.
3 
4  Licensed under the Apache License, Version 2.0 (the "License");
5  you may not use this file except in compliance with the License.
6  You may obtain a copy of the License at
7 
8  http://www.apache.org/licenses/LICENSE-2.0
9 
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15  */
16 
22 #ifndef __M2ETIS_PUBSUB_PARTITION_DIRECTBROADCASTPARTITION_H__
23 #define __M2ETIS_PUBSUB_PARTITION_DIRECTBROADCASTPARTITION_H__
24 
25 #include <map>
26 #include <sstream>
27 
31 
32 namespace m2etis {
33 namespace pubsub {
34 namespace partition {
35 
36  template<typename NetworkType, typename EventType>
37  class DirectBroadcastPartition : public BasePartition<NetworkType, EventType> {
38  public:
39  typedef boost::shared_ptr<EventType> PayloadPtr;
40 
41  static const bool DYNAMIC_PARTITION = true;
42 
43  DirectBroadcastPartition() : partition_filter_vector_() {
44  }
45 
47 
48  void createRendezvousPartition(const typename NetworkType::Key & rendezvous) {
49  partition_filter_vector_.push_back(std::make_pair(boost::shared_ptr<filter::FilterExp<EventType>>(), rendezvous));
50  }
51 
52  std::vector<int> getTreeNames() {
53  std::vector<int> ret;
54  if (partition_filter_vector_.empty()) {
55  ret.push_back(0);
56  return ret;
57  }
58  for (std::vector<int>::size_type i = 0; i < partition_filter_vector_.size(); ++i) {
59  ret.push_back(int(i));
60  }
61  return ret;
62  }
63 
64  std::vector<int>::size_type getPublishTree(const PayloadPtr message, const typename NetworkType::Key & self) {
65  std::vector<int>::size_type i = 0;
66  for (std::pair<boost::shared_ptr<filter::FilterExp<EventType>>, typename NetworkType::Key> current_filter : partition_filter_vector_) {
67  if (self == current_filter.second) {
68  return i;
69  }
70  ++i;
71  }
72  return i;
73  }
74 
75  std::vector<unsigned int> getSubscribeTrees(boost::shared_ptr<filter::FilterExp<EventType> > dynamic_filter) {
76  std::vector<unsigned int> overlapping_trees;
77  if (partition_filter_vector_.empty()) {
78  overlapping_trees.push_back(0);
79  return overlapping_trees;
80  }
81  unsigned int tree_index = 0;
82 
83  for (std::pair<boost::shared_ptr<filter::FilterExp<EventType>>, typename NetworkType::Key> partition_filter : partition_filter_vector_) {
84  overlapping_trees.push_back(tree_index);
85  ++tree_index;
86  }
87  return overlapping_trees;
88  }
89 
90  boost::shared_ptr<filter::FilterExp<EventType>> getPredicate(size_t id) {
91  assert(id < partition_filter_vector_.size());
92  return partition_filter_vector_[id].first;
93  }
94 
95  bool createPartition(const typename NetworkType::Key & root) {
96  partition_filter_vector_.push_back(std::make_pair(boost::shared_ptr<filter::FilterExp<EventType>>(), root));
97  return true;
98  }
99 
100  void addPartition(boost::shared_ptr<filter::FilterExp<EventType>> predicate, const typename NetworkType::Key & root) {
101  partition_filter_vector_.push_back(std::make_pair(predicate, root));
102  }
103 
104  void removePartition(size_t id) {
105  partition_filter_vector_.erase(partition_filter_vector_.begin() + int64_t(id));
106  }
107 
108  void changePredicate(size_t, boost::shared_ptr<filter::FilterExp<EventType>>) {
109  }
110 
111  void changeRoot(size_t id, typename NetworkType::Key & root) {
112  partition_filter_vector_[id].second = root;
113  }
114 
115  std::string toString() {
116  std::stringstream ss;
117  ss << "DBParition with " << partition_filter_vector_.size() << " elements: (";
118  for (unsigned int i = 0; i < partition_filter_vector_.size(); i++) {
119  ss << "(" << partition_filter_vector_[i].second.toStr() << ", " << partition_filter_vector_[i].first->toString() << "), ";
120  }
121  ss << ")";
122  return ss.str();
123  }
124 
125  private:
126  std::vector<std::pair<boost::shared_ptr<filter::FilterExp<EventType>>, typename NetworkType::Key> > partition_filter_vector_;
127  };
128 
129 } /* namespace partition */
130 } /* namespace pubsub */
131 } /* namespace m2etis */
132 
133 #endif /* __M2ETIS_PUBSUB_PARTITION_DIRECTBROADCASTPARTITION_H__ */
134 
void changeRoot(size_t id, typename NetworkType::Key &root)
boost::shared_ptr< filter::FilterExp< EventType > > getPredicate(size_t id)
returns the predicate for the given partition id (dynamic)
std::vector< int > getTreeNames()
returns amount of partitions this strategy contains (static & dynamic)
void removePartition(size_t id)
removes the partition with the given id (dynamic)
bool createPartition(const typename NetworkType::Key &root)
can create a new partition for this channel if a new node (root) joins, return true, if a new partition was created, otherwise false (dynamic)
std::vector< int >::size_type getPublishTree(const PayloadPtr message, const typename NetworkType::Key &self)
returns the tree index the given node with the given payload should publish on (static & dynamic) ...
void createRendezvousPartition(const typename NetworkType::Key &rendezvous)
creates a base partition for the RP (dynamic)
void addPartition(boost::shared_ptr< filter::FilterExp< EventType >> predicate, const typename NetworkType::Key &root)
std::vector< unsigned int > getSubscribeTrees(boost::shared_ptr< filter::FilterExp< EventType > > dynamic_filter)
void changePredicate(size_t, boost::shared_ptr< filter::FilterExp< EventType >>)
changes the filter predicate for the partition with the given id (dynamic)