IBR-DTNSuite  0.10
SQLiteDatabase.h
Go to the documentation of this file.
1 /*
2  * SQLiteDatabase.h
3  *
4  * Copyright (C) 2011 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 #ifndef SQLITEDATABASE_H_
23 #define SQLITEDATABASE_H_
24 
26 #include "storage/BundleSeeker.h"
27 #include "storage/BundleSelector.h"
28 #include <ibrdtn/data/EID.h>
29 #include <ibrdtn/data/MetaBundle.h>
30 #include <ibrcommon/data/File.h>
31 #include <map>
32 #include <set>
33 #include <list>
34 #include <sqlite3.h>
35 
36 namespace dtn
37 {
38  namespace storage
39  {
41  {
42  enum SQL_TABLES
43  {
44  SQL_TABLE_BUNDLE = 0,
45  SQL_TABLE_BLOCK = 1,
46  SQL_TABLE_ROUTING = 2,
47  SQL_TABLE_BUNDLE_ROUTING_INFO = 3,
48  SQL_TABLE_NODE_ROUTING_INFO = 4,
49  SQL_TABLE_PROPERTIES = 5,
50  SQL_TABLE_END = 6
51  };
52 
53  // enum of all possible statements
54  enum STORAGE_STMT
55  {
56  BUNDLE_GET_ITERATOR,
57  BUNDLE_GET_FILTER,
58  BUNDLE_GET_ID,
59  GET_DISTINCT_DESTINATIONS,
60 
61  EXPIRE_BUNDLES,
62  EXPIRE_BUNDLE_FILENAMES,
63  EXPIRE_BUNDLE_DELETE,
64  EXPIRE_NEXT_TIMESTAMP,
65 
66  EMPTY_CHECK,
67  COUNT_ENTRIES,
68 
69  BUNDLE_DELETE,
70  BUNDLE_CLEAR,
71  BUNDLE_STORE,
72  BUNDLE_UPDATE_CUSTODIAN,
73 
74  PROCFLAGS_SET,
75 
76  BLOCK_GET_ID,
77  BLOCK_GET,
78  BLOCK_CLEAR,
79  BLOCK_STORE,
80 
81  VACUUM,
82  SQL_QUERIES_END
83  };
84 
85  static const int DBSCHEMA_FRESH_VERSION;
86  static const int DBSCHEMA_VERSION;
87  static const std::string QUERY_SCHEMAVERSION;
88  static const std::string SET_SCHEMAVERSION;
89 
90  static const std::string _select_names[2];
91 
92  static const std::string _tables[SQL_TABLE_END];
93 
94  // array of sql queries
95  static const std::string _sql_queries[SQL_QUERIES_END];
96 
97  // array of the db structure as sql
98  static const std::string _db_structure[11];
99 
100  static const std::string TAG;
101 
102  public:
104  {
106  };
107 
109  {
110  public:
111  virtual ~DatabaseListener() = 0;
112  virtual void eventBundleExpired(const dtn::data::BundleID&) throw () = 0;
113  virtual void iterateDatabase(const dtn::data::MetaBundle&) = 0;
114  };
115 
117  {
118  public:
119  SQLBundleQuery();
120  virtual ~SQLBundleQuery() = 0;
121 
126  virtual const std::string getWhere() const throw () = 0;
127 
134  virtual int bind(sqlite3_stmt*, int offset) const throw ()
135  {
136  return offset;
137  }
138  };
139 
141  {
142  public:
143  SQLiteQueryException(string what = "Unable to execute Querry.") throw() : Exception(what)
144  {
145  }
146  };
147 
148  class Statement
149  {
150  public:
151  Statement(sqlite3 *database, const std::string&);
152  ~Statement();
153 
154  sqlite3_stmt* operator*();
155  void prepare() throw (SQLiteQueryException);
156  void reset() throw ();
157  int step() throw (SQLiteQueryException);
158 
159  private:
160  sqlite3 *_database;
161  sqlite3_stmt *_st;
162  const std::string _query;
163  };
164 
165  typedef std::list<std::pair<int, const ibrcommon::File> > blocklist;
166  typedef std::pair<int, const ibrcommon::File> blocklist_entry;
167 
168  SQLiteDatabase(const ibrcommon::File &file, DatabaseListener &listener);
169  virtual ~SQLiteDatabase();
170 
174  void open() throw (SQLiteQueryException);
175 
180  void close();
181 
186  void expire(const dtn::data::Timestamp &timestamp) throw ();
187 
191  void vacuum() throw (SQLiteQueryException);
192 
198  void update(UPDATE_VALUES, const dtn::data::BundleID &id, const dtn::data::EID&) throw (SQLiteQueryException);
199 
204  void remove(const dtn::data::BundleID &id) throw (SQLiteQueryException);
205 
209  virtual void get(const BundleSelector &cb, BundleResult &result) throw (NoBundleFoundException, BundleSelectorException, BundleSelectorException);
210 
216  void get(const dtn::data::BundleID &id, dtn::data::MetaBundle &meta) const throw (SQLiteQueryException, NoBundleFoundException);
217 
223  void get(const dtn::data::BundleID &id, dtn::data::Bundle &bundle, blocklist &blocks) const throw (SQLiteQueryException, NoBundleFoundException);
224 
229  void store(const dtn::data::Bundle &bundle) throw (SQLiteQueryException);
230  void store(const dtn::data::BundleID &id, int index, const dtn::data::Block &block, const ibrcommon::File &file) throw (SQLiteQueryException);
231  void transaction() throw (SQLiteQueryException);
232  void rollback() throw (SQLiteQueryException);
233  void commit() throw (SQLiteQueryException);
234 
235  bool empty() const throw (SQLiteQueryException);
236 
237  dtn::data::Size count() const throw (SQLiteQueryException);
238 
239  void clear() throw (SQLiteQueryException);
240 
244  virtual const eid_set getDistinctDestinations() throw (SQLiteQueryException);
245 
249  void iterateAll() throw (SQLiteQueryException);
250 
251  /*** BEGIN: methods for unit-testing ***/
252 
257  void setFaulty(bool mode);
258 
259  /*** END: methods for unit-testing ***/
260 
261  private:
268  void get(Statement &st, dtn::data::MetaBundle &bundle, int offset = 0) const throw (SQLiteQueryException);
269 
276  void get(Statement &st, dtn::data::Bundle &bundle, const int offset = 0) const throw (SQLiteQueryException);
277 
285  void __get(const BundleSelector &cb, Statement &st, BundleResult &ret, size_t &items_added, const int bind_offset, const size_t offset, const size_t query_limit) const throw (SQLiteQueryException, NoBundleFoundException, BundleSelectorException);
286 
290  void update_expire_time() throw (SQLiteQueryException);
291 
296  void new_expire_time(const dtn::data::Timestamp &ttl) throw ();
297  void reset_expire_time() throw ();
298  const dtn::data::Timestamp& get_expire_time() const throw ();
299 
300  void set_bundleid(Statement &st, const dtn::data::BundleID &id, int offset = 0) const throw (SQLiteQueryException);
301  void get_bundleid(Statement &st, dtn::data::BundleID &id, int offset = 0) const throw (SQLiteQueryException);
302 
306  int getVersion() throw (SQLiteQueryException);
307 
312  void setVersion(int version) throw (SQLiteQueryException);
313 
319  void doUpgrade(int oldVersion, int newVersion) throw (ibrcommon::Exception);
320 
321  ibrcommon::File _file;
322 
323  // holds the database handle
324  sqlite3 *_database;
325 
326  // next expiration
327  dtn::data::Timestamp _next_expiration;
328 
329  // listener for events on the database
330  DatabaseListener &_listener;
331 
332  bool _faulty;
333  };
334  } /* namespace storage */
335 } /* namespace dtn */
336 #endif /* SQLITEDATABASE_H_ */