IBR-DTNSuite  0.12
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  : _list(expire_listener)
30  {
31  }
32 
34  {
35  }
36 
37  bool MetaStorage::contains(const dtn::data::BundleID &id) const throw ()
38  {
39  return (_bundle_lengths.find(id) != _bundle_lengths.end());
40  }
41 
42  void MetaStorage::expire(const dtn::data::Timestamp &timestamp) throw ()
43  {
44  _list.expire(timestamp);
45  }
46 
48  {
49  for (const_iterator iter = begin(); iter != end(); ++iter)
50  {
51  const dtn::data::MetaBundle &bundle = (*iter);
52 
53  // skip removal-marked bundles
54  if (_removal_set.find(bundle) != _removal_set.end()) continue;
55 
56  if (bundle.isIn(filter))
57  {
58  return bundle;
59  }
60  }
61 
62  throw NoBundleFoundException();
63  }
64 
65  std::set<dtn::data::EID> MetaStorage::getDistinctDestinations() const throw ()
66  {
67  std::set<dtn::data::EID> ret;
68 
69  for (dtn::data::BundleList::const_iterator iter = begin(); iter != end(); ++iter)
70  {
71  const dtn::data::MetaBundle &bundle = (*iter);
72 
73  // skip removal-marked bundles
74  if (_removal_set.find(bundle) != _removal_set.end()) continue;
75 
76  ret.insert(bundle.destination);
77  }
78 
79  return ret;
80  }
81 
82  void MetaStorage::store(const dtn::data::MetaBundle &meta, const dtn::data::Length &space) throw ()
83  {
84  // increment the storage size
85  _bundle_lengths[meta] = space;
86 
87  // add it to the bundle list
88  _list.add(meta);
89 
90  // add bundle to priority list
91  _priority_index.insert(meta);
92  }
93 
95  {
96  // get length of the stored bundle
97  size_map::iterator it = _bundle_lengths.find(meta);
98 
99  // nothing to remove
100  if (it == _bundle_lengths.end()) return 0;
101 
102  // store number of bytes for return
103  dtn::data::Length ret = (*it).second;
104 
105  // remove length entry
106  _bundle_lengths.erase(it);
107 
108  // remove the bundle from removal set
109  _removal_set.erase(meta);
110 
111  // remove the bundle from BundleList
112  _list.remove(meta);
113 
114  // remove bundle from priority index
115  _priority_index.erase(meta);
116 
117  // return released number of bytes
118  return ret;
119  }
120 
122  {
123  if (contains(meta)) {
124  _removal_set.insert(meta);
125  }
126  }
127 
128  bool MetaStorage::isRemoved(const dtn::data::MetaBundle &meta) const throw ()
129  {
130  return (_removal_set.find(meta) != _removal_set.end());
131  }
132 
133  bool MetaStorage::empty() throw ()
134  {
135  if ( _priority_index.empty() )
136  {
137  return true;
138  }
139 
140  return size() == 0;
141  }
142 
144  {
145  return _priority_index.size() - _removal_set.size();
146  }
147 
148  void MetaStorage::clear() throw ()
149  {
150  _priority_index.clear();
151  _list.clear();
152  _bundle_lengths.clear();
153  _removal_set.clear();
154  }
155  } /* namespace storage */
156 } /* namespace dtn */