IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 10056


Ignore:
Timestamp:
Nov 17, 2006, 1:26:55 PM (20 years ago)
Author:
jhoblitt
Message:

revamp -copydone
remove dead/unused code

File:
1 edited

Legend:

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

    r10042 r10056  
    2525#include <string.h>
    2626#include <stdlib.h>
     27#include <inttypes.h>
    2728
    2829#include "pxtools.h"
     
    3334static bool pendingImfileMode(pxConfig *config);
    3435static bool copydoneMode(pxConfig *config);
    35 
    36 //static psArray *pztoolPendingExp(pxConfig *config);
    37 //static psArray *pztoolPendingImfiles(pxConfig *config);
    38 //static bool pztoolFlushPendingExp(pxConfig *config);
    3936
    4037# define MODECASE(caseName, func) \
     
    6865
    6966FAIL:
     67    psErrorStackPrint(stderr, "\n");
     68
    7069    psFree(config);
    7170    pmConfigDone();
     
    251250
    252251    bool status = false;
    253     psString filesetid = psMetadataLookupStr(&status, config->args, "-filesetid");
    254     if (!status) {
    255         psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -filesetid");
    256         return false;
    257     }
    258     if (!filesetid) {
    259         psError(PS_ERR_UNKNOWN, true, "-filesetid is required");
     252    psString exp_id = psMetadataLookupStr(&status, config->args, "-exp_id");
     253    if (!status) {
     254        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -exp_id");
     255        return false;
     256    }
     257    if (!exp_id) {
     258        psError(PS_ERR_UNKNOWN, true, "-exp_id is required");
    260259        return false;
    261260    }
     
    313312    // need to know exp_id, camera, telescope, class, class_id (to find the
    314313    // pzPendingImfile entry and the URI for the newImfile entry.
     314
     315    // start a transaction so it's all rows or nothing
     316    if (!psDBTransaction(config->dbh)) {
     317        psError(PS_ERR_UNKNOWN, false, "database error");
     318        return false;
     319    }
    315320 
    316 #if 0
    317     // we don't have to operate on complete frames here as it's ok to start
    318     // downloading the imfiles before the exp has been registered
    319     psArray *pending = pztoolPendingImfiles(config);
    320     if (!pending) {
    321         psError(PS_ERR_UNKNOWN, false, "no pzPendingImfiles found");
    322         return false;
    323     }
    324 
    325     // start a transaction so we don't end up with entries in newImfile that
    326     // haven't been removed from pzPendingImfile
    327     if (!psDBTransaction(config->dbh)) {
    328         psError(PS_ERR_UNKNOWN, false, "database error");
    329         psFree(pending);
    330         return false;
    331     }
    332 
    333     for (long i = 0; i < pending->n; i++) {
    334         pzPendingImfileRow *pendingImfile = pending->data[i];
    335 
    336         if (!newImfileInsert(
    337             config->dbh,
    338             pendingImfile->exp_tag,
    339             pendingImfile->class,
    340             pendingImfile->class_id
    341             )
    342             // XXX get this from the CLI
    343 //            pendingImfile->uri
    344         ) {
    345             psError(PS_ERR_UNKNOWN, false, "dbh access failed");
    346             psFree(pending);
    347             goto ROLLBACK;
    348         }
    349     }
    350 
    351     if (pzPendingImfileDeleteRowObjects(config->dbh, pending, MAX_ROWS) < 0) {
    352         psError(PS_ERR_UNKNOWN, false, "dbh access failed");
    353         psFree(pending);
    354         goto ROLLBACK;
    355     }
    356     psFree(pending);
    357 
    358     // check for completed exps
    359     if (!pztoolFlushPendingExp(config)) {
    360         psError(PS_ERR_UNKNOWN, false, "pztoolFlushPendingExp() failed");
    361         goto ROLLBACK;
     321    // insert new imfile into newImfile
     322    {
     323        char *query =
     324            "INSERT INTO newImfile"
     325            "   SElECT"
     326            "       pzPendingImfile.exp_tag,"
     327            "       pzPendingImfile.class,"
     328            "       pzPendingImfile.class_id,"
     329            "       '%s'" // uri of downloaded file
     330            "   FROM pzPendingImfile"
     331            "   WHERE"
     332            "       pzPendingImfile.exp_id = '%s'"
     333            "       AND pzPendingImfile.camera = '%s'"
     334            "       AND pzPendingImfile.telescope = '%s'"
     335            "       AND pzPendingImfile.class = '%s'"
     336            "       AND pzPendingImfile.class_id = '%s'";
     337
     338        if (!p_psDBRunQuery(config->dbh, query, uri, exp_id, camera, telescope, class, class_id)) {
     339            // rollback
     340            if (!psDBRollback(config->dbh)) {
     341                psError(PS_ERR_UNKNOWN, false, "database error");
     342            }
     343            psError(PS_ERR_UNKNOWN, false, "database error");
     344            return false;
     345        }
     346
     347        // sanity check: we should have inserted only one row
     348        psU64 affected = psDBAffectedRows(config->dbh);
     349        if (psDBAffectedRows(config->dbh) != 1) {
     350            // rollback
     351            if (!psDBRollback(config->dbh)) {
     352                psError(PS_ERR_UNKNOWN, false, "database error");
     353            }
     354            psError(PS_ERR_UNKNOWN, false, "should have affected 1 row but %" PRIu64 " rows were modified", affected);
     355            return false;
     356        }
     357    }
     358
     359    // cp the imfile into pzDoneImfile
     360    {
     361        char *query =
     362            "INSERT INTO pzDoneImfile"
     363            "   SElECT"
     364            "       pzPendingImfile.*,"
     365            "       '%s'" // uri of downloaded file
     366            "   FROM pzPendingImfile"
     367            "   WHERE"
     368            "       pzPendingImfile.exp_id = '%s'"
     369            "       AND pzPendingImfile.camera = '%s'"
     370            "       AND pzPendingImfile.telescope = '%s'"
     371            "       AND pzPendingImfile.class = '%s'"
     372            "       AND pzPendingImfile.class_id = '%s'";
     373
     374        if (!p_psDBRunQuery(config->dbh, query, uri, exp_id, camera, telescope, class, class_id)) {
     375            // rollback
     376            if (!psDBRollback(config->dbh)) {
     377                psError(PS_ERR_UNKNOWN, false, "database error");
     378            }
     379            psError(PS_ERR_UNKNOWN, false, "database error");
     380            return false;
     381        }
     382
     383        // sanity check: we should have inserted only one row
     384        psU64 affected = psDBAffectedRows(config->dbh);
     385        if (psDBAffectedRows(config->dbh) != 1) {
     386            // rollback
     387            if (!psDBRollback(config->dbh)) {
     388                psError(PS_ERR_UNKNOWN, false, "database error");
     389            }
     390            psError(PS_ERR_UNKNOWN, false, "should have affected 1 row but %" PRIu64 " rows were modified", affected);
     391            return false;
     392        }
     393    }
     394
     395    // removve the entry from pzPendingImfile
     396    {
     397        char *query =
     398            "DELETE FROM pzPendingImfile"
     399            "   WHERE"
     400            "       pzPendingImfile.exp_id = '%s'"
     401            "       AND pzPendingImfile.camera = '%s'"
     402            "       AND pzPendingImfile.telescope = '%s'"
     403            "       AND pzPendingImfile.class = '%s'"
     404            "       AND pzPendingImfile.class_id = '%s'";
     405
     406        if (!p_psDBRunQuery(config->dbh, query, exp_id, camera, telescope, class, class_id)) {
     407            // rollback
     408            if (!psDBRollback(config->dbh)) {
     409                psError(PS_ERR_UNKNOWN, false, "database error");
     410            }
     411            psError(PS_ERR_UNKNOWN, false, "database error");
     412            return false;
     413        }
     414
     415        // sanity check: we should have removed only one row
     416        psU64 affected = psDBAffectedRows(config->dbh);
     417        if (psDBAffectedRows(config->dbh) != 1) {
     418            // rollback
     419            if (!psDBRollback(config->dbh)) {
     420                psError(PS_ERR_UNKNOWN, false, "database error");
     421            }
     422            psError(PS_ERR_UNKNOWN, false, "should have affected 1 row but %" PRIu64 " rows were modified", affected);
     423            return false;
     424        }
    362425    }
    363426
    364427    // point of no return
    365428    if (!psDBCommit(config->dbh)) {
    366         psError(PS_ERR_UNKNOWN, false, "database error");
    367         // XXX is this the right thing to do after a commit failure?
    368         goto ROLLBACK;
    369     }
    370 
    371     return true;
    372 
    373 ROLLBACK:
    374     // rollback
    375     if (!psDBRollback(config->dbh)) {
    376         psError(PS_ERR_UNKNOWN, false, "database error");
    377     }
    378 #endif
    379 
    380     return false;
    381 }
    382 
    383 #if 0
    384 static psArray *pztoolPendingExp(pxConfig *config)
    385 {
    386     PS_ASSERT_PTR_NON_NULL(config, NULL);
    387 
    388     psArray *exps = pzPendingExpSelectRowObjects(config->dbh,
    389         config->where, MAX_ROWS);
    390     if (!exps) {
    391         psError(PS_ERR_UNKNOWN, false, "no pzPendingExp rows found");
    392         return NULL;
    393     }
    394 
    395     return exps;
    396 }
    397 
    398 static psArray *pztoolPendingImfiles(pxConfig *config)
    399 {
    400     PS_ASSERT_PTR_NON_NULL(config, NULL);
    401 
    402     psArray *imfiles = pzPendingImfileSelectRowObjects(config->dbh,
    403         config->where, MAX_ROWS);
    404     if (!imfiles) {
    405         psError(PS_ERR_UNKNOWN, false, "no pzPendingImfile rows found");
    406         return NULL;
    407     }
    408 
    409     return imfiles;
    410 }
    411 
    412 static bool pztoolFlushPendingExp(pxConfig *config)
    413 {
    414     PS_ASSERT_PTR_NON_NULL(config, false);
    415 
    416     psArray *pending = pztoolPendingExp(config);
    417     if (!pending) {
    418         psError(PS_ERR_UNKNOWN, false, "no pzPendingExps found");
    419         return false;
    420     }
    421 
    422     for (long i = 0; i < pending->n; i++) {
    423         pzPendingExpRow *pendingExp = pending->data[i];
    424 
    425         psMetadata *where = psMetadataAlloc();
    426         psMetadataAddStr(where, PS_LIST_TAIL, "exp_id", 0, "==",
    427             pendingExp->exp_id);
    428         psArray *pendingImfiles = pzPendingImfileSelectRowObjects(config->dbh, where, MAX_ROWS);
    429         psFree(where);
    430 
    431         if (!pendingImfiles) {
    432             // exp has no coresponding imfiles so we need to remove it from
    433             // pzPendingExp and add it to newExp
    434 
    435             if (!newExpInsert(
    436                     config->dbh,
    437                     // XXX exp_tag needs to be generated
    438                     "FIXME exp_tag",
    439                     pendingExp->exp_id,
    440                     pendingExp->camera,
    441                     pendingExp->telescope,
    442                     pendingExp->exp_type,
    443                     pendingExp->imfiles)
    444             ) {
    445                 psError(PS_ERR_UNKNOWN, false, "dbh access failed");
    446                 psFree(pending);
    447                 return false;
    448             }
    449 
    450             psArray *nukeMe = psArrayAllocEmpty(1);
    451             psArrayAdd(nukeMe, 0, pendingExp);
    452             bool status = pzPendingExpDeleteRowObjects(config->dbh, nukeMe, MAX_ROWS);
    453             psFree(nukeMe);
    454             if (!status) {
    455                 psError(PS_ERR_UNKNOWN, false, "dbh access failed");
    456                 return false;
    457             }
    458         }
    459 
    460         psFree(pendingImfiles);
     429        // rollback
     430        if (!psDBRollback(config->dbh)) {
     431            psError(PS_ERR_UNKNOWN, false, "database error");
     432        }
     433        psError(PS_ERR_UNKNOWN, false, "database error");
     434        return false;
    461435    }
    462436
    463437    return true;
    464438}
    465 
    466 #endif
Note: See TracChangeset for help on using the changeset viewer.