IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Jun 9, 2006, 6:50:21 PM (20 years ago)
Author:
jhoblitt
Message:

complete implementation of -rerun

File:
1 edited

Legend:

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

    r7471 r7509  
    3434static detInputExpRow *rawDetrenTodetInputExpRow(rawDetrendExpRow *rawExp, psS32 det_id, psS32 iteration);
    3535static psArray *searchRawImfiles(pxConfig *config, psMetadata *where);
     36static psS32 incrementIteration(pxConfig *config, const char *det_id);
    3637
    3738# define MODECASE(caseName, func) \
     
    9596        return false;
    9697    }
    97 
    98     // start a transaction so we don't end up with orphaned det_ids
    99     if (!psDBTransaction(config->dbh)) {
    100         psError(PS_ERR_UNKNOWN, false, "database error");
    101         return false;
    102     }
    103 
    104     // grab the 'position' column's value as the new det_id
    105     // the first iteration is always 0
    106     detRunInsert(config->dbh, det_type);
    107     long det_id = psDBLastInsertID(config->dbh);
    10898   
    10999    // we have to support multipe exp_ids
     
    128118                psError(PS_ERR_UNKNOWN, true,
    129119                        "at least one -exp_id is required");
    130                 // rollback
    131                 if (!psDBRollback(config->dbh)) {
    132                     psError(PS_ERR_UNKNOWN, false, "database error");
    133                 }
    134120                psFree(where);
    135121                return false;
    136122            }
    137123
    138             if (!psMetadataAddStr(where, PS_LIST_TAIL, "exp_id",
    139                         PS_META_DUPLICATE_OK, "==", exp_id)) {
     124            if (!psMetadataAddStr(where, PS_LIST_TAIL, "exp_id", 0, "==",
     125                        exp_id)) {
    140126                psError(PS_ERR_UNKNOWN, false, "failed to add item exp_id");
    141                 // rollback
    142                 if (!psDBRollback(config->dbh)) {
    143                     psError(PS_ERR_UNKNOWN, false, "database error");
    144                 }
    145127                psFree(iter);
    146128                psFree(where);
     
    150132        psFree(iter);
    151133    } else {
    152         // rollback
    153         if (!psDBRollback(config->dbh)) {
    154             psError(PS_ERR_UNKNOWN, false, "database error");
    155         }
    156134        psAbort(config->argv[0],
    157135                "-exp_id was not parsed correctly (this should not happen");
     
    168146    if (!detrendExps) {
    169147        psError(PS_ERR_UNKNOWN, false, "no rawDetrendExp rows found");
    170         // rollback
    171         if (!psDBRollback(config->dbh)) {
    172             psError(PS_ERR_UNKNOWN, false, "database error");
    173         }
    174         return false;
    175     }
    176 
    177     if (item->type == PS_DATA_METADATA_MULTI) {
    178         // we should have one rawDetrendExp row be exp_id specified
    179         if (psListLength(item->data.list) != psArrayLength(detrendExps)) {
    180             // rollback
    181             if (!psDBRollback(config->dbh)) {
    182                 psError(PS_ERR_UNKNOWN, false, "database error");
    183             }
    184             psAbort(config->argv[0],
    185         "an -exp_id matched more then one rawDetrendExp (this should not happen");
    186 
    187         }
    188     } else {
    189         // we should have matched only one exp_id
    190         if (psArrayLength(detrendExps) != 1) {
    191             // rollback
    192             if (!psDBRollback(config->dbh)) {
    193                 psError(PS_ERR_UNKNOWN, false, "database error");
    194             }
    195             psAbort(config->argv[0],
    196         "-exp_id matched more then one rawDetrendExp (this should not happen");
    197         }
    198     }
     148        return false;
     149    }
     150
     151    // we should have one rawDetrendExp row be exp_id specified
     152    if (psListLength(item->data.list) != psArrayLength(detrendExps)) {
     153        psAbort(config->argv[0],
     154    "an -exp_id matched more then one rawDetrendExp (this should not happen");
     155
     156    }
     157
     158    // start a transaction so we don't end up with orphaned det_ids
     159    if (!psDBTransaction(config->dbh)) {
     160        psError(PS_ERR_UNKNOWN, false, "database error");
     161        return false;
     162    }
     163
     164    // grab the 'position' column's value as the new det_id
     165    // the first iteration is always 0
     166    detRunInsert(config->dbh, det_type, 0);
     167    long det_id = psDBLastInsertID(config->dbh);
    199168
    200169    // create new detInputExp row(s) from the rawDetrendExp row(s)
     
    18091778        return false;
    18101779    }
     1780
    18111781    psList *exp_id_list = item->data.list;
    1812 
     1782    psMetadata *where = psMetadataAlloc();
    18131783    // make sure that -exp_id was parsed correctly
    18141784    // XXX this can be removed someday
    18151785    if (item->type == PS_DATA_METADATA_MULTI) {
    1816         psListIterator *iter = psListIteratorAlloc(exp_id_list, 0, false);
     1786        psListIterator *iter = psListIteratorAlloc(item->data.list, 0, false);
    18171787        psMetadataItem *mItem = NULL;
    18181788        while ((mItem = psListGetAndIncrement(iter))) {
    18191789            psString exp_id = mItem->data.V;
    18201790            // if exp_id is NULL then it means that -exp_id has not been
    1821             // specified or something really bad happened
     1791            // specified
    18221792            if (!exp_id) {
    18231793                psError(PS_ERR_UNKNOWN, true,
    18241794                        "at least one -exp_id is required");
     1795                psFree(where);
     1796                return false;
     1797            }
     1798
     1799            if (!psMetadataAddStr(where, PS_LIST_TAIL, "exp_id",
     1800                        PS_META_DUPLICATE_OK, "==", exp_id)) {
     1801                psError(PS_ERR_UNKNOWN, false, "failed to add item exp_id");
     1802                psFree(iter);
     1803                psFree(where);
    18251804                return false;
    18261805            }
     
    18361815
    18371816    // add the det_id & iteration == 0 to the where claus
    1838     psMetadata *where = psMetadataAlloc();
    18391817    if (!psMetadataAddS32(where, PS_LIST_TAIL, "det_id", 0, "==",
    18401818                (psS32)atol(det_id))) {
     
    18531831    if (!detrendExps) {
    18541832        psError(PS_ERR_UNKNOWN, false, "no rawDetrendExp rows found");
     1833        psFree(where);
    18551834        return false;
    18561835    }
     
    18611840        psHashAdd(valid_exp_ids,
    18621841            ((detInputExpRow *)detrendExps->data[i])->exp_id,
    1863             ((detInputExpRow *)detrendExps->data[i])->exp_id
     1842            detrendExps->data[i]
    18641843        );
    18651844    }
    18661845
     1846    // start a transaction so we don't end up with an incremented iteration
     1847    // count but no detInputExps
     1848    if (!psDBTransaction(config->dbh)) {
     1849        psError(PS_ERR_UNKNOWN, false, "database error");
     1850        psFree(valid_exp_ids);
     1851        return false;
     1852    }
     1853
     1854    // up the detRuns iteration count
     1855    psS32 newIteration = incrementIteration(config, det_id);
     1856    if (!newIteration) {
     1857        // rollback
     1858        if (!psDBRollback(config->dbh)) {
     1859            psError(PS_ERR_UNKNOWN, false, "database error");
     1860        }
     1861        psFree(valid_exp_ids);
     1862        return false;
     1863    }
     1864
     1865    // check exp_ids and build up an array of new detInputExp rows at the same
     1866    // time
    18671867    psListIterator *iter = psListIteratorAlloc(exp_id_list, 0, false);
    18681868    psMetadataItem *mItem = NULL;
     1869    psArray *newInputExps = psArrayAlloc(psListLength(exp_id_list));
    18691870    while ((mItem = psListGetAndIncrement(iter))) {
    1870         if (!psHashLookup(valid_exp_ids, (char *)mItem->data.V)) {
     1871        detInputExpRow *inputExp = psHashLookup(valid_exp_ids,
     1872                (char *)mItem->data.V);
     1873        if (!inputExp) {
     1874            // rollback
     1875            if (!psDBRollback(config->dbh)) {
     1876                psError(PS_ERR_UNKNOWN, false, "database error");
     1877            }
    18711878            // invalid exp_id
    18721879            psError(PS_ERR_UNKNOWN, false, "exp_id %s is invalid for det_id %s",
    18731880                    (char *)mItem->data.V, det_id);
     1881            psFree(iter);
    18741882            psFree(valid_exp_ids);
    1875             psFree(iter);
    18761883            return false;
    18771884        }
    1878     }
     1885        psArrayAdd(newInputExps, 0, detInputExpRowAlloc(
     1886            (psS32)atol(det_id),
     1887            newIteration,
     1888            inputExp->exp_id,
     1889            inputExp->camera,
     1890            inputExp->telescope,
     1891            inputExp->exp_type,
     1892            inputExp->imfiles,
     1893            inputExp->filter,
     1894            inputExp->stats
     1895        ));
     1896    }
     1897    psFree(iter);
    18791898    psFree(valid_exp_ids);
    1880     psFree(iter);
    1881 
    1882     psFree(exp_id_list);
     1899
     1900    for (long i = 0; i < psArrayLength(newInputExps); i++) {
     1901        if (!detInputExpInsertObject(config->dbh, newInputExps->data[i])) {
     1902            psError(PS_ERR_UNKNOWN, false, "database error");
     1903            // rollback
     1904            if (!psDBRollback(config->dbh)) {
     1905                psError(PS_ERR_UNKNOWN, false, "database error");
     1906            }
     1907            return false;
     1908        }
     1909    }
     1910
     1911    // point of no return for det_id creation
     1912    if (!psDBCommit(config->dbh)) {
     1913        psError(PS_ERR_UNKNOWN, false, "database error");
     1914        return false;
     1915    }
    18831916
    18841917    return true;
    18851918}
     1919
     1920static psS32 incrementIteration(pxConfig *config, const char *det_id)
     1921{
     1922    // this function returns zero on error
     1923    PS_ASSERT_PTR_NON_NULL(config, 0);
     1924    PS_ASSERT_PTR_NON_NULL(det_id, 0);
     1925
     1926    char *query = "UPDATE detRun SET iteration = iteration + 1 WHERE position = '%s'";
     1927    if (!p_psDBRunQuery(config->dbh, query, det_id)) {
     1928        psError(PS_ERR_UNKNOWN, false,
     1929                "failed to increment iteration for det_id %s", det_id);
     1930        return 0;
     1931    }
     1932
     1933    psMetadata *where = psMetadataAlloc();
     1934    if (!psMetadataAddS32(where, PS_LIST_TAIL, "position", 0, "==",
     1935                (psS32)atol(det_id))) {
     1936        psError(PS_ERR_UNKNOWN, false, "failed to add item det_id");
     1937        psFree(where);
     1938        return 0;
     1939    }
     1940
     1941    psArray *detRuns = detRunSelectRowObjects(config->dbh, where, 0);
     1942    psFree(where);
     1943    if (!detRuns) {
     1944        psError(PS_ERR_UNKNOWN, false, "no detRun rows found");
     1945        return 0;
     1946    }
     1947
     1948    // sanity check the database
     1949    if (psArrayLength(detRuns) != 1) {
     1950        // this should no happen
     1951        psAbort(config->argv[0],
     1952                "database query return too many rows (this should not happen");
     1953    }
     1954
     1955    psS32 newIteration = ((detRunRow *)detRuns->data[0])->iteration;
     1956    psFree(detRuns);
     1957
     1958    return newIteration;
     1959}
Note: See TracChangeset for help on using the changeset viewer.