@@ -61,38 +61,36 @@ namespace cbm {
6161 }
6262
6363 MOJA_LOG_INFO << (boost::format (" Loading results into %1% on server: %2%" )
64- % _schema % _pgConnectionString ).str ();
64+ % _schema % _chConnectionString ).str ();
6565
66- connection pgConn (_pgConnectionString);
6766 connection chConn (_chConnectionString);
68- doIsolated (pgConn, (boost::format (" SET search_path = %1%;" ) % _schema).str ());
6967
70- bool resultsPreviouslyLoaded = perform ([&pgConn, this ] {
71- return !nontransaction (pgConn).exec ((boost::format (
72- " SELECT 1 FROM CompletedJobs WHERE id = %1%;"
73- ) % _jobId).str ()).empty ();
68+ std::string guardTable = (boost::format (" completed_%1%" ) % _jobId).str ();
69+ bool resultsPreviouslyLoaded = perform ([&chConn, &guardTable, this ] {
70+ return nontransaction (chConn).exec ((boost::format (
71+ " EXISTS TABLE %1%.%2%;"
72+ ) % _schema % guardTable).str ()).at (0 , 0 ).as <int >() == 1 ;
7473 });
7574
7675 if (resultsPreviouslyLoaded) {
7776 MOJA_LOG_INFO << " Results previously loaded for jobId " << _jobId << " - skipping." ;
7877 return ;
7978 }
8079
81- perform ([&pgConn, &chConn, this ] {
82- work pgTx (pgConn);
80+ perform ([&chConn, &guardTable, this ] {
8381 work chTx (chConn);
8482
85- // First, try to insert into the completed jobs table - if this is a duplicate, the transaction
86- // will fail immediately.
87- pgTx.exec ((boost::format (" INSERT INTO CompletedJobs VALUES (%1%);" ) % _jobId).str ());
83+ // ClickHouse doesn't support unique constraints, so the guard against
84+ // duplicate loads for the same job has to be a table. If this is a
85+ // duplicate, the transaction will fail and roll back the data load.
86+ chTx.exec ((boost::format (" CREATE TABLE %1%.%2% (x INTEGER) ENGINE = MergeTree() ORDER BY x;" ) % _schema % guardTable).str ());
8887
8988 load (chTx, (boost::format (" %1%.raw_fluxes" ) % _schema).str (), _fluxDimension);
9089 load (chTx, (boost::format (" %1%.raw_pools" ) % _schema).str (), _poolDimension);
9190 load (chTx, (boost::format (" %1%.raw_errors" ) % _schema).str (), _errorDimension);
9291 load (chTx, (boost::format (" %1%.raw_ages" ) % _schema).str (), _ageDimension);
9392 load (chTx, (boost::format (" %1%.raw_disturbances" ) % _schema).str (), _disturbanceDimension);
9493
95- pgTx.commit ();
9694 chTx.commit ();
9795 });
9896
0 commit comments