LArSoft  v06_85_00
Liquid Argon Software toolkit - http://larsoft.org/
lariov::DBFolder Class Reference

#include "DBFolder.h"

Public Member Functions

 DBFolder (const std::string &name, const std::string &url, const std::string &tag="")
 
virtual ~DBFolder ()
 
int GetNamedChannelData (DBChannelID_t channel, const std::string &name, bool &data)
 
int GetNamedChannelData (DBChannelID_t channel, const std::string &name, long &data)
 
int GetNamedChannelData (DBChannelID_t channel, const std::string &name, double &data)
 
int GetNamedChannelData (DBChannelID_t channel, const std::string &name, std::string &data)
 
int GetNamedChannelData (DBChannelID_t channel, const std::string &name, std::vector< double > &data)
 
const std::string & URL () const
 
const std::string & FolderName () const
 
const std::string & Tag () const
 
const IOVTimeStamp & CachedStart () const
 
const IOVTimeStamp & CachedEnd () const
 
bool UpdateData (DBTimeStamp_t raw_time)
 
int GetChannelList (std::vector< DBChannelID_t > &channels) const
 

Private Member Functions

size_t GetTupleColumn (DBChannelID_t channel, const std::string &name, Tuple &tup)
 
bool IsValid (const IOVTimeStamp &time) const
 

Private Attributes

std::string fURL
 
std::string fFolderName
 
std::string fTag
 
int fMaximumTimeout
 
Dataset fCachedDataset
 
int fNRows
 
IOVTimeStamp fCachedStart
 
IOVTimeStamp fCachedEnd
 
std::vector< std::string > fColumns
 
std::vector< std::string > fTypes
 
int fCachedRow
 
DBChannelID_t fCachedChannel
 

Detailed Description

Definition at line 15 of file DBFolder.h.

Constructor & Destructor Documentation

lariov::DBFolder::DBFolder ( const std::string &  name,
const std::string &  url,
const std::string &  tag = "" 
)

Definition at line 25 of file DBFolder.cxx.

References fCachedChannel, fCachedDataset, fCachedRow, fColumns, fFolderName, fMaximumTimeout, fNRows, fTag, fTypes, and fURL.

25  :
26  fCachedStart(0,0), fCachedEnd(0,0) {
27 
28  fFolderName = name;
29  fURL = url;
30  fTag = tag;
31  if (fURL[fURL.length()-1] == '/') {
32  fURL = fURL.substr(0, fURL.length()-1);
33  }
34 
35  fCachedDataset = 0;
36  fNRows =0;
37  fColumns.clear();
38  fTypes.clear();
39  fCachedRow = -1;
40  fCachedChannel = 0;
41 
42  fMaximumTimeout = 4*60; //4 minutes
43  }
std::string fURL
Definition: DBFolder.h:47
IOVTimeStamp fCachedStart
Definition: DBFolder.h:54
DBChannelID_t fCachedChannel
Definition: DBFolder.h:59
int fMaximumTimeout
Definition: DBFolder.h:50
std::vector< std::string > fTypes
Definition: DBFolder.h:57
std::string fFolderName
Definition: DBFolder.h:48
IOVTimeStamp fCachedEnd
Definition: DBFolder.h:55
std::string fTag
Definition: DBFolder.h:49
Dataset fCachedDataset
Definition: DBFolder.h:52
std::vector< std::string > fColumns
Definition: DBFolder.h:56
lariov::DBFolder::~DBFolder ( )
virtual

Definition at line 45 of file DBFolder.cxx.

References fCachedDataset.

45  {
46  if (fCachedDataset) releaseDataset(fCachedDataset);
47  }
Dataset fCachedDataset
Definition: DBFolder.h:52

Member Function Documentation

const IOVTimeStamp& lariov::DBFolder::CachedEnd ( ) const
inline

Definition at line 32 of file DBFolder.h.

References fCachedEnd, GetChannelList(), GetTupleColumn(), and UpdateData().

32 {return fCachedEnd;}
IOVTimeStamp fCachedEnd
Definition: DBFolder.h:55
const IOVTimeStamp& lariov::DBFolder::CachedStart ( ) const
inline

Definition at line 31 of file DBFolder.h.

References fCachedStart.

31 {return fCachedStart;}
IOVTimeStamp fCachedStart
Definition: DBFolder.h:54
const std::string& lariov::DBFolder::FolderName ( ) const
inline

Definition at line 28 of file DBFolder.h.

