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