IBR-DTNSuite  0.12
SQLiteBundleSet.h
Go to the documentation of this file.
1 /*
2  * SQLiteBundleSet.h
3  *
4  * Copyright (C) 2013 IBR, TU Braunschweig
5  *
6  * Written-by: David Goltzsche <goltzsch@ibr.cs.tu-bs.de>
7  * Written-by: Johannes Morgenroth <morgenroth@ibr.cs.tu-bs.de>
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  * Created on: 19.04.2013
22  */
23 
24 #ifndef SQLITEBUNDLESET_H_
25 #define SQLITEBUNDLESET_H_
26 
28 #include "ibrdtn/data/BundleSet.h"
29 #include "storage/SQLiteDatabase.h"
30 #include "ibrdtn/data/MetaBundle.h"
32 #include <ibrcommon/thread/Mutex.h>
33 
34 namespace dtn
35 {
36  namespace storage
37  {
39  {
40  public:
42  public:
44  ~Factory();
45 
47  dtn::data::BundleSetImpl* create(const std::string &name, dtn::data::BundleSet::Listener* listener, dtn::data::Size bf_size);
48 
53 
57  static size_t create(SQLiteDatabase &db, const std::string &name) throw (SQLiteDatabase::SQLiteQueryException);
58  static size_t __create(SQLiteDatabase &db, const std::string &name, bool persistent) throw (SQLiteDatabase::SQLiteQueryException);
59 
60  /*
61  * returns true, if a specific bundle-set name exists
62  */
63  static bool __exists(SQLiteDatabase &db, const std::string &name, bool persistent) throw (SQLiteDatabase::SQLiteQueryException);
64 
65  private:
66  static ibrcommon::Mutex _create_lock;
67 
68  SQLiteDatabase& _sqldb;
69  };
70 
74  SQLiteBundleSet(const size_t id, bool persistant, dtn::data::BundleSet::Listener *listener, dtn::data::Size bf_size, dtn::storage::SQLiteDatabase& database);
75 
76  virtual ~SQLiteBundleSet();
77 
81  virtual refcnt_ptr<BundleSetImpl> copy() const;
82 
87  virtual void assign(const refcnt_ptr<BundleSetImpl>&);
88 
89  virtual void add(const dtn::data::MetaBundle &bundle) throw ();
90 
91  virtual void clear() throw ();
92 
93  virtual bool has(const dtn::data::BundleID &bundle) const throw ();
94 
95  virtual void expire(const dtn::data::Timestamp timestamp) throw ();
96 
100  virtual dtn::data::Size size() const throw();
101 
105  dtn::data::Length getLength() const throw();
106 
107  const ibrcommon::BloomFilter& getBloomFilter() const throw();
108 
109  std::set<dtn::data::MetaBundle> getNotIn(const ibrcommon::BloomFilter &filter) const throw();
110 
111  virtual std::ostream &serialize(std::ostream &stream) const;
112  virtual std::istream &deserialize(std::istream &stream);
113 
114  private:
115  void get_bundleid(SQLiteDatabase::Statement &st, dtn::data::BundleID &id, int offset = 0) const throw (SQLiteDatabase::SQLiteQueryException);
116 
117  // remove this bundle-set from the database
118  void destroy();
119 
120  const size_t _set_id;
121 
122  // The initial size of the bloom-filter
123  dtn::data::Length _bf_size;
124 
125  // The bloom-filter with all the bundles
126  ibrcommon::BloomFilter _bf;
127 
128  // The assigned listener
129  dtn::data::BundleSet::Listener *_listener;
130 
131  // Mark the bloom-filter and set of bundles as consistent
132  bool _consistent;
133 
134  dtn::storage::SQLiteDatabase& _sqldb;
135 
136  dtn::data::Timestamp _next_expiration;
137 
138  void new_expire_time(const dtn::data::Timestamp &ttl) throw();
139 
140  void rebuild_bloom_filter();
141 
142  const bool _persistent;
143  };
144  } /* namespace data */
145 } /* namespace dtn */
146 #endif /* MEMORYBUNDLESET_H_ */