References fFolderName.

28 {return fFolderName;}
std::string fFolderName
Definition: DBFolder.h:48
int lariov::DBFolder::GetChannelList ( std::vector< DBChannelID_t > &  channels) const

Definition at line 156 of file DBFolder.cxx.

References fCachedDataset, fNRows, and lariov::kNUMBER_HEADER_ROWS.

Referenced by CachedEnd().

156  {
157 
158  channels.clear();
159  if (!fCachedDataset) return 1;
160 
161  Tuple tup;
162  int err=0;
163  for ( int row = 0; row != fNRows; ++row) {
164  tup = getTuple(fCachedDataset, row + kNUMBER_HEADER_ROWS);
165  channels.push_back( (DBChannelID_t)getLongValue(tup,0,&err) );
166  releaseTuple(tup);
167  }
168  return err;
169  }
const unsigned int kNUMBER_HEADER_ROWS
void * Tuple
Definition: DBFolder.h:13
Dataset fCachedDataset
Definition: DBFolder.h:52
int lariov::DBFolder::GetNamedChannelData ( DBChannelID_t  channel,
const std::string &  name,
bool &  data 
)

Definition at line 49 of file DBFolder.cxx.

References col, GetTupleColumn(), and lariov::kBUFFER_SIZE.

49  {
50 
51  Tuple tup;
52  size_t col = this->GetTupleColumn(channel, name, tup);
53  int err=0;
54  char buf[kBUFFER_SIZE];
55  int str_size = getStringValue(tup, col, buf, kBUFFER_SIZE, &err);
56  data = false;
57  if (std::string(buf, str_size)=="True") {
58  data = true;
59  }
60  else if (std::string(buf, str_size)=="False") {
61  data = false;
62  }
63  else std::cout<<"(DBFolder) ERROR: Can't identify data: "<<std::string(buf, str_size)<<" as boolean!"<<std::endl;
64 
65  releaseTuple(tup);
66  return err;
67  }
size_t GetTupleColumn(DBChannelID_t channel, const std::string &name, Tuple &tup)
Definition: DBFolder.cxx:172
void * Tuple
Definition: DBFolder.h:13
Int_t col[ntarg]
Definition: Style.C:29
const unsigned int kBUFFER_SIZE
int lariov::DBFolder::GetNamedChannelData ( DBChannelID_t  channel,
const std::string &  name,
long &  data 
)

Definition at line 69 of file DBFolder.cxx.

References col, GetTupleColumn(), and lariov::kBUFFER_SIZE.

69  {
70 
71  Tuple tup;
72  size_t col = this->GetTupleColumn(channel, name, tup);
73  int err=0;
74 
75  //first handle special case that the db data is boolean, but user mistakenly used long version of this function
76  char buf[kBUFFER_SIZE];
77  int str_size = getStringValue(tup, col, buf, kBUFFER_SIZE, &err);
78  if (std::string(buf, str_size)=="True") {
79  data = 1;
80  }
81  else if (std::string(buf, str_size)=="False") {
82  data = 0;
83  }
84  else { //ok, we really have a long (hopefully)
85  data = getLongValue(tup, col, &err);
86  }
87  releaseTuple(tup);
88  return err;
89  }
size_t GetTupleColumn(DBChannelID_t channel, const std::string &name, Tuple &tup)
Definition: DBFolder.cxx:172
void * Tuple
Definition: DBFolder.h:13
Int_t col[ntarg]
Definition: Style.C:29
const unsigned int kBUFFER_SIZE
int lariov::DBFolder::GetNamedChannelData ( DBChannelID_t  channel,
const std::string &  name,
double &  data 
)

Definition at line 91 of file DBFolder.cxx.

References col, and GetTupleColumn().

91  {
92 
93  Tuple tup;
94  size_t col = this->GetTupleColumn(channel, name, tup);
95  int err=0;
96  data = getDoubleValue(tup, col, &err);
97  releaseTuple(tup);
98  return err;
99  }
size_t GetTupleColumn(DBChannelID_t channel, const std::string &name, Tuple &tup)
Definition: DBFolder.cxx:172
void * Tuple
Definition: DBFolder.h:13
Int_t col[ntarg]
Definition: Style.C:29
int lariov::DBFolder::GetNamedChannelData ( DBChannelID_t  channel,
const std::string &  name,
std::string &  data 
)

Definition at line 101 of file DBFolder.cxx.

