7 #include "StDbManager.hh"
9 #include "StDbConfigNode.hh"
10 #include "StDbTable.h"
11 #include "StDbXmlReader.h"
12 #include "StDbXmlWriter.h"
13 #include "StDbElementIndex.hh"
26 #include "spinDbAPI.h"
29 const char *cvsRevision=
"$Revision: 1.1 $";
30 const char *keyToEverything =
"jas";
31 const char *dbTimeFormat[] = {
41 enum SpinDbAction action = GetDB;
45 char *dbExpTime = NULL;
47 char *dbFlavor =
"ofl";
48 char *dbComment = getenv(
"USER");
49 char *dbName =
"Calibrations_rhic";
50 int dataOnlyMode =
false;
51 int debugMode =
false;
54 getDbIO(
const char *node,
const int ndata) {
60 if (!strncmp(node,
"spinV124" ,SpinDbMaxDbPathLen))
62 else if (!strncmp(node,
"spinStar" ,SpinDbMaxDbPathLen))
64 else if (!strncmp(node,
"spinBXmask" ,SpinDbMaxDbPathLen))
68 fprintf(stderr,
"table %s unknown to %s\n",node,argv0);
73 fprintf(stderr,
"table %s unknown to %s\n",node,argv0);
84 if(node==NULL)
return items;
85 for(
int i=0;i<level;i++) fprintf(out,
"\t");
87 if(level>0) fprintf(out,
"%s/\n",node->printName());
88 if( node->hasData() ) {
91 while( ( t = ti->next() ) ) {
92 for(
int i=0;i<=level;i++) fprintf(out,
"\t");
93 fprintf(out,
"%s",t->printName());
94 fprintf(out,
":%s",t->getCstructName());
99 (void) printTree(out,node->getFirstChildNode(),level+1);
100 (void) printTree(out,node->getNextNode() ,level );
109 FILE *null = fopen(
"/dev/null",
"w");
111 time_t tUnix=getTimeStamp(dbTimeFormat,dbTime);
113 fprintf(out,
"\nRec BeginDate (BNL) UnixTime ValidPeriod Comment\n");
115 mgr->setRequestTime(tUnix);
116 int ret = mgr->fetchDbTable(dbTab);
120 io->setData(dbTab->GetTable());
123 time_t tdiff= dbTab->getEndTime()- dbTab->getBeginTime();
124 int sec = tdiff % 60; tdiff /= 60;
125 int min = tdiff % 60; tdiff /= 60;
126 int hour = tdiff % 24; tdiff /= 24;
128 char *tstr =
new char[32];
129 time_t bt = dbTab->getBeginTime();
131 strftime(tstr,32,dbTimeFormat[0],localtime(&bt));
132 fprintf(out,
"%-3d %.20s %d %6dday(s) %02d:%02d:%02d %s\n",
133 nRec, tstr, dbTab->getBeginTime(),
134 day,hour,min,sec, io->getComment());
136 tOld = dbTab->getBeginTime();
139 tUnix= dbTab->getEndTime();
140 if(tUnix>SpinDbMaxUnixTime)
break;
149 const char *eTime=
"entryTime";
150 const char *vKey =
"versionKey";
158 Db<<
"select "<< vKey <<
"," << eTime <<
" from Nodes where nodeType='Config'"<<endsql;
160 while(Db.Output(&buff)){
161 buff.ReadArray(var,len,vKey);
162 fprintf(out,
"%s : ",*var);
163 buff.ReadArray(var,len,eTime);
164 fprintf(out,
"%s ",fmtSqlTime(*var));
174 const int vLen = strlen(cvsRevision);
175 static char *vStr =
new char[vLen];
176 strncpy(vStr,cvsRevision+1,vLen-1);
184 usage(
const char *message=NULL)
186 if (message) fprintf(stderr,
"%s: %s\n",argv0,message);
187 fprintf(stderr,
"usage: %s --path <path> [EXTRA_OPTIONS]\n",argv0);
188 fprintf(stderr,
" -D|--database <name> : data base to use (default %s)\n",dbName);
189 fprintf(stderr,
" -p|--path <path> : full path to the table\n");
190 fprintf(stderr,
" -t|--time <time> : sets query time (default: now)\n");
191 fprintf(stderr,
" -x|--expire <time> : sets query expiration time (default: forever)\n");
192 fprintf(stderr,
" -F|--flavor : set database flavor (default ofl)\n");
193 fprintf(stderr,
" -f|--file <file> : set file name for I/O (default: stdin/stdout)\n");
194 fprintf(stderr,
" -g|-r|--get|--read : get (read) data from database (default)\n");
195 fprintf(stderr,
" -s|-w|--set|--write : set (write) data to database\n");
196 fprintf(stderr,
" -c|--comment <txt> : set db comment (default user id)\n");
197 fprintf(stderr,
" -T|--tree : print config tree\n");
198 fprintf(stderr,
" -H|--history : print time line for node\n");
199 fprintf(stderr,
" -C|--config : print available config versions\n");
200 fprintf(stderr,
" -d|--dataonly : don't write #node/table line, just the data\n");
201 fprintf(stderr,
" -v|--verbose : set verbose mode on\n");
202 fprintf(stderr,
" -q|--quiet : set quiet mode on\n");
204 fprintf(stderr,
" -h|--help : this short help\n");
205 fprintf(stderr,
" supported time formats (cf. man date):\n");
206 for(
int i=0; dbTimeFormat[i]!=NULL ; i++ ) {
210 strftime(tmpstr,MLEN,dbTimeFormat[i],gmtime(&t));
211 fprintf(stderr,
" %-20s e.g.: %s\n",dbTimeFormat[i],tmpstr);
213 fprintf(stderr,
"%s\n",printVersion());
214 if(message) exit(-1);
222 main(
int argc,
char *argv[])
228 static struct option optLong[] = {
229 {
"database" , 1 , 0 ,
'D' },
230 {
"path" , 1 , 0 ,
'p' },
231 {
"time" , 1 , 0 ,
't' },
232 {
"expiration" , 1 , 0 ,
'x' },
233 {
"flavor" , 1 , 0 ,
'F' },
234 {
"file" , 1 , 0 ,
'f' },
235 {
"get" , 0 , (
int *)&action , GetDB },
236 {
"read" , 0 , (
int *)&action , GetDB },
237 {
"set" , 0 , (
int *)&action , SetDB },
238 {
"write" , 0 , (
int *)&action , SetDB },
239 {
"tree" , 0 , (
int *)&action , PrintTree },
240 {
"history" , 0 , (
int *)&action , PrintHistory },
241 {
"config" , 0 , (
int *)&action , PrintConfig },
242 {
"comment" , 1 , 0 ,
'c' },
243 {
"verbose" , 0 , &verboseMode ,
true},
244 {
"quiet" , 0 , &verboseMode ,
false},
245 {
"dataonly" , 0 , &dataOnlyMode ,
true},
246 {
"debug" , 0 , &debugMode ,
true},
247 {
"help" , 0 , 0 ,
'h' },
253 argv0 = strrchr(argv[0],
'/');
254 argv0 = ( argv0 == NULL ) ? argv[0] : ++argv0 ;
256 while((optChar = getopt_long(argc,argv,
"D:p:t:x:F:f:c:grswTHCdvqh",optLong,&optInd)) != EOF) {
259 case 'D' : dbName = optarg;
break;
260 case 'p' : dbPath = optarg;
break;
261 case 't' : dbTime = optarg;
break;
262 case 'x' : dbExpTime = optarg;
break;
263 case 'F' : dbFlavor = optarg;
break;
264 case 'f' : dbFile = optarg;
break;
266 case 'g' : action = GetDB;
break;
268 case 's' : action = SetDB;
break;
269 case 'T' : action = PrintTree;
break;
270 case 'H' : action = PrintHistory;
break;
271 case 'C' : action = PrintConfig;
break;
272 case 'c' : dbComment = optarg;
break;
273 case 'd' : dataOnlyMode=
true;
break;
274 case 'v' : verboseMode =
true;
break;
275 case 'q' : quietMode =
true;
break;
276 case 'h' : usage();
return(0);
break;
277 default : usage(
"unknown option");
break;
280 if(!dbPath && action!=PrintConfig ) usage(
"database path missing");
290 mgr->setVerbose(verboseMode);
291 mgr->setQuiet(quietMode);
294 if( ! mgr->findDefaultServer() ) {
295 fprintf(stderr,
"cannot find the default DB server\n");
300 file = fopen(dbFile,(action==SetDB) ?
"r" :
"w");
302 fprintf(stderr,
"%s: fopen '%s' failed, %s\n",argv0,dbFile,strerror(errno));
306 file = (action==SetDB) ? stdin : stdout;
311 node = node=strsep(&dbPath,
"/");
313 dbNode = mgr->initConfig(dbName,node);
315 if(verboseMode || !quietMode)
316 fprintf (file,
"DATABASE TREE:\n");
317 fprintf (file,
"%s/\n",node);
318 if(printTree(file,dbNode)<=0) {
319 fprintf (stderr,
"config %s is empty\n",node);
326 if(verboseMode || !quietMode)
327 fprintf(file,
"DATABASE VERSIONS:\n");
328 printConfig(file,mgr);
336 while( (node=strsep(&dbPath,
"/")) != NULL ) {
338 mgr->initConfig(dbName,node) : dbNode->findConfigNode(node);
341 dprintf(
" database node: %p\n",node);
344 if(!node) usage(
"invalid path");
346 dbTab = dbNode->findTable(node);
347 if(!dbTab) { fprintf(stderr,
"%s: table %s not found\n",argv0,node);
return (-1); }
349 dprintf(
"found table: %s\n",node);
351 int nrows=dbTab->GetNRows();
355 if(dbIO==NULL)
return(-1);
357 char keyLine[SpinDbMaxDbPathLen];
358 char keyBase[SpinDbMaxKeyLength];
359 time_t tUnix = getTimeStamp(dbTimeFormat,dbTime);
360 time_t tExpUnix = (dbExpTime!=NULL) ? getTimeStamp(dbTimeFormat,dbExpTime) : 0;
362 dbTab->setFlavor(dbFlavor);
365 printHistory(file,mgr,dbTab,dbIO);
369 fgets (keyLine,SpinDbMaxDbPathLen-1,file);
370 sprintf(keyBase,SpinDbKeyFormat,dbNode->printName(),node);
372 if(strstr(keyLine,keyBase)==0 && strcmp(keyLine,keyToEverything) ) {
373 fprintf(stderr,
"signature mismatch: data file key '%s', required '%s'\n",
378 dbIO->setComment(dbComment);
380 ndata = dbIO->read(file);
382 fprintf(stderr,
"%s: reading file %s failed\n",argv0,dbFile);
386 dbTab->
SetTable(dbIO->getData(),ndata, dbIO->getIndices());
388 dbTab->
SetTable(dbIO->getData(),ndata);
391 dbTab->setEndStoreTime(tExpUnix);
392 fprintf(stderr,
"%s: setting end time %ld\n",argv0,tExpUnix);
396 mgr->setStoreTime(tUnix);
397 if(! mgr->storeDbTable(dbTab) ) {
398 fprintf(stderr,
"%s: storing table %s failed\n",argv0,node);
404 mgr ->setRequestTime(tUnix);
405 if( ! mgr->fetchDbTable(dbTab) ) {
406 fprintf(stderr,
"%s: problem with fetch for time stamp %ld / %s",argv0,tUnix,ctime(&tUnix));
407 fprintf(stderr,
" There is no record for this table over the time period :\n");
408 fprintf(stderr,
" BeginDate = %s\t",dbTab->getBeginDateTime());
409 fprintf(stderr,
" EndDate = %s\n",dbTab->getEndDateTime());
410 fprintf(stderr,
" BeginTimeUnix = %d\t",dbTab->getBeginTime());
411 fprintf(stderr,
" EndTimeUnix = %d\n",dbTab->getEndTime());
414 if(! dbTab->GetTable() ) {
415 fprintf(stderr,
"no data found in table %s\n",node);
418 dbIO->setData(dbTab->GetTable());
420 if(verboseMode || !quietMode)
421 fprintf(stderr,
"BNL Time Stamp Range from: %s to: %s \n",
422 fmtSqlTime(dbTab->getBeginDateTime()),fmtSqlTime(dbTab->getEndDateTime()));
424 fprintf(file,SpinDbKeyFormat,dbNode->printName(),node);
427 ndata = dbIO->write(file);
428 dbComment = dbIO->getComment();
429 if(verboseMode || !quietMode)
430 fprintf(stderr,
"COMMENT: %s\n",dbComment);
437 dprintf(
"database access successful %d\n",ndata);
439 fprintf(stderr,
"%s: table %s access failed\n",argv0,node);
virtual void SetTable(char *data, int nrows, int *idList=0)
calloc'd version of data for StRoot
static StDbManager * Instance()
strdup(..) is not ANSI