IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 9968


Ignore:
Timestamp:
Nov 13, 2006, 5:07:50 PM (20 years ago)
Author:
jhoblitt
Message:

overhaul pzgetexp to be more error tolerant and efficient

File:
1 edited

Legend:

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

    r9735 r9968  
    4343    // a metadatadb function to retrun the last entry without removing it from
    4444    // the database
    45     psArray *summitExps = summitExpSelectRowObjects(config->dbh, NULL, 0);
    46     if (summitExps) {
     45    psArray *summitExps = summitExpSelectRowObjects(config->dbh, NULL, 1);
     46    if (!summitExps) {
     47        psError(PS_ERR_UNKNOWN, false, "database error");
     48        goto FAIL;
     49    }
     50    if (psArrayLength(summitExps)) {
    4751        haveLastFileSet = true;
    4852        summitExpRow *lastExps = summitExps->data[psArrayLength(summitExps) - 1];
     
    8387        goto FAIL;
    8488    }
    85 
    86     // try not to insert duplicate summitExp entries
    87     // XXX this will become very expensive as the number of summitExp entry
    88     // grows -- is just blindly ignoring database errors the best thing to do?
    89     if (summitExps) {
    90         for (long i = 0; i < psArrayLength(newSummitExps); i++) {
    91             summitExpRow *newSummitExp = newSummitExps->data[i];
    92             for (long j = 0; j < psArrayLength(summitExps); j++) {
    93                 summitExpRow *summitExp = summitExps->data[j];
    94                 if (strcmp(newSummitExp->exp_id,
    95                            summitExp->exp_id) == 0) {
    96                     psArrayRemoveData(newSummitExps, newSummitExp);
    97                     // dec the counter as the array just got shorter
    98                     // and we don't want to skip elemnts
    99                     i--;
    100                     break;
    101                 }
    102             }
    103         }
    104         psFree(summitExps);
    105     }
    106 
    107     // insert new entries into the database
    108     for (long i = 0; i < psArrayLength(newSummitExps); i++) {
    109         if (!summitExpInsertObject(config->dbh, newSummitExps->data[i])) {
    110             psError(PS_ERR_UNKNOWN, false, "dbh access failed");
     89    if (!psArrayLength(newSummitExps)) {
     90        psError(PS_ERR_UNKNOWN, true, "no new fileSet/exp IDs");
     91        psFree(newSummitExps);
     92        return true;
     93    }
     94
     95    // create a temporry table
     96    {
     97        char *query = "CREATE TEMPORARY TABLE incoming"
     98            " (exp_id VARCHAR(64), camera VARCHAR(64), telescope VARCHAR(64), exp_type VARCHAR(64), uri VARCHAR(255), PRIMARY KEY(exp_id, camera, telescope))"
     99           " ENGINE=MEMORY";
     100        if (!p_psDBRunQuery(config->dbh, query)) {
     101            psError(PS_ERR_UNKNOWN, false, "database error");
     102            psFree(newSummitExps);
     103            goto FAIL;
     104        }
     105    }
     106
     107    {
     108        char *query = "INSERT INTO incoming (exp_id, camera, telescope, exp_type, uri) VALUES (?, ?, ?, ?, ?)";
     109
     110        long inserted = p_psDBRunQueryPrepared(config->dbh, newSummitExps, query);
     111        if (inserted < 0) {
     112            psError(PS_ERR_UNKNOWN, false, "database error");
     113            psFree(newSummitExps);
     114            goto FAIL;
     115        }
     116        // sanity check that we actually inserted something
     117        if (inserted == 0) {
     118            psError(PS_ERR_UNKNOWN, false, "database error -- we should have inserted at least one row");
     119            psFree(newSummitExps);
     120            goto FAIL;
     121        }
     122    }
     123
     124    psFree(newSummitExps);
     125
     126    {
     127        char *query =
     128            "INSERT INTO summitExp"
     129            "   SElECT incoming.* FROM incoming"
     130            "   LEFT JOIN summitExp"
     131            "       USING(exp_id, camera, telescope)"
     132            "   WHERE"
     133            "       summitExp.exp_id is NULL"
     134            "       AND summitExp.camera is NULL"
     135            "       AND summitExp.telescope is NULL";
     136
     137        if (!p_psDBRunQuery(config->dbh, query)) {
     138            psError(PS_ERR_UNKNOWN, false, "database error");
    111139            goto FAIL;
    112140        }
     
    127155    PS_ASSERT_PTR_NON_NULL(config, NULL);
    128156    PS_ASSERT_PTR_NON_NULL(str, NULL);
     157   
     158    // these are constants for all records parsed -- look them up before we do
     159    // any work
     160    bool status = false;
     161    char *camera = psMetadataLookupStr(&status, config->args, "-inst");
     162    if (!status) {
     163        psError(PS_ERR_UNKNOWN, false, "failed to lookup item '-inst'");
     164        return NULL;
     165    }
     166    char *telescope = psMetadataLookupStr(&status, config->args, "-telescope");
     167    if (!status) {
     168        psError(PS_ERR_UNKNOWN, false, "failed to lookup item '-telescope'");
     169        return NULL;
     170    }
    129171
    130172    psList *doc = psStringSplit(str, "\n", false);
     
    155197        }
    156198
     199        // find the values of interest
    157200        psListIterator *tokenCursor = psListIteratorAlloc(tokens, 0, false);
    158201        char *uri       = psListGetAndIncrement(tokenCursor);
     
    163206        if (time) {} // silence unused warning
    164207
    165         bool status = false;
    166         char *camera_name = psMetadataLookupStr(&status, config->args,
    167             "-inst");
    168         char *telescope   = psMetadataLookupStr(&status, config->args,
    169             "-telescope");
    170 
    171         summitExpRow *row = summitExpRowAlloc(
    172             exp_id,
    173             camera_name,
    174             telescope,
    175             exp_type,
    176             uri
    177         );
     208        // create a new metadata to represent this line and it's values
     209        psMetadata *md = psMetadataAlloc();
     210        if (!psMetadataAddStr(md, PS_LIST_TAIL, "exp_id", 0, NULL, exp_id)) {
     211            psError(PS_ERR_UNKNOWN, false, "failed to add item exp_id");
     212            psFree(md);
     213            psFree(tokenCursor);
     214            psFree(tokens);
     215            return NULL;
     216        }
     217        if (!psMetadataAddStr(md, PS_LIST_TAIL, "camera", 0, NULL, camera)) {
     218            psError(PS_ERR_UNKNOWN, false, "failed to add item camera");
     219            psFree(md);
     220            psFree(tokenCursor);
     221            psFree(tokens);
     222            return NULL;
     223        }
     224        if (!psMetadataAddStr(md, PS_LIST_TAIL, "telescope", 0, NULL, telescope)) {
     225            psError(PS_ERR_UNKNOWN, false, "failed to add item telescope");
     226            psFree(md);
     227            psFree(tokenCursor);
     228            psFree(tokens);
     229            return NULL;
     230        }
     231        if (!psMetadataAddStr(md, PS_LIST_TAIL, "exp_type", 0, NULL, exp_type)) {
     232            psError(PS_ERR_UNKNOWN, false, "failed to add item exp_type");
     233            psFree(md);
     234            psFree(tokenCursor);
     235            psFree(tokens);
     236            return NULL;
     237        }
     238        if (!psMetadataAddStr(md, PS_LIST_TAIL, "uri", 0, NULL, uri)) {
     239            psError(PS_ERR_UNKNOWN, false, "failed to add item uri");
     240            psFree(md);
     241            psFree(tokenCursor);
     242            psFree(tokens);
     243            return NULL;
     244        }
     245
     246        psArrayAdd(summitExps, 0, md);
    178247
    179248        psFree(tokenCursor);
    180249        psFree(tokens);
    181250
    182         psArrayAdd(summitExps, 0, row);
    183251    }
    184252
Note: See TracChangeset for help on using the changeset viewer.