io.hpp

Go to the documentation of this file.
00001 /*************************************************************************
00002  * Copyright (C) 2011 by Eric Ford and the Swarm-NG Development Team     *
00003  *                                                                       *
00004  * This program is free software; you can redistribute it and/or modify  *
00005  * it under the terms of the GNU General Public License as published by  *
00006  * the Free Software Foundation; either version 3 of the License.        *
00007  *                                                                       *
00008  * This program is distributed in the hope that it will be useful,       *
00009  * but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00010  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00011  * GNU General Public License for more details.                          *
00012  *                                                                       *
00013  * You should have received a copy of the GNU General Public License     *
00014  * along with this program; if not, write to the                         *
00015  * Free Software Foundation, Inc.,                                       *
00016  * 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00017  ************************************************************************/
00018 
00024 #ifndef swarmio_h__
00025 #define swarmio_h__
00026 
00027 #include "../peyton/binarystream.hpp"
00028 #include "../peyton/memorymap.hpp"
00029 
00030 #include "fileformat.hpp"
00031 #include "log.hpp"
00032 
00033 namespace swarm { 
00059         namespace query {
00060 
00061         extern const char* UNSORTED_HEADER_FULL;
00062         extern const char* UNSORTED_HEADER_CHECK;
00063         extern const char* SORTED_HEADER_FULL;
00064         extern const char* SORTED_HEADER_CHECK;
00065 
00066         extern struct range_special { } ALL;
00067         extern struct range_MAX
00068         {
00069                 template<typename T> operator T() const
00070                 {
00071                         return std::numeric_limits<T>::max();
00072                 };
00073         } MAX;
00074         extern struct range_MIN
00075         {
00076                 template<typename T> operator T() const
00077                 {
00078                         return std::numeric_limits<T>::is_integer ? std::numeric_limits<T>::min() : -std::numeric_limits<T>::max();
00079                 };
00080         } MIN;
00081 
00083         template<typename T>
00084         struct range
00085         {
00086                 T first, last;
00087 
00088                 range(const T &a) : first(a), last(a) {}
00089                 range(const T &a, const T &b) : first(a), last(b) {}
00090                 range(const range_special &r = ALL) : first(MIN), last(MAX) {}
00091 
00092                 bool in(const T& v) { return first <= v && v <= last; }
00093                 operator bool() const { return first <= last; }
00094         };
00095 
00096         typedef range<int> sys_range_t;
00097         typedef range<int> body_range_t;
00098         typedef range<double> time_range_t;
00099 
00100         typedef mmapped_file_with_header<swarm_header> mmapped_swarm_file;
00101         typedef mmapped_file_with_header<swarm_index_header> mmapped_swarm_index_file;
00103         struct index_creator_base
00104         {
00105                 virtual bool start(const std::string &datafile) = 0;
00106                 virtual bool add_entry(uint64_t offs, gpulog::logrecord lr) = 0;
00107                 virtual bool finish() = 0;
00108                 virtual ~index_creator_base() {};
00109         };
00110 
00112         class swarmdb
00113         {
00115         public:
00116                 struct index_entry
00117                 {
00118                         uint64_t offs;  
00119         
00120                         double T;       
00121                         int sys;        
00122                         int body;       
00123                 };
00124 
00125         protected:
00126                 struct index_handle
00127                 {
00128                         mmapped_swarm_index_file mm;
00129                         const index_entry *begin, *end;
00130                 };
00131 
00132                 mmapped_swarm_file mmdata;
00133 
00134                 index_handle idx_time, idx_sys;
00135                 std::string datafile;
00136 
00137                 void open(const std::string &datafile);
00138                 void open_indexes(bool force_recreate = false);
00139                 bool open_index(index_handle &h, const std::string &datafile, const std::string &suffix, const std::string &filetype);
00140 
00142         public:
00143                 struct result
00144                 {
00145                         const swarmdb &db;
00146 
00147                         sys_range_t  sys;
00148                   //                    body_range_t  body;
00149                         time_range_t T;
00150                         
00151                         const index_entry *begin, *end, *at, *atprev;
00152 
00153                   result(const swarmdb &db_, const sys_range_t &sys, const time_range_t &T);
00154                   //              result(const swarmdb &db_, const sys_range_t &sys, const body_range_t &body, const time_range_t &T);
00155 
00156                         gpulog::logrecord next();
00157                         void unget();
00158                 };
00159 
00161         public:
00162                 swarmdb(const std::string &datafile);
00163 
00165           result query(sys_range_t sys, time_range_t T) const
00166                 {
00167                   return result(*this, sys, T);
00168                 }
00169 
00170           /*
00171           result query(sys_range_t sys, body_range_t body, time_range_t T) const
00172                 {
00173                   return result(*this, sys, body, T);
00174                 }
00175           */
00177         public:
00178                 struct snapshots
00179                 {
00180                         const swarmdb &db;
00181                         result r;
00182 
00183                         double Tabserr, Trelerr;
00184 
00185                         bool next(cpu_ensemble &ens/*, bool keep_existing = true*/);
00186                         snapshots(const swarmdb &db, time_range_t T, double Tabserr = 0, double Trelerr = 0);
00187                 };
00189                 snapshots get_snapshots(time_range_t T, double Tabserr = 0, double Trelerr = 0)
00190                 {
00191                         return snapshots(*this, T, Tabserr, Trelerr);
00192                 }
00193         private:
00194                 void index_binary_log_file(std::vector<boost::shared_ptr<index_creator_base> > &ic, const std::string &datafile);
00195         };
00196 
00197         bool sort_binary_log_file(const std::string &outfn, const std::string &infn);
00198 
00199 } } // end namespace query:: swarm
00200 
00201 #endif

doxygen