IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Aug 7, 2009, 4:08:25 PM (17 years ago)
Author:
Paul Price
Message:

Merging trunk (r25026) to get up-to-date on old branch.

Location:
branches/pap
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/pap

  • branches/pap/ippTools/src

    • Property svn:ignore
      •  

        old new  
        3434disttool
        3535receivetool
         36
         37pubtool
  • branches/pap/ippTools/src/magicdstool.c

    r23688 r25027  
    22 * magicdstool.c
    33 *
    4  * Copyright (C) 2006-2007  IfA
     4 * Copyright (C) 2006-2009  IfA
    55 *
    66 * This program is free software; you can redistribute it and/or modify it
     
    2727#include <math.h>
    2828#include <ippdb.h>
     29#include <ippStages.h>
    2930
    3031#include "pxtools.h"
     
    3839static bool revertdestreakedfileMode(pxConfig *config);
    3940static bool getskycellsMode(pxConfig *config);
     41static bool toremoveMode(pxConfig *config);
     42static bool torestoreMode(pxConfig *config);
     43static bool torevertMode(pxConfig *config);
    4044
    4145static bool setmagicDSRunState(pxConfig *config, psS64 magic_id, const char *state);
     
    6872        MODECASE(MAGICDSTOOL_MODE_REVERTDESTREAKEDFILE,revertdestreakedfileMode);
    6973        MODECASE(MAGICDSTOOL_MODE_GETSKYCELLS,         getskycellsMode);
     74        MODECASE(MAGICDSTOOL_MODE_TOREMOVE,            toremoveMode);
     75        MODECASE(MAGICDSTOOL_MODE_TORESTORE,           torestoreMode);
     76        MODECASE(MAGICDSTOOL_MODE_TOREVERT,            torevertMode);
    7077        default:
    7178            psAbort("invalid option (this should not happen)");
     
    9198static bool definebyqueryMode(pxConfig *config)
    9299{
    93     PS_ASSERT_PTR_NON_NULL(config, false);
    94     psError(PS_ERR_UNKNOWN, true, "definebyquery not implelmented yet");
    95 
    96     return false;
    97 #ifdef notyet
    98100
    99101    // Required
     102    PXOPT_LOOKUP_STR(stage, config->args, "-stage", true, false);
     103
     104    // Optional
    100105    PXOPT_LOOKUP_STR(workdir, config->args, "-workdir", false, false);
    101 
    102     // Optional
    103     PXOPT_LOOKUP_STR(label, config->args, "-label", false, false);
    104     PXOPT_LOOKUP_STR(dvodb, config->args, "-dvodb", false, false);
    105     PXOPT_LOOKUP_TIME(registered, config->args, "-registered", false, false);
     106    PXOPT_LOOKUP_STR(recoveryroot, config->args, "-recoveryroot", false, false);
     107    PXOPT_LOOKUP_BOOL(re_place, config->args, "-replace", false);
     108    PXOPT_LOOKUP_STR(set_label, config->args, "-set_label", false, false);
     109    PXOPT_LOOKUP_BOOL(rerun, config->args, "-rerun", false);
     110    PXOPT_LOOKUP_BOOL(dry_run, config->args, "-dry_run", false);
    106111    PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
    107 
    108     // Create temporary table of the best diffs
    109     {
    110         psString query = pxDataGet("magictool_definebyquery_temp_create.sql");
    111         if (!query) {
    112             psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
    113             return false;
    114         }
    115 
    116         if (!p_psDBRunQuery(config->dbh, query)) {
     112    PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false);
     113
     114    // search args
     115    psMetadata *where = psMetadataAlloc();
     116    PXOPT_COPY_S64(config->args, where, "-exp_id",  "exp_id", "==");
     117    PXOPT_COPY_S64(config->args, where, "-chip_id", "chip_id", "==");
     118    PXOPT_COPY_S64(config->args, where, "-cam_id",  "cam_id", "==");
     119    PXOPT_COPY_S64(config->args, where, "-warp_id", "warp_id", "==");
     120    PXOPT_COPY_S64(config->args, where, "-diff_id", "diff_id", "==");
     121    PXOPT_COPY_S64(config->args, where, "-magic_id","magic_id", "==");
     122    PXOPT_COPY_S32(config->args, where, "-streaks_max","streaks", "<=");
     123
     124    pxAddLabelSearchArgs (config, where, "-label", "magicRun.label", "==");
     125
     126    ippStage stageNum = ippStringToStage(stage);
     127   
     128    psString query = NULL;
     129    switch (stageNum) {
     130    case IPP_STAGE_RAW:
     131        query = pxDataGet("magicdstool_definebyquery_raw.sql");
     132        break;
     133    case IPP_STAGE_CHIP:
     134        query = pxDataGet("magicdstool_definebyquery_chip.sql");
     135        break;
     136    case IPP_STAGE_CAMERA:
     137        query = pxDataGet("magicdstool_definebyquery_camera.sql");
     138        break;
     139    case IPP_STAGE_WARP:
     140        query = pxDataGet("magicdstool_definebyquery_warp.sql");
     141        break;
     142    case IPP_STAGE_DIFF:
     143        query = pxDataGet("magicdstool_definebyquery_diff.sql");
     144        break;
     145    case IPP_STAGE_FAKE:
     146    case IPP_STAGE_STACK:
     147        psError(PXTOOLS_ERR_DATA, true, "%sRuns do not need to be destreaked", stage);
     148        return false;
     149    case IPP_STAGE_NONE:
     150        psError(PXTOOLS_ERR_DATA, true, "%s is not a valid stage", stage);
     151        return false;
     152    default:
     153        psError(PXTOOLS_ERR_PROG, true, "ippStageToString returned %d for invalid stage %s",
     154            stageNum, stage);
     155        return false;
     156    }
     157
     158    if (!query) {
     159        psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
     160        return false;
     161    }
     162    char *rerun_flag =  rerun ? "\n1 " : "\n0" ;
     163
     164    if (stageNum != IPP_STAGE_DIFF) {
     165        if (psListLength(where->list)) {
     166            psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
     167            psStringAppend(&query, "\nAND %s", whereClause);
     168            psFree(whereClause);
     169        }
     170        psFree(where);
     171
     172        // treat limit == 0 as "no limit"
     173        if (limit) {
     174            psString limitString = psDBGenerateLimitSQL(limit);
     175            psStringAppend(&query, " %s", limitString);
     176            psFree(limitString);
     177        }
     178        if (!p_psDBRunQueryF(config->dbh, query, rerun_flag)) {
    117179            psError(PS_ERR_UNKNOWN, false, "database error");
    118             return false;
    119         }
    120         psFree(query);
    121     }
    122 
    123     // Insert list of best diffs into temporary table
    124     {
    125         psString query = pxDataGet("magictool_definebyquery_temp_insert.sql");
    126         if (!query) {
    127             psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
    128             return false;
    129         }
    130 
    131         psMetadata *where = psMetadataAlloc();
    132         PXOPT_COPY_S64(config->args, where, "-exp_id", "exp_id", "==");
    133         PXOPT_COPY_STR(config->args, where, "-diff_label", "diffRun.label", "==");
    134         PXOPT_COPY_F32(config->args, where, "-good_frac", "warpSkyfile.good_frac", ">=");
    135 
    136         psString whereClause = NULL;    // WHERE conditions
    137         if (psListLength(where->list)) {
    138             whereClause = psDBGenerateWhereConditionSQL(where, NULL);
    139             psStringPrepend(&whereClause, "\n AND ");
    140         }
    141         psFree(where);
    142 
    143         if (!p_psDBRunQueryF(config->dbh, query, whereClause)) {
    144             psError(PS_ERR_UNKNOWN, false, "database error");
    145             psFree(whereClause);
    146180            psFree(query);
    147181            return false;
    148182        }
    149         psFree(whereClause);
    150         psFree(query);
    151     }
    152 
    153     // Get list of exposures ready to magic
    154     {
    155         psString query = pxDataGet("magictool_definebyquery_select.sql");
    156         if (!query) {
    157             psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
    158             return false;
    159         }
    160 
    161         psString magicSkyCellNumsWhere = NULL;    // WHERE conditions for magicSkyCellNums
    162         {
    163             psMetadata *where = psMetadataAlloc();
    164             PXOPT_COPY_S64(config->args, where, "-exp_id", "exp_id", "==");
    165             PXOPT_COPY_STR(config->args, where, "-diff_label", "diffRun.label", "==");
    166             PXOPT_COPY_F32(config->args, where, "-good_frac", "warpSkyfile.good_frac", ">=");
    167 
    168             if (psListLength(where->list)) {
    169                 magicSkyCellNumsWhere = psDBGenerateWhereConditionSQL(where, NULL);
    170                 psStringPrepend(&magicSkyCellNumsWhere, "\n AND ");
    171             }
    172             psFree(where);
    173         }
    174 
    175         // "available" means only concern ourselves with exposures that have all diffs completed, unless we're
    176         // told to only take what's available.
    177         // "new" means we want a new run even if there's already a magic run defined
    178         PXOPT_LOOKUP_BOOL(available, config->args, "-available", false);
    179         PXOPT_LOOKUP_BOOL(new, config->args, "-new", false);
    180 
    181         psString queryWhere = NULL;     // WHERE conditions for entire query
    182         if (available) {
    183             psStringAppend(&queryWhere, " WHERE num_done = num_todo");
    184         }
    185         if (new) {
    186             const char *newWhere = " magic_id IS NULL"; // String to add
    187             if (queryWhere) {
    188                 psStringAppend(&queryWhere, " AND %s", newWhere);
    189             } else {
    190                 psStringAppend(&queryWhere, " WHERE %s", newWhere);
    191             }
    192         }
    193         if (queryWhere) {
    194             psStringAppend(&query, " %s", queryWhere);
    195             psFree(queryWhere);
    196         }
    197 
    198 
    199         if (!p_psDBRunQueryF(config->dbh, query, magicSkyCellNumsWhere ? magicSkyCellNumsWhere : "")) {
     183    } else {
     184        // diff stage query has two types bothways and !bothways
     185        // so we need to send the rerun flag and the where data twice
     186        psString whereString = psStringCopy("");
     187        if (psListLength(where->list)) {
     188            psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
     189            psStringAppend(&whereString, "\nAND %s", whereClause);
     190            psFree(whereClause);
     191        }
     192        psFree(where);
     193
     194        // treat limit == 0 as "no limit"
     195        if (limit) {
     196            psString limitString = psDBGenerateLimitSQL(limit);
     197            psStringAppend(&query, " %s", limitString);
     198            psFree(limitString);
     199        }
     200
     201        if (!p_psDBRunQueryF(config->dbh, query, rerun_flag, whereString, rerun_flag, whereString)) {
    200202            psError(PS_ERR_UNKNOWN, false, "database error");
    201             psFree(magicSkyCellNumsWhere);
     203            psFree(whereString);
    202204            psFree(query);
    203205            return false;
    204206        }
    205         psFree(magicSkyCellNumsWhere);
    206         psFree(query);
    207     }
     207        psFree(whereString);
     208    }
     209    psFree(query);
    208210
    209211    psArray *output = p_psDBFetchResult(config->dbh);
     
    227229    }
    228230
    229     // Parse the list of exposures ready to magic
    230 
    231     if (!psDBTransaction(config->dbh)) {
    232         psError(PS_ERR_UNKNOWN, false, "database error");
    233         return false;
    234     }
    235 
    236     psString insert = pxDataGet("magictool_definebyquery_insert.sql"); // Insert query
     231    // Parse the list of runs ready to be destreaked
     232
     233    if (!dry_run && !psDBTransaction(config->dbh)) {
     234        psError(PS_ERR_UNKNOWN, false, "database error");
     235        return false;
     236    }
    237237
    238238    psArray *list = psArrayAllocEmpty(16); // List of runs, to print
    239239    for (long i = 0; i < psArrayLength(output); i++) {
    240240        psMetadata *row = output->data[i]; // Row of interest
    241         psS64 exp_id = psMetadataLookupS64(NULL, row, "exp_id"); // Exposure identifier
     241        psS64 stage_id = psMetadataLookupS64(NULL, row, "stage_id");
     242        psS64 exp_id = psMetadataLookupS64(NULL, row, "exp_id");
     243        psS64 magic_id = psMetadataLookupS64(NULL, row, "magic_id");
     244        psS64 inv_magic_id = psMetadataLookupS64(NULL, row, "inv_magic_id");
     245        psS64 cam_id = psMetadataLookupS64(NULL, row, "cam_id");
     246        psString magicRunLabel = psMetadataLookupStr(NULL, row, "label");
     247        psString magicRunWorkdir = psMetadataLookupStr(NULL, row, "workdir");
     248       
     249        // if workdir is not supplied use the magicRun's
     250        if (!workdir) {
     251            workdir = magicRunWorkdir;
     252        }
     253
     254        psString outroot = NULL;
     255        // set outroot to workdir/exp_id/stage for example /somewhere/424242/chip
     256        psStringAppend(&outroot, "%s/%" PRId64 "/%s", workdir, exp_id, stage);
    242257
    243258        // create a new magicRun for this group
    244         magicRunRow *run = magicRunRowAlloc(0, exp_id, "new", workdir, "dirty", label, dvodb, registered, 0);
     259        magicDSRunRow *run = magicDSRunRowAlloc(
     260                0, // magic_ds_id
     261                magic_id,
     262                inv_magic_id,
     263                "new",
     264                stage,
     265                stage_id,
     266                cam_id,
     267                set_label ? set_label : magicRunLabel,
     268                outroot,
     269                recoveryroot,
     270                re_place,
     271                0); // remove
     272
     273        psFree(outroot);
    245274        if (!run) {
    246             psAbort("failed to alloc magicRun object");
    247         }
    248 
    249         if (!magicRunInsertObject(config->dbh, run)) {
    250             psError(PS_ERR_UNKNOWN, false, "database error");
    251             psFree(run);
    252             psFree(insert);
    253             psFree(output);
    254             psFree(list);
    255             if (!psDBRollback(config->dbh)) {
     275            psAbort("failed to alloc magicDSRun object");
     276        }
     277
     278        if (!dry_run) {
     279            if (!magicDSRunInsertObject(config->dbh, run)) {
    256280                psError(PS_ERR_UNKNOWN, false, "database error");
     281                psFree(run);
     282                psFree(output);
     283                psFree(list);
     284                if (!psDBRollback(config->dbh)) {
     285                    psError(PS_ERR_UNKNOWN, false, "database error");
     286                }
     287                return false;
    257288            }
    258             return false;
    259         }
    260 
    261         psS64 magic_id = psDBLastInsertID(config->dbh); // Assigned identifier
    262         run->magic_id = magic_id;
     289            psS64 magic_ds_id = psDBLastInsertID(config->dbh); // Assigned identifier
     290            run->magic_ds_id = magic_ds_id;
     291        }
    263292
    264293        psArrayAdd(list, list->n, run);
    265294        psFree(run);
    266 
    267         // Create a suitable insertion query for this run
    268         psString thisInsert = psStringCopy(insert);
    269         {
    270             psString idString = NULL;
    271             psStringAppend(&idString, "%" PRId64, magic_id);
    272             psStringSubstitute(&thisInsert, idString, "@MAGIC_ID@");
    273             psFree(idString);
    274         }
    275         {
    276             psString idString = NULL;
    277             psStringAppend(&idString, "%" PRId64, exp_id);
    278             psStringSubstitute(&thisInsert, idString, "@EXP_ID@");
    279             psFree(idString);
    280         }
    281 
    282         if (!p_psDBRunQueryF(config->dbh, thisInsert, magic_id, exp_id)) {
    283             psError(PS_ERR_UNKNOWN, false, "database error");
    284             psFree(thisInsert);
    285             psFree(insert);
    286             psFree(output);
    287             psFree(list);
    288             if (!psDBRollback(config->dbh)) {
    289                 psError(PS_ERR_UNKNOWN, false, "database error");
    290             }
    291             return false;
    292         }
    293         psFree(thisInsert);
    294     }
    295 
    296     if (!psDBCommit(config->dbh)) {
     295    }
     296
     297    if (!dry_run && !psDBCommit(config->dbh)) {
    297298        psError(PS_ERR_UNKNOWN, false, "database error");
    298299        return false;
     
    300301    psFree(output);
    301302
    302     if (!magicRunPrintObjects(stdout, list, !simple)) {
     303    if (!magicDSRunPrintObjects(stdout, list, !simple)) {
    303304        psError(PS_ERR_UNKNOWN, false, "failed to print object");
    304305        psFree(list);
     
    309310
    310311    return true;
    311 #endif // notyet
    312312}
    313313
     
    338338            0,          // ID
    339339            magic_id,
     340            0,          // inv_magic_id
    340341            "new",      // state
    341342            stage,
     
    398399    PXOPT_COPY_S64(config->args, where, "-magic_ds_id", "magic_ds_id", "==");
    399400    PXOPT_COPY_S64(config->args, where, "-magic_id", "magic_id", "==");
    400     PXOPT_COPY_STR(config->args, where, "-label", "label", "==");
     401    pxAddLabelSearchArgs (config, where, "-label", "label", "==");
     402    PXOPT_COPY_STR(config->args, where, "-stage", "stage", "==");
    401403
    402404    PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false);
     
    495497    psMetadata *row = output->data[0];
    496498
    497     psString stage = psMetadataLookupStr(NULL, row, "stage");
     499    psString stage= psMetadataLookupStr(NULL, row, "stage");
    498500    psS64 stage_id = psMetadataLookupS64(NULL, row, "stage_id");
    499501
     502    ippStage stageNum = ippStringToStage(stage);
    500503
    501504    // chose the appropriate query based on the stage
    502     if (!strcmp(stage, "raw")) {
    503         query = "UPDATE rawImfile SET magicked = 1 where exp_id = %" PRId64 " AND class_id = '%s'";
    504     } else if (!strcmp(stage, "chip")) {
    505         query = "UPDATE chipProcessedImfile SET magicked = 1 where chip_id = %" PRId64 " AND class_id = '%s'";
    506     } else if (!strcmp(stage, "warp")) {
    507         query = "UPDATE warpSkyfile SET magicked = 1 where warp_id = %" PRId64 " AND skycell_id = '%s'";
    508     } else if (!strcmp(stage, "diff")) {
    509         query = "UPDATE diffSkyfile SET magicked = 1 where diff_id = %" PRId64 " AND skycell_id = '%s'";
    510     } else {
     505    switch (stageNum) {
     506    case IPP_STAGE_RAW:
     507        query = "UPDATE rawImfile SET magicked = %" PRId64 " where exp_id = %" PRId64 " AND class_id = '%s'";
     508        break;
     509    case IPP_STAGE_CHIP:
     510        query = "UPDATE chipProcessedImfile SET magicked = %" PRId64 " where chip_id = %" PRId64 " AND class_id = '%s'";
     511        break;
     512    case IPP_STAGE_CAMERA:
     513        // no there is no magicked column in camProcessedExp so we have nothing to do
     514        psFree(output);
     515        return true;
     516    case IPP_STAGE_WARP:
     517        query = "UPDATE warpSkyfile SET magicked = %" PRId64 " where warp_id = %" PRId64 " AND skycell_id = '%s'";
     518        break;
     519    case IPP_STAGE_DIFF:
     520        query = "UPDATE diffSkyfile SET magicked = %" PRId64 " where diff_id = %" PRId64 " AND skycell_id = '%s'";
     521        break;
     522    default:
    511523        psError(PS_ERR_UNKNOWN, true, "unexpected value for stage: %s found", stage);
    512524        psFree(output);
    513525        return false;
    514526    }
    515     if (!p_psDBRunQueryF(config->dbh, query, stage_id, component)) {
     527
     528    if (!p_psDBRunQueryF(config->dbh, query, magic_ds_id, stage_id, component)) {
    516529        psError(PS_ERR_UNKNOWN, false, "database error");
    517530        return false;
     
    560573    psS64 stage_id = psMetadataLookupS64(NULL, row, "stage_id");
    561574
     575    ippStage stageNum = ippStringToStage(stage);
    562576
    563577    // chose the appropriate query based on the stage
    564     if (!strcmp(stage, "raw")) {
    565         query = "UPDATE rawExp SET magicked = 1 where exp_id = %" PRId64;
    566     } else if (!strcmp(stage, "chip")) {
    567         query = "UPDATE chipRun SET magicked = 1 where chip_id = %" PRId64;
    568     } else if (!strcmp(stage, "warp")) {
    569         query = "UPDATE warpRun SET magicked = 1 where warp_id = %" PRId64;
    570     } else if (!strcmp(stage, "diff")) {
    571         query = "UPDATE diffRun SET magicked = 1 where diff_id = %" PRId64;
    572     } else {
     578    switch (stageNum) {
     579    case IPP_STAGE_RAW:
     580        query = "UPDATE rawExp SET magicked = %" PRId64 " where exp_id = %" PRId64;
     581        break;
     582    case IPP_STAGE_CHIP:
     583        query = "UPDATE chipRun SET magicked = %" PRId64 " where chip_id = %" PRId64;
     584        break;
     585    case IPP_STAGE_CAMERA:
     586        query = "UPDATE camRun SET magicked = %" PRId64 " where cam_id = %" PRId64;
     587        break;
     588    case IPP_STAGE_WARP:
     589        query = "UPDATE warpRun SET magicked = %" PRId64 " where warp_id = %" PRId64;
     590        break;
     591    case IPP_STAGE_DIFF:
     592        query = "UPDATE diffRun SET magicked = %" PRId64 " where diff_id = %" PRId64;
     593        break;
     594    default:
    573595        psError(PS_ERR_UNKNOWN, true, "unexpected value for stage: %s found", stage);
    574596        psFree(output);
    575597        return false;
    576598    }
    577     if (!p_psDBRunQueryF(config->dbh, query, stage_id)) {
     599    if (!p_psDBRunQueryF(config->dbh, query, magic_ds_id, stage_id)) {
    578600        psError(PS_ERR_UNKNOWN, false, "database error");
    579601        return false;
     
    625647    }
    626648
    627     if (!magicDSFileInsert(config->dbh, magic_ds_id, component, backup_path_base, recovery_path_base, fault)) {
     649    if (!magicDSFileInsert(config->dbh, magic_ds_id, component, backup_path_base, recovery_path_base, fault, "full")) {
    628650            // rollback
    629651        if (!psDBRollback(config->dbh)) {
     
    658680    PS_ASSERT_PTR_NON_NULL(cam_id, false);
    659681
    660     if (!strcmp(stage, "diff")) {
    661         // don't need these ids for diff stage because diff_id is in the magicRun
    662         *stage_id = 0;
    663         *cam_id = 0;
    664         return true;
    665     }
    666 
    667     int stageNum;
    668     if (!strcmp(stage, "raw")) {
    669         stageNum = 0;
    670     } else if (!strcmp(stage, "chip")) {
    671         stageNum = 1;
    672     } else if (!strcmp(stage, "warp")) {
    673         stageNum = 2;
    674     } else {
    675         psError(PXTOOLS_ERR_DATA, true, "%s is not a valid value for stage", stage);
    676         return false;
    677     }
    678 
     682    int stageNum = ippStringToStage(stage);;
     683    if (stageNum == IPP_STAGE_NONE) {
     684        psError(PXTOOLS_ERR_DATA, false, "%s is not a valid value for stage", stage);
     685        return false;
     686    }
    679687
    680688    psString query = pxDataGet("magicdstool_getrunids.sql");
     
    709717
    710718    *cam_id = psMetadataLookupS64(NULL, row, "cam_id");
    711     if (stageNum == 0) {
     719    switch (stageNum) {
     720    case IPP_STAGE_RAW:
    712721        *stage_id = psMetadataLookupS64(NULL, row, "exp_id");
    713     } else if (stageNum == 1) {
     722        break;
     723    case IPP_STAGE_CHIP:
    714724        *stage_id = psMetadataLookupS64(NULL, row, "chip_id");
    715     } else if (stageNum == 2) {
     725        break;
     726    case IPP_STAGE_CAMERA:
     727        *stage_id = *cam_id;
     728        return true;
     729    case IPP_STAGE_WARP:
    716730        *stage_id = psMetadataLookupS64(NULL, row, "warp_id");
     731        break;
     732    case IPP_STAGE_DIFF:
     733        *stage_id = psMetadataLookupS64(NULL, row, "diff_id");
     734        break;
    717735    }
    718736
     
    779797
    780798    psMetadata *where = psMetadataAlloc();
    781     PXOPT_COPY_S64(config->args, where, "-magic_ds_id", "magic_ds_id", "==");
     799    PXOPT_COPY_S64(config->args, where, "-magic_ds_id", "magicDSRun.magic_ds_id", "==");
    782800    PXOPT_COPY_STR(config->args, where, "-component", "component", "==");
    783801    PXOPT_COPY_S16(config->args, where, "-fault", "fault", "==");
    784 
    785     psString query = psStringCopy("DELETE FROM magicDSFile WHERE fault != 0");
     802    pxAddLabelSearchArgs (config, where, "-label", "label", "==");
     803
     804    psString query = psStringCopy("DELETE FROM magicDSFile USING magicDSFile, magicDSRun  WHERE (magicDSRun.magic_ds_id = magicDSFile.magic_ds_id) AND magicDSFile.fault != 0");
    786805
    787806    if (psListLength(where->list)) {
     
    889908}
    890909
     910static bool toremoveMode(pxConfig *config)
     911{
     912    PS_ASSERT_PTR_NON_NULL(config, false);
     913
     914    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);
     920    PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
     921
     922    // look for "inputs" that need to processed
     923    psString query = pxDataGet("magicdstool_toremove.sql");
     924    if (!query) {
     925        psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
     926        return false;
     927    }
     928
     929    if (psListLength(where->list)) {
     930        psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
     931        psStringAppend(&query, " WHERE %s", whereClause);
     932        psFree(whereClause);
     933    }
     934    psFree(where);
     935
     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)) {
     944        psError(PS_ERR_UNKNOWN, false, "database error");
     945        psFree(query);
     946        return false;
     947    }
     948    psFree(query);
     949
     950    psArray *output = p_psDBFetchResult(config->dbh);
     951    if (!output) {
     952        psErrorCode err = psErrorCodeLast();
     953        switch (err) {
     954            case PS_ERR_DB_CLIENT:
     955                psError(PXTOOLS_ERR_SYS, false, "database error");
     956            case PS_ERR_DB_SERVER:
     957                psError(PXTOOLS_ERR_PROG, false, "database error");
     958            default:
     959                psError(PXTOOLS_ERR_PROG, false, "unknown error");
     960        }
     961
     962        return false;
     963    }
     964    if (!psArrayLength(output)) {
     965        psTrace("magicdstool", PS_LOG_INFO, "no rows found");
     966        psFree(output);
     967        return true;
     968    }
     969
     970    if (psArrayLength(output)) {
     971        // negative simple so the default is true
     972        if (!ippdbPrintMetadatas(stdout, output, "toremove", !simple)) {
     973            psError(PS_ERR_UNKNOWN, false, "failed to print array");
     974            psFree(output);
     975            return false;
     976        }
     977    }
     978
     979    psFree(output);
     980
     981    return true;
     982}
     983static bool torestoreMode(pxConfig *config)
     984{
     985    PS_ASSERT_PTR_NON_NULL(config, false);
     986
     987    psMetadata *where = psMetadataAlloc();
     988    PXOPT_COPY_S64(config->args, where, "-magic_ds_id", "magic_ds_id", "==");
     989    PXOPT_COPY_S64(config->args, where, "-magic_id", "magic_id", "==");
     990    pxAddLabelSearchArgs (config, where, "-label", "label", "==");
     991
     992    PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false);
     993    PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
     994
     995    // look for "inputs" that need to processed
     996    psString query = pxDataGet("magicdstool_torestore.sql");
     997    if (!query) {
     998        psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
     999        return false;
     1000    }
     1001
     1002    if (psListLength(where->list)) {
     1003        psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
     1004        psStringAppend(&query, " WHERE %s", whereClause);
     1005        psFree(whereClause);
     1006    }
     1007    psFree(where);
     1008
     1009    // treat limit == 0 as "no limit"
     1010    if (limit) {
     1011        psString limitString = psDBGenerateLimitSQL(limit);
     1012        psStringAppend(&query, " %s", limitString);
     1013        psFree(limitString);
     1014    }
     1015
     1016    if (!p_psDBRunQuery(config->dbh, query)) {
     1017        psError(PS_ERR_UNKNOWN, false, "database error");
     1018        psFree(query);
     1019        return false;
     1020    }
     1021    psFree(query);
     1022
     1023    psArray *output = p_psDBFetchResult(config->dbh);
     1024    if (!output) {
     1025        psErrorCode err = psErrorCodeLast();
     1026        switch (err) {
     1027            case PS_ERR_DB_CLIENT:
     1028                psError(PXTOOLS_ERR_SYS, false, "database error");
     1029            case PS_ERR_DB_SERVER:
     1030                psError(PXTOOLS_ERR_PROG, false, "database error");
     1031            default:
     1032                psError(PXTOOLS_ERR_PROG, false, "unknown error");
     1033        }
     1034
     1035        return false;
     1036    }
     1037    if (!psArrayLength(output)) {
     1038        psTrace("magicdstool", PS_LOG_INFO, "no rows found");
     1039        psFree(output);
     1040        return true;
     1041    }
     1042
     1043    if (psArrayLength(output)) {
     1044        // negative simple so the default is true
     1045        if (!ippdbPrintMetadatas(stdout, output, "torestore", !simple)) {
     1046            psError(PS_ERR_UNKNOWN, false, "failed to print array");
     1047            psFree(output);
     1048            return false;
     1049        }
     1050    }
     1051
     1052    psFree(output);
     1053
     1054    return true;
     1055}
     1056
     1057
     1058static bool torevertMode(pxConfig *config)
     1059{
     1060    PS_ASSERT_PTR_NON_NULL(config, false);
     1061
     1062    psMetadata *where = psMetadataAlloc();
     1063    PXOPT_LOOKUP_STR(stage, config->args, "-stage", true, false);
     1064
     1065    PXOPT_COPY_S64(config->args, where, "-magic_ds_id", "magic_ds_id", "==");
     1066    PXOPT_COPY_S64(config->args, where, "-magic_id", "magic_id", "==");
     1067    pxAddLabelSearchArgs (config, where, "-label", "magicDSRun.label", "==");
     1068
     1069    PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false);
     1070    PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
     1071
     1072    psString sql_file = NULL;
     1073    psStringAppend(&sql_file, "magicdstool_torevert_%s.sql", stage);
     1074
     1075    psString query = pxDataGet(sql_file);
     1076    if (!query) {
     1077        psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement from %s", sql_file);
     1078        psFree(sql_file);
     1079        return false;
     1080    }
     1081    psFree(sql_file);
     1082
     1083    if (psListLength(where->list)) {
     1084        psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
     1085        psStringAppend(&query, " AND %s", whereClause);
     1086        psFree(whereClause);
     1087    }
     1088    psFree(where);
     1089
     1090    // treat limit == 0 as "no limit"
     1091    if (limit) {
     1092        psString limitString = psDBGenerateLimitSQL(limit);
     1093        psStringAppend(&query, " %s", limitString);
     1094        psFree(limitString);
     1095    }
     1096
     1097    if (!p_psDBRunQuery(config->dbh, query)) {
     1098        psError(PS_ERR_UNKNOWN, false, "database error");
     1099        psFree(query);
     1100        return false;
     1101    }
     1102    psFree(query);
     1103
     1104    psArray *output = p_psDBFetchResult(config->dbh);
     1105    if (!output) {
     1106        psErrorCode err = psErrorCodeLast();
     1107        switch (err) {
     1108            case PS_ERR_DB_CLIENT:
     1109                psError(PXTOOLS_ERR_SYS, false, "database error");
     1110            case PS_ERR_DB_SERVER:
     1111                psError(PXTOOLS_ERR_PROG, false, "database error");
     1112            default:
     1113                psError(PXTOOLS_ERR_PROG, false, "unknown error");
     1114        }
     1115
     1116        return false;
     1117    }
     1118    if (!psArrayLength(output)) {
     1119        psTrace("magicdstool", PS_LOG_INFO, "no rows found");
     1120        psFree(output);
     1121        return true;
     1122    }
     1123
     1124    if (psArrayLength(output)) {
     1125        // negative simple so the default is true
     1126        if (!ippdbPrintMetadatas(stdout, output, "torevert", !simple)) {
     1127            psError(PS_ERR_UNKNOWN, false, "failed to print array");
     1128            psFree(output);
     1129            return false;
     1130        }
     1131    }
     1132
     1133    psFree(output);
     1134
     1135    return true;
     1136}
     1137
     1138
Note: See TracChangeset for help on using the changeset viewer.