Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:01:29

0001 #include "FWCore/PluginManager/interface/PluginManager.h"
0002 #include "FWCore/PluginManager/interface/standard.h"
0003 //
0004 #include "CondCore/CondDB/interface/ConnectionPool.h"
0005 #include "CondCore/CondDB/interface/PayloadProxy.h"
0006 //
0007 #include "MyTestData.h"
0008 //
0009 #include <fstream>
0010 #include <iomanip>
0011 #include <cstdlib>
0012 #include <iostream>
0013 
0014 using namespace cond::persistency;
0015 
0016 cond::Iov_t getIovFromTag(Session& session, const std::string& tagName, cond::Time_t targetTime) {
0017   cond::Iov_t ret;
0018   session.transaction().start(true);
0019   auto iovP = session.readIov(tagName);
0020   ret = iovP.getInterval(targetTime);
0021   session.transaction().commit();
0022   return ret;
0023 }
0024 
0025 int main(int argc, char** argv) {
0026   edmplugin::PluginManager::Config config;
0027   edmplugin::PluginManager::configure(edmplugin::standard::config());
0028 
0029   std::string connectionString("sqlite_file:PayloadProxy.db");
0030   std::cout << "# Connecting with db in " << connectionString << std::endl;
0031   try {
0032     //*************
0033     ConnectionPool connPool;
0034     connPool.setMessageVerbosity(coral::Debug);
0035     Session session = connPool.createSession(connectionString, true);
0036     session.transaction().start(false);
0037     MyTestData d0(20000);
0038     MyTestData d1(30000);
0039     std::cout << "# Storing payloads..." << std::endl;
0040     cond::Hash p0 = session.storePayload(d0, boost::posix_time::microsec_clock::universal_time());
0041     cond::Hash p1 = session.storePayload(d1, boost::posix_time::microsec_clock::universal_time());
0042     std::string d2("abcd1234");
0043     cond::Hash p2 = session.storePayload(d2, boost::posix_time::microsec_clock::universal_time());
0044     std::string d3("abcd1234");
0045     cond::Hash p3 = session.storePayload(d3, boost::posix_time::microsec_clock::universal_time());
0046     IOVEditor editor;
0047     if (!session.existsIov("MyNewIOV2")) {
0048       editor = session.createIov<MyTestData>("MyNewIOV2", cond::runnumber);
0049       editor.setDescription("Test with MyTestData class");
0050       editor.insert(1, p0);
0051       editor.insert(100, p1);
0052       std::cout << "# inserted 2 iovs..." << std::endl;
0053       editor.flush();
0054       std::cout << "# iov changes flushed..." << std::endl;
0055     }
0056     if (!session.existsIov("StringData2")) {
0057       editor = session.createIov<std::string>("StringData2", cond::timestamp);
0058       editor.setDescription("Test with std::string class");
0059       editor.insert(1000000, p2);
0060       editor.insert(2000000, p3);
0061       editor.flush();
0062     }
0063     if (!session.existsIov("StringData3")) {
0064       editor = session.createIov<std::string>("StringData3", cond::lumiid);
0065       editor.setDescription("Test with std::string class");
0066       editor.insert(4294967297, p2);
0067       editor.flush();
0068     }
0069 
0070     session.transaction().commit();
0071     std::cout << "# iov changes committed!..." << std::endl;
0072     ::sleep(2);
0073 
0074     cond::Iov_t iov0;
0075     auto requests0 = std::make_shared<std::vector<cond::Iov_t>>();
0076     PayloadProxy<MyTestData> pp0(&iov0, &session, &requests0);
0077 
0078     cond::Iov_t iov1;
0079     auto requests1 = std::make_shared<std::vector<cond::Iov_t>>();
0080     PayloadProxy<std::string> pp1(&iov1, &session, &requests1);
0081 
0082     iov0 = getIovFromTag(session, "MyNewIOV2", 25);
0083     pp0.initializeForNewIOV();
0084     pp0.make();
0085 
0086     const MyTestData& rd0 = pp0();
0087     if (rd0 != d0) {
0088       std::cout << "ERROR: MyTestData object read different from source." << std::endl;
0089     } else {
0090       std::cout << "MyTestData instance valid from " << iov0.since << " to " << iov0.till << std::endl;
0091     }
0092 
0093     iov0 = getIovFromTag(session, "MyNewIOV2", 35);
0094     pp0.initializeForNewIOV();
0095     pp0.make();
0096 
0097     const MyTestData& rd1 = pp0();
0098     if (rd1 != d0) {
0099       std::cout << "ERROR: MyTestData object read different from source." << std::endl;
0100     } else {
0101       std::cout << "MyTestData instance valid from " << iov0.since << " to " << iov0.till << std::endl;
0102     }
0103 
0104     iov0 = getIovFromTag(session, "MyNewIOV2", 100000);
0105     pp0.initializeForNewIOV();
0106     pp0.make();
0107 
0108     const MyTestData& rd2 = pp0();
0109     if (rd2 != d1) {
0110       std::cout << "ERROR: MyTestData object read different from source." << std::endl;
0111     } else {
0112       std::cout << "MyTestData instance valid from " << iov0.since << " to " << iov0.till << std::endl;
0113     }
0114 
0115     try {
0116       iov1 = getIovFromTag(session, "StringData2", 345);
0117     } catch (cond::persistency::Exception& e) {
0118       std::cout << "Expected error: " << e.what() << std::endl;
0119     }
0120 
0121     iov1 = getIovFromTag(session, "StringData2", 1000000);
0122     pp1.initializeForNewIOV();
0123     pp1.make();
0124     const std::string& rd3 = pp1();
0125     if (rd3 != d2) {
0126       std::cout << "ERROR: std::string object read different from source." << std::endl;
0127     } else {
0128       std::cout << "std::string instance valid from " << iov1.since << " to " << iov1.till << std::endl;
0129     }
0130 
0131     iov1 = getIovFromTag(session, "StringData2", 3000000);
0132     pp1.initializeForNewIOV();
0133     pp1.make();
0134     const std::string& rd4 = pp1();
0135     if (rd4 != d3) {
0136       std::cout << "ERROR: std::string object read different from source." << std::endl;
0137     } else {
0138       std::cout << "std::string instance valid from " << iov1.since << " to " << iov1.till << std::endl;
0139     }
0140 
0141     cond::Iov_t iov2;
0142     auto requests2 = std::make_shared<std::vector<cond::Iov_t>>();
0143     PayloadProxy<std::string> pp2(&iov2, &session, &requests2);
0144 
0145     try {
0146       iov2 = getIovFromTag(session, "StringData3", 3000000);
0147     } catch (cond::persistency::Exception& e) {
0148       std::cout << "Expected error: " << e.what() << std::endl;
0149     }
0150   } catch (const std::exception& e) {
0151     std::cout << "ERROR: " << e.what() << std::endl;
0152     return -1;
0153   } catch (...) {
0154     std::cout << "UNEXPECTED FAILURE." << std::endl;
0155     return -1;
0156   }
0157 }