890 {
891
892 std::cout <<
"Copying tag " << sourcetag <<
" of folder " <<
folder <<
893 " to destination tag " << desttag << std::endl;
894 cool::FolderVersioning::Mode vermode=sourcefl->versioningMode();
895 cool::FolderVersioning::Mode dvermode=destfl->versioningMode();
896
897 int updatemode=getUpdateMode(destfl->description(),desttag);
898 std::vector<std::string> dtaglist=destfl->listTags();
899
900
901 if (updatemode==1 && m_destdb.find("oracle")!=std::string::npos &&
902 (!m_getonline || (!m_truncate && !m_alliov))) {
903 if (m_ignoremode) {
904 std::cout << "Folder is online (UPD1) mode but IGNORING PROTECTION"
905 << std::endl;
906 } else {
907 if(
find(dtaglist.begin(),dtaglist.end(),desttag)!=dtaglist.end()) {
908 std::cout << "Folder is online mode (UPD1) and tag already exist - -getonline and -truncate or -alliov options MUST be used" << std::endl;
909 return 35;
910 }
911 else {
912 std::cout << "Folder is online mode (UPD1), new tag will be created" << std::endl;
913 }
914
915 }
916 }
917
918 if (updatemode==4 && m_destdb.find("oracle")!=std::string::npos &&
919 (!m_getbulk || (!m_truncate && !m_alliov))) {
920 if (m_ignoremode) {
921 std::cout << "Folder is bulkreco (UPD4) mode but IGNORING PROTECTION"
922 << std::endl;
923 } else {
924 if(
find(dtaglist.begin(),dtaglist.end(),desttag)!=dtaglist.end()) {
925 std::cout << "Folder is bulkreco mode (UPD4) - -getbulk and -truncate or -alliov options MUST be used" << std::endl;
926 return 35;
927 } else {
928 std::cout << "Folder is bulkreco mode (UPD4), new tag will be created" << std::endl;
929 }
930
931 }
932 }
933
934
935
936
937 if (m_checkdesttag) {
938 if (
find(dtaglist.begin(),dtaglist.end(),desttag)!=dtaglist.end()
939 || (desttag=="HEAD" && !created)) {
940 std::cout << "Destination tag " << desttag <<
941 " already exists in folder " <<
folder <<
" - skip copy" << std::endl;
942 return 0;
943 }
944 }
945 bool relock=false;
946 bool prot=false;
947 if ((m_applock || (updatemode==1 && m_getonline) || updatemode==2 ||
948 (updatemode==4 && m_getbulk)) &&
949 dvermode==cool::FolderVersioning::MULTI_VERSION) {
950
951 std::vector<std::string> dtaglist=destfl->listTags();
952 for (std::vector<std::string>::const_iterator itr=dtaglist.begin();
953 itr!=dtaglist.end();++itr) {
954 if (desttag==*itr) {
955
956 prot=!m_ignoremode;
957
958 if (destfl->tagLockStatus(desttag)==cool::HvsTagLock::LOCKED) {
959 std::cout << "Unlocking destination tag " << desttag <<
960 " for append or UPDx access" << std::endl;
961 if (m_applocksv) {
962 std::cout << "Appending according to SV folder rules" << std::endl;
963 if (updatemode!=3) {
964 std::cout <<
965 "ERROR: Only allowed for UPD3 mode tags" << std::endl;
966 return 36;
967 }
968 }
969 destfl->setTagLockStatus(desttag,cool::HvsTagLock::UNLOCKED);
970 relock=true;
971 }
972 }
973 }
974 }
975 int nobj=0;
976 int nbuf=0;
978 int nbad=0;
979
981 if (dvermode==cool::FolderVersioning::MULTI_VERSION &&
982 desttag!=
"HEAD" && m_usertags)
localtag=desttag;
983
984 try {
985 if (iscora) {
986
987 sourceflc->setPrefetchAll(false);
989 if (vermode==cool::FolderVersioning::SINGLE_VERSION) {
990 sourceitr=sourceflc->browseObjects(since,until,m_chansel);
991 } else {
992 sourceitr=sourceflc->browseObjects(since,until,m_chansel,sourcetag);
993 }
994
995 std::map<int,int> insertkeymap;
996 unsigned int nref=0;
997 while (sourceitr->hasNext()) {
999
1000 if (m_skipout &&
1001 (
obj->since()<m_srunemin ||
obj->until()>m_srunemax)) {
1003 continue;
1004 }
1005
1006 if (!m_excludechans.empty()) {
1007 if (
find(m_excludechans.begin(),m_excludechans.end(),
1008 obj->channelId())!=m_excludechans.end()) {
1010 continue;
1011 }
1012 }
1013 if (nbuf==0) {
1014 if (m_debug) std::cout <<
1015 "Setup new CoraCool storage buffer at object " << nobj << std::endl;
1016 destflc->setupStorageBuffer();
1017 }
1018 cool::ValidityKey newsince,newuntil;
1019 adjustIOVs(
obj->since(),
obj->until(),since,until,newsince,newuntil,
1020 timestamp);
1021
1022 if (newsince>=newuntil) {
1023 std::cout << "WARNING: Skipping IOV with since " << newsince <<
1024 ">= until" << newuntil << std::endl;
1025 ++nbad;
1026 continue;
1027 }
1028
1029
1030 int oldfk=0;
1031 std::map<int,int>::const_iterator ikey=insertkeymap.end();
1032 bool foundkey=false;
1033
1034
1035 if (
obj->size()>0) {
1036 oldfk=sourceflc->getAttrKey(
1037 (*
obj->begin())[sourceflc->coralFKey()]);
1038 ikey=insertkeymap.find(oldfk);
1039 foundkey=true;
1040 }
1041 if (ikey==insertkeymap.end()) {
1042 int newfk=
1043 destflc->storeObject(newsince,newuntil,
obj->begin(),
obj->end(),
1044 obj->channelId(),localtag,
1045 (!m_userupdatehead && !
localtag.empty()));
1046 if (foundkey) insertkeymap[oldfk]=newfk;
1047 } else {
1048 destflc->referenceObject(newsince,newuntil,ikey->second,
1049 obj->channelId(),localtag,
1050 (!m_userupdatehead && !
localtag.empty()));
1051 ++nref;
1052 }
1053 ++nbuf;
1054
1055 if (nbuf==m_bufsize) {
1056 if (m_debug) std::cout << "Flush buffer after " << nobj << "," <<
1057 nbuf << " objects " << std::endl;
1058
1059 destflc->flushStorageBuffer();
1060 destflc->setupStorageBuffer();
1061 nbuf=0;
1062 }
1063 ++nobj;
1064 }
1065 sourceitr->close();
1066 if (nref>0) std::cout << "Reference-to-existing used for " << nref
1067 << " payload objects" << std::endl;
1068 if (nbuf>0) {
1069 if (m_debug) std::cout << "Final buffer flush at " << nobj <<
1070 "," << nbuf << std::endl;
1071 destflc->flushStorageBuffer();
1072 }
1073 } else {
1074
1075 cool::IObjectIteratorPtr sourceitr;
1076 if (vermode==cool::FolderVersioning::SINGLE_VERSION) {
1077 sourceitr=sourcefl->browseObjects(since,until,m_chansel);
1078 } else {
1079 sourceitr=sourcefl->browseObjects(since,until,m_chansel,sourcetag);
1080 }
1081 while (sourceitr->goToNext()) {
1082 const cool::IObject&
obj=sourceitr->currentRef();
1083
1084 if (m_skipout && (
obj.since()<m_srunemin ||
obj.until()>m_srunemax)) {
1086 continue;
1087 }
1088
1089 if (!m_excludechans.empty()) {
1090 if (
find(m_excludechans.begin(),m_excludechans.end(),
1091 obj.channelId())!=m_excludechans.end()) {
1093 continue;
1094 }
1095 }
1096 if (nbuf==0) {
1097 if (m_debug) std::cout << "Setup new storage buffer at object " <<
1098 nobj << std::endl;
1099 destfl->setupStorageBuffer();
1100 }
1101 cool::ValidityKey newsince;
1102 cool::ValidityKey newuntil;
1103 adjustIOVs(
obj.since(),
obj.until(),since,until,newsince,newuntil,
1104 timestamp);
1105
1106 if (newsince>=newuntil) {
1107 std::cout << "WARNING: Skipping IOV with since " << newsince <<
1108 ">= until" << newuntil << std::endl;
1109 ++nbad;
1110 continue;
1111 }
1112 if (checkrefs) checkRef(
obj.payload(),folder,sourcetag);
1113 if (prot) {
1114
1115 if (m_applocksv) {
1116
1117
1118 if (newuntil!=cool::ValidityKeyMax) {
1119 std::cout << "New IOVs must have until=cool::ValidityKeyMax" << std::endl;
1120 throw cool::Exception("Illegal insert over locked IOV",
1121 "AtlCoolCopy");
1122 }
1123 cool::IObjectIteratorPtr checkitr=destfl->browseObjects(newsince,
1124 newuntil,
obj.channelId(),desttag);
1125 while (checkitr->goToNext()) {
1126 const cool::IObject& checkobj=checkitr->currentRef();
1127 if (checkobj.since()>=newsince) {
1128 std::cout << "ERROR:: Attempt to insert SV overlapping IOV whilst appending to locked tag" << std::endl;
1129 throw cool::Exception("Illegal insert over locked IOV",
1130 "AtlCoolCopy");
1131 }
1132 if (checkobj.until()!=cool::ValidityKeyMax) {
1133 std::cout << "Existing IOVs must have until=cool::ValidityKeyMax" << std::endl;
1134 throw cool::Exception("Illegal insert over locked IOV",
1135 "AtlCoolCopy");
1136 }
1137 }
1138 checkitr->close();
1139 } else if (m_applock) {
1140
1141 const unsigned int nexist=
1142 destfl->countObjects(newsince,newuntil,
obj.channelId(),desttag);
1143 if (nexist>0) {
1144 std::cout << "ERROR: Attempt to insert IOV over " << nexist <<
1145 " objects whilst appending to locked tag" << std::endl;
1146 throw cool::Exception("Illegal insert over locked IOV",
1147 "AtlCoolCopy");
1148 }
1149 } else if (updatemode==2) {
1150
1151
1152 if (timestamp)
1153 throw cool::Exception(
1154 "Attempt to insert into locked UPD2 tag with timestamp format",
1155 "AtlCoolCopy");
1156 unsigned int run1=newsince >> 32;
1157 unsigned int run2=(newuntil-1) >> 32;
1158 for (
unsigned int irun=run1;irun<=
run2;++irun) {
1159 if (!std::binary_search(m_runlist.begin(),m_runlist.end(),irun))
1160 {
1161 std::cout <<
"Run " << irun <<
" from range [" <<
run1 <<
","
1162 <<
run2 <<
"] not found in runfile list" << std::endl;
1163 throw cool::Exception("Illegal insert over locked IOV",
1164 "AtlCoolCopy");
1165 }
1166 }
1167 }
1168
1169 }
1170 if (paymode==cool::PayloadMode::VECTORPAYLOAD) {
1171
1172 cool::IRecordIterator& pitr=
obj.payloadIterator();
1173 const cool::IRecordVectorPtr vptr=pitr.fetchAllAsVector();
1174 destfl->storeObject(newsince,newuntil,
1175 *vptr,
obj.channelId(),localtag,
1176 (!m_userupdatehead && !
localtag.empty()));
1177 nbuf+=vptr->size();
1178 pitr.close();
1179 } else {
1180
1181 destfl->storeObject(newsince,newuntil,
1182 obj.payload(),
obj.channelId(),localtag,
1183 (!m_userupdatehead && !
localtag.empty()));
1184 ++nbuf;
1185 }
1186
1187 if (nbuf>=m_bufsize) {
1188 if (m_debug) std::cout << "Flush buffer after " << nobj << "," <<
1189 nbuf << " objects " << std::endl;
1190 destfl->flushStorageBuffer();
1191 nbuf=0;
1192 }
1193 ++nobj;
1194 }
1195
1196 sourceitr->close();
1197 if (nbuf>0) {
1198 if (m_debug) std::cout << "Final buffer flush at " << nobj <<
1199 "," << nbuf << std::endl;
1200 destfl->flushStorageBuffer();
1201 }
1202 }
1203 std::cout << "Folder copied with " << nobj << " objects" << std::endl;
1204 if (nskip>0) std::cout <<
nskip <<
1205 " objects were skipped extending outside IOV selection or excluded channel"
1206 << std::endl;
1207 if (nbad>0) std::cout << nbad <<
1208 " objects were skipped having zero or negative IOV lengths" << std::endl;
1209 }
1210
1211 catch (cool::Exception& e) {
1212 std::cout <<
"Exception thrown from copy loop: " <<
e.what() <<
1213 std::endl;
1214 if (relock) {
1215 std::cout << "Relocking destination tag " << desttag << std::endl;
1216 destfl->setTagLockStatus(desttag,cool::HvsTagLock::LOCKED);
1217 }
1218 return 31;
1219 }
1220
1221 if (relock) {
1222 std::cout << "Relocking destination tag " << desttag << std::endl;
1223 destfl->setTagLockStatus(desttag,cool::HvsTagLock::LOCKED);
1224 }
1225
1226 if (dvermode==cool::FolderVersioning::MULTI_VERSION && desttag!="HEAD") {
1227 if (!m_usertags) {
1228
1229 std::cout << "Tag folder with HEAD-style tagging for tag: " << desttag
1230 << std::endl;
1231 try {
1232 destfl->tagCurrentHead(desttag,sourcefl->tagDescription(sourcetag));
1233 }
1234 catch (cool::Exception& e) {
1235 std::cout << "Exception thrown in HEAD-style folder-tag: " <<
1236 e.what() << std::endl;
1237 return 32;
1238 }
1239 }
1240
1241 if (m_copytaginfo) m_cooltagmap.insert(
1242 CoolTagMap::value_type(desttag,
CoolTagInfo(m_sourceDbPtr,folder,
1243 destfolder,sourcetag,desttag,m_taglabel)));
1244
1245 if (!m_hitagmap.empty()) {
1246 for (HiTagMap::const_iterator imap=m_hitagmap.begin();
1247 imap!=m_hitagmap.end();++imap) {
1248 try {
1249 if (sourcefl->findTagRelation(imap->first)==sourcetag) {
1250 std::cout << "Create hierarchical tag between " << desttag <<
1251 " and " << imap->first << " in folder " << imap->second <<
1252 std::endl;
1253
1254 try {
1255 std::string etag=destfl->resolveTag(imap->first);
1256 if (etag==desttag) {
1257 std::cout << "This relation has already been created" << std::endl;
1258 } else {
1259 std::cout << "ERROR: Tag in parent already related to " <<
1260 desttag << std::endl;
1261 }
1262 }
1263 catch (cool::Exception& e ) {
1264
1265 try {
1266 destfl->createTagRelation(imap->first,desttag);
1267
1268
1269 if (
find(m_hiparent.begin(),m_hiparent.end(),imap->second)==
1270 m_hiparent.end()) m_hiparent.push_back(imap->second);
1271
1272
1273 if (m_copytaginfo &&
1274 m_cooltagmap.find(imap->first)==m_cooltagmap.end())
1275 m_cooltagmap.insert(CoolTagMap::value_type(imap->first,
1276 CoolTagInfo(m_sourceDbPtr,imap->second,imap->second,
1277 imap->first,imap->first)));
1278 }
1279 catch (cool::Exception& e) {
1280 std::cout <<
"Cool exception " <<
e.what() <<
1281 "thrown in hierarchical tag creation" << std::endl;
1282 return 34;
1283 }
1284 }
1285 }
1286 }
1287 catch (cool::Exception& e) {
1288 }
1289 }
1290 }
1291 }
1292 return 0;
1293}
boost::shared_ptr< CoraCoolObject > CoraCoolObjectPtr
boost::shared_ptr< CoraCoolObjectIter > CoraCoolObjectIterPtr
std::string find(const std::string &s)
return a remapped string