References col, GetTupleColumn(), and lariov::kBUFFER_SIZE.

101  {
102 
103  Tuple tup;
104  size_t col = this->GetTupleColumn(channel, name, tup);
105  int err=0;
106  char buf[kBUFFER_SIZE];
107  int str_size = getStringValue(tup, col, buf, kBUFFER_SIZE, &err);
108  data = std::string(buf, str_size);
109  releaseTuple(tup);
110  return err;
111  }
size_t GetTupleColumn(DBChannelID_t channel, const std::string &name, Tuple &tup)
Definition: DBFolder.cxx:172
void * Tuple
Definition: DBFolder.h:13
Int_t col[ntarg]
Definition: Style.C:29
const unsigned int kBUFFER_SIZE
int lariov::DBFolder::GetNamedChannelData ( DBChannelID_t  channel,
const std::string &  name,
std::vector< double > &  data 
)

Definition at line 113 of file DBFolder.cxx.

References col, lariov::DataRec::columns, GetTupleColumn(), lariov::kBUFFER_SIZE, and lariov::DataRec::ncolumns.

113  {
114 
115  data.clear();
116 
117  Tuple tup;
118  size_t col = this->GetTupleColumn(channel, name, tup);
119  int err=0;
120  double buf[kBUFFER_SIZE];
121 
122  DataRec *dataRec = (DataRec *)tup;
123  // for c2: col is an unsigned int and cannot be less than 0
124  // if (col < 0 || col >= dataRec->ncolumns) {
125  if (col >= dataRec->ncolumns) {
126  err=-1;
127  return err;
128  }
129 
130  char* sptr = dataRec->columns[col];
131  if ( *sptr == '[') sptr +=1; //expect an initial bracket and skip it
132  else {
133  err=-2;
134  return err;
135  }
136 
137  char* eptr;
138  double val;
139  unsigned int array_size=0;
140  for (unsigned int i=0; i < kBUFFER_SIZE; ++i) {
141  val = strtod(sptr, &eptr); //Try to convert
142  if (sptr==eptr) break; //conversion failed
143  if (*sptr=='\0') break; //end loop if buffer ends
144 
145  buf[array_size++] = val;
146 
147  if ( *eptr == ']') break; //found the closing bracket, we're done
148  else sptr = eptr+1; //point to the next value
149  }
150 
151  data.insert(data.begin(), buf, buf + array_size);
152  releaseTuple(tup);
153  return err;
154  }
size_t GetTupleColumn(DBChannelID_t channel, const std::string &name, Tuple &tup)
Definition: DBFolder.cxx:172
void * Tuple
Definition: DBFolder.h:13
Int_t col[ntarg]
Definition: Style.C:29
const unsigned int kBUFFER_SIZE
size_t lariov::DBFolder::GetTupleColumn ( DBChannelID_t  channel,
const std::string &  name,
Tuple tup 
)
private

Definition at line 172 of file DBFolder.cxx.

References fCachedChannel, fCachedDataset, fCachedRow, fColumns, fNRows, lariov::kNUMBER_HEADER_ROWS, and util::flags::to_string().

Referenced by CachedEnd(), and GetNamedChannelData().

172  {
173 
174  //check if cached row is still valid
175  int err;
176  int row = -1;
177  if (fCachedRow != -1 && fCachedChannel == channel) {
178  tup = getTuple(fCachedDataset, fCachedRow + kNUMBER_HEADER_ROWS);
179  if ( channel == (DBChannelID_t)getLongValue(tup,0,&err) ) {
180  row = fCachedRow;
181  }
182  else releaseTuple(tup);
183  }
184 
185  //if cached row is not valid, find the new row
186  if (row == -1) {
187 //std::cout<<"Channel "<<channel<<" not cached"<<std::endl;
188  //binary search for channel
189  DBChannelID_t val;
190  int l = 0, h = fNRows - 1;
191  row = (l + h )/2;
192  while ( l <= h ) {
193 //std::cout<<" "<<l<<" "<<h<<" "<<row<<std::endl;
194  tup = getTuple(fCachedDataset, row + kNUMBER_HEADER_ROWS);
195  val = getLongValue(tup, 0, &err);
196  releaseTuple(tup);
197 
198  if (val == channel ) break;
199 
200  if (val > channel) h = row - 1;
201  else l = row + 1;
202 
203  row = (l + h)/2;
204  }
205 
206  //get the tuple to be returned, check that the found row matches the requested channel
207  tup = getTuple(fCachedDataset, row + kNUMBER_HEADER_ROWS);
208  if ( channel != (DBChannelID_t)getLongValue(tup, 0, &err) ) {
209  releaseTuple(tup);
210  std::string msg = "Channel " + std::to_string(channel) + " is not found in database!";
211  throw WebError(msg);
212  }
213 
214 
215  //update caching info
216  fCachedChannel = channel;
217  fCachedRow = row;
218 
219  }
220 
221  //get the column corresponding to input string name and return
222  for (size_t c=1; c < fColumns.size(); ++c ) {
223  if (name == fColumns[c]) return c;
224  }
225 
226  std::string msg = "Column named " + name + " is not found in the database!";
227  throw WebError(msg);
228  return 0;
229  }
const unsigned int kNUMBER_HEADER_ROWS
DBChannelID_t fCachedChannel
Definition: DBFolder.h:59
Dataset fCachedDataset
Definition: DBFolder.h:52
std::vector< std::string > fColumns
Definition: DBFolder.h:56
std::string to_string(Flag_t< Storage > const flag)
Convert a flag into a stream (shows its index).
Definition: BitMask.h:187
bool lariov::DBFolder::IsValid ( const IOVTimeStamp &  time) const
inlineprivate

