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