Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:23:06

0001 #include <stdexcept>
0002 #include <string>
0003 #include <fstream>
0004 #include <iostream>
0005 #include <cstdio>
0006 #include <cstring>
0007 
0008 #include "OnlineDB/Oracle/interface/Oracle.h"
0009 
0010 #include "OnlineDB/EcalCondDB/interface/MODCCSHFDat.h"
0011 #include "OnlineDB/EcalCondDB/interface/MODRunIOV.h"
0012 
0013 using namespace std;
0014 using namespace oracle::occi;
0015 
0016 MODCCSHFDat::MODCCSHFDat() {
0017   m_env = nullptr;
0018   m_conn = nullptr;
0019   m_writeStmt = nullptr;
0020   m_readStmt = nullptr;
0021 
0022   //  m_clob = 0;
0023   m_size = 0;
0024   m_file = "";
0025 }
0026 
0027 MODCCSHFDat::~MODCCSHFDat() {}
0028 
0029 void MODCCSHFDat::setFile(std::string x) {
0030   m_file = x;
0031   //try {
0032   std::cout << "file is " << m_file << endl;
0033   // }catch (Exception &e) {
0034   //throw(std::runtime_error(std::string("MODCCSHFDat::setFile():  ")+e.getMessage()));
0035   //}
0036   // here we must open the file and read the CCS Clob
0037   std::cout << "Going to read CCS file: " << m_file << endl;
0038   ifstream inpFile;
0039   inpFile.open(m_file.c_str());
0040   // tell me size of file
0041   int bufsize = 0;
0042   inpFile.seekg(0, ios::end);
0043   bufsize = inpFile.tellg();
0044   std::cout << " bufsize =" << bufsize << std::endl;
0045   // set file pointer to start again
0046   inpFile.seekg(0, ios::beg);
0047   m_size = bufsize;
0048   inpFile.close();
0049   std::cout << "Going to read CCS file: " << m_file << endl;
0050 }
0051 
0052 void MODCCSHFDat::prepareWrite() noexcept(false) {
0053   this->checkConnection();
0054 
0055   try {
0056     m_writeStmt = m_conn->createStatement();
0057     m_writeStmt->setSQL(
0058         "INSERT INTO OD_CCS_HF_dat (iov_id, logic_id, "
0059         "ccs_log) "
0060         "VALUES (:iov_id, :logic_id, "
0061         ":ccs_log )");
0062 
0063   } catch (SQLException& e) {
0064     throw(std::runtime_error(std::string("MODCCSHFDat::prepareWrite():  ") + e.getMessage()));
0065   }
0066 }
0067 
0068 void MODCCSHFDat::writeDB(const EcalLogicID* ecid, const MODCCSHFDat* item, MODRunIOV* iov) noexcept(false) {
0069   this->checkConnection();
0070   this->checkPrepare();
0071 
0072   int iovID = iov->fetchID();
0073   if (!iovID) {
0074     throw(std::runtime_error("MODCCSHFDat::writeDB:  IOV not in DB"));
0075   }
0076 
0077   int logicID = ecid->getLogicID();
0078   if (!logicID) {
0079     throw(std::runtime_error("MODCCSHFDat::writeDB:  Bad EcalLogicID"));
0080   }
0081 
0082   std::string fname = item->getFile();
0083   std::cout << "Going to read CCS file: " << fname << endl;
0084 
0085   try {
0086     m_writeStmt->setInt(1, iovID);
0087     m_writeStmt->setInt(2, logicID);
0088 
0089     // and now the clob
0090     oracle::occi::Clob clob(m_conn);
0091     clob.setEmpty();
0092     m_writeStmt->setClob(3, clob);
0093     m_writeStmt->executeUpdate();
0094 
0095     m_conn->terminateStatement(m_writeStmt);
0096     std::cout << "empty CCS Clob inserted into DB" << std::endl;
0097 
0098     // now we read and update it
0099     m_writeStmt = m_conn->createStatement();
0100     m_writeStmt->setSQL(
0101         "SELECT CCS_LOG FROM OD_CCS_HF_DAT WHERE"
0102         " iov_ID=:1 and logic_ID=:2 FOR UPDATE");
0103     m_writeStmt->setInt(1, iovID);
0104     m_writeStmt->setInt(2, logicID);
0105     ResultSet* rset = m_writeStmt->executeQuery();
0106     rset->next();
0107     oracle::occi::Clob clob2 = rset->getClob(1);
0108     cout << "Opening the clob in read write mode" << endl;
0109 
0110     unsigned int clob_size = item->getSize();
0111 
0112     populateClob(clob2, fname, clob_size);
0113 
0114     int clobLength = clob2.length();
0115     cout << "Length of the clob is: " << clobLength << endl;
0116 
0117     m_writeStmt->executeUpdate();
0118     m_writeStmt->closeResultSet(rset);
0119 
0120   } catch (SQLException& e) {
0121     throw(std::runtime_error(std::string("MODCCSHFDat::writeDB():  ") + e.getMessage()));
0122   }
0123 }
0124 
0125 void MODCCSHFDat::fetchData(std::map<EcalLogicID, MODCCSHFDat>* fillMap, MODRunIOV* iov) noexcept(false) {
0126   this->checkConnection();
0127   fillMap->clear();
0128 
0129   iov->setConnection(m_env, m_conn);
0130   int iovID = iov->fetchID();
0131   if (!iovID) {
0132     //  throw(std::runtime_error("MODCCSHFDat::writeDB:  IOV not in DB"));
0133     return;
0134   }
0135 
0136   try {
0137     m_readStmt->setSQL(
0138         "SELECT cv.name, cv.logic_id, cv.id1, cv.id2, cv.id3, cv.maps_to, "
0139         " d.ccs_log "
0140         "FROM channelview cv JOIN OD_CCS_HF_dat d "
0141         "ON cv.logic_id = d.logic_id AND cv.name = cv.maps_to "
0142         "WHERE d.iov_id = :iov_id");
0143     m_readStmt->setInt(1, iovID);
0144     ResultSet* rset = m_readStmt->executeQuery();
0145 
0146     std::pair<EcalLogicID, MODCCSHFDat> p;
0147     MODCCSHFDat dat;
0148     while (rset->next()) {
0149       p.first = EcalLogicID(rset->getString(1),   // name
0150                             rset->getInt(2),      // logic_id
0151                             rset->getInt(3),      // id1
0152                             rset->getInt(4),      // id2
0153                             rset->getInt(5),      // id3
0154                             rset->getString(6));  // maps_to
0155       // to be corrected
0156       //      dat.setClob( rset->getString(7) );
0157 
0158       p.second = dat;
0159       fillMap->insert(p);
0160     }
0161   } catch (SQLException& e) {
0162     throw(std::runtime_error(std::string("MODCCSHFDat::fetchData():  ") + e.getMessage()));
0163   }
0164 }
0165 
0166 void MODCCSHFDat::writeArrayDB(const std::map<EcalLogicID, MODCCSHFDat>* data, MODRunIOV* iov) noexcept(false) {
0167   this->checkConnection();
0168   this->checkPrepare();
0169 
0170   int iovID = iov->fetchID();
0171   if (!iovID) {
0172     throw(std::runtime_error("MODCCSHFDat::writeArrayDB:  IOV not in DB"));
0173   }
0174 
0175   int nrows = data->size();
0176   int* ids = new int[nrows];
0177   int* iovid_vec = new int[nrows];
0178   int* xx = new int[nrows];
0179 
0180   ub2* ids_len = new ub2[nrows];
0181   ub2* iov_len = new ub2[nrows];
0182   ub2* x_len = new ub2[nrows];
0183 
0184   const EcalLogicID* channel;
0185   //const MODCCSHFDat* dataitem;
0186   int count = 0;
0187   typedef map<EcalLogicID, MODCCSHFDat>::const_iterator CI;
0188   for (CI p = data->begin(); p != data->end(); ++p) {
0189     channel = &(p->first);
0190     int logicID = channel->getLogicID();
0191     if (!logicID) {
0192       throw(std::runtime_error("MODCCSHFDat::writeArrayDB:  Bad EcalLogicID"));
0193     }
0194     ids[count] = logicID;
0195     iovid_vec[count] = iovID;
0196 
0197     // dataitem = &(p->second);
0198     // dataIface.writeDB( channel, dataitem, iov);
0199     // to be corrected
0200 
0201     int x = 0;
0202     //=dataitem->getWord();
0203 
0204     xx[count] = x;
0205 
0206     ids_len[count] = sizeof(ids[count]);
0207     iov_len[count] = sizeof(iovid_vec[count]);
0208 
0209     x_len[count] = sizeof(xx[count]);
0210 
0211     count++;
0212   }
0213 
0214   try {
0215     m_writeStmt->setDataBuffer(1, (dvoid*)iovid_vec, OCCIINT, sizeof(iovid_vec[0]), iov_len);
0216     m_writeStmt->setDataBuffer(2, (dvoid*)ids, OCCIINT, sizeof(ids[0]), ids_len);
0217     m_writeStmt->setDataBuffer(3, (dvoid*)xx, OCCIINT, sizeof(xx[0]), x_len);
0218 
0219     m_writeStmt->executeArrayUpdate(nrows);
0220 
0221     delete[] ids;
0222     delete[] iovid_vec;
0223     delete[] xx;
0224 
0225     delete[] ids_len;
0226     delete[] iov_len;
0227     delete[] x_len;
0228 
0229   } catch (SQLException& e) {
0230     throw(std::runtime_error(std::string("MonPedestalsDat::writeArrayDB():  ") + e.getMessage()));
0231   }
0232 }
0233 
0234 void MODCCSHFDat::populateClob(Clob& clob, std::string fname, unsigned int clob_size) noexcept(false) {
0235   try {
0236     // Uses stream here
0237     cout << "Populating the Clob using writeBuffer(Stream) method" << endl;
0238     std::cout << "we are here0" << std::endl;
0239 
0240     std::cout << "we are here0.5 file is:" << fname << std::endl;
0241 
0242     ifstream inFile;
0243     inFile.open(fname.c_str(), ios::in);
0244     if (!inFile) {
0245       cout << fname << " file not found\n";
0246       inFile.close();
0247 
0248       std::string fname2 = "/u1/fra/null_file.txt";
0249       inFile.open(fname2.c_str(), ios::in);
0250     }
0251     if (clob_size == 0) {
0252       inFile.seekg(0, ios::end);
0253       clob_size = inFile.tellg();
0254       std::cout << " bufsize =" << clob_size << std::endl;
0255       // set file pointer to start again
0256       inFile.seekg(0, ios::beg);
0257     }
0258 
0259     char* buffer = new char[clob_size + 1];
0260 
0261     std::cout << "we are here1" << std::endl;
0262     unsigned int size;
0263     Stream* strm = clob.getStream();
0264     std::cout << "we are here2" << std::endl;
0265     //    while(inFile)
0266     //  {
0267     int buf = 0;
0268     memset(buffer, buf, clob_size + 1);
0269     inFile.read(buffer, clob_size);
0270     std::cout << "we are here2.5" << std::endl;
0271 
0272     strm->writeBuffer(buffer, strlen(buffer));
0273     std::cout << "we are here2.6" << std::endl;
0274 
0275     //}
0276     std::cout << "we are here3" << std::endl;
0277     strcpy(buffer, " ");
0278     size = strlen(buffer);
0279     strm->writeLastBuffer(buffer, size);
0280     clob.closeStream(strm);
0281     inFile.close();
0282     std::cout << "we are here4" << std::endl;
0283     delete[] buffer;
0284 
0285   } catch (SQLException& e) {
0286     throw(std::runtime_error(std::string("populateClob():  ") + e.getMessage()));
0287   }
0288 
0289   cout << "Populating the Clob - Success" << endl;
0290 }
0291 
0292 unsigned char* MODCCSHFDat::readClob(oracle::occi::Clob& clob, int size) noexcept(false) {
0293   try {
0294     Stream* instream = clob.getStream(1, 0);
0295     unsigned char* buffer = new unsigned char[size];
0296     int buf = 0;
0297     memset(buffer, buf, size);
0298 
0299     instream->readBuffer((char*)buffer, size);
0300     cout << "remember to delete the char* at the end of the program ";
0301     for (int i = 0; i < size; ++i)
0302       cout << (char)buffer[i];
0303     cout << endl;
0304 
0305     clob.closeStream(instream);
0306 
0307     return buffer;
0308 
0309   } catch (SQLException& e) {
0310     throw(std::runtime_error(std::string("readClob():  ") + e.getMessage()));
0311   }
0312 }