IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
May 3, 2010, 8:45:22 AM (16 years ago)
Author:
eugene
Message:

updates from trunk

Location:
branches/simmosaic_branches
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/simmosaic_branches

  • branches/simmosaic_branches/ippTools/src

    • Property svn:ignore
      •  

        old new  
         1*.la
         2*.lo
        13.deps
        24.gdb_history
         
        46Makefile
        57Makefile.in
         8addtool
         9caltool
         10camtool
         11chiptool
        612config.h
        713config.h.in
        8 stamp-h1
        9 *.la
        10 *.lo
         14detselect
         15dettool
         16difftool
         17disttool
         18dqstatstool
         19faketool
         20flatcorr
         21guidetool
         22magicdstool
         23magictool
         24pstamptool
         25pubtool
         26pxadmin
         27pxdata.c
         28pxinject
        1129pxtoolsErrorCodes.c
        1230pxtoolsErrorCodes.h
        13 pxadmin
        14 pxinject
        15 pztool
        1631pzgetexp
        1732pzgetimfiles
         33pztool
         34receivetool
        1835regtool
        19 guidetool
        20 chiptool
        21 camtool
         36stacktool
         37stamp-h1
        2238warptool
        23 difftool
        24 stacktool
        25 faketool
        26 dettool
        27 detselect
        28 pxdata.c
        29 magictool
        30 magicdstool
        31 caltool
        32 flatcorr
        33 pstamptool
        34 disttool
        35 receivetool
        36 
        37 pubtool
  • branches/simmosaic_branches/ippTools/src/disttool.c

    r24731 r27839  
    22 * disttool.c
    33 *
    4  * Copyright (C) 2008
     4 * Copyright (C) 2008-2009
    55 *
    66 * This program is free software; you can redistribute it and/or modify it
     
    3232
    3333static bool definebyqueryMode(pxConfig *config);
    34 static bool definerunMode(pxConfig *config);
    3534static bool updaterunMode(pxConfig *config);
    3635static bool revertrunMode(pxConfig *config);
    3736static bool pendingcomponentMode(pxConfig *config);
    3837static bool addprocessedcomponentMode(pxConfig *config);
     38static bool revertcomponentMode(pxConfig *config);
    3939static bool processedcomponentMode(pxConfig *config);
    4040static bool toadvanceMode(pxConfig *config);
     
    4242static bool addfilesetMode(pxConfig *config);
    4343static bool revertfilesetMode(pxConfig *config);
     44static bool updatefilesetMode(pxConfig *config);
    4445static bool queuercrunMode(pxConfig *config);
    4546static bool updatercrunMode(pxConfig *config);
    4647static bool revertrcrunMode(pxConfig *config);
    4748static bool pendingdestMode(pxConfig *config);
     49static bool pendingcleanupMode(pxConfig *config);
     50static bool listfilesetsMode(pxConfig *config);
    4851
    4952static bool definetargetMode(pxConfig *config);
    5053static bool updatetargetMode(pxConfig *config);
    51 static bool listtargetMode(pxConfig *config);
    52 
    53 static bool definedsproductMode(pxConfig *config);
    54 static bool updatedsproductMode(pxConfig *config);
     54static bool listtargetsMode(pxConfig *config);
    5555
    5656static bool definedestinationMode(pxConfig *config);
     
    5959static bool defineinterestMode(pxConfig *config);
    6060static bool updateinterestMode(pxConfig *config);
     61static bool listinterestsMode(pxConfig *config);
    6162
    6263# define MODECASE(caseName, func) \
     
    8081    switch (config->mode) {
    8182        MODECASE(DISTTOOL_MODE_DEFINEBYQUERY, definebyqueryMode);
    82         MODECASE(DISTTOOL_MODE_DEFINERUN, definerunMode);
    8383        MODECASE(DISTTOOL_MODE_UPDATERUN, updaterunMode);
    8484        MODECASE(DISTTOOL_MODE_REVERTRUN, revertrunMode);
     
    8686        MODECASE(DISTTOOL_MODE_ADDPROCESSEDCOMPONENT, addprocessedcomponentMode);
    8787        MODECASE(DISTTOOL_MODE_PROCESSEDCOMPONENT, processedcomponentMode);
     88        MODECASE(DISTTOOL_MODE_REVERTCOMPONENT, revertcomponentMode);
    8889        MODECASE(DISTTOOL_MODE_TOADVANCE, toadvanceMode);
    8990        MODECASE(DISTTOOL_MODE_PENDINGFILESET, pendingfilesetMode);
     91        MODECASE(DISTTOOL_MODE_PENDINGCLEANUP, pendingcleanupMode);
    9092        MODECASE(DISTTOOL_MODE_ADDFILESET, addfilesetMode);
    9193        MODECASE(DISTTOOL_MODE_REVERTFILESET, revertfilesetMode);
     94        MODECASE(DISTTOOL_MODE_LISTFILESETS, listfilesetsMode);
     95        MODECASE(DISTTOOL_MODE_UPDATEFILESET, updatefilesetMode);
    9296        MODECASE(DISTTOOL_MODE_QUEUERCRUN, queuercrunMode);
    9397        MODECASE(DISTTOOL_MODE_UPDATERCRUN, updatercrunMode);
     
    96100        MODECASE(DISTTOOL_MODE_DEFINETARGET, definetargetMode);
    97101        MODECASE(DISTTOOL_MODE_UPDATETARGET, updatetargetMode);
    98         MODECASE(DISTTOOL_MODE_LISTTARGET, listtargetMode);
    99         MODECASE(DISTTOOL_MODE_DEFINEDSPRODUCT, definedsproductMode);
    100         MODECASE(DISTTOOL_MODE_UPDATEDSPRODUCT, updatedsproductMode);
     102        MODECASE(DISTTOOL_MODE_LISTTARGETS, listtargetsMode);
    101103        MODECASE(DISTTOOL_MODE_DEFINEDESTINATION, definedestinationMode);
    102104        MODECASE(DISTTOOL_MODE_UPDATEDESTINATION, updatedestinationMode);
    103105        MODECASE(DISTTOOL_MODE_DEFINEINTEREST, defineinterestMode);
    104106        MODECASE(DISTTOOL_MODE_UPDATEINTEREST, updateinterestMode);
     107        MODECASE(DISTTOOL_MODE_LISTINTERESTS, listinterestsMode);
    105108        default:
    106109            psAbort("invalid option (this should not happen)");
     
    124127}
    125128
    126 static bool definerunMode(pxConfig *config)
    127 {
    128     PS_ASSERT_PTR_NON_NULL(config, false);
    129 
    130     // required
    131     PXOPT_LOOKUP_STR(stage, config->args, "-stage", true, false);
    132     PXOPT_LOOKUP_S64(stage_id, config->args, "-stage_id",  true, false);
    133     PXOPT_LOOKUP_STR(outroot, config->args, "-outroot", true, false);
    134     PXOPT_LOOKUP_S64(target_id, config->args, "-target_id",  true, false);
    135 
    136     // optional
    137     PXOPT_LOOKUP_BOOL(clean, config->args, "-clean", false);
    138     PXOPT_LOOKUP_BOOL(no_magic, config->args, "-no_magic", false);
    139     PXOPT_LOOKUP_STR(set_label, config->args, "-set_label", false, false);
    140 
    141     // TODO: check that stage has an expected value
    142     // XXX: all of the following concerns will be managed properly by definebyquery
    143 
    144     // TODO: should we check that stage_id actually exists for stage
    145     // in magicdstool we queue off of a magic_id so the stage_id, exp_id, and cam_id get looked up
    146     // when the run is queued
    147 
    148     if (!distRunInsert(config->dbh,
    149             0,          // dist_id
    150             target_id,
    151             stage,
    152             stage_id,
    153             set_label,
    154             outroot,
    155             clean,
    156             no_magic,
    157             "new",
    158             NULL,       // time_stamp
    159             0           // fault
    160             )) {
    161         psError(PS_ERR_UNKNOWN, false, "database error");
    162         return false;
    163     }
    164 
    165     return true;
    166 }
    167129
    168130static bool definebyqueryMode(pxConfig *config)
     
    175137
    176138    // optional
    177     PXOPT_LOOKUP_S64(magic_ds_id, config->args, "-magic_ds_id",  false, false);
     139    PXOPT_LOOKUP_BOOL(use_alternate, config->args, "-use_alternate", false);
    178140    PXOPT_LOOKUP_BOOL(no_magic, config->args, "-no_magic", false);
    179141    PXOPT_LOOKUP_STR(set_label, config->args, "-set_label", false, false);
     142    PXOPT_LOOKUP_STR(set_data_group, config->args, "-set_data_group", false, false);
     143    PXOPT_LOOKUP_STR(set_note, config->args, "-set_note", false, false);
     144
    180145    PXOPT_LOOKUP_S64(limit, config->args, "-limit", false, false);
    181146    PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
    182    
    183     PXOPT_LOOKUP_BOOL(dry_run, config->args, "-dry_run", false);
     147
     148    PXOPT_LOOKUP_BOOL(pretend, config->args, "-pretend", false);
     149
     150    if (use_alternate) {
     151        if (strcmp(stage, "raw")) {
     152            psError(PXTOOLS_ERR_SYS, true, "alternate inputs only supported for raw stage");
     153            return false;
     154        }
     155        if (no_magic) {
     156            psError(PXTOOLS_ERR_SYS, true, "no_magic forbidden with alternate inputs");
     157            return false;
     158        }
     159    }
    184160
    185161    // select arguments
     
    191167
    192168    PXOPT_LOOKUP_STR(label, config->args, "-label", false, false);
     169    PXOPT_LOOKUP_STR(dist_group, config->args, "-dist_group", false, false);
    193170
    194171    psString query = NULL;
     
    196173    psString runJoinStr = NULL;
    197174    if (!strcmp(stage, "raw")) {
    198         magicRunType = "rawExp";
    199         runJoinStr = "rawExp.exp_id";
     175        if (! use_alternate ) {
     176            magicRunType = "rawExp";
     177            runJoinStr = "rawExp.exp_id";
     178        } else {
     179            magicRunType = "camRun";
     180            runJoinStr = "camRun.exp_id";
     181        }
    200182        query = pxDataGet("disttool_definebyquery_raw.sql");
    201183        if (!query) {
    202             psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
     184            psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
    203185            psFree(where);
    204186            return false;
    205187        }
    206188
    207         // for raw stage we select by chipRun.label because raw labels typically aren't set
     189        // for raw stage we select by camRun.label and dist_group because rawExp doesn't have those columns
    208190        if (label) {
    209             psStringAppend(&query, " AND (chipRun.label = '%s')", label);
     191            psStringAppend(&query, " AND (magicDSRun.label = '%s')", label);
     192        }
     193        if (dist_group) {
     194            psStringAppend(&query, " AND (camRun.dist_group = '%s')", dist_group);
    210195        }
    211196    } else if (!strcmp(stage, "chip")) {
     
    214199        query = pxDataGet("disttool_definebyquery_chip.sql");
    215200        if (!query) {
    216             psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
     201            psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
    217202            psFree(where);
    218203            return false;
     
    222207            psStringAppend(&query, " AND (chipRun.label = '%s')", label);
    223208        }
     209        if (dist_group) {
     210            psStringAppend(&query, " AND (chipRun.dist_group = '%s')", dist_group);
     211        }
    224212    } else if (!strcmp(stage, "camera")) {
    225         magicRunType = "chipRun";    // This is used below to set the magicked business
     213        magicRunType = "camRun";    // This is used below to set the magicked business
     214        runJoinStr = "camRun.cam_id";
    226215        query = pxDataGet("disttool_definebyquery_camera.sql");
    227216        if (!query) {
    228             psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
     217            psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
    229218            psFree(where);
    230219            return false;
     
    233222        if (label) {
    234223            psStringAppend(&query, " AND (camRun.label = '%s')", label);
     224        }
     225        if (dist_group) {
     226            psStringAppend(&query, " AND (camRun.dist_group = '%s')", dist_group);
    235227        }
    236228    } else if (!strcmp(stage, "fake")) {
     
    238230        query = pxDataGet("disttool_definebyquery_fake.sql");
    239231        if (!query) {
    240             psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
     232            psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
    241233            psFree(where);
    242234            return false;
     
    245237        if (label) {
    246238            psStringAppend(&query, " AND (fakeRun.label = '%s')", label);
     239        }
     240        if (dist_group) {
     241            psStringAppend(&query, " AND (fakeRun.dist_group = '%s')", dist_group);
    247242        }
    248243        // fake stage doesn't require magic
     
    253248        query = pxDataGet("disttool_definebyquery_warp.sql");
    254249        if (!query) {
    255             psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
     250            psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
    256251            psFree(where);
    257252            return false;
     
    260255        if (label) {
    261256            psStringAppend(&query, " AND (warpRun.label = '%s')", label);
     257        }
     258        if (dist_group) {
     259            psStringAppend(&query, " AND (warpRun.dist_group = '%s')", dist_group);
    262260        }
    263261
     
    267265        query = pxDataGet("disttool_definebyquery_diff.sql");
    268266        if (!query) {
    269             psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
     267            psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
    270268            psFree(where);
    271269            return false;
     
    274272        if (label) {
    275273            psStringAppend(&query, " AND (diffRun.label = '%s')", label);
     274        }
     275        if (dist_group) {
     276            psStringAppend(&query, " AND (diffRun.dist_group = '%s')", dist_group);
    276277        }
    277278
     
    280281        query = pxDataGet("disttool_definebyquery_stack.sql");
    281282        if (!query) {
    282             psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
     283            psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
    283284            psFree(where);
    284285            return false;
     
    288289            psStringAppend(&query, " AND (stackRun.label = '%s')", label);
    289290        }
    290         // stack stage doesn't require magic (perhaps let the script do this?
     291        if (dist_group) {
     292            psStringAppend(&query, " AND (stackRun.dist_group = '%s')", dist_group);
     293        }
     294        // stack stage doesn't require magic
    291295        no_magic = true;
     296    } else if (!strcmp(stage, "SSdiff")) {
     297      magicRunType = "diffRun";
     298      runJoinStr = "diffRun.diff_id";
     299      query = pxDataGet("disttool_definebyquery_SSdiff.sql");
     300      if (!query) {
     301        psError(PXTOOLS_ERR_SYS, false, "failed to retrieve SQL statement");
     302        psFree(where);
     303        return(false);
     304      }
     305
     306      if (label) {
     307        psStringAppend(&query, " AND (diffRun.label = '%s') ", label);
     308      }
     309      if (dist_group) {
     310        psStringAppend(&query, " AND (diffRun.dist_group = '%s') ", dist_group);
     311      }
     312
     313      no_magic = true;   
    292314    } else {
    293315        psError(PS_ERR_UNKNOWN, true, "unknown value for stage: %s", stage);
    294316        psFree(where);
    295317        return false;
     318    }
     319
     320    if (!strcmp(stage, "raw")) {
     321        if (!no_magic) {
     322            psStringAppend(&query, " AND (magicDSRun.re_place = %d)", !use_alternate);
     323            psStringAppend(&query, " AND (camRun.state = 'full')");
     324        }
    296325    }
    297326
     
    306335
    307336    if (!no_magic) {
    308         psStringAppend(&query, " AND (distTarget.clean OR %s.magicked)", magicRunType);
    309 
    310         // is selecting by magic_ds_id really interesting?
    311         if (magic_ds_id) {
    312             if (strcmp(stage, "camera")) {
    313                 // stage other than camera
    314                 if (!runJoinStr) {
    315                     psError(PS_ERR_PROGRAMMING, true, "cannot select by magic_ds_id for stage: %s", stage);
    316                     psFree(query);
    317                     return false;
    318                 }
    319                 psStringAppend(&joinHook, "\nJOIN magicDSRun ON magicDSRun.stage = distTarget.stage"
    320                                               " AND magicDSRun.stage_id = %s", runJoinStr);
    321             } else {
    322                 // camera masks are magicked when the chipRun is magicked
    323                 // XXX: This is confusing. Is it dangerous?
    324                 // Maybe I should add a magicked bit to camRun. Note this isn't
    325                 psStringAppend(&joinHook, "\nJOIN magicDSRun ON magicDSRun.stage = 'chip'"
    326                                               " AND magicDSRun.stage_id = chipRun.chip_id");
    327             }
    328             psStringAppend(&query, " AND (magicDSRun.state = 'full' AND magicDSRun.re_place AND (magic_ds_id = %" PRId64 "))", magic_ds_id);
    329         }
     337        psStringAppend(&query, " AND (%s.magicked > 0)", magicRunType);
    330338    }
    331339
     
    335343        psFree(limitString);
    336344    }
    337 
     345    psTrace("disttool",2,query,joinHook ? joinHook : "");
    338346    if (!p_psDBRunQueryF(config->dbh, query, joinHook ? joinHook : "")) {
    339         psError(PS_ERR_UNKNOWN, false, "database error");
    340         psFree(query);
    341         return false;
     347      psError(PS_ERR_UNKNOWN, false, "database error");
     348      psFree(query);
     349      return false;
    342350    }
    343351    psFree(query);
     
    356364
    357365
    358     if (dry_run) {
     366    if (pretend) {
    359367        if (!ippdbPrintMetadatas(stdout, output, "newdistRuns", true)) {
    360368            psError(PS_ERR_UNKNOWN, false, "failed to print array");
     
    377385        psString run_tag = psMetadataLookupStr(NULL, md, "run_tag");
    378386        psS64 stage_id = psMetadataLookupS64(NULL, md, "stage_id");
     387        psS64 magic_ds_id = psMetadataLookupS64(NULL, md, "magicked");
    379388        psS64 target_id = psMetadataLookupS64(NULL, md, "target_id");
    380         psString target_label = psMetadataLookupStr(NULL, md, "label");
     389        psString stage_label = psMetadataLookupStr(NULL, md, "label");
     390        psString stage_data_group = psMetadataLookupStr(NULL, md, "data_group");
    381391        bool clean = psMetadataLookupBool(NULL, md, "clean");
    382392
    383393        psString outroot = NULL;
    384         psStringAppend(&outroot, "%s/%s/%s", workdir, run_tag, stage);
    385 
    386         if (set_label == NULL) {
    387             set_label = target_label;
    388         }
     394        psStringAppend(&outroot, "%s/%s/%s", workdir, run_tag, stage);
     395
     396        psString new_label;
     397        if (set_label != NULL) {
     398            new_label = set_label;
     399        } else {
     400            new_label = stage_label;
     401        }
     402        psString new_data_group;
     403        if (set_data_group != NULL) {
     404            new_data_group = set_data_group;
     405        } else {
     406            new_data_group = stage_data_group;
     407        }
     408
    389409        distRunRow *row = distRunRowAlloc(
    390410                0,      // dist_id
     
    392412                stage,
    393413                stage_id,
    394                 set_label,
     414                magic_ds_id,
     415                new_label,
    395416                outroot,
     417                NULL,     // outdir
    396418                clean,
    397419                no_magic,
     420                use_alternate,
    398421                "new",
    399                 NULL,   // time_stamp
    400                 0       // fault
     422                NULL,    // time_stamp
     423                0,       // fault
     424                new_data_group,
     425                set_note // note does not propagate
    401426                );
    402427
     
    441466    PXOPT_COPY_S64(config->args, where, "-dist_id", "dist_id", "==");
    442467    PXOPT_COPY_STR(config->args, where, "-stage", "stage", "==");;
    443     PXOPT_COPY_STR(config->args, where, "-state", "state", "==");
     468    PXOPT_COPY_STR(config->args, where, "-state", "distRun.state", "==");
    444469    PXOPT_COPY_STR(config->args, where, "-label", "label", "==");
     470    PXOPT_COPY_STR(config->args, where, "-data_group", "distRun.data_group", "LIKE");
     471    PXOPT_COPY_STR(config->args, where, "-dist_group", "distTarget.dist_group", "==");
     472    PXOPT_COPY_STR(config->args, where, "-label", "label", "==");
    445473
    446474    if (!psListLength(where->list)) {
    447475        psFree(where);
    448         psError(PXTOOLS_ERR_DATA, false, "search parameters are required");
     476        psError(PXTOOLS_ERR_CONFIG, false, "search parameters are required");
    449477        return false;
    450478    }
     
    452480    PXOPT_LOOKUP_STR(state, config->args, "-set_state", false, false);
    453481    PXOPT_LOOKUP_STR(label, config->args, "-set_label", false, false);
     482    PXOPT_LOOKUP_STR(data_group, config->args, "-set_data_group", false, false);
     483    PXOPT_LOOKUP_STR(outdir, config->args, "-set_outdir", false, false);
    454484    PXOPT_LOOKUP_S16(fault, config->args, "-fault", false, false);
    455 
    456     if ((!state) && (!label) && (!fault)) {
    457         psError(PXTOOLS_ERR_DATA, false, "parameters (-fault or -set_state or -set_label) are required");
    458         psFree(where);
    459         return false;
    460     }
    461 
    462     psString query = psStringCopy("UPDATE distRun SET time_stamp = UTC_TIMESTAMP()");
    463 
     485    PXOPT_LOOKUP_STR(set_note, config->args, "-set_note", false, false);
     486
     487    if ((!state) && (!label) && (!fault) &&(!data_group)) {
     488        psError(PXTOOLS_ERR_CONFIG, false, "parameters (-fault or -set_state or -set_label -set_data_group) are required");
     489        psFree(where);
     490        return false;
     491    }
     492
     493    psString query = psStringCopy("UPDATE distRun join distTarget using(target_id, stage) SET distRun.time_stamp = UTC_TIMESTAMP()");
     494
     495    if (outdir) {
     496        psStringAppend(&query, " , distRun.outdir = '%s'", outdir);
     497    }
    464498    if (state) {
    465         psStringAppend(&query, " , state = '%s'", state);
     499        psStringAppend(&query, " , distRun.state = '%s'", state);
    466500    }
    467501
    468502    if (label) {
    469         psStringAppend(&query, " , label = '%s'", label);
     503        psStringAppend(&query, " , distRun.label = '%s'", label);
    470504    }
    471505
    472506    if (fault) {
    473         psStringAppend(&query, " , fault = %d", fault);
    474     }
     507        psStringAppend(&query, " , distRun.fault = %d", fault);
     508    }
     509
     510    if (data_group) {
     511        psStringAppend(&query, " , distRun.data_group = '%s'", data_group);
     512    }
     513
     514    if (set_note) {
     515        psStringAppend(&query, " , distRun.note = '%s'", set_note);
     516    }
     517
    475518
    476519    psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
     
    497540    PXOPT_COPY_STR(config->args, where, "-label", "label", "==");
    498541
    499     // we need to disambiguate fault so make a copy of the where list before adding fault
    500     psMetadata *whereComponent = psMetadataCopy(NULL, where);
    501542    PXOPT_COPY_S16(config->args, where,  "-fault", "distRun.fault", "==");
    502     PXOPT_COPY_S16(config->args, whereComponent, "-fault", "distComponent.fault", "==");
    503543
    504544    // It might be useful to be able to query by the parameters of the underlying runs
     
    506546    if (!psListLength(where->list) && !psMetadataLookupBool(NULL, config->args, "-all")) {
    507547        psFree(where);
    508         psError(PXTOOLS_ERR_DATA, false, "search parameters are required");
    509         return false;
    510     }
    511 
    512     if (!psDBTransaction(config->dbh)) {
    513         psError(PS_ERR_UNKNOWN, false, "database error");
    514         psFree(where);
    515         return false;
    516     }
    517 
    518     // Update state to 'new'
    519     int numUpdated;                     // Number updated
    520     {
    521         psString query = pxDataGet("disttool_revertrun_update.sql");
    522         if (!query) {
    523             psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
    524             if (!psDBRollback(config->dbh)) {
    525                 psError(PS_ERR_UNKNOWN, false, "database error");
    526             }
    527             return false;
    528         }
    529 
    530         if (psListLength(where->list)) {
    531             psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
    532             psStringAppend(&query, " AND %s", whereClause);
    533             psFree(whereClause);
    534         }
    535 
    536         if (!p_psDBRunQuery(config->dbh, query)) {
     548        psError(PXTOOLS_ERR_CONFIG, false, "search parameters are required");
     549        return false;
     550    }
     551
     552    psString query = pxDataGet("disttool_revertrun.sql");
     553    if (!query) {
     554        psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
     555        if (!psDBRollback(config->dbh)) {
    537556            psError(PS_ERR_UNKNOWN, false, "database error");
    538             psFree(query);
    539             if (!psDBRollback(config->dbh)) {
    540                 psError(PS_ERR_UNKNOWN, false, "database error");
    541             }
    542             return false;
    543         }
    544         psFree(query);
    545 
    546         numUpdated = psDBAffectedRows(config->dbh);
    547     }
     557        }
     558        return false;
     559    }
     560
     561    if (psListLength(where->list)) {
     562        psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
     563        psStringAppend(&query, " AND %s", whereClause);
     564        psFree(whereClause);
     565    }
     566    psFree(where);
     567
     568    if (!p_psDBRunQuery(config->dbh, query)) {
     569        psError(PS_ERR_UNKNOWN, false, "database error");
     570        psFree(query);
     571        return false;
     572    }
     573
     574    int numUpdated = psDBAffectedRows(config->dbh);
    548575
    549576    psLogMsg("disttool", PS_LOG_INFO, "Updated %d dist runs", numUpdated);
    550577
    551     // Delete product
    552     int numDeleted;                     // Number deleted
    553     {
    554         psString query = pxDataGet("disttool_revertrun_delete.sql");
    555         if (!query) {
    556             psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
    557             if (!psDBRollback(config->dbh)) {
    558                 psError(PS_ERR_UNKNOWN, false, "database error");
    559             }
    560             return false;
    561         }
    562 
    563         if (psListLength(whereComponent->list)) {
    564             psString whereClause = psDBGenerateWhereConditionSQL(whereComponent, NULL);
    565             psStringAppend(&query, " AND %s", whereClause);
    566             psFree(whereClause);
    567         }
    568 
    569         if (!p_psDBRunQuery(config->dbh, query)) {
    570             psError(PS_ERR_UNKNOWN, false, "database error");
    571             psFree(query);
    572             if (!psDBRollback(config->dbh)) {
    573                 psError(PS_ERR_UNKNOWN, false, "database error");
    574             }
    575             return false;
    576         }
    577         psFree(query);
    578 
    579         numDeleted = psDBAffectedRows(config->dbh);
    580     }
     578    return true;
     579}
     580
     581static bool revertcomponentMode(pxConfig *config)
     582{
     583    psMetadata *where = psMetadataAlloc();
     584    PXOPT_COPY_S64(config->args, where, "-dist_id", "distRun.dist_id", "==");
     585    PXOPT_COPY_STR(config->args, where, "-stage", "stage", "==");;
     586    PXOPT_COPY_STR(config->args, where, "-component", "component", "==");;
     587    PXOPT_COPY_S64(config->args, where, "-stage_id", "stage_id", "==");
     588    PXOPT_COPY_STR(config->args, where, "-state", "state", "==");
     589    pxAddLabelSearchArgs(config, where, "-label", "distRun.label", "==");
     590
     591    PXOPT_COPY_S16(config->args, where,  "-fault", "distComponent.fault", "==");
     592
     593    // It might be useful to be able to query by the parameters of the underlying runs
     594
     595    if (!psListLength(where->list) && !psMetadataLookupBool(NULL, config->args, "-all")) {
     596        psFree(where);
     597        psError(PXTOOLS_ERR_CONFIG, false, "search parameters are required");
     598        return false;
     599    }
     600
     601    psString query = pxDataGet("disttool_revertcomponent.sql");
     602    if (!query) {
     603        psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
     604        psFree(where);
     605        return false;
     606    }
     607
     608    if (psListLength(where->list)) {
     609        psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
     610        psStringAppend(&query, " AND %s", whereClause);
     611        psFree(whereClause);
     612    }
     613    psFree(where);
     614
     615    if (!p_psDBRunQuery(config->dbh, query)) {
     616        psError(PS_ERR_UNKNOWN, false, "database error");
     617        psFree(query);
     618        return false;
     619    }
     620
     621    int numDeleted = psDBAffectedRows(config->dbh);
    581622
    582623    psLogMsg("disttool", PS_LOG_INFO, "Deleted %d distComponents", numDeleted);
    583624
    584     psFree(where);
    585     psFree(whereComponent);
    586 
    587     if (!psDBCommit(config->dbh)) {
    588         psError(PS_ERR_UNKNOWN, false, "database error");
    589         return false;
    590     }
    591 
    592625    return true;
    593626}
     
    597630    PS_ASSERT_PTR_NON_NULL(config, false);
    598631
    599     psMetadata *where = psMetadataAlloc();
    600     PXOPT_COPY_S64(config->args, where, "-dist_id", "dist_id", "==");
    601     PXOPT_COPY_STR(config->args, where, "-stage", "stage", "==");
    602     pxAddLabelSearchArgs (config, where, "-label", "label", "==");
     632    PXOPT_LOOKUP_STR(stage, config->args, "-stage", true, false);
     633
     634    psMetadata *where = psMetadataAlloc();
     635    PXOPT_COPY_S64(config->args, where, "-dist_id", "distRun.dist_id", "==");
     636    pxAddLabelSearchArgs (config, where, "-label", "distRun.label", "==");
    603637
    604638    PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false);
    605639    PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
    606640
    607     // look for "inputs" that need to processed
    608     psString query = pxDataGet("disttool_pendingcomponent.sql");
     641    psString queryFile = NULL;
     642    psStringAppend(&queryFile, "disttool_pending_%s.sql", stage);
     643    psString query = pxDataGet(queryFile);
    609644    if (!query) {
    610         psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
     645        psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement from %s", queryFile);
    611646        return false;
    612647    }
     
    614649    if (psListLength(where->list)) {
    615650        psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
    616         psStringAppend(&query, " WHERE %s", whereClause);
     651        psStringAppend(&query, " AND %s", whereClause);
    617652        psFree(whereClause);
    618653    }
     
    626661    }
    627662
    628     // the query has where hooks for each stage.
    629     // right now we aren't using them.
    630     // XXX: I think that I want to change the query from a union of selects on the various
    631     // stages to separate queries. As it is pending data at the later stages of the pipline
    632     // will get blocked by pending earlier stages
    633     psString    raw_where = "";
    634     psString    raw_clean_where = "";
    635     psString    chip_where = "";
    636     psString    camera_where = "";
    637     psString    fake_where = "";
    638     psString    warp_where = "";
    639     psString    diff_where = "";
    640     psString    stack_where = "";
    641 
    642     if (!p_psDBRunQueryF(config->dbh,
    643             query,
    644             raw_where,
    645             raw_clean_where,
    646             chip_where,
    647             camera_where,
    648             fake_where,
    649             warp_where,
    650             diff_where,
    651             stack_where)) {
     663    if (!p_psDBRunQuery(config->dbh, query)) {
    652664        psError(PS_ERR_UNKNOWN, false, "database error");
    653665        psFree(query);
     
    705717    PXOPT_LOOKUP_S32(bytes, config->args, "-bytes", require_fileinfo, false);
    706718    PXOPT_LOOKUP_STR(md5sum, config->args, "-md5sum", require_fileinfo, false);
     719    PXOPT_LOOKUP_STR(outdir, config->args, "-outdir", require_fileinfo, false);
    707720    PXOPT_LOOKUP_STR(name, config->args, "-name", require_fileinfo, false);
    708721
    709     if (!distComponentInsert(config->dbh, dist_id, component, bytes, md5sum, "full", name, fault)) {
     722    if (!distComponentInsert(config->dbh, dist_id, component, bytes, md5sum, "full", outdir, name, fault)) {
    710723        psError(PS_ERR_UNKNOWN, false, "database error");
    711724        return false;
     
    729742    psString query = pxDataGet("disttool_toadvance.sql");
    730743    if (!query) {
    731         psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
     744        psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
    732745        return false;
    733746    }
     
    800813    psString query = pxDataGet("disttool_processedcomponent.sql");
    801814    if (!query) {
    802         psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
     815        psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
    803816        return false;
    804817    }
     
    865878    psMetadata *where = psMetadataAlloc();
    866879    PXOPT_COPY_S64(config->args, where, "-dist_id", "dist_id", "==");
     880    PXOPT_COPY_STR(config->args, where, "-stage", "distRun.stage", "==");
    867881    pxAddLabelSearchArgs (config, where, "-label", "distRun.label", "==");
    868882
     
    873887    psString query = pxDataGet("disttool_pendingfileset.sql");
    874888    if (!query) {
    875         psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
     889        psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
    876890        return false;
    877891    }
     
    936950    // required values
    937951    PXOPT_LOOKUP_S64(dist_id, config->args, "-dist_id", true, false);
    938     PXOPT_LOOKUP_S64(prod_id, config->args, "-prod_id", true, false);
     952    PXOPT_LOOKUP_S64(dest_id, config->args, "-dest_id", true, false);
    939953
    940954    PXOPT_LOOKUP_S16(fault, config->args, "-fault", false, false);
     
    943957    PXOPT_LOOKUP_STR(name, config->args, "-name", fault == 0, false);
    944958
    945     if (!rcDSFilesetInsert(config->dbh, 
     959    if (!rcDSFilesetInsert(config->dbh,
    946960            0,          // fs_id
    947961            dist_id,
    948             prod_id,
     962            dest_id,
    949963            name,
    950964            "full",
     
    956970    return true;
    957971}
     972
    958973static bool revertfilesetMode(pxConfig *config)
    959974{
     
    961976    PXOPT_COPY_S64(config->args, where, "-fs_id", "fs_id", "==");
    962977    PXOPT_COPY_S64(config->args, where, "-dist_id", "rcDSFileset.dist_id", "==");
    963     PXOPT_COPY_S64(config->args, where, "-prod_id", "prod_id", "==");
     978    PXOPT_COPY_S64(config->args, where, "-dest_id", "dest_id", "==");
    964979    PXOPT_COPY_STR(config->args, where, "-stage", "stage", "==");;
    965980    PXOPT_COPY_S64(config->args, where, "-stage_id", "stage_id", "==");
     
    972987    if (!psListLength(where->list) && !psMetadataLookupBool(NULL, config->args, "-all")) {
    973988        psFree(where);
    974         psError(PXTOOLS_ERR_DATA, false, "search parameters are required");
     989        psError(PXTOOLS_ERR_CONFIG, false, "search parameters are required");
    975990        return false;
    976991    }
     
    978993    psString query = pxDataGet("disttool_revertfileset.sql");
    979994    if (!query) {
    980         psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
     995        psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
    981996        psFree(where);
    982997        return false;
     
    10201035    psString query = pxDataGet("disttool_pendingdest.sql");
    10211036    if (!query) {
    1022         psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
     1037        psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
    10231038        return false;
    10241039    }
     
    10891104    PXOPT_COPY_S64(config->args, where, "-dist_id",  "dist_id", "==");
    10901105    PXOPT_COPY_S64(config->args, where, "-dest_id",  "dest_id", "==");
    1091     PXOPT_COPY_S64(config->args, where, "-prod_id",  "prod_id", "==");
    10921106    PXOPT_COPY_S64(config->args, where, "-target_id","target_id", "==");
    10931107    PXOPT_COPY_S64(config->args, where, "-fs_id",    "fs_id", "==");
     
    11001114    psString query = pxDataGet("disttool_queuercrun.sql");
    11011115    if (!query) {
    1102         psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
     1116        psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
    11031117        return false;
    11041118    }
     
    11461160    // We either need rc_id or (dest_id and fs_name) to identifiy the rcRun
    11471161    if ((!rc_id) && !(dest_id && fs_name)) {
    1148         psError(PXTOOLS_ERR_DATA, true, "either -rc_id or (-fs_name and -dest_id) are required");
     1162        psError(PXTOOLS_ERR_CONFIG, true, "either -rc_id or (-fs_name and -dest_id) are required");
    11491163        return false;
    11501164    }
     
    11581172        // this can't happen because we checked above
    11591173        psFree(where);
    1160         psError(PXTOOLS_ERR_DATA, false, "search parameters are required");
     1174        psError(PXTOOLS_ERR_CONFIG, false, "search parameters are required");
    11611175        return false;
    11621176    }
     
    11681182
    11691183    if (!state && (fault < 0)) {
    1170         psError(PXTOOLS_ERR_DATA, false, "parameters (-fault or -set_state) are required");
     1184        psError(PXTOOLS_ERR_CONFIG, false, "parameters (-fault or -set_state) are required");
    11711185        psFree(where);
    11721186        return false;
     
    11751189    psString query = pxDataGet("disttool_updatercrun.sql");
    11761190    if (!query) {
    1177         psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
     1191        psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
    11781192        return false;
    11791193    }
     
    12321246    if (!psListLength(where->list) && !psMetadataLookupBool(NULL, config->args, "-all")) {
    12331247        psFree(where);
    1234         psError(PXTOOLS_ERR_DATA, false, "search parameters are required");
     1248        psError(PXTOOLS_ERR_CONFIG, false, "search parameters are required");
    12351249        return false;
    12361250    }
     
    12381252    psString query = pxDataGet("disttool_revertrcrun.sql");
    12391253    if (!query) {
    1240         psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
     1254        psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
    12411255        if (!psDBRollback(config->dbh)) {
    12421256            psError(PS_ERR_UNKNOWN, false, "database error");
     
    12731287
    12741288    // required
    1275     PXOPT_LOOKUP_STR(label, config->args, "-label", true, false);
     1289    PXOPT_LOOKUP_STR(dist_group, config->args, "-dist_group", true, false);
    12761290    PXOPT_LOOKUP_STR(filter, config->args, "-filter", true, false);
    12771291    PXOPT_LOOKUP_STR(stage, config->args, "-stage", true, false);
     
    12841298    distTargetRow *row = distTargetRowAlloc(
    12851299            0,          // target_id
    1286             label,
     1300            dist_group,
    12871301            filter,
    12881302            stage,
     
    12911305            comment
    12921306            );
    1293            
     1307
    12941308    if (!row) {
    12951309        psError(PS_ERR_UNKNOWN, false, "failed to allocate distTarget object");
     
    13211335    psMetadata *where = psMetadataAlloc();
    13221336    PXOPT_COPY_S64(config->args, where, "-target_id", "target_id", "==");
    1323     PXOPT_COPY_STR(config->args, where, "-label", "label", "==");
     1337    PXOPT_COPY_STR(config->args, where, "-dist_group", "dist_group", "==");
    13241338    PXOPT_COPY_STR(config->args, where, "-filter", "filter", "==");
    13251339    PXOPT_COPY_STR(config->args, where, "-stage", "stage", "==");
     
    13511365}
    13521366
    1353 static bool listtargetMode(pxConfig *config)
     1367static bool listtargetsMode(pxConfig *config)
    13541368{
    13551369    PS_ASSERT_PTR_NON_NULL(config, false);
     
    13571371    psMetadata *where = psMetadataAlloc();
    13581372    PXOPT_COPY_S64(config->args, where, "-target_id", "target_id", "==");
    1359     PXOPT_COPY_STR(config->args, where, "-label", "label", "==");
     1373    PXOPT_COPY_STR(config->args, where, "-dist_group", "dist_group", "LIKE");
    13601374    PXOPT_COPY_STR(config->args, where, "-filter", "filter", "==");
    13611375    PXOPT_COPY_STR(config->args, where, "-stage", "stage", "==");
     
    14301444}
    14311445
    1432 static bool definedsproductMode(pxConfig *config)
     1446static bool definedestinationMode(pxConfig *config)
    14331447{
    14341448    PS_ASSERT_PTR_NON_NULL(config, false);
    14351449
    14361450    // required
    1437     PXOPT_LOOKUP_STR(name, config->args,   "-name", true, false);
    1438     PXOPT_LOOKUP_STR(dbname, config->args, "-ds_dbname", true, false);
    1439     PXOPT_LOOKUP_STR(dbhost, config->args, "-ds_dbhost", true, false);
     1451    PXOPT_LOOKUP_STR(name, config->args,         "-name", true, false);
     1452    PXOPT_LOOKUP_STR(dbname, config->args,       "-ds_dbname", true, false);
     1453    PXOPT_LOOKUP_STR(dbhost, config->args,       "-ds_dbhost", true, false);
     1454
     1455    // optional
     1456    PXOPT_LOOKUP_STR(status_uri, config->args,   "-status_uri", false, false);
     1457    PXOPT_LOOKUP_STR(comment, config->args,      "-comment", false, false);
     1458    PXOPT_LOOKUP_STR(last_fileset, config->args, "-last_fileset", false, false);
     1459    PXOPT_LOOKUP_STR(state, config->args,        "-set_state", false, false);
    14401460
    14411461    // XXX: should we insure that these names do not contatin any whitespace?
    14421462
    1443     rcDSProductRow *row = rcDSProductRowAlloc(
    1444             0,          // prod_id
     1463    rcDestinationRow *row = rcDestinationRowAlloc(
     1464            0,          // dest_id
    14451465            name,
     1466            status_uri,
     1467            comment,
     1468            last_fileset,
    14461469            dbname,
    1447             dbhost
     1470            dbhost,
     1471            state ? state : "enabled"
    14481472            );
    1449            
     1473
    14501474    if (!row) {
    1451         psError(PS_ERR_UNKNOWN, false, "failed to allocate rcDSProduct object");
    1452         return false;
    1453     }
    1454    if (!rcDSProductInsertObject(config->dbh, row)) {
     1475        psError(PS_ERR_UNKNOWN, false, "failed to allocate rcDestination object");
     1476        return false;
     1477    }
     1478   if (!rcDestinationInsertObject(config->dbh, row)) {
    14551479        psError(PS_ERR_UNKNOWN, false, "database error");
    14561480        psFree(row);
     
    14591483
    14601484    // get the assigned target_id
    1461     row->prod_id = psDBLastInsertID(config->dbh);
    1462 
    1463     if (!rcDSProductPrintObject(stdout, row, true)) {
     1485    row->dest_id = psDBLastInsertID(config->dbh);
     1486
     1487    if (!rcDestinationPrintObject(stdout, row, true)) {
    14641488        psError(PS_ERR_UNKNOWN, false, "failed to print object");
    14651489        psFree(row);
     
    14711495    return true;
    14721496}
    1473 static bool updatedsproductMode(pxConfig *config)
     1497
     1498static bool updatedestinationMode(pxConfig *config)
    14741499{
    14751500    PS_ASSERT_PTR_NON_NULL(config, false);
    14761501
    14771502    psMetadata *where = psMetadataAlloc();
    1478     PXOPT_COPY_S64(config->args, where, "-prod_id", "prod_id", "==");
    1479 
    1480     PXOPT_LOOKUP_STR(dbname, config->args, "-ds_dbname", false, false);
    1481     PXOPT_LOOKUP_STR(dbhost, config->args, "-ds_dbhost", false, false);
    1482 
    1483     if (!(dbname || dbhost)) {
    1484         psError(PS_ERR_UNKNOWN, true, "one or more of dbname or dbhost is required");
    1485         psFree(where);
    1486         return false;
    1487     }
    1488     psString query = psStringCopy("UPDATE rcDSProduct SET");
     1503    PXOPT_COPY_S64(config->args, where, "-dest_id", "dest_id", "==");
     1504
     1505    PXOPT_LOOKUP_STR(state, config->args, "-set_state", false, false);
     1506#ifdef ALLOW_UPDATE_LAST_FILESET
     1507    PXOPT_LOOKUP_STR(last_fileset, config->args, "-set_last_fileset", false, false);
     1508    if (!(state || last_fileset)) {
     1509        psError(PS_ERR_UNKNOWN, true, "one or more of -set_state or -set_last_fileset is required");
     1510# else
     1511    if (!state) {
     1512#endif
     1513        psFree(where);
     1514        return false;
     1515    }
     1516    psString query = psStringCopy("UPDATE rcDestination SET");
    14891517    psString sep = "";
    1490     if (dbname) {
    1491         psStringAppend(&query, " dbname = '%s'", dbname);
     1518    if (state) {
     1519        psStringAppend(&query, " state = '%s'", state);
    14921520        sep = ",";
    14931521    }
    1494     if (dbhost) {
    1495         psStringAppend(&query, " %s dbhost = '%s'", sep, dbhost);
    1496     }
     1522#ifdef ALLOW_UPDATE_LAST_FILESET
     1523    // last_fileset normally gets set by updatercrunMode
     1524    // Allowing it to be set here might cause problems
     1525    // especially since we are allowing selection by dest_id
     1526    if (last_fileset) {
     1527        psStringAppend(&query, " %s last_fileset = '%s'", sep, last_fileset);
     1528    }
     1529#endif
    14971530
    14981531    if (psListLength(where->list)) {
     
    15181551}
    15191552
    1520 static bool definedestinationMode(pxConfig *config)
     1553static bool defineinterestMode(pxConfig *config)
    15211554{
    15221555    PS_ASSERT_PTR_NON_NULL(config, false);
    15231556
    1524     // required
    1525     PXOPT_LOOKUP_S64(prod_id, config->args,      "-prod_id", true, false);
    1526     PXOPT_LOOKUP_STR(name, config->args,         "-name", true, false);
     1557    // one of these is required
     1558    PXOPT_LOOKUP_S64(dest_id, config->args,      "-dest_id", false, false);
     1559    PXOPT_LOOKUP_STR(dest_name, config->args,    "-dest_name", false, false);
     1560    if (!dest_id && !dest_name) {
     1561        psError(PS_ERR_UNKNOWN, true, "either dest_id or dest_name is required");
     1562        return false;
     1563    }
     1564
     1565    // either target_id or stage and label are required
     1566    PXOPT_LOOKUP_S64(target_id, config->args,    "-target_id", false, false);
     1567    PXOPT_LOOKUP_STR(stage, config->args,        "-stage", false, false);
     1568    PXOPT_LOOKUP_STR(dist_group, config->args,   "-dist_group", false, false);
     1569    PXOPT_LOOKUP_STR(filter, config->args,       "-filter", false, false);
     1570    PXOPT_LOOKUP_BOOL(clean, config->args,       "-clean", false);
     1571
     1572    if (!target_id) {
     1573        bool error = false;
     1574        if (!stage) {
     1575            psError(PS_ERR_UNKNOWN, true, "stage is required if target_id is not supplied");
     1576            error = true;
     1577        }
     1578        if (!dist_group) {
     1579            psError(PS_ERR_UNKNOWN, !error, "dist_group is required if target_id is not supplied");
     1580            error = true;
     1581        }
     1582        if (error) {
     1583            return false;
     1584        }
     1585    }
    15271586
    15281587    // optional
    1529     PXOPT_LOOKUP_STR(status_uri, config->args,   "-status_uri", false, false);
    1530     PXOPT_LOOKUP_STR(comment, config->args,      "-comment", false, false);
    1531     PXOPT_LOOKUP_STR(last_fileset, config->args, "-last_fileset", false, false);
    1532     PXOPT_LOOKUP_STR(state, config->args,        "-set_state", false, false);
    1533 
    1534     // XXX: should we insure that these names do not contatin any whitespace?
    1535 
    1536     rcDestinationRow *row = rcDestinationRowAlloc(
    1537             0,          // dest_id
    1538             prod_id,
    1539             name,
    1540             status_uri,
    1541             comment,
    1542             last_fileset,
    1543             state ? state : "enabled"
    1544             );
    1545            
    1546     if (!row) {
    1547         psError(PS_ERR_UNKNOWN, false, "failed to allocate rcDestination object");
    1548         return false;
    1549     }
    1550    if (!rcDestinationInsertObject(config->dbh, row)) {
    1551         psError(PS_ERR_UNKNOWN, false, "database error");
    1552         psFree(row);
    1553         return false;
    1554     }
    1555 
    1556     // get the assigned target_id
    1557     row->dest_id = psDBLastInsertID(config->dbh);
    1558 
    1559     if (!rcDestinationPrintObject(stdout, row, true)) {
    1560         psError(PS_ERR_UNKNOWN, false, "failed to print object");
    1561         psFree(row);
    1562         return false;
    1563     }
    1564 
    1565     psFree(row);
    1566 
    1567     return true;
    1568 }
    1569 
    1570 static bool updatedestinationMode(pxConfig *config)
     1588    PXOPT_LOOKUP_S64(limit, config->args, "-limit", false, false);
     1589    PXOPT_LOOKUP_STR(state, config->args,  "-set_state", false, false);
     1590    if (state) {
     1591        if (strcmp(state, "enabled") && strcmp(state, "disabled")) {
     1592            psError(PS_ERR_PROGRAMMING, true, "state must be enabled or disabled");
     1593            return false;
     1594        }
     1595    } else {
     1596        // default state
     1597        state = "enabled";
     1598    }
     1599
     1600    // now that we've done all of our argument checking, copy the values to where
     1601    psMetadata *where = psMetadataAlloc();
     1602    PXOPT_COPY_S64(config->args, where, "-dest_id", "dest_id", "==");
     1603    PXOPT_COPY_STR(config->args, where, "-dest_name", "rcDestination.name", "==");
     1604    PXOPT_COPY_S64(config->args, where, "-target_id", "target_id", "==");
     1605    PXOPT_COPY_STR(config->args, where, "-dist_group", "dist_group", "LIKE");
     1606    PXOPT_COPY_STR(config->args, where, "-filter", "filter", "LIKE");
     1607    // if stage is all don't add it to the query (match all stages)
     1608    if (stage && strcmp(stage, "all")) {
     1609        PXOPT_COPY_STR(config->args, where, "-stage", "stage", "==");
     1610    }
     1611
     1612    psString query = pxDataGet("disttool_defineinterest.sql");
     1613
     1614    if (!psListLength(where->list)) {
     1615        // can't get here
     1616        psError(PS_ERR_PROGRAMMING, true, "search parameters are required");
     1617        psFree(where);
     1618        psFree(query);
     1619        return false;
     1620    }
     1621    psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
     1622    psStringAppend(&query, " AND %s", whereClause);
     1623    psFree(whereClause);
     1624    psFree(where);
     1625    if (clean) {
     1626        psStringAppend(&query, " AND (distTarget.clean)");
     1627    } else {
     1628        psStringAppend(&query, " AND (!distTarget.clean)");
     1629    }
     1630    if (limit) {
     1631        psString limitString = psDBGenerateLimitSQL(limit);
     1632        psStringAppend(&query, " %s", limitString);
     1633        psFree(limitString);
     1634    }
     1635    {
     1636        // psStringSubstitute fails unless the input is a psString which it determines by
     1637        // comparing the memory blocks free function to an expected value.
     1638        // pxDataGet uses psSlurp which leaves a different free function on the memory block.
     1639        // To work around this make a copy of the query before doing the substitution.
     1640        psString queryCopy = psStringCopy(query);
     1641        psFree(query);
     1642        query = queryCopy;
     1643    }
     1644    // change the @STATE@ in the sql file to our state
     1645    if (!psStringSubstitute(&query, state, "@STATE@")) {
     1646        psError(PS_ERR_UNKNOWN, false, "failed to substitute state string");
     1647        return false;
     1648    }
     1649
     1650    if (!p_psDBRunQuery(config->dbh, query)) {
     1651        psError(PS_ERR_UNKNOWN, false, "database error");
     1652        psFree(query);
     1653        return false;
     1654    }
     1655    psFree(query);
     1656    int numInserted = psDBAffectedRows(config->dbh);
     1657    printf("inserted %d rows into rcInterest\n", numInserted);
     1658
     1659    return true;
     1660}
     1661
     1662static bool updateinterestMode(pxConfig *config)
    15711663{
    15721664    PS_ASSERT_PTR_NON_NULL(config, false);
    15731665
    15741666    psMetadata *where = psMetadataAlloc();
    1575     PXOPT_COPY_S64(config->args, where, "-dest_id", "dest_id", "==");
    1576     PXOPT_COPY_S64(config->args, where, "-prod_id", "prod_id", "==");
     1667    PXOPT_COPY_S64(config->args, where, "-int_id",    "int_id", "==");
     1668    PXOPT_COPY_S64(config->args, where, "-dest_id",   "dest_id", "==");
     1669    PXOPT_COPY_S64(config->args, where, "-target_id", "target_id", "==");
     1670    PXOPT_COPY_STR(config->args, where, "-dest_name", "rcDestination.name", "LIKE");
     1671    PXOPT_COPY_STR(config->args, where, "-stage", "stage", "==");
     1672    PXOPT_COPY_STR(config->args, where, "-filter", "filter", "LIKE");
     1673    PXOPT_COPY_STR(config->args, where, "-dist_group", "dist_group", "LIKE");
     1674
     1675    PXOPT_LOOKUP_BOOL(clean, config->args, "-clean", false);
     1676    PXOPT_LOOKUP_BOOL(full, config->args, "-full", false);
     1677    if (full && clean) {
     1678        psError(PS_ERR_UNKNOWN, true, "-full and -clean makes no sense, chose one or the other");
     1679        psFree(where);
     1680        return false;
     1681    }
     1682       
    15771683
    15781684    PXOPT_LOOKUP_STR(state, config->args, "-set_state", false, false);
    1579 #ifdef ALLOW_UPDATE_LAST_FILESET
    1580     PXOPT_LOOKUP_STR(last_fileset, config->args, "-set_last_fileset", false, false);
    1581     if (!(state || last_fileset)) {
    1582         psError(PS_ERR_UNKNOWN, true, "one or more of -set_state or -set_last_fileset is required");
    1583 # else
    15841685    if (!state) {
    1585 #endif
    1586         psFree(where);
    1587         return false;
    1588     }
    1589     psString query = psStringCopy("UPDATE rcDestination SET");
    1590     psString sep = "";
    1591     if (state) {
    1592         psStringAppend(&query, " state = '%s'", state);
    1593         sep = ",";
    1594     }
    1595 #ifdef ALLOW_UPDATE_LAST_FILESET
    1596     // last_fileset normally gets set by updatercrunMode
    1597     // Allowing it to be set here might cause problems
    1598     // especially since we are allowing selection by prod_id
    1599     if (last_fileset) {
    1600         psStringAppend(&query, " %s last_fileset = '%s'", sep, last_fileset);
    1601     }
    1602 #endif
     1686        psError(PS_ERR_UNKNOWN, true, "-set_state is required");
     1687        psFree(where);
     1688        return false;
     1689    }
     1690    psString query = pxDataGet("disttool_updateinterest.sql");
    16031691
    16041692    if (psListLength(where->list)) {
     
    16141702    psFree(where);
    16151703
    1616     if (!p_psDBRunQuery(config->dbh, query)) {
     1704    if (clean) {
     1705        psStringAppend(&query, " AND distTarget.clean");
     1706    } else {
     1707        psStringAppend(&query, " AND NOT distTarget.clean");
     1708    }
     1709
     1710    if (!p_psDBRunQueryF(config->dbh, query, state)) {
    16171711        psError(PS_ERR_UNKNOWN, false, "database error");
    16181712        psFree(query);
     
    16211715    psFree(query);
    16221716
    1623     return true;
    1624 }
    1625 
    1626 static bool defineinterestMode(pxConfig *config)
     1717    psS64 numUpdated = psDBAffectedRows(config->dbh);
     1718    printf("updated %" PRId64 " interests\n", numUpdated);
     1719
     1720    return true;
     1721}
     1722static bool listinterestsMode(pxConfig *config)
    16271723{
    16281724    PS_ASSERT_PTR_NON_NULL(config, false);
    16291725
    1630     // required
    1631     PXOPT_LOOKUP_S64(dest_id, config->args,      "-dest_id", true, false);
    1632     PXOPT_LOOKUP_S64(target_id, config->args,    "-target_id", true, false);
    1633 
    1634     // optional
    1635     PXOPT_LOOKUP_STR(state, config->args,        "-set_state", false, false);
    1636 
    1637     // XXX: should we insure that these names do not contatin any whitespace?
    1638 
    1639     rcInterestRow *row = rcInterestRowAlloc(
    1640             0,          // int_id
    1641             dest_id,
    1642             target_id,
    1643             state ? state : "enabled"
    1644             );
    1645            
    1646     if (!row) {
    1647         psError(PS_ERR_UNKNOWN, false, "failed to allocate rcInterest object");
    1648         return false;
    1649     }
    1650    if (!rcInterestInsertObject(config->dbh, row)) {
    1651         psError(PS_ERR_UNKNOWN, false, "database error");
    1652         psFree(row);
    1653         return false;
    1654     }
    1655 
    1656     // get the assigned target_id
    1657     row->int_id = psDBLastInsertID(config->dbh);
    1658 
    1659     if (!rcInterestPrintObject(stdout, row, true)) {
    1660         psError(PS_ERR_UNKNOWN, false, "failed to print object");
    1661         psFree(row);
    1662         return false;
    1663     }
    1664 
    1665     psFree(row);
    1666 
    1667     return true;
    1668 }
    1669 
    1670 static bool updateinterestMode(pxConfig *config)
    1671 {
    1672     PS_ASSERT_PTR_NON_NULL(config, false);
    1673 
    1674     psMetadata *where = psMetadataAlloc();
    1675     PXOPT_COPY_S64(config->args, where, "-int_id",    "int_id", "==");
    1676     PXOPT_COPY_S64(config->args, where, "-dest_id",   "dest_id", "==");
     1726    psMetadata *where = psMetadataAlloc();
     1727    PXOPT_COPY_S64(config->args, where, "-int_id", "int_id", "==");
     1728    PXOPT_COPY_S64(config->args, where, "-dest_id", "dest_id", "==");
     1729    PXOPT_COPY_STR(config->args, where, "-dest_name", "name", "==");
    16771730    PXOPT_COPY_S64(config->args, where, "-target_id", "target_id", "==");
    1678 
    1679     PXOPT_LOOKUP_STR(state, config->args, "-set_state", false, false);
    1680     if (!state) {
    1681         psError(PS_ERR_UNKNOWN, true, "-set_state is required");
    1682         psFree(where);
    1683         return false;
    1684     }
    1685     psString query = NULL;
    1686     psStringAppend(&query, "UPDATE rcInterest SET state = '%s'", state);
     1731    PXOPT_COPY_STR(config->args, where, "-stage", "stage", "==");
     1732    PXOPT_COPY_STR(config->args, where, "-dist_group", "dist_group", "LIKE");
     1733    PXOPT_COPY_STR(config->args, where, "-filter", "filter", "LIKE");
     1734    PXOPT_COPY_STR(config->args, where, "-state", "rcInterest.state", "==");
     1735
     1736    PXOPT_LOOKUP_BOOL(clean, config->args, "-clean", false);
     1737    PXOPT_LOOKUP_BOOL(full, config->args, "-full", false);
     1738
     1739    PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false);
     1740    PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
     1741
     1742    if (clean && full) {
     1743        psError(PS_ERR_UNKNOWN, false, "can't select both -clean and -full");
     1744        return false;
     1745    }
     1746
     1747    psString query = pxDataGet("disttool_listinterests.sql");
    16871748
    16881749    if (psListLength(where->list)) {
     
    16901751        psStringAppend(&query, " WHERE %s", whereClause);
    16911752        psFree(whereClause);
    1692     } else {
    1693         psError(PS_ERR_UNKNOWN, true, "search parameters are required");
    1694         psFree(where);
    1695         psFree(query);
    1696         return false;
     1753        if (clean) {
     1754            psStringAppend(&query, " AND (clean)");
     1755        } else if (full) {
     1756            psStringAppend(&query, " AND (!clean)");
     1757        }
     1758    } else if (clean) {
     1759        psStringAppend(&query, " WHERE clean");
     1760    } else if (full) {
     1761        psStringAppend(&query, " WHERE !clean");
    16971762    }
    16981763    psFree(where);
    16991764
     1765    // treat limit == 0 as "no limit"
     1766    if (limit) {
     1767        psString limitString = psDBGenerateLimitSQL(limit);
     1768        psStringAppend(&query, " %s", limitString);
     1769        psFree(limitString);
     1770    }
     1771
    17001772    if (!p_psDBRunQuery(config->dbh, query)) {
    17011773        psError(PS_ERR_UNKNOWN, false, "database error");
    17021774        psFree(query);
     1775        if (!psDBRollback(config->dbh)) {
     1776            psError(PS_ERR_UNKNOWN, false, "database error");
     1777        }
    17031778        return false;
    17041779    }
    17051780    psFree(query);
    17061781
    1707     return true;
    1708 }
    1709 
     1782    psArray *output = p_psDBFetchResult(config->dbh);
     1783    if (!output) {
     1784        psError(PS_ERR_UNKNOWN, false, "database error");
     1785        return false;
     1786    }
     1787    if (!psArrayLength(output)) {
     1788        psTrace("disttool", PS_LOG_INFO, "no rows found");
     1789        psFree(output);
     1790        return true;
     1791    }
     1792
     1793    if (!ippdbPrintMetadatas(stdout, output, "rcInterest", !simple)) {
     1794        psError(PS_ERR_UNKNOWN, false, "failed to print array");
     1795        psFree(output);
     1796        return false;
     1797    }
     1798
     1799    psFree(output);
     1800
     1801    return true;
     1802}
     1803static bool pendingcleanupMode(pxConfig *config)
     1804{
     1805    PS_ASSERT_PTR_NON_NULL(config, false);
     1806
     1807    psMetadata *where = psMetadataAlloc();
     1808    PXOPT_COPY_S64(config->args, where, "-dist_id", "dist_id", "==");
     1809    PXOPT_COPY_STR(config->args, where, "-stage", "stage", "==");
     1810    pxAddLabelSearchArgs (config, where, "-label", "distRun.label", "==");
     1811
     1812    PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false);
     1813    PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
     1814    PXOPT_LOOKUP_BOOL(all, config->args, "-all", false);
     1815
     1816    psString query = pxDataGet("disttool_pendingcleanup.sql");
     1817
     1818    if (psListLength(where->list)) {
     1819        psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
     1820        psStringAppend(&query, " AND %s", whereClause);
     1821        psFree(whereClause);
     1822        psFree(where);
     1823    } else if (!all) {
     1824        psFree(where);
     1825        psError(PXTOOLS_ERR_CONFIG, false, "search parameters or -all are required");
     1826        return false;
     1827    }
     1828
     1829    // treat limit == 0 as "no limit"
     1830    if (limit) {
     1831        psString limitString = psDBGenerateLimitSQL(limit);
     1832        psStringAppend(&query, " %s", limitString);
     1833        psFree(limitString);
     1834    }
     1835
     1836    if (!p_psDBRunQuery(config->dbh, query)) {
     1837        psError(PS_ERR_UNKNOWN, false, "database error");
     1838        psFree(query);
     1839        if (!psDBRollback(config->dbh)) {
     1840            psError(PS_ERR_UNKNOWN, false, "database error");
     1841        }
     1842        return false;
     1843    }
     1844    psFree(query);
     1845
     1846    psArray *output = p_psDBFetchResult(config->dbh);
     1847    if (!output) {
     1848        psError(PS_ERR_UNKNOWN, false, "database error");
     1849        return false;
     1850    }
     1851    if (!psArrayLength(output)) {
     1852        psTrace("disttool", PS_LOG_INFO, "no rows found");
     1853        psFree(output);
     1854        return true;
     1855    }
     1856
     1857    if (!ippdbPrintMetadatas(stdout, output, "distToCleanup", !simple)) {
     1858        psError(PS_ERR_UNKNOWN, false, "failed to print array");
     1859        psFree(output);
     1860        return false;
     1861    }
     1862
     1863    psFree(output);
     1864
     1865    return true;
     1866}
     1867static bool listfilesetsMode(pxConfig *config)
     1868{
     1869    PS_ASSERT_PTR_NON_NULL(config, false);
     1870
     1871    psMetadata *where = psMetadataAlloc();
     1872    PXOPT_COPY_S64(config->args, where, "-dist_id", "dist_id", "==");
     1873    PXOPT_COPY_S64(config->args, where, "-int_id", "int_id", "==");
     1874    PXOPT_COPY_S64(config->args, where, "-dest_id", "dest_id", "==");
     1875    PXOPT_COPY_STR(config->args, where, "-dest_name", "name", "==");
     1876    PXOPT_COPY_S64(config->args, where, "-target_id", "target_id", "==");
     1877    PXOPT_COPY_STR(config->args, where, "-stage", "stage", "==");
     1878    PXOPT_COPY_STR(config->args, where, "-dist_group", "dist_group", "LIKE");
     1879    PXOPT_COPY_STR(config->args, where, "-filter", "filter", "LIKE");
     1880    PXOPT_COPY_STR(config->args, where, "-state", "state", "==");
     1881
     1882    PXOPT_LOOKUP_BOOL(clean, config->args, "-clean", false);
     1883    PXOPT_LOOKUP_BOOL(full, config->args, "-full", false);
     1884
     1885    pxAddLabelSearchArgs (config, where, "-label", "distRun.label", "==");
     1886
     1887    PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false);
     1888    PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
     1889
     1890    if (!psListLength(where->list)) {
     1891        psFree(where);
     1892        psError(PXTOOLS_ERR_CONFIG, false, "search parameters are required");
     1893        return false;
     1894    }
     1895
     1896    psString query = pxDataGet("disttool_listfilesets.sql");
     1897
     1898    psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
     1899    psStringAppend(&query, " WHERE %s", whereClause);
     1900    psFree(whereClause);
     1901    psFree(where);
     1902
     1903    // treat limit == 0 as "no limit"
     1904    if (limit) {
     1905        psString limitString = psDBGenerateLimitSQL(limit);
     1906        psStringAppend(&query, " %s", limitString);
     1907        psFree(limitString);
     1908    }
     1909
     1910    if (!p_psDBRunQuery(config->dbh, query)) {
     1911        psError(PS_ERR_UNKNOWN, false, "database error");
     1912        psFree(query);
     1913        if (!psDBRollback(config->dbh)) {
     1914            psError(PS_ERR_UNKNOWN, false, "database error");
     1915        }
     1916        return false;
     1917    }
     1918    psFree(query);
     1919
     1920    psArray *output = p_psDBFetchResult(config->dbh);
     1921    if (!output) {
     1922        psError(PS_ERR_UNKNOWN, false, "database error");
     1923        return false;
     1924    }
     1925    if (!psArrayLength(output)) {
     1926        psTrace("disttool", PS_LOG_INFO, "no rows found");
     1927        psFree(output);
     1928        return true;
     1929    }
     1930
     1931    if (!ippdbPrintMetadatas(stdout, output, "distFilesets", !simple)) {
     1932        psError(PS_ERR_UNKNOWN, false, "failed to print array");
     1933        psFree(output);
     1934        return false;
     1935    }
     1936
     1937    psFree(output);
     1938
     1939    return true;
     1940}
     1941static bool updatefilesetMode(pxConfig *config)
     1942{
     1943    psMetadata *where = psMetadataAlloc();
     1944    PXOPT_COPY_S64(config->args, where, "-fs_id", "fs_id", "==");
     1945
     1946    if (!psListLength(where->list)) {
     1947        psFree(where);
     1948        psError(PXTOOLS_ERR_CONFIG, false, "search parameters are required");
     1949        return false;
     1950    }
     1951
     1952    PXOPT_LOOKUP_STR(state, config->args, "-set_state", false, false);
     1953
     1954    // We don't use PXOPT_LOOKUP_S16(fault, config->args, "-fault", false, false); here
     1955    // because we want -fault 0 to work
     1956    bool gotFault = false;
     1957    psS16 fault = psMetadataLookupS16(&gotFault, config->args, "-fault");
     1958
     1959    if ((!state) && (!gotFault)) {
     1960        psError(PXTOOLS_ERR_CONFIG, true, "parameters (-fault or -set_state) is required");
     1961        psFree(where);
     1962        return false;
     1963    }
     1964
     1965    psString query = psStringCopy("UPDATE rcDSFileset SET ");
     1966
     1967    if (state) {
     1968        psStringAppend(&query, " state = '%s'", state);
     1969    }
     1970
     1971    if (gotFault) {
     1972        psStringAppend(&query, "%s fault = %d", state ? ", " : "", fault);
     1973    }
     1974
     1975    psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
     1976    psStringAppend(&query, " WHERE %s", whereClause);
     1977    psFree(whereClause);
     1978    psFree(where);
     1979
     1980    if (!p_psDBRunQuery(config->dbh, query)) {
     1981        psError(PS_ERR_UNKNOWN, false, "database error");
     1982        psFree(query);
     1983        return false;
     1984    }
     1985
     1986    return true;
     1987}
Note: See TracChangeset for help on using the changeset viewer.