IBR-DTNSuite  0.8
daemon/src/storage/SQLiteDatabase.h
Go to the documentation of this file.
00001 /*
00002  * SQLiteDatabase.h
00003  *
00004  *  Created on: 26.03.2012
00005  *      Author: morgenro
00006  */
00007 
00008 #ifndef SQLITEDATABASE_H_
00009 #define SQLITEDATABASE_H_
00010 
00011 #include "core/BundleExpiredEvent.h"
00012 #include "storage/BundleStorage.h"
00013 #include <ibrdtn/data/EID.h>
00014 #include <ibrdtn/data/MetaBundle.h>
00015 #include <ibrcommon/data/File.h>
00016 #include <map>
00017 #include <set>
00018 #include <list>
00019 #include <sqlite3.h>
00020 
00021 namespace dtn
00022 {
00023         namespace storage
00024         {
00025                 class SQLiteDatabase
00026                 {
00027                         enum SQL_TABLES
00028                         {
00029                                 SQL_TABLE_BUNDLE = 0,
00030                                 SQL_TABLE_BLOCK = 1,
00031                                 SQL_TABLE_ROUTING = 2,
00032                                 SQL_TABLE_BUNDLE_ROUTING_INFO = 3,
00033                                 SQL_TABLE_NODE_ROUTING_INFO = 4,
00034                                 SQL_TABLE_PROPERTIES = 5,
00035                                 SQL_TABLE_END = 6
00036                         };
00037 
00038                         // enum of all possible statements
00039                         enum STORAGE_STMT
00040                         {
00041                                 BUNDLE_GET_FILTER,
00042                                 BUNDLE_GET_ID,
00043                                 FRAGMENT_GET_ID,
00044                                 BUNDLE_GET_FRAGMENT,
00045 
00046                                 EXPIRE_BUNDLES,
00047                                 EXPIRE_BUNDLE_FILENAMES,
00048                                 EXPIRE_BUNDLE_DELETE,
00049                                 EXPIRE_NEXT_TIMESTAMP,
00050 
00051                                 EMPTY_CHECK,
00052                                 COUNT_ENTRIES,
00053 
00054                                 BUNDLE_DELETE,
00055                                 FRAGMENT_DELETE,
00056                                 BUNDLE_CLEAR,
00057                                 BUNDLE_STORE,
00058                                 BUNDLE_UPDATE_CUSTODIAN,
00059                                 FRAGMENT_UPDATE_CUSTODIAN,
00060 
00061                                 PROCFLAGS_SET,
00062 
00063                                 BLOCK_GET_ID,
00064                                 BLOCK_GET_ID_FRAGMENT,
00065                                 BLOCK_GET,
00066                                 BLOCK_GET_FRAGMENT,
00067                                 BLOCK_CLEAR,
00068                                 BLOCK_STORE,
00069 
00070                                 VACUUM,
00071                                 SQL_QUERIES_END
00072                         };
00073 
00074                         static const int DBSCHEMA_VERSION;
00075                         static const std::string QUERY_SCHEMAVERSION;
00076                         static const std::string SET_SCHEMAVERSION;
00077 
00078                         static const std::string _tables[SQL_TABLE_END];
00079 
00080                         // array of sql queries
00081                         static const std::string _sql_queries[SQL_QUERIES_END];
00082 
00083                         // array of the db structure as sql
00084                         static const std::string _db_structure[11];
00085 
00086                 public:
00087                         enum UPDATE_VALUES
00088                         {
00089                                 UPDATE_CUSTODIAN
00090                         };
00091 
00092                         class SQLBundleQuery
00093                         {
00094                         public:
00095                                 SQLBundleQuery();
00096                                 virtual ~SQLBundleQuery() = 0;
00097 
00102                                 virtual const std::string getWhere() const = 0;
00103 
00110                                 virtual size_t bind(sqlite3_stmt*, size_t offset) const
00111                                 {
00112                                         return offset;
00113                                 }
00114                         };
00115 
00116                         class SQLiteQueryException : public ibrcommon::Exception
00117                         {
00118                         public:
00119                                 SQLiteQueryException(string what = "Unable to execute Querry.") throw(): Exception(what)
00120                                 {
00121                                 }
00122                         };
00123 
00124                         class Statement
00125                         {
00126                         public:
00127                                 Statement(sqlite3 *database, const std::string&);
00128                                 ~Statement();
00129 
00130                                 sqlite3_stmt* operator*();
00131                                 void prepare();
00132                                 void reset();
00133                                 int step();
00134 
00135                         private:
00136                                 sqlite3 *_database;
00137                                 sqlite3_stmt *_st;
00138                                 const std::string _query;
00139                         };
00140 
00141                         typedef std::list<std::pair<int, const ibrcommon::File> > blocklist;
00142                         typedef std::pair<int, const ibrcommon::File> blocklist_entry;
00143 
00144                         SQLiteDatabase(const ibrcommon::File &file, const size_t &size);
00145                         virtual ~SQLiteDatabase();
00146 
00150                         void open();
00151 
00156                         void close();
00157 
00162                         void expire(size_t timestamp);
00163 
00167                         void vacuum();
00168 
00174                         void update(UPDATE_VALUES, const dtn::data::BundleID &id, const dtn::data::EID&);
00175 
00180                         void remove(const dtn::data::BundleID &id);
00181 
00187                         const std::list<dtn::data::MetaBundle> get(dtn::storage::BundleStorage::BundleFilterCallback &cb) const;
00188 
00194                         void get(const dtn::data::BundleID &id, dtn::data::MetaBundle &meta) const;
00195 
00201                         void get(const dtn::data::BundleID &id, dtn::data::Bundle &bundle, blocklist &blocks) const;
00202 
00207                         void store(const dtn::data::Bundle &bundle);
00208                         void store(const dtn::data::BundleID &id, int index, const dtn::data::Block &block, const ibrcommon::File &file);
00209                         void transaction();
00210                         void rollback();
00211                         void commit();
00212 
00213                         bool empty() const;
00214 
00215                         unsigned int count() const;
00216 
00217                         void clear();
00218 
00222                         virtual const std::set<dtn::data::EID> getDistinctDestinations();
00223 
00224                 private:
00231                         void get(Statement &st, dtn::data::MetaBundle &bundle, size_t offset = 0) const;
00232 
00239                         void get(Statement &st, dtn::data::Bundle &bundle, size_t offset = 0) const;
00240 
00248                         void __get(const dtn::storage::BundleStorage::BundleFilterCallback &cb, Statement &st, std::list<dtn::data::MetaBundle> &ret, size_t bind_offset, size_t offset) const;
00249 
00250 //                      /**
00251 //                       * Checks the files on the filesystem against the filenames in the database
00252 //                       */
00253 //                      void check_consistency();
00254 //
00255 //                      void check_fragments(set<string> &payloadFiles);
00256 //
00257 //                      void check_bundles(set<string> &blockFiles);
00258 
00262                         void update_expire_time();
00263 
00268                         void new_expire_time(size_t ttl);
00269                         void reset_expire_time();
00270                         size_t get_expire_time();
00271 
00272                         void set_bundleid(Statement &st, const dtn::data::BundleID &id, size_t offset = 0) const;
00273                         void get_bundleid(Statement &st, dtn::data::BundleID &id, size_t offset = 0) const;
00274 
00278                         int getVersion();
00279 
00284                         void setVersion(int version);
00285 
00291                         void doUpgrade(int oldVersion, int newVersion);
00292 
00293                         ibrcommon::File _file;
00294                         int _size;
00295 
00296                         // holds the database handle
00297                         sqlite3 *_database;
00298 
00299                         // next expiration
00300                         size_t _next_expiration;
00301 
00302                         void add_deletion(const dtn::data::BundleID &id);
00303                         void remove_deletion(const dtn::data::BundleID &id);
00304                         bool contains_deletion(const dtn::data::BundleID &id) const;
00305 
00306                         // set of bundles to delete
00307                         std::set<dtn::data::BundleID> _deletion_list;
00308                 };
00309         } /* namespace storage */
00310 } /* namespace dtn */
00311 #endif /* SQLITEDATABASE_H_ */