IBR-DTN  1.0.0
SQLiteBundleStorage.h
Go to the documentation of this file.
1 /*
2  * SQLiteBundleStorage.h
3  *
4  * Copyright (C) 2011 IBR, TU Braunschweig
5  *
6  * Written-by: Johannes Morgenroth <morgenroth@ibr.cs.tu-bs.de>
7  * Written-by: Matthias Myrtus
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  */
22 
23 
24 #ifndef SQLITEBUNDLESTORAGE_H_
25 #define SQLITEBUNDLESTORAGE_H_
26 
27 #include "storage/BundleStorage.h"
28 #include "storage/SQLiteDatabase.h"
30 
31 #include "Component.h"
32 #include "core/EventReceiver.h"
33 #include "core/TimeEvent.h"
34 #include "core/GlobalEvent.h"
35 #include <ibrdtn/data/MetaBundle.h>
36 
37 #include <ibrcommon/thread/Thread.h>
38 #include <ibrcommon/thread/RWMutex.h>
39 #include <ibrcommon/thread/Conditional.h>
40 #include <ibrcommon/data/File.h>
41 #include <ibrcommon/thread/Queue.h>
42 
43 #include <string>
44 #include <list>
45 #include <set>
46 
47 namespace dtn
48 {
49  namespace storage
50  {
51  class SQLiteBundleStorage: public BundleStorage, public dtn::core::EventReceiver<dtn::core::GlobalEvent>, public dtn::core::EventReceiver<dtn::core::TimeEvent>, public dtn::daemon::IndependentComponent, public ibrcommon::BLOB::Provider, public SQLiteDatabase::DatabaseListener
52  {
53  static const std::string TAG;
54 
55  public:
60  ibrcommon::BLOB::Reference create();
61 
69  SQLiteBundleStorage(const ibrcommon::File &path, const dtn::data::Length &maxsize, bool usePersistentBundleSets = false);
70 
74  virtual ~SQLiteBundleStorage();
75 
80  void store(const dtn::data::Bundle &bundle);
81 
86  bool contains(const dtn::data::BundleID &id);
87 
92 
100 
104  virtual void get(const BundleSelector &cb, BundleResult &result) throw (NoBundleFoundException, BundleSelectorException);
105 
109  virtual const eid_set getDistinctDestinations();
110 
116  void remove(const dtn::data::BundleID &id);
117 
121  void clear();
122 
126  bool empty();
127 
132 
136  void releaseCustody(const dtn::data::EID &custodian, const dtn::data::BundleID &id);
137 
142  void raiseEvent(const dtn::core::TimeEvent &evt) throw ();
143  void raiseEvent(const dtn::core::GlobalEvent &evt) throw ();
144 
148  void eventBundleExpired(const dtn::data::BundleID &id, const dtn::data::Length size) throw ();
149  void iterateDatabase(const dtn::data::MetaBundle &bundle, const dtn::data::Length size);
150 
151 
152  /*** BEGIN: methods for unit-testing ***/
153 
157  virtual void wait();
158 
163  virtual void setFaulty(bool mode);
164 
165  /*** END: methods for unit-testing ***/
166 
167 
168  protected:
169  virtual void componentRun() throw ();
170  virtual void componentUp() throw ();
171  virtual void componentDown() throw ();
172  void __cancellation() throw ();
173 
174  private:
175 // enum Position
176 // {
177 // FIRST_FRAGMENT = 0,
178 // LAST_FRAGMENT = 1,
179 // BOTH_FRAGMENTS = 2
180 // };
181 
182  class Task
183  {
184  public:
185  virtual ~Task() {};
186  virtual void run(SQLiteBundleStorage &storage) = 0;
187  };
188 
189  class BlockingTask : public Task
190  {
191  public:
192  BlockingTask() : _done(false), _abort(false) {};
193  virtual ~BlockingTask() {};
194  virtual void run(SQLiteBundleStorage &storage) = 0;
195 
199  void wait()
200  {
201  ibrcommon::MutexLock l(_cond);
202  while (!_done && !_abort) _cond.wait();
203 
204  if (_abort) throw ibrcommon::Exception("Task aborted");
205  }
206 
207  void abort()
208  {
209  ibrcommon::MutexLock l(_cond);
210  _abort = true;
211  _cond.signal(true);
212  }
213 
214  void done()
215  {
216  ibrcommon::MutexLock l(_cond);
217  _done = true;
218  _cond.signal(true);
219  }
220 
221  private:
222  ibrcommon::Conditional _cond;
223  bool _done;
224  bool _abort;
225  };
226 
227  class TaskIdle : public Task
228  {
229  public:
230  TaskIdle() { };
231 
232  virtual ~TaskIdle() {};
233  virtual void run(SQLiteBundleStorage &storage);
234 
235  static ibrcommon::Mutex _mutex;
236  static bool _idle;
237  };
238 
239  class TaskExpire : public Task
240  {
241  public:
242  TaskExpire(const dtn::data::Timestamp &timestamp)
243  : _timestamp(timestamp) { };
244 
245  virtual ~TaskExpire() {};
246  virtual void run(SQLiteBundleStorage &storage);
247 
248  private:
249  const dtn::data::Timestamp _timestamp;
250  };
251 
256  class SQLiteBLOB : public ibrcommon::BLOB
257  {
258  friend class SQLiteBundleStorage;
259  public:
260  virtual ~SQLiteBLOB();
261 
262  virtual void clear();
263 
264  virtual void open();
265  virtual void close();
266 
267  protected:
268  std::iostream &__get_stream()
269  {
270  return _filestream;
271  }
272 
273  std::streamsize __get_size();
274 
275  private:
276  SQLiteBLOB(const ibrcommon::File &path);
277  std::fstream _filestream;
278  ibrcommon::TemporaryFile _file;
279  };
280 
281 
282 // /**
283 // * This Funktion gets e list and a bundle. Every block of the bundle except the PrimaryBlock is saved in a File.
284 // * The filenames of the blocks are stored in the List. The order of the filenames matches the order of the blocks.
285 // * @param An empty Stringlist
286 // * @param A Bundle which should be prepared to be Stored.
287 // * @return A number bigges than zero is returned indicating an error. Zero is returned if no error occurred.
288 // */
289 // int prepareBundle(list<std::string> &filenames, dtn::data::Bundle &bundle);
290 
294  virtual const std::string getName() const;
295 
296  SQLiteDatabase _database;
297 
298  ibrcommon::File _blobPath;
299  ibrcommon::File _blockPath;
300 
301  // contains all jobs to do
302  ibrcommon::Queue<Task*> _tasks;
303 
304  ibrcommon::RWMutex _global_lock;
305  };
306  }
307 }
308 
309 #endif /* SQLITEBUNDLESTORAGE_H_ */
void releaseCustody(const dtn::data::EID &custodian, const dtn::data::BundleID &id)
std::set< dtn::data::EID > eid_set
Definition: BundleSeeker.h:39
dtn::data::Length size() const
void raiseEvent(const dtn::core::TimeEvent &evt)
size_t Length
Definition: Number.h:33
SQLiteBundleStorage(const ibrcommon::File &path, const dtn::data::Length &maxsize, bool usePersistentBundleSets=false)
virtual const eid_set getDistinctDestinations()
void iterateDatabase(const dtn::data::MetaBundle &bundle, const dtn::data::Length size)
void eventBundleExpired(const dtn::data::BundleID &id, const dtn::data::Length size)
size_t Size
Definition: Number.h:34
bool contains(const dtn::data::BundleID &id)
virtual dtn::data::MetaBundle info(const dtn::data::BundleID &id)
ibrcommon::BLOB::Reference create()
void store(const dtn::data::Bundle &bundle)