IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 10021


Ignore:
Timestamp:
Nov 16, 2006, 2:23:19 PM (20 years ago)
Author:
jhoblitt
Message:

further work towards supporting the new database schema
init/cleanup pslib & psmodules
convert to psTrace() for debugging statements

File:
1 edited

Legend:

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

    r10014 r10021  
    3636int main(int argc, char **argv)
    3737{
     38    psLibInit(NULL);
     39
    3840    pxConfig *config = pzgetimfilesConfig(NULL, argc, argv);
    3941
     
    4345
    4446    psFree(config);
     47    pmConfigDone();
     48    psLibFinalize();
     49
    4550    exit(EXIT_SUCCESS);
    4651
    4752FAIL:
    4853    psFree(config);
     54    pmConfigDone();
     55    psLibFinalize();
    4956
    5057    exit(EXIT_FAILURE);
    5158}
    5259
    53 static bool go (pxConfig *config)
     60static bool go(pxConfig *config)
    5461{
    5562    PS_ASSERT_PTR_NON_NULL(config, NULL);
     
    7481        psError(PS_ERR_UNKNOWN, true, "-filesetid is required");
    7582        return false;
     83    }
     84
     85    psString camera = psMetadataLookupStr(&status, config->args, "-inst");
     86    if (!status) {
     87        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -inst");
     88        return NULL;
     89    }
     90    if (!camera) {
     91        psError(PS_ERR_UNKNOWN, true, "-inst is required");
     92        return NULL;
     93    }
     94
     95    psString telescope = psMetadataLookupStr(&status, config->args, "-telescope");
     96    if (!status) {
     97        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -telescope");
     98        return NULL;
     99    }
     100    if (!telescope) {
     101        psError(PS_ERR_UNKNOWN, true, "-telescope is required");
     102        return NULL;
    76103    }
    77104
     
    80107    psStringAppend(&cmd, "%s --uri %s", FILESET_LS_CMD, uri);
    81108
    82     // XXX debugging
    83     fprintf(stderr, "cmd is: %s\n", cmd);
     109    psTrace("pzgetimfiles", PS_LOG_INFO, "cmd is: %s\n", cmd);
    84110
    85111    FILE *output = popen(cmd, "r");
     
    108134        return false;
    109135    }
    110 
    111136    psFree(cmdOutput);
    112  
     137   
     138    // save the number of new Imfiles;
     139    long imfiles = psArrayLength(newImfiles);
     140
     141    // if the fileset was empty (no files) then we can bail out early
     142    if (imfiles == 0) {
     143        char *query =
     144            "UPDATE summitExp"
     145            " SET imfiles = %d"
     146            " WHERE exp_id = '%s'"
     147            " AND camera = '%s'"
     148            " AND telescope = '%s'";
     149        if (!p_psDBRunQuery(config->dbh, query, imfiles, filesetid, camera, telescope)) {
     150            psError(PS_ERR_UNKNOWN, false, "database error");
     151            return false;
     152        }
     153        psFree(newImfiles);
     154        return true;
     155    }
     156
    113157    // start a transaction so it's all rows or nothing
    114158    if (!psDBTransaction(config->dbh)) {
     
    125169
    126170        if (!p_psDBRunQuery(config->dbh, query)) {
     171            // rollback
     172            if (!psDBRollback(config->dbh)) {
     173                psError(PS_ERR_UNKNOWN, false, "database error");
     174            }
    127175            psError(PS_ERR_UNKNOWN, false, "database error");
    128176            psFree(newImfiles);
     
    136184        long inserted = p_psDBRunQueryPrepared(config->dbh, newImfiles, query);
    137185        if (inserted < 0) {
     186            // rollback
     187            if (!psDBRollback(config->dbh)) {
     188                psError(PS_ERR_UNKNOWN, false, "database error");
     189            }
    138190            psError(PS_ERR_UNKNOWN, false, "database error");
    139191            psFree(newImfiles);
     
    142194        // sanity check that we actually inserted something
    143195        if (inserted == 0) {
     196            // rollback
     197            if (!psDBRollback(config->dbh)) {
     198                psError(PS_ERR_UNKNOWN, false, "database error");
     199            }
    144200            psError(PS_ERR_UNKNOWN, false, "database error -- we should have inserted at least one row");
    145201            psFree(newImfiles);
     
    148204    }
    149205
    150     // save the number of new Imfiles;
    151     long imfiles = psArrayLength(newImfiles);
    152206    psFree(newImfiles);
     207
    153208    {
    154209        char *query =
     
    174229
    175230        if (!p_psDBRunQuery(config->dbh, query)) {
     231            // rollback
     232            if (!psDBRollback(config->dbh)) {
     233                psError(PS_ERR_UNKNOWN, false, "database error");
     234            }
    176235            psError(PS_ERR_UNKNOWN, false, "database error");
    177236            return false;
     
    203262
    204263        if (!p_psDBRunQuery(config->dbh, query)) {
     264            // rollback
     265            if (!psDBRollback(config->dbh)) {
     266                psError(PS_ERR_UNKNOWN, false, "database error");
     267            }
    205268            psError(PS_ERR_UNKNOWN, false, "database error");
    206269            return false;
     
    210273    // update summitExp.imfiles
    211274    {
    212         char *query = "UPDATE summitExp SET imfiles = %d";
    213         if (!p_psDBRunQuery(config->dbh, query, imfiles)) {
     275        char *query =
     276            "UPDATE summitExp"
     277            " SET imfiles = (SELECT COUNT(*) FROM summitImfile"
     278            "   WHERE exp_id = '%s'"
     279            "   AND camera = '%s'"
     280            "   AND telescope = '%s'"
     281            ")"
     282            " WHERE exp_id = '%s'"
     283            " AND camera = '%s'"
     284            " AND telescope = '%s'";
     285        if (!p_psDBRunQuery(config->dbh, query, filesetid, camera, telescope, filesetid, camera, telescope)) {
    214286            psError(PS_ERR_UNKNOWN, false, "database error");
    215287            return false;
     
    271343    psString line;
    272344    while ((line = psListGetAndIncrement(lineCursor))) {
     345        psTrace("pzgetimfiles", PS_LOG_INFO, "parsing line: %s\n", line);
     346
    273347        // split line into tokens
    274348        psList *tokens = psStringSplit(line, " ", false);
     
    355429        }
    356430
     431        // must be freed after the new metadata is built -- holds the strings
    357432        psFree(tokenCursor);
    358433        psFree(tokens);
    359434
     435        // debugging
     436        if (psTraceGetLevel("pzgetimfiles") == PS_LOG_INFO) {
     437            psString doc = psMetadataConfigFormat(md);
     438            psTrace("pzgetimfiles", PS_LOG_INFO, "parsed line as:\n %s\n", doc);
     439            psFree(doc);
     440        }
     441
    360442        psArrayAdd(pzPendingImfiles, 0, md);
    361         psMetadataConfigPrint(stdout, md);
     443
     444        psFree(md);
    362445    }
    363446
Note: See TracChangeset for help on using the changeset viewer.