Definition at line 41 of file DBFolder.h.

References fCachedEnd, and fCachedStart.

Referenced by UpdateData().

41  {
42  if (time >= fCachedStart && time < fCachedEnd) return true;
43  else return false;
44  }
IOVTimeStamp fCachedStart
Definition: DBFolder.h:54
IOVTimeStamp fCachedEnd
Definition: DBFolder.h:55
const std::string& lariov::DBFolder::Tag ( ) const
inline

Definition at line 29 of file DBFolder.h.

References fTag.

29 {return fTag;}
std::string fTag
Definition: DBFolder.h:49
bool lariov::DBFolder::UpdateData ( DBTimeStamp_t  raw_time)

Definition at line 232 of file DBFolder.cxx.

References fCachedDataset, fCachedEnd, fCachedStart, fColumns, fFolderName, fMaximumTimeout, fNRows, fTag, fTypes, fURL, IsValid(), lariov::kBUFFER_SIZE, lariov::kNUMBER_HEADER_ROWS, and util::flags::to_string().

Referenced by CachedEnd().

232  {
233 
234  //convert to IOVTimeStamp
235  IOVTimeStamp ts = TimeStampDecoder::DecodeTimeStamp(raw_time);
236 
237  //check if cache is updated
238  if (this->IsValid(ts)) return false;
239 
240  int err=0;
241 
242  //release old dataset
243  if (fCachedDataset) releaseDataset(fCachedDataset);
244 
245  //get full url string
246  std::stringstream fullurl;
247  fullurl << fURL << "/data?f=" << fFolderName
248  << "&t=" << ts.DBStamp();
249  if (fTag.length() > 0) fullurl << "&tag=" << fTag;
250 
251  //get new dataset
252  int status = -1;
253  fCachedDataset = getDataWithTimeout(fullurl.str().c_str(), NULL, fMaximumTimeout, &err);
254  status = getHTTPstatus(fCachedDataset);
255 
256  //Can add some more queries here if we get http error 504
257  /*if (status == 504) {
258  //try again
259  }*/
260 
261  if (status != 200) {
262  std::string msg = "HTTP error from " + fullurl.str()+": status: " + std::to_string(status) + ": " + std::string(getHTTPmessage(fCachedDataset));
263  throw WebError(msg);
264  }
265 
266  //update info about cached data
267  fNRows = getNtuples(fCachedDataset) - kNUMBER_HEADER_ROWS;
268  //std::cout<<"Retrieved "<<fNRows<<" rows from "<<fullurl.str()<<std::endl;
269  if (fNRows < 1) {
270  std::stringstream msg;
271  msg << "Time " << ts.DBStamp() << ": Data not found in database.";
272  throw WebError(msg.str());
273  fCachedStart = fCachedEnd = ts;
274  }
275 
276  //start and end times
277  Tuple tup;
278  tup = getTuple(fCachedDataset, 0);
279  char buf[kBUFFER_SIZE];
280  getStringValue(tup,0, buf, kBUFFER_SIZE, &err);
281  fCachedStart = IOVTimeStamp::GetFromString(std::string(buf));
282  releaseTuple(tup);
283 
284  tup = getTuple(fCachedDataset, 1);
285  getStringValue(tup,0, buf, kBUFFER_SIZE, &err);
286  if ( 0 == strcmp(buf,"-") ) {
287  fCachedEnd = IOVTimeStamp::MaxTimeStamp();
288  }
289  else {
290  fCachedEnd = IOVTimeStamp::GetFromString(std::string(buf));
291  }
292  releaseTuple(tup);
293 
294  //column names
295  tup = getTuple(fCachedDataset, 2);
296  fColumns.clear();
297  for (int c=0; c < getNfields(tup); ++c) {
298  getStringValue(tup, c, buf, kBUFFER_SIZE, &err);
299  fColumns.push_back(buf);
300  }
301  releaseTuple(tup);
302 
303  //column types
304  tup = getTuple(fCachedDataset, 3);
305  fTypes.clear();
306  for (int c=0; c < getNfields(tup); ++c) {
307  getStringValue(tup, c, buf, kBUFFER_SIZE, &err);
308  fTypes.push_back(buf);
309  }
310  releaseTuple(tup);
311 
312  return true;
313  }
const unsigned int kNUMBER_HEADER_ROWS
std::string fURL
Definition: DBFolder.h:47
IOVTimeStamp fCachedStart
Definition: DBFolder.h:54
void * Tuple
Definition: DBFolder.h:13
int fMaximumTimeout
Definition: DBFolder.h:50
bool IsValid(const IOVTimeStamp &time) const
Definition: DBFolder.h:41
std::vector< std::string > fTypes
Definition: DBFolder.h:57
std::string fFolderName
Definition: DBFolder.h:48
const unsigned int kBUFFER_SIZE
IOVTimeStamp fCachedEnd
Definition: DBFolder.h:55
std::string fTag
Definition: DBFolder.h:49
Dataset fCachedDataset
Definition: DBFolder.h:52
std::vector< std::string > fColumns
Definition: DBFolder.h:56
std::string to_string(Flag_t< Storage > const flag)
Convert a flag into a stream (shows its index).
Definition: BitMask.h:187
const std::string& lariov::DBFolder::URL ( ) const
inline

