- Timestamp:
- Sep 27, 2009, 11:34:07 AM (17 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/eam_branches/20090715/ippTools/src/magicdstool.c
r25022 r25624 37 37 static bool todestreakMode(pxConfig *config); 38 38 static bool adddestreakedfileMode(pxConfig *config); 39 static bool advancerunMode(pxConfig *config); 39 40 static bool revertdestreakedfileMode(pxConfig *config); 40 41 static bool getskycellsMode(pxConfig *config); 41 42 static bool toremoveMode(pxConfig *config); 42 static bool torestoreMode(pxConfig *config);43 43 static bool torevertMode(pxConfig *config); 44 static bool completedrevertMode(pxConfig *config); 44 45 45 46 static bool setmagicDSRunState(pxConfig *config, psS64 magic_id, const char *state); 46 static bool magicDSRunComplete(pxConfig *config, bool setmagicked);47 47 static bool magicDSGetIDs(pxConfig *config, psString stage, psS64 magic_id, psS64 *stage_id, psS64 *cam_id); 48 48 … … 70 70 MODECASE(MAGICDSTOOL_MODE_TODESTREAK, todestreakMode); 71 71 MODECASE(MAGICDSTOOL_MODE_ADDDESTREAKEDFILE, adddestreakedfileMode); 72 MODECASE(MAGICDSTOOL_MODE_ADVANCERUN, advancerunMode); 72 73 MODECASE(MAGICDSTOOL_MODE_REVERTDESTREAKEDFILE,revertdestreakedfileMode); 73 74 MODECASE(MAGICDSTOOL_MODE_GETSKYCELLS, getskycellsMode); 74 75 MODECASE(MAGICDSTOOL_MODE_TOREMOVE, toremoveMode); 75 MODECASE(MAGICDSTOOL_MODE_TORESTORE, torestoreMode);76 76 MODECASE(MAGICDSTOOL_MODE_TOREVERT, torevertMode); 77 MODECASE(MAGICDSTOOL_MODE_COMPLETEDREVERT, completedrevertMode); 77 78 default: 78 79 psAbort("invalid option (this should not happen)"); … … 105 106 PXOPT_LOOKUP_STR(workdir, config->args, "-workdir", false, false); 106 107 PXOPT_LOOKUP_STR(recoveryroot, config->args, "-recoveryroot", false, false); 107 PXOPT_LOOKUP_BOOL( re_place, config->args, "-replace", false);108 PXOPT_LOOKUP_BOOL(noreplace, config->args, "-noreplace", false); 108 109 PXOPT_LOOKUP_STR(set_label, config->args, "-set_label", false, false); 109 110 PXOPT_LOOKUP_BOOL(rerun, config->args, "-rerun", false); … … 119 120 PXOPT_COPY_S64(config->args, where, "-warp_id", "warp_id", "=="); 120 121 PXOPT_COPY_S64(config->args, where, "-diff_id", "diff_id", "=="); 121 PXOPT_COPY_S64(config->args, where, "-magic_id","magic _id", "==");122 PXOPT_COPY_S64(config->args, where, "-magic_id","magicRun.magic_id", "=="); 122 123 PXOPT_COPY_S32(config->args, where, "-streaks_max","streaks", "<="); 123 124 … … 268 269 outroot, 269 270 recoveryroot, 270 re_place,271 noreplace ? 0 :1, // re_place 271 272 0); // remove 272 273 … … 396 397 PS_ASSERT_PTR_NON_NULL(config, false); 397 398 399 PXOPT_LOOKUP_STR(stage, config->args, "-stage", true, false); 400 398 401 psMetadata *where = psMetadataAlloc(); 399 PXOPT_COPY_S64(config->args, where, "-magic_ds_id", "magic _ds_id", "==");402 PXOPT_COPY_S64(config->args, where, "-magic_ds_id", "magicDSRun.magic_ds_id", "=="); 400 403 PXOPT_COPY_S64(config->args, where, "-magic_id", "magic_id", "=="); 401 pxAddLabelSearchArgs (config, where, "-label", "label", "=="); 402 PXOPT_COPY_STR(config->args, where, "-stage", "stage", "=="); 404 pxAddLabelSearchArgs (config, where, "-label", "magicDSRun.label", "=="); 403 405 404 406 PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false); 405 407 PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false); 406 408 407 // look for "inputs" that need to processed 408 psString query = pxDataGet("magicdstool_todestreak.sql"); 409 psString sql_file = NULL; 410 psStringAppend(&sql_file, "magicdstool_todestreak_%s.sql", stage); 411 412 psString query = pxDataGet(sql_file); 409 413 if (!query) { 410 psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement"); 411 return false; 412 } 414 psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement from %s", sql_file); 415 psFree(sql_file); 416 return false; 417 } 418 psFree(sql_file); 413 419 414 420 if (psListLength(where->list)) { 415 421 psString whereClause = psDBGenerateWhereConditionSQL(where, NULL); 416 psStringAppend(&query, " WHERE%s", whereClause);422 psStringAppend(&query, " AND %s", whereClause); 417 423 psFree(whereClause); 418 424 } … … 656 662 } 657 663 658 if (!magicDSRunComplete(config, setmagicked)) {659 // rollback660 if (!psDBRollback(config->dbh)) {661 psError(PS_ERR_UNKNOWN, false, "database error");662 }663 psError(PS_ERR_UNKNOWN, false, "database error");664 return false;665 }666 667 664 if (!psDBCommit(config->dbh)) { 668 665 psError(PS_ERR_UNKNOWN, false, "database error"); … … 738 735 } 739 736 740 static bool magicDSRunComplete(pxConfig *config, bool setmagicked)737 static bool advancerunMode(pxConfig *config) 741 738 { 742 739 PS_ASSERT_PTR_NON_NULL(config, false); 740 741 psMetadata *where = psMetadataAlloc(); 742 PXOPT_COPY_S64(config->args, where, "-magic_ds_id", "magicDSRun.magic_ds_id", "=="); 743 pxAddLabelSearchArgs (config, where, "-label", "magicDSRun.label", "=="); 744 745 PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false); 743 746 744 747 // look for completed magicDSRuns … … 749 752 } 750 753 754 if (psListLength(where->list)) { 755 psString whereClause = psDBGenerateWhereConditionSQL(where, NULL); 756 psStringAppend(&query, " WHERE %s", whereClause); 757 psFree(whereClause); 758 } 759 751 760 if (!p_psDBRunQuery(config->dbh, query)) { 752 761 psError(PS_ERR_UNKNOWN, false, "database error"); … … 766 775 return true; 767 776 } 777 if (!psDBTransaction(config->dbh)) { 778 psError(PS_ERR_UNKNOWN, false, "database error"); 779 return false; 780 } 768 781 for (long i = 0; i < psArrayLength(output); i++) { 769 782 psMetadata *row = output->data[i]; … … 771 784 psS64 magic_ds_id = psMetadataLookupS64(NULL, row, "magic_ds_id"); 772 785 773 // if requested, set stageRun.magicked 786 // if re_place, set stageRun.magicked 787 bool setmagicked = psMetadataLookupBool(NULL, row, "re_place"); 774 788 if (setmagicked && !setRunMagicked(config, magic_ds_id)) { 775 789 psError(PS_ERR_UNKNOWN, false, "failed to change stageRun.magicked for magic_ds_id: %" PRId64, 776 790 magic_ds_id); 791 if (!psDBRollback(config->dbh)) { 792 psError(PS_ERR_UNKNOWN, false, "database error"); 793 } 777 794 return false; 778 795 } … … 783 800 magic_ds_id); 784 801 psFree(output); 802 if (!psDBRollback(config->dbh)) { 803 psError(PS_ERR_UNKNOWN, false, "database error"); 804 } 785 805 return false; 786 806 } 787 807 } 788 808 if (!psDBCommit(config->dbh)) { 809 psError(PS_ERR_UNKNOWN, false, "database error"); 810 return false; 811 } 789 812 790 813 return true; … … 795 818 { 796 819 PS_ASSERT_PTR_NON_NULL(config, false); 820 821 PXOPT_LOOKUP_BOOL(i_am_sure, config->args, "-i_am_sure", true); 822 if (!i_am_sure) { 823 psError(PS_ERR_UNKNOWN, true, "Reverting destreaked files must be done carefully. -i_am_sure is required."); 824 return false; 825 } 797 826 798 827 psMetadata *where = psMetadataAlloc(); … … 802 831 pxAddLabelSearchArgs (config, where, "-label", "label", "=="); 803 832 804 psString query = p sStringCopy("DELETE FROM magicDSFile USING magicDSFile, magicDSRun WHERE (magicDSRun.magic_ds_id = magicDSFile.magic_ds_id) AND magicDSFile.fault != 0");833 psString query = pxDataGet("magicdstool_revertdestreakedfile.sql"); 805 834 806 835 if (psListLength(where->list)) { … … 808 837 psStringAppend(&query, " AND %s", whereClause); 809 838 psFree(whereClause); 839 } else { 840 psError(PS_ERR_UNKNOWN, true, "search arguments are required"); 841 return false; 810 842 } 811 843 psFree(where); … … 813 845 if (!p_psDBRunQuery(config->dbh, query)) { 814 846 psError(PS_ERR_UNKNOWN, false, "failed to revert"); 815 return false; 816 } 817 return true; 818 } 819 820 static bool getskycellsMode(pxConfig *config) 821 { 822 // required 823 PXOPT_LOOKUP_S64(magic_ds_id, config->args, "-magic_ds_id", true, false); 847 psFree(query); 848 return false; 849 } 850 psFree(query); 851 return true; 852 } 853 854 static bool completedrevertMode(pxConfig *config) 855 { 856 PS_ASSERT_PTR_NON_NULL(config, false); 824 857 825 858 psMetadata *where = psMetadataAlloc(); 826 PXOPT_COPY_STR(config->args, where, "-class_id", "warpSkyCellMap.class_id", "=="); 827 PXOPT_COPY_STR(config->args, where, "-skycell_id", "warpSkyCellMap.skycell_id", "=="); 828 829 PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false); 830 831 psString query = pxDataGet("magicdstool_getskycells.sql"); 832 if (!query) { 833 psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement"); 834 return false; 835 } 836 859 PXOPT_COPY_S64(config->args, where, "-magic_ds_id", "magicDSRun.magic_ds_id", "=="); 860 pxAddLabelSearchArgs (config, where, "-label", "label", "=="); 861 862 PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false); 863 864 psString query = pxDataGet("magicdstool_completedrevert.sql"); 865 // treat limit == 0 as "no limit" 866 if (limit) { 867 psString limitString = psDBGenerateLimitSQL(limit); 868 psStringAppend(&query, " %s", limitString); 869 psFree(limitString); 870 } 871 872 psString whereString = NULL; 837 873 if (psListLength(where->list)) { 838 874 psString whereClause = psDBGenerateWhereConditionSQL(where, NULL); 839 psStringAppend(& query, "AND %s", whereClause);875 psStringAppend(&whereString, "\nAND %s", whereClause); 840 876 psFree(whereClause); 841 877 } 842 878 psFree(where); 843 879 844 if (!p_psDBRunQueryF(config->dbh, query, magic_ds_id)) { 845 psError(PS_ERR_UNKNOWN, false, "database error"); 846 psFree(query); 847 return false; 848 } 880 if (!p_psDBRunQueryF(config->dbh, query, whereString ? whereString : "")) { 881 psFree(whereString); 882 psError(PS_ERR_UNKNOWN, false, "failed to revert"); 883 return false; 884 } 885 psFree(whereString); 849 886 psFree(query); 850 851 887 psArray *output = p_psDBFetchResult(config->dbh); 852 888 if (!output) { … … 868 904 return true; 869 905 } 870 871 if (psArrayLength(output)) { 872 // negative simple so the default is true 873 if (!ippdbPrintMetadatas(stdout, output, "magicDiffSkyfile", !simple)) { 874 psError(PS_ERR_UNKNOWN, false, "failed to print array"); 906 for (int i=0; i<psArrayLength(output); i++) { 907 psMetadata *row = output->data[i]; 908 psS64 magic_ds_id = psMetadataLookupS64(NULL, row, "magic_ds_id"); 909 psString old_state = psMetadataLookupStr(NULL, row, "state"); 910 psString new_state; 911 if (!strcmp(old_state, "goto_censored")) { 912 new_state = "censored"; 913 } else if (!strcmp(old_state, "goto_restored")) { 914 new_state = "restored"; 915 } else { 916 psError(PXTOOLS_ERR_PROG, true, "unexpected state found: %s", old_state); 875 917 psFree(output); 876 918 return false; 877 919 } 878 } 879 920 char *query2 = "UPDATE magicDSRun SET state = '%s' WHERE magic_ds_id = %" PRId64; 921 if (!p_psDBRunQueryF(config->dbh, query2, new_state, magic_ds_id)) { 922 psError(PS_ERR_UNKNOWN, false, "failed to set run magicDSRun.state to %s", new_state); 923 return false; 924 } 925 } 880 926 psFree(output); 881 927 … … 883 929 } 884 930 885 static bool setmagicDSRunState(pxConfig *config, psS64 magic_ds_id, const char *state) 886 { 887 PS_ASSERT_PTR_NON_NULL(state, false); 888 889 // check that state is a valid string value 890 if (!( 891 (strncmp(state, "new", 4) == 0) 892 || (strncmp(state, "full", 5) == 0) 893 ) 894 ) { 895 psError(PS_ERR_UNKNOWN, false, 896 "invalid magicDSRun state: %s", state); 897 return false; 898 } 899 900 char *query = "UPDATE magicDSRun SET state = '%s' WHERE magic_ds_id = %" PRId64; 901 if (!p_psDBRunQueryF(config->dbh, query, state, magic_ds_id)) { 902 psError(PS_ERR_UNKNOWN, false, 903 "failed to change state for magic_id %" PRId64, magic_ds_id); 904 return false; 905 } 906 907 return true; 908 } 909 910 static bool toremoveMode(pxConfig *config) 911 { 912 PS_ASSERT_PTR_NON_NULL(config, false); 931 static bool getskycellsMode(pxConfig *config) 932 { 933 // required 934 PXOPT_LOOKUP_S64(magic_ds_id, config->args, "-magic_ds_id", true, false); 913 935 914 936 psMetadata *where = psMetadataAlloc(); 915 PXOPT_COPY_S64(config->args, where, "-magic_ds_id", "magic_ds_id", "=="); 916 PXOPT_COPY_S64(config->args, where, "-magic_id", "magic_id", "=="); 917 pxAddLabelSearchArgs (config, where, "-label", "label", "=="); 918 919 PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false); 937 PXOPT_COPY_STR(config->args, where, "-class_id", "warpSkyCellMap.class_id", "=="); 938 PXOPT_COPY_STR(config->args, where, "-skycell_id", "warpSkyCellMap.skycell_id", "=="); 939 920 940 PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false); 921 941 922 // look for "inputs" that need to processed 923 psString query = pxDataGet("magicdstool_toremove.sql"); 942 psString query = pxDataGet("magicdstool_getskycells.sql"); 924 943 if (!query) { 925 944 psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement"); … … 929 948 if (psListLength(where->list)) { 930 949 psString whereClause = psDBGenerateWhereConditionSQL(where, NULL); 931 psStringAppend(&query, " WHERE%s", whereClause);950 psStringAppend(&query, " AND %s", whereClause); 932 951 psFree(whereClause); 933 952 } 934 953 psFree(where); 935 954 936 // treat limit == 0 as "no limit" 937 if (limit) { 938 psString limitString = psDBGenerateLimitSQL(limit); 939 psStringAppend(&query, " %s", limitString); 940 psFree(limitString); 941 } 942 943 if (!p_psDBRunQuery(config->dbh, query)) { 955 if (!p_psDBRunQueryF(config->dbh, query, magic_ds_id)) { 944 956 psError(PS_ERR_UNKNOWN, false, "database error"); 945 957 psFree(query); … … 970 982 if (psArrayLength(output)) { 971 983 // negative simple so the default is true 972 if (!ippdbPrintMetadatas(stdout, output, " toremove", !simple)) {984 if (!ippdbPrintMetadatas(stdout, output, "magicDiffSkyfile", !simple)) { 973 985 psError(PS_ERR_UNKNOWN, false, "failed to print array"); 974 986 psFree(output); … … 981 993 return true; 982 994 } 983 static bool torestoreMode(pxConfig *config) 995 996 static bool setmagicDSRunState(pxConfig *config, psS64 magic_ds_id, const char *state) 997 { 998 PS_ASSERT_PTR_NON_NULL(state, false); 999 1000 // check that state is a valid string value 1001 if (!((strcmp(state, "new") == 0) || 1002 (strcmp(state, "full") == 0) || 1003 (strcmp(state, "restored") == 0) || 1004 (strcmp(state, "censored") == 0) || 1005 (strcmp(state, "purged") == 0) || 1006 (strcmp(state, "goto_restored") == 0) || 1007 (strcmp(state, "goto_censored") == 0) || 1008 (strcmp(state, "goto_purged") == 0)) 1009 ) { 1010 psError(PS_ERR_UNKNOWN, false, 1011 "invalid magicDSRun state: %s", state); 1012 return false; 1013 } 1014 1015 char *query = "UPDATE magicDSRun SET state = '%s' WHERE magic_ds_id = %" PRId64; 1016 if (!p_psDBRunQueryF(config->dbh, query, state, magic_ds_id)) { 1017 psError(PS_ERR_UNKNOWN, false, 1018 "failed to change state for magic_id %" PRId64, magic_ds_id); 1019 return false; 1020 } 1021 1022 return true; 1023 } 1024 1025 static bool toremoveMode(pxConfig *config) 984 1026 { 985 1027 PS_ASSERT_PTR_NON_NULL(config, false); … … 994 1036 995 1037 // look for "inputs" that need to processed 996 psString query = pxDataGet("magicdstool_tore store.sql");1038 psString query = pxDataGet("magicdstool_toremove.sql"); 997 1039 if (!query) { 998 1040 psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement"); … … 1043 1085 if (psArrayLength(output)) { 1044 1086 // negative simple so the default is true 1045 if (!ippdbPrintMetadatas(stdout, output, "tore store", !simple)) {1087 if (!ippdbPrintMetadatas(stdout, output, "toremove", !simple)) { 1046 1088 psError(PS_ERR_UNKNOWN, false, "failed to print array"); 1047 1089 psFree(output); … … 1054 1096 return true; 1055 1097 } 1056 1057 1098 1058 1099 static bool torevertMode(pxConfig *config)
Note:
See TracChangeset
for help on using the changeset viewer.
