IBR-DTNSuite  0.10
MetaStorage.cpp
Go to the documentation of this file.
1 /*
2  * MetaStorage.cpp
3  *
4  * Copyright (C) 2013 IBR, TU Braunschweig
5  *
6  * Written-by: Johannes Morgenroth <morgenroth@ibr.cs.tu-bs.de>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21 
22 #include "storage/MetaStorage.h"
23 
24 namespace dtn
25 {
26  namespace storage
27  {
29  : _expire_listener(expire_listener), _list(this)
30  {
31  }
32 
34  {
35  }
36 
38  {
39  _expire_listener.eventBundleExpired(b);
40 
41  // remove the bundle out of all lists
42  _priority_index.erase(b);
43  _bundle_lengths.erase(b);
44  _removal_set.erase(b);
45  }
46 
47  bool MetaStorage::has(const dtn::data::MetaBundle &m) const throw ()
48  {
49  dtn::data::BundleList::const_iterator it = _list.find(m);
50  return (it != _list.end());
51  }
52 
53  void MetaStorage::expire(const dtn::data::Timestamp &timestamp) throw ()
54  {
55  _list.expire(timestamp);
56  }
57 
59  {
60  for (const_iterator iter = begin(); iter != end(); ++iter)
61  {
62  const dtn::data::MetaBundle &bundle = (*iter);
63 
64  // skip removal-marked bundles
65  if (_removal_set.find(bundle) != _removal_set.end()) continue;
66 
67  if (filter.contains(bundle.toString()))
68  {
69  return bundle;
70  }
71  }
72 
73  throw NoBundleFoundException();
74  }
75 
76  std::set<dtn::data::EID> MetaStorage::getDistinctDestinations() const throw ()
77  {
78  std::set<dtn::data::EID> ret;
79 
80  for (dtn::data::BundleList::const_iterator iter = begin(); iter != end(); ++iter)
81  {
82  const dtn::data::MetaBundle &bundle = (*iter);
83 
84  // skip removal-marked bundles
85  if (_removal_set.find(bundle) != _removal_set.end()) continue;
86 
87  ret.insert(bundle.destination);
88  }
89 
90  return ret;
91  }
92 
93  void MetaStorage::store(const dtn::data::MetaBundle &meta, const dtn::data::Length &space) throw ()
94  {
95  // increment the storage size
96  _bundle_lengths[meta] = space;
97 
98  // add it to the bundle list
99  _list.add(meta);
100 
101  // add bundle to priority list
102  _priority_index.insert(meta);
103  }
104 
105  void MetaStorage::remove(const dtn::data::MetaBundle &meta) throw ()
106  {
107  _bundle_lengths.erase(meta);
108  _removal_set.erase(meta);
109 
110  const dtn::data::MetaBundle mcopy = meta;
111  _list.remove(mcopy);
112  _priority_index.erase(mcopy);
113  }
114 
116  {
117  if (has(meta)) {
118  _removal_set.insert(meta);
119  }
120  }
121 
122  bool MetaStorage::isRemoved(const dtn::data::MetaBundle &meta) const throw ()
123  {
124  return (_removal_set.find(meta) != _removal_set.end());
125  }
126 
127  bool MetaStorage::empty() throw ()
128  {
129  if ( _priority_index.empty() )
130  {
131  return true;
132  }
133 
134  return size() == 0;
135  }
136 
138  {
139  return _priority_index.size() - _removal_set.size();
140  }
141 
142  void MetaStorage::clear() throw ()
143  {
144  _priority_index.clear();
145  _list.clear();
146  _bundle_lengths.clear();
147  _removal_set.clear();
148  }
149 
151  {
152  size_map::const_iterator it = _bundle_lengths.find(meta);
153  if (it == _bundle_lengths.end())
154  throw NoBundleFoundException();
155 
156  return (*it).second;
157  }
158  } /* namespace storage */
159 } /* namespace dtn */