Changeset 18574 for trunk/ippTools/src/stacktool.c
- Timestamp:
- Jul 15, 2008, 7:55:19 PM (18 years ago)
- File:
-
- 1 edited
-
trunk/ippTools/src/stacktool.c (modified) (7 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/ippTools/src/stacktool.c
r18561 r18574 101 101 psMetadata *whereRE = psMetadataAlloc(); // rawExp where 102 102 psMetadata *whereWSF = psMetadataAlloc(); // warpSkyfile where 103 psMetadata *having = psMetadataAlloc(); // having 103 104 // map -inst -> camera, etc 104 105 PXOPT_COPY_STR(config->args, whereRE, "-select_inst", "camera", "=="); … … 119 120 PXOPT_COPY_F32(config->args, whereRE, "-select_solang_min", "solang", ">="); 120 121 PXOPT_COPY_F32(config->args, whereRE, "-select_solang_max", "solang", "<="); 121 PXOPT_COPY_STR(config->args, whereWSF, "-select_skycell_id", "skycell_id", "=="); 122 PXOPT_LOOKUP_BOOL(randomSubset, config->args, "-random_subset", false); 123 PXOPT_LOOKUP_S32(randomLimit, config->args, "-random_limit", false, false); 122 123 PXOPT_COPY_STR(config->args, having, "-select_skycell_id", "skycell_id", "=="); 124 125 PXOPT_LOOKUP_S32(randomLimit, config->args, "-random", false, false); 126 127 PXOPT_COPY_S32(config->args, having, "-min_num", "num_avail", ">="); 128 PXOPT_COPY_S32(config->args, having, "-min_new", "(num_avail - num_extant)", ">="); 129 130 PXOPT_LOOKUP_F32(min_frac, config->args, "-min_frac", false, false); 124 131 125 132 if (!psListLength(whereRE->list) && !psListLength(whereWSF->list) && … … 137 144 PXOPT_COPY_F32(config->args, whereWSF, "-select_good_frac_min", "good_frac", ">="); 138 145 139 psString query = pxDataGet("stacktool_find_complete_warps.sql");140 if (! query) {146 psString select = pxDataGet("stacktool_definebyquery_select.sql"); 147 if (!select) { 141 148 psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement"); 142 149 return false; 143 150 } 144 151 145 if ( whereRE) {152 if (psListLength(whereRE->list)) { 146 153 psString whereClause = psDBGenerateWhereConditionSQL(whereRE, "rawExp"); 147 psStringAppend(& query, " AND %s", whereClause);154 psStringAppend(&select, " AND %s", whereClause); 148 155 psFree(whereClause); 149 156 } 150 157 psFree(whereRE); 151 158 152 if ( whereWSF) {159 if (psListLength(whereWSF->list)) { 153 160 psString whereClause = psDBGenerateWhereConditionSQL(whereWSF, "warpSkyfile"); 154 psStringAppend(& query, " AND %s", whereClause);161 psStringAppend(&select, " AND %s", whereClause); 155 162 psFree(whereClause); 156 163 } 157 164 psFree(whereWSF); 158 165 159 if (!p_psDBRunQuery(config->dbh, query)) { 160 psError(PS_ERR_UNKNOWN, false, "database error"); 161 psFree(query); 162 return false; 163 } 164 psFree(query); 166 psString groupby = pxDataGet("stacktool_definebyquery_groupby.sql"); 167 if (!groupby) { 168 psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement"); 169 return false; 170 } 171 psStringAppend(&select, " %s", groupby); 172 psFree(groupby); 173 174 if (psListLength(having->list)) { 175 psString havingClause = psDBGenerateWhereConditionSQL(having, NULL); 176 psStringAppend(&select, " HAVING %s", havingClause); 177 psFree(havingClause); 178 } 179 180 // Gotta handle this one explicitly --- want to avoid divisions 181 if (isfinite(min_frac)) { 182 if (psListLength(having->list)) { 183 psStringAppend(&select, " AND"); 184 } else { 185 psStringAppend(&select, " HAVING"); 186 } 187 psStringAppend(&select, " num_avail >= %f * num_extant", (double)min_frac); 188 } 189 190 psFree(having); 191 192 if (!p_psDBRunQuery(config->dbh, select)) { 193 psError(PS_ERR_UNKNOWN, false, "database error"); 194 psFree(select); 195 return false; 196 } 197 psFree(select); 165 198 166 199 psArray *output = p_psDBFetchResult(config->dbh); … … 184 217 } 185 218 186 // valid_warpids should be NULL unless randomSubset is specified 187 psVector *valid_warpids = NULL; 188 if (randomSubset && (randomLimit < psArrayLength(output))) { 189 // loop over the array of metadata and figure out which warp_id are in 190 // the result set 191 psArray *warps = psArrayAlloc(0); 192 for (long i = 0; i < psArrayLength(output); i++) { 193 // psMetadata *md = output->data[i]; 194 195 // pull out the warp_id 196 bool status = false; 197 // psS64 warp_id = psMetadataLookupS64(&status, md, "warp_id"); 198 if (!status) { 199 psError(PS_ERR_UNKNOWN, false, "failed to lookup warp_id"); 200 psFree(output); 201 psFree(warps); 202 return NULL; 203 } 204 205 // psArrayAdd(warps, 0, warp_id); 206 } 207 208 // generate a random-valued vector, return an index sorted by the 209 // random values 210 psVector *randomVector = psVectorAlloc(psArrayLength(output), PS_TYPE_F32); 211 psRandom *rng = psRandomAlloc(PS_RANDOM_TAUS, 0); 212 for (int i = 0; i < randomVector->n; i++) { 213 randomVector->data.F32[i] = psRandomUniform(rng); 214 } 215 psVector *indexVector = psVectorSortIndex(NULL, randomVector); 216 psFree(randomVector); 217 218 // accept for first n of the sequence 219 valid_warpids = psVectorAlloc(randomLimit, PS_TYPE_S64); 220 for (int i = 0; i < randomLimit; i++ ){ 221 // int j = indexVector->data.S32[i]; 222 // valid_warpids->data[i] = psMemIncrRefCounter (warps->data[j]); 223 } 224 psFree(indexVector); 225 psFree(warps); 226 } 227 228 229 psHash *stacks = psHashAlloc(psArrayLength(output)); 230 231 // loop over the array of metadata and build a hash keyed by tess_id & 232 // skycell_id 233 for (long i = 0; i < psArrayLength(output); i++) { 234 psMetadata *md = output->data[i]; 235 219 psString insert = NULL; // Insertion query 220 if (randomLimit > 0) { 221 insert = pxDataGet("stacktool_definebyquery_insert_random.sql"); 222 } else { 223 insert = pxDataGet("stacktool_definebyquery_insert.sql"); 224 } 225 if (!insert) { 226 psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement"); 227 return false; 228 } 229 230 if (!psDBTransaction(config->dbh)) { 231 psError(PS_ERR_UNKNOWN, false, "database error"); 232 return false; 233 } 234 235 for (long i = 0; i < output->n; i++) { 236 psMetadata *row = output->data[i]; // Row from select 236 237 bool status; 237 if (valid_warpids) { 238 // psS64 warp_id = psMetadataLookupS64(&status, md, "warp_id"); 239 if (!status) { 240 psError(PS_ERR_UNKNOWN, false, "failed to lookup warp_id"); 241 psFree(valid_warpids); 242 psFree(output); 243 return NULL; 244 } 245 246 // is this a warp_id that should be left out of the stack set(s)? 247 // for (long i = 0; i < psArrayLength(valid_warpids); i++) { 248 // if (valid_warpids->data[i] == warp_id) { 249 // continue; 250 // } 251 // } 252 } 253 254 // pull out the skycell_id, & tess_id 255 psString skycell_id = psMetadataLookupStr(&status, md, "skycell_id"); 238 239 // pull out the skycell_id, tess_id, filter 240 psString skycell_id = psMetadataLookupStr(&status, row, "skycell_id"); 256 241 if (!status) { 257 242 psError(PS_ERR_UNKNOWN, false, "failed to lookup skycell_id"); 258 psFree(valid_warpids);259 243 psFree(output); 260 return NULL; 261 } 262 263 psString tess_id = psMetadataLookupStr(&status, md, "tess_id"); 244 psFree(insert); 245 if (!psDBRollback(config->dbh)) { 246 psError(PS_ERR_UNKNOWN, false, "database error"); 247 } 248 return false; 249 } 250 251 psString tess_id = psMetadataLookupStr(&status, row, "tess_id"); 264 252 if (!status) { 265 253 psError(PS_ERR_UNKNOWN, false, "failed to lookup tess_id"); 266 psFree(valid_warpids);267 254 psFree(output); 268 return NULL; 269 } 270 271 // group the warps by skycell_id & tess_id 272 psString key = NULL; 273 psStringAppend(&key, "%s:%s", skycell_id, tess_id); 274 275 // check to see if the hash key already exists 276 psArray *col = psHashLookup(stacks, key); 277 if (!col) { 278 // if it doesn't, create an new psArray for this stack 279 col = psArrayAllocEmpty(0); 280 psHashAdd(stacks, key, col); 281 } 282 // add this warp to the stack for this key 283 psArrayAdd(col, 0, md); 284 psFree(key); 285 286 } 287 psFree(valid_warpids); 288 psFree(output); 289 290 psArray *grouped = psHashToArray(stacks); 291 psFree(stacks); 292 293 if (!psDBTransaction(config->dbh)) { 294 psError(PS_ERR_UNKNOWN, false, "database error"); 295 return false; 296 } 297 298 // loop over groups 299 for (long i = 0; i < psArrayLength(grouped); i++) { 300 psArray *col = grouped->data[i]; 301 302 // pull the skycell_id & tess_id from the first warp in the stack 303 bool status; 304 psString skycell_id = psMetadataLookupStr(&status, col->data[0], "skycell_id"); 255 psFree(insert); 256 if (!psDBRollback(config->dbh)) { 257 psError(PS_ERR_UNKNOWN, false, "database error"); 258 } 259 return false; 260 } 261 262 psString filter = psMetadataLookupStr(&status, row, "filter"); 305 263 if (!status) { 306 psError(PS_ERR_UNKNOWN, false, "failed to lookup skycell_id"); 307 psFree(grouped); 308 return NULL; 309 } 310 311 psString tess_id = psMetadataLookupStr(&status, col->data[0], "tess_id"); 312 if (!status) { 313 psError(PS_ERR_UNKNOWN, false, "failed to lookup tess_id"); 314 psFree(grouped); 315 return NULL; 316 } 317 318 // create a new stackRun for the group 319 stackRunRow *run = stackRunRowAlloc( 320 0, // ID 321 "run", // state 322 workdir, 323 NULL, // dvodb 324 registered, 325 skycell_id, 326 tess_id 327 ); 264 psError(PS_ERR_UNKNOWN, false, "failed to lookup filter"); 265 psFree(output); 266 psFree(insert); 267 if (!psDBRollback(config->dbh)) { 268 psError(PS_ERR_UNKNOWN, false, "database error"); 269 } 270 return false; 271 } 272 273 // create a new stackRun for this stack 274 stackRunRow *run = stackRunRowAlloc(0, "run", workdir, NULL, registered, skycell_id, tess_id); 328 275 if (!stackRunInsertObject(config->dbh, run)) { 329 276 if (!psDBRollback(config->dbh)) { … … 331 278 } 332 279 psError(PS_ERR_UNKNOWN, false, "database error"); 280 psFree(output); 333 281 psFree(run); 334 psFree(grouped); 282 psFree(insert); 283 if (!psDBRollback(config->dbh)) { 284 psError(PS_ERR_UNKNOWN, false, "database error"); 285 } 335 286 return false; 336 287 } … … 343 294 psError(PS_ERR_UNKNOWN, false, "failed to print object"); 344 295 psFree(run); 345 psFree(grouped); 296 psFree(insert); 297 if (!psDBRollback(config->dbh)) { 298 psError(PS_ERR_UNKNOWN, false, "database error"); 299 } 346 300 return false; 347 301 } 348 302 psFree(run); 349 303 350 // loop over this stack and add each warp to the stackRun 351 for (long j = 0; j < psArrayLength(col); j++) { 352 bool status; 353 psS64 warp_id = psMetadataLookupS64(&status, col->data[j], "warp_id"); 354 if (!status) { 355 if (!psDBRollback(config->dbh)) { 356 psError(PS_ERR_UNKNOWN, false, "database error"); 357 } 358 psError(PS_ERR_UNKNOWN, false, "failed to lookup warp_id"); 359 psFree(grouped); 360 return NULL; 304 // Create a suitable insertion query for this run 305 psString thisInsert = psStringCopy(insert); 306 psString idString = NULL; 307 psStringAppend(&idString, "%" PRId64, stack_id); 308 psStringSubstitute(&thisInsert, idString, "@STACK_ID@"); 309 psFree(idString); 310 311 if ((randomLimit > 0 && !p_psDBRunQuery(config->dbh, thisInsert, skycell_id, tess_id, filter, 312 randomLimit)) || 313 (randomLimit <= 0 && !p_psDBRunQuery(config->dbh, thisInsert, skycell_id, tess_id, filter))) { 314 psError(PS_ERR_UNKNOWN, false, "database error"); 315 psFree(thisInsert); 316 psFree(insert); 317 psFree(output); 318 if (!psDBRollback(config->dbh)) { 319 psError(PS_ERR_UNKNOWN, false, "database error"); 361 320 } 362 363 if (!stackInputSkyfileInsert(config->dbh, 364 stack_id, 365 warp_id 366 )) { 367 if (!psDBRollback(config->dbh)) { 368 psError(PS_ERR_UNKNOWN, false, "database error"); 369 } 370 psError(PS_ERR_UNKNOWN, false, "database error"); 371 psFree(grouped); 372 return false; 373 } 374 375 } 376 377 } 378 379 // point of no return 321 return false; 322 } 323 psFree(thisInsert); 324 } 325 380 326 if (!psDBCommit(config->dbh)) { 381 327 psError(PS_ERR_UNKNOWN, false, "database error"); … … 383 329 } 384 330 385 psFree( grouped);331 psFree(output); 386 332 387 333 return true;
Note:
See TracChangeset
for help on using the changeset viewer.
