Changeset 33003 for branches/haf_add201112/ippTools/src/mergetool.c
- Timestamp:
- Dec 22, 2011, 2:21:40 PM (15 years ago)
- File:
-
- 1 edited
-
branches/haf_add201112/ippTools/src/mergetool.c (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
branches/haf_add201112/ippTools/src/mergetool.c
r32980 r33003 37 37 static bool revertmergedMode(pxConfig *config); 38 38 static bool updatemergedMode(pxConfig *config); 39 static bool definebyquerymergecopyMode(pxConfig *config); 40 static bool listmergedvodbcopyMode(pxConfig *config); 41 static bool revertmergedvodbcopyMode(pxConfig *config); 42 static bool updatemergedvodbcopyMode(pxConfig *config); 39 43 40 44 … … 65 69 MODECASE(MERGETOOL_MODE_REVERTMERGED, revertmergedMode); 66 70 MODECASE(MERGETOOL_MODE_UPDATEMERGED, updatemergedMode); 71 MODECASE(MERGETOOL_MODE_DEFINEBYQUERYMERGECOPY, definebyquerymergecopyMode); 72 MODECASE(MERGETOOL_MODE_LISTMERGEDVODBCOPY, listmergedvodbcopyMode); 73 MODECASE(MERGETOOL_MODE_REVERTMERGEDVODBCOPY, revertmergedvodbcopyMode); 74 MODECASE(MERGETOOL_MODE_UPDATEMERGEDVODBCOPY, updatemergedvodbcopyMode); 67 75 68 76 … … 803 811 } 804 812 805 806 807 808 809 810 813 static bool definebyquerymergecopyMode(pxConfig *config) 814 { 815 PS_ASSERT_PTR_NON_NULL(config, NULL); 816 817 psMetadata *where = psMetadataAlloc(); 818 PXOPT_COPY_S64(config->args, where, "-merge_id", "mergedvodbRun.merge_id", "=="); 819 pxAddLabelSearchArgs (config, where, "-mergedvodb", "mergedvodbRun.mergedvodb", "=="); // define using camRun label 820 821 822 823 if (!psListLength(where->list)) { 824 psFree(where); 825 psError(PXTOOLS_ERR_CONFIG, false, "search parameters are required"); 826 return false; 827 } 828 829 PXOPT_LOOKUP_STR(destination_host, config->args, "-set_destination_host", false, false); 830 PXOPT_LOOKUP_STR(mergedvodb_rsync_path, config->args, "-set_mergedvodb_rsync_path", false, false); 831 PXOPT_LOOKUP_BOOL(pretend, config->args, "-pretend", false); 832 PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false); 833 PXOPT_LOOKUP_BOOL(last_merged, config->args, "-last_merged", false); 834 // prevent queueing an addRun if a given exposure has already been added to 835 // the given dvo database 836 psString dvodb_string = NULL; 837 psString bare_query = NULL; 838 if (destination_host) { 839 psTrace("mergedvodbtool.c", PS_LOG_INFO, "destination_host argument found (%s) using mergedvodbtool_find_merge_id_dvo.sql\n", destination_host); 840 // find the cam_id of all the exposures that we want to queue up. 841 bare_query = pxDataGet("mergedvodbtool_find_merge_id_dvo.sql"); 842 // user supplied dvodb 843 psStringAppend(&dvodb_string, "mergedvodbCopy.destination_host = '%s'", destination_host); 844 } else { 845 psError(PS_ERR_UNKNOWN, false, "cannot queue mergedvodbcopy run without a defined destination_host"); 846 847 return false; 848 } 849 850 if (!bare_query) { 851 psError(PXTOOLS_ERR_SYS, false, "failed to retrieve SQL statement"); 852 psFree(where); 853 return false; 854 } 855 // Take the bare query and add the dvodb selector 856 psString query = NULL; 857 psStringAppend(&query, bare_query, dvodb_string); 858 psFree(dvodb_string); 859 psFree(bare_query); 860 861 // use psDBGenerateWhereConditionSQL because the SQL ends in a WHERE 862 if (where && psListLength(where->list)) { 863 psString whereClause = psDBGenerateWhereConditionSQL(where, NULL); 864 psStringAppend(&query, " AND %s", whereClause); 865 psFree(whereClause); 866 } else { 867 psError(PS_ERR_UNKNOWN, true, "search parameters are required"); 868 return false; 869 } 870 if (last_merged) { 871 psStringAppend(&query, " ORDER BY merge_order DESC LIMIT 1"); 872 } 873 874 psFree(where); 875 876 if (!p_psDBRunQuery(config->dbh, query)) { 877 psError(PS_ERR_UNKNOWN, false, "database error"); 878 psFree(query); 879 return false; 880 } 881 psFree(query); 882 883 psArray *output = p_psDBFetchResult(config->dbh); 884 if (!output) { 885 psError(PS_ERR_UNKNOWN, false, "database error"); 886 return false; 887 } 888 if (!psArrayLength(output)) { 889 psTrace("addtool", PS_LOG_INFO, "no rows found"); 890 psFree(output); 891 return true; 892 } 893 894 if (pretend) { 895 // negative simple so the default is true 896 if (!ippdbPrintMetadatas(stdout, output, "mergedvodbCopy", !simple)) { 897 psError(PS_ERR_UNKNOWN, false, "failed to print array"); 898 psFree(output); 899 return false; 900 } 901 psFree(output); 902 return true; 903 } 904 905 // loop over our list of camRun rows to check the supplied and selected dvodb and workdir values: 906 for (long i = 0; i < psArrayLength(output); i++) { 907 psMetadata *md = output->data[i]; 908 909 mergedvodbRunRow *row = mergedvodbRunObjectFromMetadata(md); 910 if (!row) { 911 psError(PS_ERR_UNKNOWN, false, "failed to convert metadata into mergedvodbRun"); 912 psFree(output); 913 return false; 914 } 915 916 if (!mergedvodb_rsync_path) { 917 psError(PS_ERR_UNKNOWN, false, "cannot queue mergedvodbcopy run without a defined mergedvodb_rsync_path: merge_id %" PRId64, row->merge_id); 918 psFree(output); 919 return false; 920 } 921 if (!destination_host) { 922 psError(PS_ERR_UNKNOWN, false, "cannot queue mergedvodbcopy run without a defined destination_host: merge_id %" PRId64, row->merge_id); 923 psFree(output); 924 return false; 925 } 926 927 psFree(row); 928 } 929 930 // start a transaction so we don't end up with an exp without any associted 931 // imfiles 932 if (!psDBTransaction(config->dbh)) { 933 psError(PS_ERR_UNKNOWN, false, "database error"); 934 psFree(output); 935 return false; 936 } 937 938 for (long i = 0; i < psArrayLength(output); i++) { 939 psMetadata *md = output->data[i]; 940 941 mergedvodbRunRow *row = mergedvodbRunObjectFromMetadata(md); 942 if (!row) { 943 psError(PS_ERR_UNKNOWN, false, "failed to convert metadata into camRun"); 944 psFree(output); 945 return false; 946 } 947 948 // queue the exp 949 if (!pxaddQueueByMergeID(config, 950 row->merge_id, 951 destination_host ? destination_host : "NULL", 952 mergedvodb_rsync_path ?mergedvodb_rsync_path : "NULL" 953 )) { 954 if (!psDBRollback(config->dbh)) { 955 psError(PS_ERR_UNKNOWN, false, "database error sfg"); 956 } 957 psError(PS_ERR_UNKNOWN, false, 958 "failed to trying to queue merge_id: %" PRId64, row->merge_id); 959 psFree(row); 960 psFree(output); 961 return false; 962 } 963 psFree(row); 964 } 965 psFree(output); 966 967 if (!psDBCommit(config->dbh)) { 968 psError(PS_ERR_UNKNOWN, false, "database error"); 969 return false; 970 } 971 972 return true; 973 } 974 975 976 static bool listmergedvodbcopyMode(pxConfig *config) { 977 psMetadata *where = psMetadataAlloc(); 978 PXOPT_COPY_S64(config->args, where, "-merge_id", "mergedvodbCopy.merge_id", "=="); 979 PXOPT_COPY_STR(config->args, where, "-mergedvodbcopy_id", "mergedvodbCopy.mergedvodbcopy_id", "=="); 980 PXOPT_COPY_STR(config->args, where, "-destination_host", "mergedvodbCopy.destination_host", "=="); 981 PXOPT_COPY_STR(config->args, where, "-mergedvodb", "mergedvodbRun.mergedvodb", "=="); 982 PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false); 983 PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false); 984 PXOPT_LOOKUP_BOOL(pending, config->args, "-pending", false); 985 PXOPT_LOOKUP_BOOL(faulted, config->args, "-faulted", false); 986 if (!psListLength(where->list)) { 987 psFree(where); 988 psError(PXTOOLS_ERR_CONFIG, false, "search parameters are required"); 989 return false; 990 } 991 992 psString query = pxDataGet("mergedvodbtool_find_mergedvodbcopy.sql"); 993 if (!query) { 994 psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement"); 995 return false; 996 } 997 998 if (psListLength(where->list)) { 999 psString whereClause = psDBGenerateWhereConditionSQL(where, NULL); 1000 psStringAppend(&query, " WHERE %s", whereClause); 1001 psFree(whereClause); 1002 } 1003 1004 // we either add AND (condition) or WHERE (condition): 1005 if (where->list && faulted) { 1006 // list only faulted rows 1007 psStringAppend(&query, " %s", " AND mergedvodbCopy.fault != 0"); 1008 } 1009 if (where->list && !faulted) { 1010 // don't list faulted rows 1011 psStringAppend(&query, " %s", " AND mergedvodbCopy.fault = 0"); 1012 } 1013 if (!where->list && faulted) { 1014 1015 // list only faulted rows 1016 psStringAppend(&query, " %s", " AND mergedvodbCopy.fault != 0"); 1017 } 1018 if (where->list && !faulted) { 1019 // don't list faulted rows 1020 psStringAppend(&query, " %s", " AND mergedvodbCopy.fault = 0"); 1021 } 1022 if (!where->list && faulted) { 1023 // list only faulted rows 1024 psStringAppend(&query, " %s", " WHERE mergedvodbCopy.fault != 0"); 1025 } 1026 if (!where->list && !faulted) { 1027 // don't list faulted rows 1028 psStringAppend(&query, " %s", " WHERE mergedvodbCopy.fault = 0"); 1029 } 1030 psFree(where); 1031 1032 if (pending) { 1033 //add the cuts for pending (state new, no faults) 1034 psStringAppend(&query, " %s", " AND mergedvodbCopy.state = 'new' AND mergedvodbCopy.fault = 0 AND mergedvodbCopy.destination_host IS NOT NULL AND mergedvodbCopy.mergedvodb_rsync_path IS NOT NULL"); 1035 } 1036 1037 1038 // order by epoch 1039 psStringAppend(&query, " ORDER BY mergedvodbcopy_id"); 1040 1041 // treat limit == 0 as "no limit" 1042 if (limit) { 1043 psString limitString = psDBGenerateLimitSQL(limit); 1044 psStringAppend(&query, " %s", limitString); 1045 psFree(limitString); 1046 } 1047 1048 if (!p_psDBRunQuery(config->dbh, query)) { 1049 psError(PS_ERR_UNKNOWN, false, "database error %s ", query); 1050 psFree(query); 1051 return false; 1052 } 1053 psFree(query); 1054 1055 psArray *output = p_psDBFetchResult(config->dbh); 1056 if (!output) { 1057 psError(PS_ERR_UNKNOWN, false, "database error"); 1058 return false; 1059 } 1060 if (!psArrayLength(output)) { 1061 psTrace("addtool", PS_LOG_INFO, "no rows found"); 1062 psFree(output); 1063 return true; 1064 } 1065 1066 // negate simple so the default is true 1067 if (!ippdbPrintMetadatas(stdout, output, "mergedvodbCopy", !simple)) { 1068 psError(PS_ERR_UNKNOWN, false, "failed to print array"); 1069 psFree(output); 1070 return false; 1071 } 1072 1073 psFree(output); 1074 1075 return true; 1076 } 1077 1078 static bool revertmergedvodbcopyMode(pxConfig *config) { 1079 psMetadata *where = psMetadataAlloc(); 1080 PS_ASSERT_PTR_NON_NULL(config, false); 1081 PXOPT_COPY_S64(config->args, where, "-mergedvodbcopy_id", "mergedvodbCopy.mergedvodbcopy_id", "=="); 1082 PXOPT_COPY_S64(config->args, where, "-merge_id", "mergedvodbCopy.merge_id", "=="); 1083 PXOPT_COPY_STR(config->args, where, "-destination_host", "destination_host", "=="); 1084 PXOPT_COPY_S16(config->args, where, "-fault", "mergedvodbCopy.fault", "=="); 1085 1086 if (!psListLength(where->list) && !psMetadataLookupBool(NULL, config->args, "-all")) { 1087 psFree(where); 1088 psError(PXTOOLS_ERR_CONFIG, false, "search parameters are required"); 1089 return false; 1090 } 1091 1092 if (!psDBTransaction(config->dbh)) { 1093 psError(PS_ERR_UNKNOWN, false, "database error"); 1094 psFree(where); 1095 return false; 1096 } 1097 1098 { 1099 psString query = pxDataGet("mergedvodbtool_revertmergedvodbcopy.sql"); 1100 if (!query) { 1101 // rollback 1102 if (!psDBRollback(config->dbh)) { 1103 psError(PS_ERR_UNKNOWN, false, "database error"); 1104 } 1105 psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement"); 1106 psFree(where); 1107 return false; 1108 } 1109 1110 // use psDBGenerateWhereConditionalSQL with AND ... because the SQL ends in a WHERE 1111 if (where && psListLength(where->list)) { 1112 psString whereClause = psDBGenerateWhereConditionSQL(where, NULL); 1113 psStringAppend(&query, " AND %s", whereClause); 1114 psFree(whereClause); 1115 } 1116 if (!p_psDBRunQuery(config->dbh, query)) { 1117 // rollback 1118 if (!psDBRollback(config->dbh)) { 1119 psError(PS_ERR_UNKNOWN, false, "database error %s", query); 1120 } 1121 psError(PS_ERR_UNKNOWN, false, "database error %s", query ); 1122 psFree(query); 1123 psFree(where); 1124 return false; 1125 } 1126 psFree(query); 1127 } 1128 psFree(where); 1129 1130 if (!psDBCommit(config->dbh)) { 1131 psError(PS_ERR_UNKNOWN, false, "database error"); 1132 return false; 1133 } 1134 1135 return true; 1136 } 1137 1138 static bool updatemergedvodbcopyMode(pxConfig *config) { 1139 PS_ASSERT_PTR_NON_NULL(config, false); 1140 psMetadata *where = psMetadataAlloc(); 1141 PXOPT_LOOKUP_U64(mergedvodbcopy_id, config->args, "-mergedvodbcopy_id", false, false); 1142 PXOPT_LOOKUP_U64(merge_id, config->args, "-merge_id", false, false); 1143 PXOPT_LOOKUP_STR(state, config->args, "-state", false, false); 1144 PXOPT_LOOKUP_STR(host, config->args, "-host", false, false); 1145 PXOPT_LOOKUP_STR(rsync_path, config->args, "-mergedvodb_rsync_path", false, false); 1146 PXOPT_LOOKUP_S16(fault, config->args, "-fault", false, false); 1147 1148 PXOPT_LOOKUP_S16(set_fault, config->args, "-set_fault", false, false); 1149 PXOPT_LOOKUP_STR(set_rsync_path, config->args, "-set_mergedvodb_rsync_path", false, false); 1150 PXOPT_LOOKUP_STR(set_host, config->args, "-set_destination_host", false, false); 1151 PXOPT_LOOKUP_STR(set_state, config->args, "-set_state", false, false); 1152 PXOPT_LOOKUP_F32(dtime, config->args, "-set_dtime", false, false); 1153 PXOPT_COPY_S64(config->args, where, "-mergedvodbcopy_id", "mergedvodbCopy.mergedvodbcopy_id", "=="); 1154 PXOPT_COPY_S64(config->args, where, "-merge_id", "mergedvodbCopy.merge_id", "=="); 1155 1156 PXOPT_COPY_STR(config->args, where, "-state", "mergedvodbCopy.state", "=="); 1157 PXOPT_COPY_STR(config->args, where, "-host", "mergedvodbCopy.destination_host", "=="); 1158 PXOPT_COPY_STR(config->args, where, "-mergedvodb_rsync_path", "mergedvodbCopy.mergedvodb_rsync_path", "=="); 1159 1160 1161 if (!psListLength(where->list)) { 1162 psFree(where); 1163 psError(PXTOOLS_ERR_CONFIG, false, "search parameters are required"); 1164 return false; 1165 } 1166 1167 psString query = psStringCopy("UPDATE mergedvodbCopy JOIN mergedvodbRun USING (merge_id) SET "); 1168 int cnt = 0; 1169 psString comma = ","; 1170 if (set_fault) { 1171 psStringAppend(&query, " mergedvodbCopy.fault = %d", set_fault); 1172 cnt++; 1173 } 1174 if (set_rsync_path) { 1175 if (cnt) { 1176 psStringAppend(&query, "%s", comma); 1177 } 1178 1179 psStringAppend(&query, " mergedvodbCopy.mergedvodb_rsync_path = '%s'" , set_rsync_path); 1180 cnt++; 1181 } 1182 1183 if (set_host) { 1184 if (cnt) { 1185 psStringAppend(&query, "%s", comma); 1186 } 1187 1188 psStringAppend(&query, " mergedvodbCopy.destination_host = '%s'" , set_host); 1189 cnt++; 1190 } 1191 1192 1193 if (set_state) { 1194 if (cnt) { 1195 psStringAppend(&query, "%s", comma); 1196 } 1197 1198 psStringAppend(&query, " mergedvodbCopy.state = '%s'" , set_state); 1199 cnt++; 1200 } 1201 1202 if (dtime) { 1203 if (cnt) { 1204 psStringAppend(&query, "%s", comma); 1205 } 1206 psStringAppend(&query, " mergedvodbCopy.dtime = %f", dtime); 1207 cnt++; 1208 } 1209 1210 psString whereClause = psDBGenerateWhereConditionSQL(where, NULL); 1211 psStringAppend(&query, " WHERE %s", whereClause); 1212 1213 1214 if (!p_psDBRunQuery(config->dbh, query)) { 1215 psError(PS_ERR_UNKNOWN, false, "database error %s", query); 1216 psFree(query); 1217 return false; 1218 } 1219 1220 psFree(query); 1221 psFree(where); 1222 1223 return true; 1224 } 1225 1226 1227
Note:
See TracChangeset
for help on using the changeset viewer.
