IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Feb 21, 2013, 1:18:35 PM (13 years ago)
Author:
watersc1
Message:

Updated SQL and database interactions for WSdiffs. This should allow it to run in a reasonable amount of time and without blocking the database.

File:
1 edited

Legend:

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

    r34770 r35192  
    12261226}
    12271227
    1228 
     1228static bool definewarpstackMode(pxConfig *config)
     1229{
     1230  PS_ASSERT_PTR_NON_NULL(config, false);
     1231
     1232  psMetadata *where = psMetadataAlloc();
     1233
     1234  PXOPT_COPY_S64(config->args, where, "-exp_id", "exp_id", "==");
     1235  PXOPT_COPY_STR(config->args, where, "-filter", "filter", "==");
     1236  PXOPT_COPY_STR(config->args, where, "-comment", "comment", "LIKE");
     1237  PXOPT_COPY_S64(config->args, where, "-warp_id", "warpRun.warp_id", "==");
     1238  PXOPT_COPY_STR(config->args, where, "-warp_label", "warpRun.label", "==");
     1239  PXOPT_COPY_STR(config->args, where, "-tess_id", "warpRun.tess_id", "==");
     1240  PXOPT_COPY_STR(config->args, where, "-data_group", "warpRun.data_group", "==");
     1241  PXOPT_COPY_STR(config->args, where, "-skycell_id", "warpSkyfile.skycell_id", "==");
     1242  PXOPT_COPY_F32(config->args, where,  "-good_frac", "warpSkyfile.good_frac", ">=");
     1243  PXOPT_COPY_STR(config->args, where, "-stack_label", "stackRun.label", "==");
     1244  PXOPT_COPY_STR(config->args, where, "-stack_data_group", "stackRun.data_group", "==");
     1245
     1246  PXOPT_LOOKUP_BOOL(bothways, config->args, "-bothways", false);
     1247 
     1248  PXOPT_LOOKUP_STR(workdir, config->args, "-set_workdir", true, false); // required option
     1249  PXOPT_LOOKUP_STR(reduction, config->args, "-set_reduction", false, false); // option
     1250  PXOPT_LOOKUP_STR(label, config->args, "-set_label", true, false); // option
     1251  PXOPT_LOOKUP_STR(data_group, config->args, "-set_data_group", false, false);
     1252  PXOPT_LOOKUP_STR(dist_group, config->args, "-set_dist_group", false, false);
     1253  PXOPT_LOOKUP_STR(note, config->args, "-set_note", false, false);
     1254  PXOPT_LOOKUP_TIME(registered, config->args, "-set_registered", false, false);
     1255 
     1256  PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
     1257  PXOPT_LOOKUP_BOOL(newTemplates, config->args, "-new-templates", false);
     1258  PXOPT_LOOKUP_BOOL(reRun, config->args, "-rerun", false);
     1259  PXOPT_LOOKUP_BOOL(available, config->args, "-available", false);
     1260  PXOPT_LOOKUP_BOOL(pretend, config->args, "-pretend", false);
     1261
     1262
     1263  // Get query file
     1264  psString query = pxDataGet("difftool_definewarpstack.sql");
     1265  if (!query) {
     1266    psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
     1267    return false;
     1268  }
     1269
     1270  psString whereClause = psDBGenerateWhereConditionSQL(where,NULL);
     1271 
     1272  // Don't queue things that already have diffs.
     1273  if (! (reRun || newTemplates) ) {
     1274    psStringAppend(&whereClause, "\nAND diff_id IS NULL\n");
     1275  }
     1276  // Append diff qualifiers, if we have them
     1277  if (label) {
     1278    psStringAppend(&whereClause, "\nAND ((diffRun.label = '%s') OR (diffRun.label IS NULL))",label);
     1279  }
     1280  if (data_group) {
     1281    psStringAppend(&whereClause, "\nAND ((diffRun.data_group = '%s') OR (diffRun.data_group IS NULL))",data_group);
     1282  }
     1283  if (reduction) {
     1284    psStringAppend(&whereClause, "\nAND ((diffRun.reduction = '%s') OR (diffRun.reduction IS NULL))",reduction);
     1285  }
     1286
     1287  psString whereClauseString = psStringCopy("");
     1288  psStringAppend(&whereClauseString, " \n AND %s ", whereClause);
     1289  //  fprintf(stderr,query,whereClauseString);
     1290
     1291  // This is just a simple query, so we don't need to do a transaction
     1292  if (!p_psDBRunQueryF(config->dbh, query, whereClauseString)) {
     1293    psError(PS_ERR_UNKNOWN, false, "database error");
     1294    psFree(query);
     1295    psFree(whereClause);
     1296    return(false);
     1297  }
     1298  psFree(query);
     1299  psFree(whereClause);
     1300  psFree(whereClauseString);
     1301 
     1302  psArray *output = p_psDBFetchResult(config->dbh);
     1303  if (!output) {
     1304    psErrorCode err = psErrorCodeLast();
     1305    switch (err) {
     1306    case PS_ERR_DB_CLIENT:
     1307      psError(PXTOOLS_ERR_SYS, false, "database error");
     1308      break;
     1309    case PS_ERR_DB_SERVER:
     1310      psError(PXTOOLS_ERR_PROG, false, "database error");
     1311      break;
     1312    default:
     1313      psError(PXTOOLS_ERR_PROG, false, "unknown error");
     1314      break;
     1315    }
     1316    return false;
     1317  }
     1318  if (!psArrayLength(output)) {
     1319    psTrace("difftool", PS_LOG_INFO, "no rows found");
     1320    psFree(output);
     1321    return true;
     1322  }
     1323  if (pretend) {
     1324    // negative simple so the default is true
     1325    if (!ippdbPrintMetadatas(stdout, output, "diffRun", !simple)) {
     1326      psError(PS_ERR_UNKNOWN, false, "failed to print array");
     1327      psFree(output);
     1328      return false;
     1329    }
     1330    psFree(output);
     1331    return true;
     1332  }
     1333
     1334  psArray *list = psArrayAllocEmpty(16); // List of runs, to print
     1335  long numGood = 0;
     1336  psS64 last_exp_id = 0;
     1337  psS64 diff_id = 0;
     1338  for (long i = 0; i < output->n; i++) {
     1339    psMetadata *row = output->data[i];
     1340    bool mdok;
     1341
     1342    psS64 exp_id = psMetadataLookupS64(&mdok, row, "exp_id");
     1343    if (!mdok) {
     1344        psError(PXTOOLS_ERR_PROG, false, "exp_id not found");
     1345        if (!psDBRollback(config->dbh)) {
     1346          psError(PS_ERR_UNKNOWN, false, "database error");
     1347        }
     1348        return false;
     1349    }
     1350    if (exp_id != last_exp_id) {
     1351      if (diff_id != 0) { // We've added a run already, and are now switching to a new one.
     1352        // Set state to new
     1353        if (!setdiffRunState(config, diff_id, "new", false)) {
     1354          psError(PS_ERR_UNKNOWN, false, "failed to change diffRun.state for diff_id: %" PRId64, diff_id);
     1355          if (!psDBRollback(config->dbh)) {
     1356            psError(PS_ERR_UNKNOWN, false, "database error");
     1357          }
     1358          return false;
     1359        }
     1360        // Commit results
     1361        if (!psDBCommit(config->dbh)) {
     1362          psError(PS_ERR_UNKNOWN, false, "database error");
     1363          psFree(list);
     1364          return false;
     1365        }
     1366      }
     1367     
     1368      // Begin transaction
     1369      if (!psDBTransaction(config->dbh)) {
     1370        psError(PS_ERR_UNKNOWN, false, "database error");
     1371        return false;
     1372      }
     1373      // Add a new diffRun row
     1374      psString tess_id = psMetadataLookupStr(&mdok, row, "tess_id");
     1375      if (!mdok) {
     1376        psError(PXTOOLS_ERR_PROG, false, "tess_id not found");
     1377        if (!psDBRollback(config->dbh)) {
     1378          psError(PS_ERR_UNKNOWN, false, "database error");
     1379        }
     1380        return false;
     1381      }
     1382      diffRunRow *run = diffRunRowAlloc(
     1383                                        0,        // ID
     1384                                        "reg",    // state
     1385                                        workdir,
     1386                                        label,
     1387                                        data_group ? data_group : label,
     1388                                        dist_group,
     1389                                        reduction,
     1390                                        NULL,     // dvodb
     1391                                        registered,
     1392                                        tess_id,
     1393                                        bothways, // bothways (default is false)
     1394                                        true,     // exposure
     1395                                        0,        // magicked
     1396                                        NULL,     // software_version
     1397                                        0,        // mask stat npix
     1398                                        NAN,      // static
     1399                                        NAN,      // dynamic
     1400                                        NAN,      // magic
     1401                                        NAN,      // advisory
     1402                                        IPP_DIFF_MODE_WARP_STACK,
     1403                                        note
     1404                                        );
     1405      if (!diffRunInsertObject(config->dbh, run)) {
     1406        psError(PS_ERR_UNKNOWN, false, "database error");
     1407        psFree(run);
     1408        if (!psDBRollback(config->dbh)) {
     1409          psError(PS_ERR_UNKNOWN, false, "database error");
     1410        }
     1411      }
     1412      diff_id = psDBLastInsertID(config->dbh);
     1413      run->diff_id = diff_id;
     1414
     1415      psArrayAdd(list, list->n, run);
     1416      numGood++;
     1417     
     1418      last_exp_id = exp_id;                                     
     1419    } // End Adding diffRun
     1420   
     1421    if (exp_id == last_exp_id) {
     1422      psString skycell_id = psMetadataLookupStr(&mdok, row, "skycell_id");
     1423      if (!mdok) {
     1424        psError(PXTOOLS_ERR_PROG, false, "skycell_id not found");
     1425        if (!psDBRollback(config->dbh)) {
     1426          psError(PS_ERR_UNKNOWN, false, "database error");
     1427        }
     1428        return false;
     1429      }
     1430      psString tess_id = psMetadataLookupStr(&mdok, row, "tess_id");
     1431      if (!mdok) {
     1432        psError(PXTOOLS_ERR_PROG, false, "tess_id not found");
     1433        if (!psDBRollback(config->dbh)) {
     1434          psError(PS_ERR_UNKNOWN, false, "database error");
     1435        }
     1436        return false;
     1437      }
     1438      psS64 warp_id = psMetadataLookupS64(&mdok, row, "warp_id");
     1439      if (!mdok) {
     1440        psError(PXTOOLS_ERR_PROG, false, "warp_id not found");
     1441        if (!psDBRollback(config->dbh)) {
     1442          psError(PS_ERR_UNKNOWN, false, "database error");
     1443        }
     1444        return false;
     1445      }
     1446      psS64 stack_id = psMetadataLookupS64(&mdok, row, "stack_id");
     1447      if (!mdok) {
     1448        psError(PXTOOLS_ERR_PROG, false, "stack_id not found");
     1449        if (!psDBRollback(config->dbh)) {
     1450          psError(PS_ERR_UNKNOWN, false, "database error");
     1451        }
     1452        return false;
     1453      }
     1454
     1455      // Add a new skyfile row
     1456      diffInputSkyfileRow *skyfile = diffInputSkyfileRowAlloc(
     1457                                                              diff_id,   // ID
     1458                                                              skycell_id,
     1459                                                              warp_id, // warp1_id
     1460                                                              PS_MAX_S64, // stack1 -> NULL
     1461                                                              PS_MAX_S64, // warp2_id -> NULL
     1462                                                              stack_id, // stack2
     1463                                                              tess_id,
     1464                                                              0 // diff_skyfile_id
     1465                                                              );
     1466      //      fprintf(stderr,"%"PRId64 " %"PRId64 " %"PRId64 " %s %s\n",diff_id,warp_id,stack_id,skycell_id, tess_id);
     1467      if (!diffInputSkyfileInsertObject(config->dbh, skyfile)) {
     1468        psError(PS_ERR_UNKNOWN, false, "database error");
     1469        psFree(list);
     1470        if (!psDBRollback(config->dbh)) {
     1471          psError(PS_ERR_UNKNOWN, false, "database error");
     1472        }
     1473        return false;
     1474      }
     1475    } // End Adding skyfile
     1476  } // End parsing result set
     1477 
     1478  // Finish the last run's update and close the connection
     1479  if (diff_id != 0) {
     1480    if (!setdiffRunState(config, diff_id, "new", false)) {
     1481      psError(PS_ERR_UNKNOWN, false, "failed to change diffRun.state for diff_id: %" PRId64, diff_id);
     1482      if (!psDBRollback(config->dbh)) {
     1483        psError(PS_ERR_UNKNOWN, false, "database error");
     1484      }
     1485      return false;
     1486    }
     1487    // Commit results
     1488    if (!psDBCommit(config->dbh)) {
     1489      psError(PS_ERR_UNKNOWN, false, "database error");
     1490      psFree(list);
     1491      return false;
     1492    }
     1493  }
     1494 
     1495  if (numGood && !diffRunPrintObjects(stdout, list, !simple)) {
     1496    psError(PS_ERR_UNKNOWN, false, "failed to print object");
     1497    psFree(list);
     1498    return(false);
     1499  }
     1500  psFree(list);
     1501  // Free things
     1502  return true;
     1503
     1504
     1505#if (0)
    12291506static bool definewarpstackMode(pxConfig *config)
    12301507{
     
    16141891    return true;
    16151892}
     1893#endif
    16161894
    16171895static bool definewarpwarpMode(pxConfig *config)
Note: See TracChangeset for help on using the changeset viewer.