00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00119 #include "jsoc_main.h"
00120 #include "drms.h"
00121 #include "drms_names.h"
00122
00123 ModuleArgs_t module_args[] =
00124 {
00125 {ARG_STRING, "ds", "Not Specified", "<record_set query>"},
00126 {ARG_STRING, "key", "Not Specified", "<comma delimited keyword list>"},
00127 {ARG_STRING, "seg", "Not Specified", "<comma delimited segment list>"},
00128 {ARG_FLAG, "a", "0", "Show info for all keywords"},
00129 {ARG_FLAG, "h", "0", "help - print usage info"},
00130 {ARG_FLAG, "l", "0", "just list series keywords with descriptions"},
00131 {ARG_INT, "n", "0", "number of records to show, +from first, -from last"},
00132 {ARG_FLAG, "p", "0", "list the record\'s storage_unit path"},
00133 {ARG_FLAG, "P", "0", "list the record\'s storage_unit path but no retrieve"},
00134 {ARG_FLAG, "k", "0", "keyword list one per line"},
00135 {ARG_FLAG, "q", "0", "quiet - skip header of chosen keywords"},
00136 {ARG_FLAG, "r", "0", "recnum - show record number as first keyword"},
00137 {ARG_END}
00138 };
00139
00140 char *module_name = "show_keys";
00141 int nice_intro ()
00142 {
00143 int usage = cmdparams_get_int (&cmdparams, "h", NULL);
00144 if (usage)
00145 {
00146 printf ("Usage:\nshow_keys [-ahklpqr] "
00147 "ds=<recordset query> {n=0} {key=<keylist>} {seg=<segment_list>}\n"
00148 " details are:\n"
00149 " -a: show information for all keywords\n"
00150 " -h: help - show this message then exit\n"
00151 " -k: list keyword names and values, one per line\n"
00152 " -l: list all keywords with description, then exit\n"
00153 " -p: list the record's storage_unit path (retrieve if necessary)\n"
00154 " -P: list the record's storage_unit path (no retrieve)\n"
00155 " -q: quiet - skip header of chosen keywords\n"
00156 " -r: recnum - show record number as first keyword\n"
00157 "ds=<recordset query> as <series>{[record specifier]} - required\n"
00158 "n=0 number of records in query to show, +n from start or -n from end\n"
00159 "key=<comma delimited keyword list>, for all use -a flag\n"
00160 "seg=<comma delimited segment list>\n"
00161 "The -p or -P flag will show the record directory by itself or as part of the\n"
00162 "full path to the segment file if seg=<segmentname> is specified.\n"
00163 "Note that the -p flag will cause the data to be staged if offline.\n");
00164 return(1);
00165 }
00166 return (0);
00167 }
00168
00169
00170 int DoIt(void)
00171 {
00172 int firstrec=1;
00173 int status = 0;
00174 DRMS_RecordSet_t *recordset;
00175 DRMS_Record_t *rec;
00176 int first_rec, last_rec, nrecs, irec;
00177 char *keys[1000];
00178 char *segs[1000];
00179 int ikey, nkeys = 0;
00180 int iseg, nsegs = 0;
00181 char *inqry;
00182
00183 char *in = cmdparams_get_str (&cmdparams, "ds", NULL);
00184 char *keylist = strdup (cmdparams_get_str (&cmdparams, "key", NULL));
00185 char *seglist = strdup (cmdparams_get_str (&cmdparams, "seg", NULL));
00186 int show_keys = strcmp (keylist, "Not Specified");
00187 int show_segs = strcmp (seglist, "Not Specified");
00188 int list_keys = cmdparams_get_int (&cmdparams, "l", NULL) != 0;
00189 int show_all = cmdparams_get_int (&cmdparams, "a", NULL) != 0;
00190 int max_recs = cmdparams_get_int (&cmdparams, "n", NULL);
00191 int quiet = cmdparams_get_int (&cmdparams, "q", NULL) != 0;
00192 int show_recnum = cmdparams_get_int(&cmdparams, "r", NULL) != 0;
00193 int keyword_list = cmdparams_get_int(&cmdparams, "k", NULL) != 0;
00194 int want_path = cmdparams_get_int (&cmdparams, "p", NULL) != 0;
00195 int want_path_noret = cmdparams_get_int (&cmdparams, "P", NULL) != 0;
00196
00197 if (nice_intro ()) return (0);
00198
00199 if(want_path_noret) want_path = 1;
00200 if (strcmp(in, "Not Specified") == 0)
00201 {
00202 printf("### show_keys: ds=<record_query> parameter is required, but I will look for a query without the ds=\n");
00203 if (cmdparams_numargs(&cmdparams) >= 1 && (in = cmdparams_getarg (&cmdparams, 1)))
00204 {
00205 printf("### found \"%s\", using it for the record_query.\n",in);
00206 }
00207 else
00208 {
00209 printf("### show_keys: Oops, still no query found, quit\n");
00210 return(1);
00211 }
00212 }
00213 if (list_keys)
00214 {
00215 char *p, *seriesname;
00216 DRMS_Record_t *rec;
00217 DRMS_Keyword_t *key, **prime_keys;
00218 DRMS_Segment_t *seg;
00219 HIterator_t hit;
00220 int nprime, iprime;
00221
00222
00223 seriesname = strdup (in);
00224 if ((p = index(seriesname,'['))) *p = '\0';
00225 rec = drms_template_record (drms_env, seriesname, &status);
00226 if (status)
00227 {
00228 fprintf(stderr,"### show_keys: series %s not found.\n",seriesname);
00229 return(1);
00230 }
00231
00232
00233 nprime = rec->seriesinfo->pidx_num;
00234 prime_keys = rec->seriesinfo->pidx_keywords;
00235 if (nprime > 0)
00236 {
00237 printf("Prime Keys are:\n");
00238 for (iprime = 0; iprime < nprime; iprime++)
00239 printf("\t%s\n", prime_keys[iprime]->info->name);
00240 }
00241
00242
00243 printf("All Keywords for series %s:\n",seriesname);
00244 hiter_new (&hit, &rec->keywords);
00245 while ((key = (DRMS_Keyword_t *)hiter_getnext (&hit)))
00246 printf ("\t%-10s\t%s (%s)\n", key->info->name, key->info->description,
00247 drms_type_names[key->info->type]);
00248
00249
00250 if (rec->segments.num_total)
00251 {
00252 printf("Segments for series %s:\n",seriesname);
00253 hiter_new (&hit, &rec->segments);
00254 while ((seg = (DRMS_Segment_t *)hiter_getnext (&hit)))
00255 printf ("\t%-10s\t%s\n", seg->info->name, seg->info->description);
00256 }
00257
00258 if (seriesname)
00259 {
00260 free (seriesname);
00261 }
00262
00263 return (0);
00264 }
00265
00266
00267 inqry = index(in, '[');
00268 if (!inqry && !max_recs)
00269 {
00270 fprintf(stderr, "### show_keys query must have n=recs or record query specified\n");
00271 return(1);
00272 }
00273
00274
00275 recordset = drms_open_records (drms_env, in, &status);
00276 if (!recordset)
00277 {
00278 fprintf(stderr,"### show_keys: series %s not found.\n",in);
00279 return (1);
00280 }
00281
00282
00283
00284
00285
00286 nrecs = recordset->n;
00287 if (nrecs == 0)
00288 {
00289 if (!quiet)
00290 printf ("** No records in selected data set, query was %s **\n",in);
00291 return (0);
00292 }
00293 if (max_recs > 0 && max_recs < nrecs)
00294 nrecs = max_recs;
00295 last_rec = nrecs - 1;
00296 if (max_recs < 0 && nrecs+max_recs > 0)
00297 first_rec = nrecs + max_recs;
00298 else
00299 first_rec = 0;
00300
00301
00302 nkeys = 0;
00303 if (show_all)
00304 {
00305 DRMS_Keyword_t *key;
00306 HIterator_t hit;
00307 hiter_new (&hit, &recordset->records[0]->keywords);
00308 while ((key = (DRMS_Keyword_t *)hiter_getnext (&hit)))
00309 keys[nkeys++] = strdup (key->info->name);
00310 }
00311 else if (show_keys)
00312 {
00313 char *thiskey;
00314 for (thiskey=strtok(keylist, ","); thiskey; thiskey=strtok(NULL,","))
00315 keys[nkeys++] = strdup(thiskey);
00316 }
00317 free (keylist);
00318
00319
00320 nsegs = 0;
00321 if (show_segs)
00322 {
00323 char *thisseg;
00324 for (thisseg=strtok(seglist, ","); thisseg; thisseg=strtok(NULL,","))
00325 segs[nsegs++] = strdup(thisseg);
00326 }
00327 free (seglist);
00328
00329
00330 for (irec = first_rec; irec <= last_rec; irec++)
00331 {
00332 rec = recordset->records[irec];
00333 if (firstrec)
00334 {
00335 firstrec=0;
00336 if (!quiet && !keyword_list)
00337 {
00338 if (show_recnum)
00339 printf ("recnum\t");
00340 for (ikey=0 ; ikey<nkeys; ikey++)
00341 printf ("%s\t",keys[ikey]);
00342 for (iseg = 0; iseg<nsegs; iseg++)
00343 printf ("%s\t",segs[iseg]);
00344 if (nsegs==0 && want_path)
00345 printf("SUDIR");
00346 printf ("\n");
00347 }
00348 }
00349
00350 if (keyword_list)
00351 {
00352 printf("# ");
00353 drms_print_rec_query(rec);
00354 printf("\n");
00355 }
00356
00357
00358
00359
00360 if (show_recnum)
00361 {
00362 if (keyword_list)
00363 printf("recnum=%lld\n",rec->recnum);
00364 else
00365 printf ("%6lld\t", rec->recnum);
00366 }
00367
00368
00369 for (ikey=0; ikey<nkeys; ikey++)
00370 {
00371 DRMS_Keyword_t *rec_key_ikey = drms_keyword_lookup (rec, keys[ikey], 1);
00372 if (ikey)
00373 printf (keyword_list ? "\n" : "\t");
00374 if (rec_key_ikey)
00375 {
00376 if (keyword_list)
00377 {
00378 printf("%s=", keys[ikey]);
00379 if (rec_key_ikey->info->type != DRMS_TYPE_STRING)
00380 drms_keyword_printval (rec_key_ikey);
00381 else
00382 {
00383 printf("\"");
00384 drms_keyword_printval (rec_key_ikey);
00385 printf("\"");
00386 }
00387 }
00388 else
00389 drms_keyword_printval (rec_key_ikey);
00390 }
00391 else if (!keyword_list)
00392 printf ("MISSING");
00393 }
00394 if(nkeys)
00395 printf (keyword_list ? "\n" : "\t");
00396
00397
00398 for (iseg=0; iseg<nsegs; iseg++)
00399 {
00400 DRMS_Segment_t *rec_seg_iseg = drms_segment_lookup (rec, segs[iseg]);
00401 if(iseg)
00402 printf (keyword_list ? "\n" : "\t");
00403 if (rec_seg_iseg)
00404 {
00405 char fname[DRMS_MAXPATHLEN];
00406 char path[DRMS_MAXPATHLEN];
00407 if (want_path)
00408 if(want_path_noret) drms_record_directory (rec, path, 0);
00409 else drms_record_directory (rec, path, 1);
00410 else
00411 strcpy(path,"");
00412 strncpy(fname, rec_seg_iseg->filename, DRMS_MAXPATHLEN);
00413 if (keyword_list)
00414 printf("%s=", segs[iseg]);
00415 printf("%s%s%s", path, (want_path ? "/" : ""), fname);
00416 }
00417 else if (!keyword_list)
00418 printf ("NO_FILENAME");
00419 }
00420 if (nsegs==0 && want_path)
00421 {
00422 char path[DRMS_MAXPATHLEN];
00423 if(want_path_noret) drms_record_directory (rec, path, 0);
00424 else drms_record_directory (rec, path, 1);
00425 if (keyword_list)
00426 printf("SUDIR=");
00427 printf("%s", path);
00428 }
00429 if (show_recnum || nkeys || nsegs || want_path)
00430 printf ("\n");
00431 }
00432
00433
00434 for (ikey=0; ikey<nkeys; ikey++)
00435 free(keys[ikey]);
00436 drms_close_records(recordset, DRMS_FREE_RECORD);
00437 return status;
00438 }