Swarm-NG  1.1
io.hpp
Go to the documentation of this file.
1 /*************************************************************************
2  * Copyright (C) 2011 by Eric Ford and the Swarm-NG Development Team *
3  * *
4  * This program is free software; you can redistribute it and/or modify *
5  * it under the terms of the GNU General Public License as published by *
6  * the Free Software Foundation; either version 3 of the License. *
7  * *
8  * This program is distributed in the hope that it will be useful, *
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
11  * GNU General Public License for more details. *
12  * *
13  * You should have received a copy of the GNU General Public License *
14  * along with this program; if not, write to the *
15  * Free Software Foundation, Inc., *
16  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
17  ************************************************************************/
18 
24 #ifndef swarmio_h__
25 #define swarmio_h__
26 
27 #include "../peyton/binarystream.hpp"
28 #include "../peyton/memorymap.hpp"
29 #include "../ensemble_alloc.hpp"
30 
31 #include "fileformat.hpp"
32 #include "log.hpp"
33 
34 namespace swarm {
60  namespace query {
61 
62  extern const char* UNSORTED_HEADER_FULL;
63  extern const char* UNSORTED_HEADER_CHECK;
64  extern const char* SORTED_HEADER_FULL;
65  extern const char* SORTED_HEADER_CHECK;
66 
67  extern struct range_special { } ALL;
68  extern struct range_MAX
69  {
70  template<typename T> operator T() const
71  {
72  return std::numeric_limits<T>::max();
73  };
74  } MAX;
75  extern struct range_MIN
76  {
77  template<typename T> operator T() const
78  {
79  return std::numeric_limits<T>::is_integer ? std::numeric_limits<T>::min() : -std::numeric_limits<T>::max();
80  };
81  } MIN;
82 
84  template<typename T>
85  struct range
86  {
87  T first, last;
88 
89  range(const T &a) : first(a), last(a) {}
90  range(const T &a, const T &b) : first(a), last(b) {}
91  range(const range_special &r = ALL) : first(MIN), last(MAX) {}
92 
93  bool in(const T& v) { return first <= v && v <= last; }
94  operator bool() const { return first <= last; }
95  };
96 
97  typedef range<int> sys_range_t;
98  typedef range<int> body_range_t;
100 
104  struct index_creator_base
105  {
106  virtual bool start(const std::string &datafile) = 0;
107  virtual bool add_entry(uint64_t offs, gpulog::logrecord lr) = 0;
108  virtual bool finish() = 0;
109  virtual ~index_creator_base() {};
110  };
111 
113  class swarmdb
114  {
116  public:
117  struct index_entry
118  {
119  uint64_t offs;
120 
121  double T;
122  int sys;
123  int body;
124  };
125 
126  protected:
127  struct index_handle
128  {
130  const index_entry *begin, *end;
131  };
132 
133  mmapped_swarm_file mmdata;
134 
135  index_handle idx_time, idx_sys;
136  std::string datafile;
137 
138  void open(const std::string &datafile);
139  void open_indexes(bool force_recreate = false);
140  bool open_index(index_handle &h, const std::string &datafile, const std::string &suffix, const std::string &filetype);
141 
143  public:
144  struct result
145  {
146  const swarmdb &db;
147 
148  sys_range_t sys;
149  // body_range_t body;
150  time_range_t T;
151 
152  const index_entry *begin, *end, *at, *atprev;
153 
154  result(const swarmdb &db_, const sys_range_t &sys, const time_range_t &T);
155  // result(const swarmdb &db_, const sys_range_t &sys, const body_range_t &body, const time_range_t &T);
156 
157  gpulog::logrecord next();
158  void unget();
159  };
160 
162  public:
163  swarmdb(const std::string &datafile);
164 
167  {
168  return result(*this, sys, T);
169  }
170 
171  /*
172  result query(sys_range_t sys, body_range_t body, time_range_t T) const
173  {
174  return result(*this, sys, body, T);
175  }
176  */
178  public:
179  struct snapshots
180  {
181  const swarmdb &db;
182  result r;
183 
184  double Tabserr, Trelerr;
185 
186  bool next(cpu_ensemble &ens/*, bool keep_existing = true*/);
187  snapshots(const swarmdb &db, time_range_t T, double Tabserr = 0, double Trelerr = 0);
188  };
190  snapshots get_snapshots(time_range_t T, double Tabserr = 0, double Trelerr = 0)
191  {
192  return snapshots(*this, T, Tabserr, Trelerr);
193  }
194  private:
195  void index_binary_log_file(std::vector<boost::shared_ptr<index_creator_base> > &ic, const std::string &datafile);
196  };
197 
198  bool sort_binary_log_file(const std::string &outfn, const std::string &infn);
199 
200 } } // end namespace query:: swarm
201 
202 #endif