IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Nov 4, 2010, 1:42:23 PM (16 years ago)
Author:
bills
Message:

Cleanup for chipBackgroundRun and warpBackgrounRun

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/ippTools/src/bgtool.c

    r29493 r29671  
    4949static bool revertwarpMode(pxConfig *config);
    5050static bool listwarpMode(pxConfig *config);
    51 static bool tocleanchipMode(pxConfig *config);
    52 static bool cleanedchipMode(pxConfig *config);
    53 static bool tocleanwarpMode(pxConfig *config);
    54 static bool cleanedwarpMode(pxConfig *config);
     51
     52static bool pendingcleanupchiprunMode(pxConfig *config);
     53static bool pendingcleanupchipimfileMode(pxConfig *config);
     54static bool tocleanedchipimfileMode(pxConfig *config);
     55static bool updatechipimfileMode(pxConfig *config);
     56
     57static bool pendingcleanupwarprunMode(pxConfig *config);
     58static bool pendingcleanupwarpskyfileMode(pxConfig *config);
     59static bool tocleanedwarpskyfileMode(pxConfig *config);
     60
    5561static bool exportchipMode(pxConfig *config);
    5662static bool importchipMode(pxConfig *config);
    5763static bool exportwarpMode(pxConfig *config);
    5864static bool importwarpMode(pxConfig *config);
     65
     66static bool validDataState(psString data_state);
    5967
    6068// Tables to import/export
     
    112120        MODECASE(BGTOOL_MODE_REVERTWARP,  revertwarpMode);
    113121        MODECASE(BGTOOL_MODE_LISTWARP,    listwarpMode);
    114         MODECASE(BGTOOL_MODE_TOCLEANCHIP, tocleanchipMode);
    115         MODECASE(BGTOOL_MODE_CLEANEDCHIP, cleanedchipMode);
    116         MODECASE(BGTOOL_MODE_TOCLEANWARP, tocleanwarpMode);
    117         MODECASE(BGTOOL_MODE_CLEANEDWARP, cleanedwarpMode);
     122        MODECASE(BGTOOL_MODE_PENDINGCLEANUPCHIPRUN, pendingcleanupchiprunMode);
     123        MODECASE(BGTOOL_MODE_PENDINGCLEANUPCHIPIMFILE, pendingcleanupchipimfileMode);
     124        MODECASE(BGTOOL_MODE_TOCLEANEDCHIPIMFILE, tocleanedchipimfileMode);
     125        MODECASE(BGTOOL_MODE_UPDATECHIPIMFILE, updatechipimfileMode);
     126        MODECASE(BGTOOL_MODE_PENDINGCLEANUPWARPRUN, pendingcleanupwarprunMode);
     127        MODECASE(BGTOOL_MODE_PENDINGCLEANUPWARPSKYFILE, pendingcleanupwarpskyfileMode);
     128        MODECASE(BGTOOL_MODE_TOCLEANEDWARPSKYFILE, tocleanedwarpskyfileMode);
    118129        MODECASE(BGTOOL_MODE_EXPORTCHIP,  exportchipMode);
    119130        MODECASE(BGTOOL_MODE_IMPORTCHIP,  importchipMode);
     
    914925    return true;
    915926}
     927
    916928static bool listchipMode(pxConfig *config)
    917929{
     
    978990}
    979991
    980 static bool tocleanchipMode(pxConfig *config)
     992static bool pendingcleanupchiprunMode(pxConfig *config)
    981993{
    982994    PS_ASSERT_PTR_NON_NULL(config, NULL);
     
    987999    psMetadata *where = psMetadataAlloc();
    9881000    pxAddLabelSearchArgs(config, where, "-label", "chipBackgroundRun.label", "==");
    989 
    990     psString query = pxDataGet("bgtool_tocleanchip.sql");
     1001    pxAddLabelSearchArgs(config, where, "-data_group", "chipBackgroundRun.data_group", "==");
     1002
     1003    psString query = pxDataGet("bgtool_pendingcleanupchiprun.sql");
    9911004    if (!query) {
    9921005        psError(psErrorCodeLast(), false, "failed to retreive SQL statement");
     
    10351048    return true;
    10361049}
    1037 
    1038 static bool cleanedchipMode(pxConfig *config)
    1039 {
    1040     PS_ASSERT_PTR_NON_NULL(config, false);
    1041 
    1042     psMetadata *where = psMetadataAlloc();
     1050static bool pendingcleanupchipimfileMode(pxConfig *config)
     1051{
     1052    PS_ASSERT_PTR_NON_NULL(config, NULL);
     1053
     1054    PXOPT_LOOKUP_S64(chip_bg_id, config->args, "-chip_bg_id", true, false);
     1055    PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false);
     1056    PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
     1057
     1058    psMetadata *where = psMetadataAlloc();
     1059
    10431060    PXOPT_COPY_S64(config->args, where, "-chip_bg_id", "chipBackgroundRun.chip_bg_id", "==");
    10441061
    1045     PXOPT_LOOKUP_STR(state, config->args, "-state", true, false);
    1046     if (!pxIsValidCleanedState(state)) {
    1047         psError(PXTOOLS_ERR_CONFIG, true, "Invalid state: %s", state);
    1048         return false;
    1049     }
    1050 
    1051     if (!psListLength(where->list)) {
    1052         psError(PXTOOLS_ERR_CONFIG, true, "No search restrictions set.");
    1053         return false;
    1054     }
    1055 
    1056     psString query = pxDataGet("bgtool_cleanedchip.sql");
     1062    psString query = pxDataGet("bgtool_pendingcleanupchipimfile.sql");
    10571063    if (!query) {
    10581064        psError(psErrorCodeLast(), false, "failed to retreive SQL statement");
     
    10601066    }
    10611067
    1062     {
     1068    if (where && psListLength(where->list)) {
    10631069        psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
    10641070        psStringAppend(&query, " AND %s", whereClause);
     
    10671073    psFree(where);
    10681074
    1069     if (!p_psDBRunQueryF(config->dbh, query, state)) {
    1070         psError(psErrorCodeLast(), false, "database error");
     1075    if (limit) {
     1076        psString limitString = psDBGenerateLimitSQL(limit);
     1077        psStringAppend(&query, " %s", limitString);
     1078        psFree(limitString);
     1079    }
     1080
     1081    if (!p_psDBRunQuery(config->dbh, query)) {
     1082        psError(psErrorCodeLast(), false, "database error");
     1083        psFree(query);
     1084        return false;
     1085    }
     1086    psFree(query);
     1087
     1088    psArray *output = p_psDBFetchResult(config->dbh);
     1089    if (!output) {
     1090        psError(psErrorCodeLast(), false, "database error");
     1091        return false;
     1092    }
     1093    if (!psArrayLength(output)) {
     1094        psTrace("bgtool", PS_LOG_INFO, "no rows found");
     1095        psFree(output);
     1096        return true;
     1097    }
     1098
     1099    // negative simple so the default is true
     1100    if (!ippdbPrintMetadatas(stdout, output, "chipBackgroundImfile", !simple)) {
     1101        psError(psErrorCodeLast(), false, "failed to print array");
     1102        psFree(output);
     1103        return false;
     1104    }
     1105    psFree(output);
     1106
     1107    return true;
     1108}
     1109
     1110static bool change_imfile_data_state(pxConfig *config, psString data_state)
     1111{
     1112    PS_ASSERT_PTR_NON_NULL(config, false);
     1113
     1114    // chip_id, class_id are required
     1115    PXOPT_LOOKUP_S64(chip_bg_id, config->args, "-chip_bg_id", true, false);
     1116    PXOPT_LOOKUP_STR(class_id, config->args, "-class_id", true, false);
     1117
     1118    psString query = pxDataGet("bgtool_change_imfile_data_state.sql");
     1119
     1120    if (!psDBTransaction(config->dbh)) {
     1121        psError(PS_ERR_UNKNOWN, false, "database error");
     1122        return false;
     1123    }
     1124
     1125    if (!p_psDBRunQueryF(config->dbh, query, data_state, chip_bg_id, class_id)) {
     1126        psFree(query);
     1127        psError(PS_ERR_UNKNOWN, false, "database error");
     1128        // rollback
     1129        if (!psDBRollback(config->dbh)) {
     1130            psError(PS_ERR_UNKNOWN, false, "database error");
     1131        }
     1132        psError(PS_ERR_UNKNOWN, false, "database error");
     1133        return false;
     1134    }
     1135    psFree(query);
     1136    if (psDBAffectedRows(config->dbh) < 1) {
     1137        psError(PS_ERR_UNKNOWN, false, "should have affected atleast 1 row");
     1138        return false;
     1139    }
     1140
     1141    query = pxDataGet("bgtool_change_chiprun_state.sql");
     1142    if (!p_psDBRunQueryF(config->dbh, query, data_state, chip_bg_id, data_state)) {
     1143        psFree(query);
     1144        // rollback
     1145        if (!psDBRollback(config->dbh)) {
     1146            psError(PS_ERR_UNKNOWN, false, "database error");
     1147        }
     1148        psError(PS_ERR_UNKNOWN, false, "database error");
     1149        return false;
     1150    }
     1151    psFree(query);
     1152
     1153    if (!psDBCommit(config->dbh)) {
     1154        psError(PS_ERR_UNKNOWN, false, "database error");
     1155        return false;
     1156    }
     1157
     1158    return true;
     1159}
     1160
     1161static bool tocleanedchipimfileMode(pxConfig *config)
     1162{
     1163    return change_imfile_data_state(config, "cleaned");
     1164}
     1165
     1166static bool updatechipimfileMode(pxConfig *config)
     1167{
     1168    PS_ASSERT_PTR_NON_NULL(config, false);
     1169
     1170    // required
     1171    PXOPT_LOOKUP_S64(chip_bg_id, config->args, "-chip_bg_id", true, false);
     1172    PXOPT_LOOKUP_STR(class_id, config->args, "-class_id", true, false);
     1173
     1174    psMetadata *where = psMetadataAlloc();
     1175
     1176    PXOPT_COPY_S64(config->args, where, "-chip_bg_id", "chipBackgroundImfile.chip_bg_id", "==");
     1177    PXOPT_COPY_S64(config->args, where, "-class_id", "chipBackgroundImfile.class_id", "==");
     1178
     1179    // optional
     1180
     1181    PXOPT_LOOKUP_STR(data_state, config->args, "-set_data_state", false, false);
     1182    PXOPT_LOOKUP_S16(fault, config->args, "-set_fault", false, false);
     1183    if (!data_state && !fault) {
     1184        psError(PS_ERR_UNKNOWN, true, "must supply either -set_fault or -set_data_state");
     1185        return false;
     1186    }
     1187    char *sep = "";
     1188    psString query = psStringCopy("UPDATE chipBackgroundImfile SET ");
     1189    if (data_state) {
     1190        if (!validDataState(data_state)) {
     1191            return false;
     1192        }
     1193        psStringAppend(&query, "%s data_state = '%s'", sep, data_state);
     1194        sep = ", ";
     1195    }
     1196    if (fault) {
     1197        psStringAppend(&query, "%s fault = %d", sep, fault);
     1198        sep = ", ";
     1199    }
     1200    if (psListLength(where->list)) {
     1201        psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
     1202        psStringAppend(&query, " WHERE %s", whereClause);
     1203        psFree(whereClause);
     1204    } else {
     1205        psFree(where);
     1206        psError(PXTOOLS_ERR_CONFIG, false, "search parameters are required");
     1207        return false;
     1208    }
     1209    psFree(where);
     1210    if (!p_psDBRunQuery(config->dbh, query)) {
     1211        psError(psErrorCodeLast(), false, "database error");
     1212        psFree(query);
    10711213        return false;
    10721214    }
     
    14491591        return true;
    14501592    }
    1451     if (!ippdbPrintMetadatas(stdout, output, "chipBackgroundSkyfile", !simple)) {
     1593    if (!ippdbPrintMetadatas(stdout, output, "warpBackgroundSkyfile", !simple)) {
    14521594        psError(psErrorCodeLast(), false, "failed to print array");
    14531595        psFree(output);
     
    18141956}
    18151957
    1816 static bool tocleanwarpMode(pxConfig *config)
     1958static bool pendingcleanupwarprunMode(pxConfig *config)
    18171959{
    18181960    PS_ASSERT_PTR_NON_NULL(config, NULL);
     
    18241966    pxAddLabelSearchArgs(config, where, "-label", "warpBackgroundRun.label", "==");
    18251967
    1826     psString query = pxDataGet("bgtool_tocleanwarp.sql");
     1968    psString query = pxDataGet("bgtool_pendingcleanupwarprun.sql");
    18271969    if (!query) {
    18281970        psError(psErrorCodeLast(), false, "failed to retreive SQL statement");
     
    18712013    return true;
    18722014}
    1873 
    1874 static bool cleanedwarpMode(pxConfig *config)
    1875 {
    1876     PS_ASSERT_PTR_NON_NULL(config, false);
    1877 
    1878     psMetadata *where = psMetadataAlloc();
     2015static bool pendingcleanupwarpskyfileMode(pxConfig *config)
     2016{
     2017    PS_ASSERT_PTR_NON_NULL(config, NULL);
     2018
     2019    PXOPT_LOOKUP_S64(warp_bg_id, config->args, "-warp_bg_id", true, false);
     2020    PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false);
     2021    PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
     2022
     2023    psMetadata *where = psMetadataAlloc();
     2024
    18792025    PXOPT_COPY_S64(config->args, where, "-warp_bg_id", "warpBackgroundRun.warp_bg_id", "==");
    18802026
    1881     PXOPT_LOOKUP_STR(state, config->args, "-state", true, false);
    1882     if (!pxIsValidCleanedState(state)) {
    1883         psError(PXTOOLS_ERR_CONFIG, true, "Invalid state: %s", state);
    1884         return false;
    1885     }
    1886 
    1887     if (!psListLength(where->list)) {
    1888         psError(PXTOOLS_ERR_CONFIG, true, "No search restrictions set.");
    1889         return false;
    1890     }
    1891 
    1892     psString query = pxDataGet("bgtool_cleanedwarp.sql");
     2027    psString query = pxDataGet("bgtool_pendingcleanupwarpskyfile.sql");
    18932028    if (!query) {
    18942029        psError(psErrorCodeLast(), false, "failed to retreive SQL statement");
     
    18962031    }
    18972032
    1898     {
     2033    if (where && psListLength(where->list)) {
    18992034        psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
    19002035        psStringAppend(&query, " AND %s", whereClause);
     
    19032038    psFree(where);
    19042039
    1905     if (!p_psDBRunQueryF(config->dbh, query, state)) {
    1906         psError(psErrorCodeLast(), false, "database error");
    1907         return false;
    1908     }
    1909     psFree(query);
    1910 
    1911     return true;
     2040    if (limit) {
     2041        psString limitString = psDBGenerateLimitSQL(limit);
     2042        psStringAppend(&query, " %s", limitString);
     2043        psFree(limitString);
     2044    }
     2045
     2046    if (!p_psDBRunQuery(config->dbh, query)) {
     2047        psError(psErrorCodeLast(), false, "database error");
     2048        psFree(query);
     2049        return false;
     2050    }
     2051    psFree(query);
     2052
     2053    psArray *output = p_psDBFetchResult(config->dbh);
     2054    if (!output) {
     2055        psError(psErrorCodeLast(), false, "database error");
     2056        return false;
     2057    }
     2058    if (!psArrayLength(output)) {
     2059        psTrace("bgtool", PS_LOG_INFO, "no rows found");
     2060        psFree(output);
     2061        return true;
     2062    }
     2063
     2064    // negative simple so the default is true
     2065    if (!ippdbPrintMetadatas(stdout, output, "warpBackgroundSkyfile", !simple)) {
     2066        psError(psErrorCodeLast(), false, "failed to print array");
     2067        psFree(output);
     2068        return false;
     2069    }
     2070    psFree(output);
     2071
     2072    return true;
     2073}
     2074
     2075static bool change_skyfile_data_state(pxConfig *config, psString data_state)
     2076{
     2077    PS_ASSERT_PTR_NON_NULL(config, false);
     2078
     2079    // warp_id, skycell_id are required
     2080    PXOPT_LOOKUP_S64(warp_bg_id, config->args, "-warp_bg_id", true, false);
     2081    PXOPT_LOOKUP_STR(skycell_id, config->args, "-skycell_id", true, false);
     2082
     2083    psString query = pxDataGet("bgtool_change_skyfile_data_state.sql");
     2084
     2085    if (!psDBTransaction(config->dbh)) {
     2086        psError(PS_ERR_UNKNOWN, false, "database error");
     2087        return false;
     2088    }
     2089
     2090    if (!p_psDBRunQueryF(config->dbh, query, data_state, warp_bg_id, skycell_id)) {
     2091        psFree(query);
     2092        psError(PS_ERR_UNKNOWN, false, "database error");
     2093        // rollback
     2094        if (!psDBRollback(config->dbh)) {
     2095            psError(PS_ERR_UNKNOWN, false, "database error");
     2096        }
     2097        psError(PS_ERR_UNKNOWN, false, "database error");
     2098        return false;
     2099    }
     2100    psFree(query);
     2101    if (psDBAffectedRows(config->dbh) < 1) {
     2102        psError(PS_ERR_UNKNOWN, false, "should have affected atleast 1 row");
     2103        return false;
     2104    }
     2105
     2106    query = pxDataGet("bgtool_change_warprun_state.sql");
     2107    if (!p_psDBRunQueryF(config->dbh, query, data_state, warp_bg_id, data_state)) {
     2108        psFree(query);
     2109        // rollback
     2110        if (!psDBRollback(config->dbh)) {
     2111            psError(PS_ERR_UNKNOWN, false, "database error");
     2112        }
     2113        psError(PS_ERR_UNKNOWN, false, "database error");
     2114        return false;
     2115    }
     2116    psFree(query);
     2117
     2118    if (!psDBCommit(config->dbh)) {
     2119        psError(PS_ERR_UNKNOWN, false, "database error");
     2120        return false;
     2121    }
     2122
     2123    return true;
     2124}
     2125
     2126static bool tocleanedwarpskyfileMode(pxConfig *config)
     2127{
     2128    return change_skyfile_data_state(config, "cleaned");
    19122129}
    19132130
     
    19352152    return importTables(config, infile, warpTables);
    19362153}
     2154
     2155static bool validDataState(psString data_state)
     2156{
     2157    // NOTE: can't use pxIsValidState because
     2158    // update, scrubbed and purged are not supported for the backround stages (yet?)
     2159    if (!strcmp(data_state, "new") ||
     2160        !strcmp(data_state, "full") ||
     2161        !strcmp(data_state, "drop") ||
     2162        !strcmp(data_state, "cleaned") ||
     2163        !strcmp(data_state, "goto_cleaned") ||
     2164        !strcmp(data_state, "error_cleaned")) {
     2165        return true;
     2166    } else {
     2167        psError(PS_ERR_UNKNOWN, true, "%s is not a valid value data_state", data_state);
     2168        return false;
     2169    }
     2170}
Note: See TracChangeset for help on using the changeset viewer.