CMS 3D CMS Logo

MODCCSHFDat.cc
Go to the documentation of this file.
1 #include <stdexcept>
2 #include <string>
3 #include <fstream>
4 #include <iostream>
5 #include <cstdio>
6 #include <cstring>
7 
9 
12 
13 
14 using namespace std;
15 using namespace oracle::occi;
16 
18 {
19  m_env = nullptr;
20  m_conn = nullptr;
21  m_writeStmt = nullptr;
22  m_readStmt = nullptr;
23 
24  // m_clob = 0;
25  m_size=0;
26  m_file="";
27 
28 }
29 
30 
31 
33 {
34 }
35 
36 
38  m_file=x;
39  //try {
40  std::cout<< "file is "<< m_file<<endl;
41  // }catch (Exception &e) {
42  //throw(std::runtime_error(std::string("MODCCSHFDat::setFile(): ")+getOraMessage(&e)));
43  //}
44  // here we must open the file and read the CCS Clob
45  std::cout << "Going to read CCS file: " << m_file << endl;
46  ifstream inpFile;
47  inpFile.open(m_file.c_str());
48  // tell me size of file
49  int bufsize = 0;
50  inpFile.seekg( 0,ios::end );
51  bufsize = inpFile.tellg();
52  std::cout <<" bufsize ="<<bufsize<< std::endl;
53  // set file pointer to start again
54  inpFile.seekg( 0,ios::beg );
55  m_size=bufsize;
56  inpFile.close();
57  std::cout << "Going to read CCS file: " << m_file << endl;
58 
59 }
60 
63 {
64  this->checkConnection();
65 
66  try {
67  m_writeStmt = m_conn->createStatement();
68  m_writeStmt->setSQL("INSERT INTO OD_CCS_HF_dat (iov_id, logic_id, "
69  "ccs_log) "
70  "VALUES (:iov_id, :logic_id, "
71  ":ccs_log )");
72 
73 
74  } catch (SQLException &e) {
75  throw(std::runtime_error(std::string("MODCCSHFDat::prepareWrite(): ")+getOraMessage(&e)));
76  }
77 }
78 
79 
80 
81 void MODCCSHFDat::writeDB(const EcalLogicID* ecid, const MODCCSHFDat* item, MODRunIOV* iov )
82  noexcept(false)
83 {
84 
85 
86 
87  this->checkConnection();
88  this->checkPrepare();
89 
90  int iovID = iov->fetchID();
91  if (!iovID) { throw(std::runtime_error("MODCCSHFDat::writeDB: IOV not in DB")); }
92 
93  int logicID = ecid->getLogicID();
94  if (!logicID) { throw(std::runtime_error("MODCCSHFDat::writeDB: Bad EcalLogicID")); }
95 
96  std::string fname=item->getFile();
97  std::cout << "Going to read CCS file: " << fname << endl;
98 
99 
100  try {
101 
102  m_writeStmt->setInt(1, iovID);
103  m_writeStmt->setInt(2, logicID);
104 
105  // and now the clob
106  oracle::occi::Clob clob(m_conn);
107  clob.setEmpty();
108  m_writeStmt->setClob(3,clob);
109  m_writeStmt->executeUpdate ();
110 
111  m_conn->terminateStatement(m_writeStmt);
112  std::cout<<"empty CCS Clob inserted into DB" <<std::endl;
113 
114  // now we read and update it
115  m_writeStmt = m_conn->createStatement();
116  m_writeStmt->setSQL ("SELECT CCS_LOG FROM OD_CCS_HF_DAT WHERE"
117  " iov_ID=:1 and logic_ID=:2 FOR UPDATE");
118  m_writeStmt->setInt(1, iovID);
119  m_writeStmt->setInt(2, logicID);
120  ResultSet* rset = m_writeStmt->executeQuery();
121  rset->next ();
122  oracle::occi::Clob clob2 = rset->getClob (1);
123  cout << "Opening the clob in read write mode" << endl;
124 
125  unsigned int clob_size=item->getSize();
126 
127  populateClob (clob2 , fname, clob_size);
128 
129  int clobLength=clob2.length ();
130  cout << "Length of the clob is: " << clobLength << endl;
131 
132  m_writeStmt->executeUpdate();
133  m_writeStmt->closeResultSet (rset);
134 
135  } catch (SQLException &e) {
136  throw(std::runtime_error(std::string("MODCCSHFDat::writeDB(): ")+getOraMessage(&e)));
137  }
138 }
139 
140 
141 
142 void MODCCSHFDat::fetchData(std::map< EcalLogicID, MODCCSHFDat >* fillMap, MODRunIOV* iov)
143  noexcept(false)
144 {
145  this->checkConnection();
146  fillMap->clear();
147 
148  iov->setConnection(m_env, m_conn);
149  int iovID = iov->fetchID();
150  if (!iovID) {
151  // throw(std::runtime_error("MODCCSHFDat::writeDB: IOV not in DB"));
152  return;
153  }
154 
155  try {
156 
157  m_readStmt->setSQL("SELECT cv.name, cv.logic_id, cv.id1, cv.id2, cv.id3, cv.maps_to, "
158  " d.ccs_log "
159  "FROM channelview cv JOIN OD_CCS_HF_dat d "
160  "ON cv.logic_id = d.logic_id AND cv.name = cv.maps_to "
161  "WHERE d.iov_id = :iov_id");
162  m_readStmt->setInt(1, iovID);
163  ResultSet* rset = m_readStmt->executeQuery();
164 
165  std::pair< EcalLogicID, MODCCSHFDat > p;
166  MODCCSHFDat dat;
167  while(rset->next()) {
168  p.first = EcalLogicID( getOraString(rset,1), // name
169  rset->getInt(2), // logic_id
170  rset->getInt(3), // id1
171  rset->getInt(4), // id2
172  rset->getInt(5), // id3
173  getOraString(rset,6)); // maps_to
174  // to be corrected
175  // dat.setClob( getOraString(rset,7) );
176 
177  p.second = dat;
178  fillMap->insert(p);
179  }
180  } catch (SQLException &e) {
181  throw(std::runtime_error(std::string("MODCCSHFDat::fetchData(): ")+getOraMessage(&e)));
182  }
183 }
184 
185 void MODCCSHFDat::writeArrayDB(const std::map< EcalLogicID, MODCCSHFDat >* data, MODRunIOV* iov)
186  noexcept(false)
187 {
188  this->checkConnection();
189  this->checkPrepare();
190 
191  int iovID = iov->fetchID();
192  if (!iovID) { throw(std::runtime_error("MODCCSHFDat::writeArrayDB: IOV not in DB")); }
193 
194 
195  int nrows=data->size();
196  int* ids= new int[nrows];
197  int* iovid_vec= new int[nrows];
198  int* xx= new int[nrows];
199 
200  ub2* ids_len= new ub2[nrows];
201  ub2* iov_len= new ub2[nrows];
202  ub2* x_len= new ub2[nrows];
203 
204  const EcalLogicID* channel;
205  //const MODCCSHFDat* dataitem;
206  int count=0;
207  typedef map< EcalLogicID, MODCCSHFDat >::const_iterator CI;
208  for (CI p = data->begin(); p != data->end(); ++p) {
209  channel = &(p->first);
210  int logicID = channel->getLogicID();
211  if (!logicID) { throw(std::runtime_error("MODCCSHFDat::writeArrayDB: Bad EcalLogicID")); }
212  ids[count]=logicID;
213  iovid_vec[count]=iovID;
214 
215  // dataitem = &(p->second);
216  // dataIface.writeDB( channel, dataitem, iov);
217  // to be corrected
218 
219  int x=0;
220  //=dataitem->getWord();
221 
222  xx[count]=x;
223 
224  ids_len[count]=sizeof(ids[count]);
225  iov_len[count]=sizeof(iovid_vec[count]);
226 
227  x_len[count]=sizeof(xx[count]);
228 
229  count++;
230  }
231 
232 
233  try {
234  m_writeStmt->setDataBuffer(1, (dvoid*)iovid_vec, OCCIINT, sizeof(iovid_vec[0]),iov_len);
235  m_writeStmt->setDataBuffer(2, (dvoid*)ids, OCCIINT, sizeof(ids[0]), ids_len );
236  m_writeStmt->setDataBuffer(3, (dvoid*)xx, OCCIINT , sizeof(xx[0]), x_len );
237 
238  m_writeStmt->executeArrayUpdate(nrows);
239 
240  delete [] ids;
241  delete [] iovid_vec;
242  delete [] xx;
243 
244 
245  delete [] ids_len;
246  delete [] iov_len;
247  delete [] x_len;
248 
249 
250 
251  } catch (SQLException &e) {
252  throw(std::runtime_error(std::string("MonPedestalsDat::writeArrayDB(): ")+getOraMessage(&e)));
253  }
254 }
255 
256 
257 
258 void MODCCSHFDat::populateClob (Clob &clob, std::string fname, unsigned int clob_size )
259  noexcept(false)
260 {
261 
262  try{
263  // Uses stream here
264  cout << "Populating the Clob using writeBuffer(Stream) method" << endl;
265  std::cout<<"we are here0"<<std::endl;
266 
267  std::cout<<"we are here0.5 file is:"<<fname<<std::endl;
268 
269  ifstream inFile;
270  inFile.open(fname.c_str(),ios::in);
271  if (!inFile)
272  {
273  cout << fname<<" file not found\n";
274  inFile.close();
275 
276  std::string fname2="/u1/fra/null_file.txt";
277  inFile.open(fname2.c_str(),ios::in);
278 
279 
280 
281  }
282  if(clob_size==0){
283 
284 
285  inFile.seekg( 0,ios::end );
286  clob_size = inFile.tellg();
287  std::cout <<" bufsize ="<<clob_size<< std::endl;
288  // set file pointer to start again
289  inFile.seekg( 0,ios::beg );
290 
291  }
292 
293  char *buffer = new char[clob_size + 1];
294 
295 
296  std::cout<<"we are here1"<<std::endl;
297  unsigned int size;
298  Stream *strm=clob.getStream();
299  std::cout<<"we are here2"<<std::endl;
300  // while(inFile)
301  // {
302  int buf=0;
303  memset (buffer, buf, clob_size + 1);
304  inFile.read(buffer,clob_size);
305  std::cout<<"we are here2.5"<<std::endl;
306 
307  strm->writeBuffer(buffer,strlen(buffer));
308  std::cout<<"we are here2.6"<<std::endl;
309 
310  //}
311  std::cout<<"we are here3"<<std::endl;
312  strcpy(buffer," ");
313  size=strlen(buffer);
314  strm->writeLastBuffer(buffer,size);
315  clob.closeStream(strm);
316  inFile.close();
317  std::cout<<"we are here4"<<std::endl;
318  delete[] buffer;
319 
320 
321  }catch (SQLException &e) {
322  throw(std::runtime_error(std::string("populateClob(): ")+getOraMessage(&e)));
323  }
324 
325  cout << "Populating the Clob - Success" << endl;
326 }
327 
328 
329 unsigned char* MODCCSHFDat::readClob (oracle::occi::Clob &clob, int size)
330  noexcept(false)
331 {
332 
333  try{
334  Stream *instream = clob.getStream (1,0);
335  unsigned char *buffer= new unsigned char[size];
336  int buf=0;
337  memset (buffer, buf, size);
338 
339  instream->readBuffer ((char*)buffer, size);
340  cout << "remember to delete the char* at the end of the program ";
341  for (int i = 0; i < size; ++i)
342  cout << (char) buffer[i];
343  cout << endl;
344 
345 
346  clob.closeStream (instream);
347 
348  return buffer;
349 
350  }catch (SQLException &e) {
351  throw(std::runtime_error(std::string("readClob(): ")+getOraMessage(&e)));
352  }
353 
354 }
size
Write out results.
#define noexcept
oracle::occi::Clob Clob
Definition: MODCCSHFDat.h:21
unsigned char * readClob(Clob &clob, int size) noexcept(false)
Definition: MODCCSHFDat.cc:329
void prepareWrite() noexcept(false) override
Definition: MODCCSHFDat.cc:61
#define end
Definition: vmac.h:39
int getLogicID() const
Definition: EcalLogicID.cc:41
void fetchData(std::map< EcalLogicID, MODCCSHFDat > *fillMap, MODRunIOV *iov) noexcept(false)
Definition: MODCCSHFDat.cc:142
~MODCCSHFDat() override
Definition: MODCCSHFDat.cc:32
void writeDB(const EcalLogicID *ecid, const MODCCSHFDat *item, MODRunIOV *iov) noexcept(false)
Definition: MODCCSHFDat.cc:81
void setFile(std::string x)
Definition: MODCCSHFDat.cc:37
void writeArrayDB(const std::map< EcalLogicID, MODCCSHFDat > *data, MODRunIOV *iov) noexcept(false)
Definition: MODCCSHFDat.cc:185
void populateClob(Clob &clob, std::string fname, unsigned int clob_size) noexcept(false)
Definition: MODCCSHFDat.cc:258
string fname
main script
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:82