Definition at line 27 of file DBFolder.h.

References fURL.

27 {return fURL;}
std::string fURL
Definition: DBFolder.h:47

Member Data Documentation

DBChannelID_t lariov::DBFolder::fCachedChannel
private

Definition at line 59 of file DBFolder.h.

Referenced by DBFolder(), and GetTupleColumn().

Dataset lariov::DBFolder::fCachedDataset
private

Definition at line 52 of file DBFolder.h.

Referenced by DBFolder(), GetChannelList(), GetTupleColumn(), UpdateData(), and ~DBFolder().

IOVTimeStamp lariov::DBFolder::fCachedEnd
private

Definition at line 55 of file DBFolder.h.

Referenced by CachedEnd(), IsValid(), and UpdateData().

int lariov::DBFolder::fCachedRow
private

Definition at line 58 of file DBFolder.h.

Referenced by DBFolder(), and GetTupleColumn().

IOVTimeStamp lariov::DBFolder::fCachedStart
private

Definition at line 54 of file DBFolder.h.

Referenced by CachedStart(), IsValid(), and UpdateData().

std::vector<std::string> lariov::DBFolder::fColumns
private

Definition at line 56 of file DBFolder.h.

Referenced by DBFolder(), GetTupleColumn(), and UpdateData().

std::string lariov::DBFolder::fFolderName
private

Definition at line 48 of file DBFolder.h.

Referenced by DBFolder(), FolderName(), and UpdateData().

int lariov::DBFolder::fMaximumTimeout
private

Definition at line 50 of file DBFolder.h.

Referenced by DBFolder(), and UpdateData().

int lariov::DBFolder::fNRows
private

Definition at line 53 of file DBFolder.h.

Referenced by DBFolder(), GetChannelList(), GetTupleColumn(), and UpdateData().

std::string lariov::DBFolder::fTag
private

Definition at line 49 of file DBFolder.h.

Referenced by DBFolder(), Tag(), and UpdateData().

std::vector<std::string> lariov::DBFolder::fTypes
private

Definition at line 57 of file DBFolder.h.

Referenced by DBFolder(), and UpdateData().

std::string lariov::DBFolder::fURL
private

Definition at line 47 of file DBFolder.h.

Referenced by DBFolder(), UpdateData(), and URL().


The documentation for this class was generated from the following files: