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