Changeset 10014
- Timestamp:
- Nov 15, 2006, 6:51:26 PM (20 years ago)
- File:
-
- 1 edited
-
trunk/ippTools/src/pzgetimfiles.c (modified) (9 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/ippTools/src/pzgetimfiles.c
r9735 r10014 31 31 #define FILESET_LS_CMD "dsfilesetls" 32 32 33 static bool go (pxConfig *config); 33 34 static psArray *parseFiles(pxConfig *config, const char *str); 34 35 … … 37 38 pxConfig *config = pzgetimfilesConfig(NULL, argc, argv); 38 39 39 // invoke dsfilesetls 40 if (!go(config)) { 41 goto FAIL; 42 } 43 44 psFree(config); 45 exit(EXIT_SUCCESS); 46 47 FAIL: 48 psFree(config); 49 50 exit(EXIT_FAILURE); 51 } 52 53 static bool go (pxConfig *config) 54 { 55 PS_ASSERT_PTR_NON_NULL(config, NULL); 56 40 57 bool status = false; 41 58 psString uri = psMetadataLookupStr(&status, config->args, "-uri"); 59 if (!status) { 60 psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -uri"); 61 return false; 62 } 63 if (!uri) { 64 psError(PS_ERR_UNKNOWN, true, "-uri is required"); 65 return false; 66 } 67 68 psString filesetid = psMetadataLookupStr(&status, config->args, "-filesetid"); 69 if (!status) { 70 psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -filesetid"); 71 return false; 72 } 73 if (!filesetid) { 74 psError(PS_ERR_UNKNOWN, true, "-filesetid is required"); 75 return false; 76 } 77 78 // invoke dsfilesetls 42 79 psString cmd = NULL; 43 80 psStringAppend(&cmd, "%s --uri %s", FILESET_LS_CMD, uri); … … 51 88 if (!output) { 52 89 psError(PS_ERR_UNKNOWN, true, "popen() failed"); 53 goto FAIL;90 return false; 54 91 } 55 92 … … 60 97 psError(PS_ERR_UNKNOWN, true, "%s failed with exit status %d", 61 98 FILESET_LS_CMD, exitStatus); 62 goto FAIL;99 return false; 63 100 } 64 101 … … 68 105 // XXX not nessicarily an error 69 106 psError(PS_ERR_UNKNOWN, true, "no new files/imfiles"); 70 goto FAIL; 107 psFree(cmdOutput); 108 return false; 109 } 110 111 psFree(cmdOutput); 112 113 // start a transaction so it's all rows or nothing 114 if (!psDBTransaction(config->dbh)) { 115 psError(PS_ERR_UNKNOWN, false, "database error"); 116 return false; 71 117 } 72 118 73 119 // try not to insert duplicate pzPendingExp/pzPendingImfile entries 74 // XXX this will become very expensive as the number of records grows 75 // XXX change psDB to support inserting data with 'not exists'76 psArray *pzPendingImfiles =77 pzPendingImfileSelectRowObjects(config->dbh, NULL, 0);78 if (pzPendingImfiles) {79 for (long i = 0; i < psArrayLength(newImfiles); i++) { 80 pzPendingImfileRow *newImfile = newImfiles->data[i];81 for (long j = 0; j < psArrayLength(pzPendingImfiles); j++) {82 pzPendingImfileRow *pendingImfile = pzPendingImfiles->data[j];83 if (strcmp(newImfile->exp_id,84 pendingImfile->exp_id) == 0) {85 psArrayRemoveData(newImfiles, newImfile);86 // dec the counter as the array just got shorter 87 // and we don't want to skip elemnts88 i--;89 break; 90 }91 }92 }93 psFree(pzPendingImfiles);94 }95 96 // insert new entries into the database97 for (long i = 0; i < psArrayLength(newImfiles); i++) {98 if (!pzPendingImfileInsertObject(config->dbh, newImfiles->data[i])) {99 ps Error(PS_ERR_UNKNOWN, false, "dbh access failed");100 goto FAIL;120 121 // create a temp table 122 { 123 char *query = 124 "CREATE TEMPORARY TABLE incoming (exp_id VARCHAR(64), camera VARCHAR(64), telescope VARCHAR(64), bytes INT, md5sum VARCHAR(32), class VARCHAR(64), class_id VARCHAR(64), uri VARCHAR(255), PRIMARY KEY(exp_id, camera, telescope, class, class_id)) ENGINE=MEMORY"; 125 126 if (!p_psDBRunQuery(config->dbh, query)) { 127 psError(PS_ERR_UNKNOWN, false, "database error"); 128 psFree(newImfiles); 129 return false; 130 } 131 } 132 133 { 134 char *query = "INSERT INTO incoming (exp_id, camera, telescope, bytes, md5sum, class, class_id, uri) VALUES (?, ?, ?, ?, ?, ?, ?, ?)"; 135 136 long inserted = p_psDBRunQueryPrepared(config->dbh, newImfiles, query); 137 if (inserted < 0) { 138 psError(PS_ERR_UNKNOWN, false, "database error"); 139 psFree(newImfiles); 140 return false; 141 } 142 // sanity check that we actually inserted something 143 if (inserted == 0) { 144 psError(PS_ERR_UNKNOWN, false, "database error -- we should have inserted at least one row"); 145 psFree(newImfiles); 146 return false; 101 147 } 102 148 } 103 149 104 150 // save the number of new Imfiles; 105 long nImfiles = psArrayLength(newImfiles);151 long imfiles = psArrayLength(newImfiles); 106 152 psFree(newImfiles); 107 108 // find the summitExp entry for our exp_id 109 psArray *summitExps = NULL; 110 { 111 psMetadata *where = psMetadataAlloc(); 112 bool status = false; 113 psString exp_id = psMetadataLookupStr(&status, config->args, 114 "-filesetid"); 115 if (!status) { 116 psError(PS_ERR_UNKNOWN, false, "failed to find arg exp_id"); 117 psFree(where); 118 goto FAIL; 119 } 120 if (!psMetadataAddStr(where, PS_LIST_TAIL, "exp_id", 0, "==", exp_id)) { 121 psError(PS_ERR_UNKNOWN, false, "failed to add item exp_id"); 122 psFree(where); 123 goto FAIL; 124 } 125 summitExps = summitExpSelectRowObjects(config->dbh, where, 0); 126 psFree(where); 127 if (!summitExps) { 128 // this REALLY should not happen 129 psError(PS_ERR_UNKNOWN, false, 130 "failed to find summitExp with exp_id %s (should not happen)" 131 , exp_id); 132 goto FAIL; 133 } 134 } 135 136 // insert a new pzPendingExp 137 summitExpRow *summitExp = summitExps->data[0]; 138 pzPendingExpRow *pzPendingExp = pzPendingExpRowAlloc( 139 summitExp->exp_id, 140 summitExp->camera, 141 summitExp->telescope, 142 summitExp->exp_type, 143 nImfiles 144 ); 145 psFree(summitExps); 146 if (!pzPendingExp) { 147 psError(PS_ERR_UNKNOWN, false, "pzPendingExpRowAlloc() failed"); 148 goto FAIL; 149 } 150 151 if (!pzPendingExpInsertObject(config->dbh, pzPendingExp)) { 152 psError(PS_ERR_UNKNOWN, false, "dbh access failed"); 153 psFree(pzPendingExp); 154 goto FAIL; 155 } 156 157 psFree(pzPendingExp); 158 159 psFree(config); 160 exit(EXIT_SUCCESS); 161 162 FAIL: 163 psFree(config); 164 165 exit(EXIT_FAILURE); 153 { 154 char *query = 155 "INSERT INTO pzPendingImfile" 156 " SElECT" 157 " incoming.exp_id," 158 " incoming.camera," 159 " incoming.telescope," 160 " incoming.class," 161 " incoming.class_id," 162 " pzPendingExp.exp_tag" 163 " FROM incoming" 164 " JOIN pzPendingExp" 165 " USING(exp_id, camera, telescope)" 166 " LEFT JOIN summitImfile" 167 " USING(exp_id, camera, telescope, class, class_id)" 168 " WHERE" 169 " summitImfile.exp_id is NULL" 170 " AND summitImfile.camera is NULL" 171 " AND summitImfile.telescope is NULL" 172 " AND summitImfile.class is NULL" 173 " AND summitImfile.class_id is NULL"; 174 175 if (!p_psDBRunQuery(config->dbh, query)) { 176 psError(PS_ERR_UNKNOWN, false, "database error"); 177 return false; 178 } 179 } 180 181 182 { 183 char *query = 184 "INSERT INTO summitImfile" 185 " SElECT" 186 " incoming.exp_id," 187 " incoming.camera," 188 " incoming.telescope," 189 " incoming.bytes," 190 " incoming.md5sum," 191 " incoming.class," 192 " incoming.class_id," 193 " incoming.uri" 194 " FROM incoming" 195 " LEFT JOIN summitImfile" 196 " USING(exp_id, camera, telescope, class, class_id)" 197 " WHERE" 198 " summitImfile.exp_id is NULL" 199 " AND summitImfile.camera is NULL" 200 " AND summitImfile.telescope is NULL" 201 " AND summitImfile.class is NULL" 202 " AND summitImfile.class_id is NULL"; 203 204 if (!p_psDBRunQuery(config->dbh, query)) { 205 psError(PS_ERR_UNKNOWN, false, "database error"); 206 return false; 207 } 208 } 209 210 // update summitExp.imfiles 211 { 212 char *query = "UPDATE summitExp SET imfiles = %d"; 213 if (!p_psDBRunQuery(config->dbh, query, imfiles)) { 214 psError(PS_ERR_UNKNOWN, false, "database error"); 215 return false; 216 } 217 } 218 219 if (!psDBCommit(config->dbh)) { 220 psError(PS_ERR_UNKNOWN, false, "database error"); 221 return false; 222 } 223 224 return true; 166 225 } 167 226 … … 171 230 PS_ASSERT_PTR_NON_NULL(str, NULL); 172 231 232 // these are constants for all records parsed -- look them up before we do 233 // any work 173 234 bool status = false; 174 psString camera = psMetadataLookupStr(&status, config->args, "-inst"); 175 psString telescope = psMetadataLookupStr(&status, config->args, "-telescope"); 176 235 psString exp_id = psMetadataLookupStr(&status, config->args, "-filesetid"); 236 if (!status) { 237 psError(PS_ERR_UNKNOWN, false, "failed to lookup value for '-filesetid'"); 238 return NULL; 239 } 240 if (!exp_id) { 241 psError(PS_ERR_UNKNOWN, true, "-filesetid is required"); 242 return NULL; 243 } 244 245 psString camera = psMetadataLookupStr(&status, config->args, "-inst"); 246 if (!status) { 247 psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -inst"); 248 return NULL; 249 } 250 if (!camera) { 251 psError(PS_ERR_UNKNOWN, true, "-inst is required"); 252 return NULL; 253 } 254 255 psString telescope = psMetadataLookupStr(&status, config->args, "-telescope"); 256 if (!status) { 257 psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -telescope"); 258 return NULL; 259 } 260 if (!telescope) { 261 psError(PS_ERR_UNKNOWN, true, "-telescope is required"); 262 return NULL; 263 } 264 265 // split the string into lines 177 266 psList *doc = psStringSplit(str, "\n", false); 178 267 … … 182 271 psString line; 183 272 while ((line = psListGetAndIncrement(lineCursor))) { 184 // XXX debugging185 printf("-> %s\n", line);186 187 273 // split line into tokens 188 274 psList *tokens = psStringSplit(line, " ", false); … … 202 288 } 203 289 290 // find the values of interest 204 291 psListIterator *tokenCursor = psListIteratorAlloc(tokens, 0, false); 205 292 char *uri = psListGetAndIncrement(tokenCursor); … … 209 296 char *class = psListGetAndIncrement(tokenCursor); // type 210 297 211 bool status = false; 212 psString exp_id = psMetadataLookupStr(&status, config->args, 213 "-filesetid"); 214 215 pzPendingImfileRow *row = pzPendingImfileRowAlloc( 216 exp_id, 217 camera, 218 telescope, 219 (psS32)atol(bytes), 220 md5sum, 221 class, 222 class_id, 223 uri 224 ); 298 // create a new metadata to represent this line and it's values 299 psMetadata *md = psMetadataAlloc(); 300 if (!psMetadataAddStr(md, PS_LIST_TAIL, "exp_id", 0, NULL, exp_id)) { 301 psError(PS_ERR_UNKNOWN, false, "failed to add item exp_id"); 302 psFree(md); 303 psFree(tokenCursor); 304 psFree(tokens); 305 return NULL; 306 } 307 if (!psMetadataAddStr(md, PS_LIST_TAIL, "camera", 0, NULL, camera)) { 308 psError(PS_ERR_UNKNOWN, false, "failed to add item camera"); 309 psFree(md); 310 psFree(tokenCursor); 311 psFree(tokens); 312 return NULL; 313 } 314 if (!psMetadataAddStr(md, PS_LIST_TAIL, "telescope", 0, NULL, telescope)) { 315 psError(PS_ERR_UNKNOWN, false, "failed to add item telescope"); 316 psFree(md); 317 psFree(tokenCursor); 318 psFree(tokens); 319 return NULL; 320 } 321 if (!psMetadataAddS32(md, PS_LIST_TAIL, "bytes", 0, NULL, (psS32)atoi(bytes))) { 322 psError(PS_ERR_UNKNOWN, false, "failed to add item bytes"); 323 psFree(md); 324 psFree(tokenCursor); 325 psFree(tokens); 326 return NULL; 327 } 328 if (!psMetadataAddStr(md, PS_LIST_TAIL, "md5sum", 0, NULL, md5sum)) { 329 psError(PS_ERR_UNKNOWN, false, "failed to add item md5sum"); 330 psFree(md); 331 psFree(tokenCursor); 332 psFree(tokens); 333 return NULL; 334 } 335 if (!psMetadataAddStr(md, PS_LIST_TAIL, "class", 0, NULL, class)) { 336 psError(PS_ERR_UNKNOWN, false, "failed to add item class"); 337 psFree(md); 338 psFree(tokenCursor); 339 psFree(tokens); 340 return NULL; 341 } 342 if (!psMetadataAddStr(md, PS_LIST_TAIL, "class_id", 0, NULL, class_id)) { 343 psError(PS_ERR_UNKNOWN, false, "failed to add item class_id"); 344 psFree(md); 345 psFree(tokenCursor); 346 psFree(tokens); 347 return NULL; 348 } 349 if (!psMetadataAddStr(md, PS_LIST_TAIL, "uri", 0, NULL, uri)) { 350 psError(PS_ERR_UNKNOWN, false, "failed to add item uri"); 351 psFree(md); 352 psFree(tokenCursor); 353 psFree(tokens); 354 return NULL; 355 } 225 356 226 357 psFree(tokenCursor); 227 358 psFree(tokens); 228 359 229 psArrayAdd(pzPendingImfiles, 0, row); 360 psArrayAdd(pzPendingImfiles, 0, md); 361 psMetadataConfigPrint(stdout, md); 230 362 } 231 363
Note:
See TracChangeset
for help on using the changeset viewer.
