File indexing completed on 2024-04-06 12:01:29
0001
0002 #include "FWCore/PluginManager/interface/PluginManager.h"
0003 #include "FWCore/PluginManager/interface/standard.h"
0004 #include "FWCore/PluginManager/interface/SharedLibrary.h"
0005 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0006 #include "FWCore/ServiceRegistry/interface/ServiceRegistry.h"
0007
0008 #include "CondCore/CondDB/interface/ConnectionPool.h"
0009
0010 #include "MyTestData.h"
0011
0012
0013
0014 #include <fstream>
0015 #include <iomanip>
0016 #include <cstdlib>
0017 #include <iostream>
0018
0019 using namespace cond::persistency;
0020
0021
0022 const int iVal0(18);
0023 const int iVal1(909);
0024 const std::string sVal("7890abcd");
0025
0026 int doWrite(const std::string& connectionString) {
0027 try {
0028
0029 std::cout << "> Connecting for writing with db in " << connectionString << std::endl;
0030 ConnectionPool connPool;
0031 connPool.setMessageVerbosity(coral::Debug);
0032 Session session = connPool.createSession(connectionString, true);
0033 session.transaction().start(false);
0034
0035 MyTestData d0(iVal0);
0036 MyTestData d1(iVal1);
0037 std::cout << "> Storing payload ptr=" << &d0 << std::endl;
0038 cond::Hash p0 = session.storePayload(d0, boost::posix_time::microsec_clock::universal_time());
0039 cond::Hash p1 = session.storePayload(d1, boost::posix_time::microsec_clock::universal_time());
0040
0041 std::string d(sVal);
0042 cond::Hash p3 = session.storePayload(d, boost::posix_time::microsec_clock::universal_time());
0043
0044
0045
0046
0047
0048
0049
0050 IOVEditor editor;
0051 if (!session.existsIov("MyNewIOV")) {
0052 editor = session.createIov<MyTestData>("MyNewIOV", cond::runnumber);
0053 editor.setDescription("Test with MyTestData class");
0054 editor.insert(1, p0);
0055 editor.insert(100, p1);
0056 std::cout << "> inserted 2 iovs..." << std::endl;
0057 editor.flush();
0058 std::cout << "> iov changes flushed..." << std::endl;
0059 }
0060
0061 if (!session.existsIov("StringData")) {
0062 editor = session.createIov<std::string>("StringData", cond::timestamp);
0063 editor.setDescription("Test with std::string class");
0064 editor.insert(1000000, p3);
0065 editor.insert(2000000, p3);
0066 editor.flush();
0067 }
0068
0069 session.transaction().commit();
0070 std::cout << "> iov changes committed!..." << std::endl;
0071
0072 session.transaction().commit();
0073 } catch (const std::exception& e) {
0074 std::cout << "ERROR: " << e.what() << std::endl;
0075 return -1;
0076 } catch (...) {
0077 std::cout << "UNEXPECTED FAILURE." << std::endl;
0078 return -1;
0079 }
0080 std::cout << "## Run successfully completed." << std::endl;
0081 return 0;
0082 }
0083
0084 int doRead(const std::string& connectionString) {
0085 int nFail = 0;
0086 try {
0087
0088 std::cout << "> Connecting for reading with db in " << connectionString << std::endl;
0089 ConnectionPool connPool;
0090 connPool.setMessageVerbosity(coral::Debug);
0091 Session session = connPool.createSession(connectionString, true);
0092 session.transaction().start(false);
0093
0094 session.transaction().start();
0095
0096 IOVProxy proxy = session.readIov("MyNewIOV");
0097 std::cout << "> iov loaded size=" << proxy.loadedSize() << std::endl;
0098 std::cout << "> iov sequence size=" << proxy.sequenceSize() << std::endl;
0099 IOVArray iovs = proxy.selectAll();
0100 IOVArray::Iterator iovIt = iovs.find(57);
0101 if (iovIt == iovs.end()) {
0102 std::cout << ">[0] not found!" << std::endl;
0103 } else {
0104 cond::Iov_t val = *iovIt;
0105 std::cout << "#[0] iov since=" << val.since << " till=" << val.till << " pid=" << val.payloadId << std::endl;
0106 std::shared_ptr<MyTestData> pay0 = session.fetchPayload<MyTestData>(val.payloadId);
0107 pay0->print();
0108 if (*pay0 != MyTestData(iVal0)) {
0109 nFail++;
0110 std::cout << "ERROR, pay0 found to be wrong, expected : " << iVal0 << " IOV: " << val.since << std::endl;
0111 }
0112 iovIt++;
0113 }
0114 if (iovIt == iovs.end()) {
0115 std::cout << "#[1] not found!" << std::endl;
0116 } else {
0117 cond::Iov_t val = *iovIt;
0118 std::cout << "#[1] iov since=" << val.since << " till=" << val.till << " pid=" << val.payloadId << std::endl;
0119 std::shared_ptr<MyTestData> pay1 = session.fetchPayload<MyTestData>(val.payloadId);
0120 pay1->print();
0121 if (*pay1 != MyTestData(iVal1)) {
0122 nFail++;
0123 std::cout << "ERROR, pay1 found to be wrong, expected : " << iVal1 << " IOV: " << val.since << std::endl;
0124 }
0125 }
0126 iovIt = iovs.find(176);
0127 if (iovIt == iovs.end()) {
0128 std::cout << "#[2] not found!" << std::endl;
0129 } else {
0130 cond::Iov_t val = *iovIt;
0131 std::cout << "#[2] iov since=" << val.since << " till=" << val.till << " pid=" << val.payloadId << std::endl;
0132 std::shared_ptr<MyTestData> pay2 = session.fetchPayload<MyTestData>(val.payloadId);
0133 pay2->print();
0134 if (*pay2 != MyTestData(iVal1)) {
0135 nFail++;
0136 std::cout << "ERROR, pay2 found to be wrong, expected : " << iVal1 << " IOV: " << val.since << std::endl;
0137 }
0138 iovIt++;
0139 }
0140 if (iovIt == iovs.end()) {
0141 std::cout << "#[3] not found!" << std::endl;
0142 } else {
0143 cond::Iov_t val = *iovIt;
0144 std::cout << "#[3] iov since=" << val.since << " till=" << val.till << " pid=" << val.payloadId << std::endl;
0145 std::shared_ptr<MyTestData> pay3 = session.fetchPayload<MyTestData>(val.payloadId);
0146 pay3->print();
0147 if (*pay3 != MyTestData(iVal1)) {
0148 nFail++;
0149 std::cout << "ERROR, pay3 found to be wrong, expected : " << iVal1 << " IOV: " << val.since << std::endl;
0150 }
0151 }
0152
0153 proxy = session.readIov("StringData");
0154 iovs = proxy.selectAll();
0155 auto iov2It = iovs.find(1000022);
0156 if (iov2It == iovs.end()) {
0157 std::cout << "#[4] not found!" << std::endl;
0158 } else {
0159 cond::Iov_t val = *iov2It;
0160 std::cout << "#[4] iov since=" << val.since << " till=" << val.till << " pid=" << val.payloadId << std::endl;
0161 std::shared_ptr<std::string> pay4 = session.fetchPayload<std::string>(val.payloadId);
0162 std::cout << "#pay4=" << *pay4 << std::endl;
0163 if (*pay4 != sVal) {
0164 nFail++;
0165 std::cout << "ERROR, pay4 found to be " << *pay4 << " expected : " << sVal << " IOV: " << val.since
0166 << std::endl;
0167 }
0168 }
0169
0170 session.transaction().commit();
0171 } catch (const std::exception& e) {
0172 std::cout << "ERROR: " << e.what() << std::endl;
0173 return -1;
0174 } catch (...) {
0175 std::cout << "UNEXPECTED FAILURE." << std::endl;
0176 return -1;
0177 }
0178 if (nFail == 0) {
0179 std::cout << "## Run successfully completed." << std::endl;
0180 } else {
0181 std::cout << "## Run completed with ERRORS. nFail = " << nFail << std::endl;
0182 }
0183
0184 return nFail;
0185 }
0186
0187 int run(const std::string& connectionString) {
0188 int ret = doWrite(connectionString);
0189 if (ret != 0)
0190 return ret;
0191
0192 ::sleep(2);
0193 ret = doRead(connectionString);
0194
0195 return ret;
0196 }
0197
0198 int main(int argc, char** argv) {
0199 int ret = 0;
0200 edmplugin::PluginManager::Config config;
0201 edmplugin::PluginManager::configure(edmplugin::standard::config());
0202 std::cout << "## Running with CondDBV2 format..." << std::endl;
0203
0204 if (argc < 3) {
0205 std::cout << "Not enough arguments given, assuming automatic run in unit-tests. Will not do anything. "
0206 << std::endl;
0207 std::cout << "If you did not expect this, please run it with the arguments as follows:" << std::endl;
0208 std::cout << "testReadWritePayloads (write|read) <dbName> " << std::endl;
0209 return 0;
0210 }
0211
0212 std::string connectionString0("sqlite_file:cms_conditions_v2.db");
0213 if (std::string(argv[2]).size() > 3) {
0214 connectionString0 = std::string(argv[2]);
0215 }
0216
0217 if (std::string(argv[1]) == "write") {
0218 ret = doWrite(connectionString0);
0219 } else if (std::string(argv[1]) == "read") {
0220 ret = doRead(connectionString0);
0221 } else {
0222 ret = run(connectionString0);
0223 }
0224
0225 return ret;
0226 }