CMS 3D CMS Logo

revisionDML.py
Go to the documentation of this file.
1 from __future__ import print_function
2 from __future__ import absolute_import
3 ############################################################
4 # LumiDB Revision and Versioning API
5 #
6 # Author: Zhen Xie
7 ############################################################
8 
9 import coral
10 from RecoLuminosity.LumiDB import nameDealer,idDealer,dbUtil
11 #==============================
12 # SELECT
13 #==============================
14 def revisionsInTag(schema,tagrevisionid,branchid):
15  '''
16  returns all revisions before tag in selected branch
17  select revision_id from revisions where revision_id!=0 and revision_id<tagrevisionid and branch_id=:branchid
18  result=[revision_id]
19  '''
20  result=[]
21  qHandle=schema.newQuery()
22  try:
23  nextbranches=[]
24  qHandle.addToTableList( nameDealer.revisionTableName() )
25  qHandle.addToOutputList('distinct BRANCH_ID','branch_id')
26  qCondition=coral.AttributeList()
27  qCondition.extend('branchid','unsigned long long')
28  qCondition['branchid'].setData(branchid)
29  qResult=coral.AttributeList()
30  qResult.extend('branch_id','unsigned long long')
31  qHandle.defineOutput(qResult)
32  qHandle.setCondition('BRANCH_ID>:branchid',qCondition)
33  cursor=qHandle.execute()
34  while next(cursor):
35  nextbranches.append(cursor.currentRow()['branch_id'].data())
36  del qHandle
37  candidates=[]
38  conditionStr='REVISION_ID!=0 and BRANCH_ID=:branchid and REVISION_ID<:tagrevisionid'
39  qHandle=schema.newQuery()
40  qHandle.addToTableList( nameDealer.revisionTableName() )
41  qHandle.addToOutputList('REVISION_ID','revision_id')
42  qCondition=coral.AttributeList()
43  qCondition.extend('branchid','unsigned long long')
44  qCondition.extend('tagrevisionid','unsigned long long')
45  qCondition['branchid'].setData(branchid)
46  qCondition['tagrevisionid'].setData(tagrevisionid)
47  qResult=coral.AttributeList()
48  qResult.extend('revision_id','unsigned long long')
49  qHandle.defineOutput(qResult)
50  qHandle.setCondition(conditionStr,qCondition)
51  cursor=qHandle.execute()
52  while next(cursor):
53  candidates.append(cursor.currentRow()['revision_id'].data())
54  del qHandle
55  for c in candidates:
56  if c in nextbranches:
57  continue
58  result.append(c)
59  return result
60  except:
61  if qHandle:del qHandle
62  raise
63 def revisionsInBranch(schema,branchid):
64  '''
65  returns all revision values in a branch
66  result=[revision_id]
67  select distinct branch_id from revisions where branch_id>:branchid;
68  select revision_id from revisions where branch_id=:branchid ;
69  if the branchid matches and the revisionid is not in the branchid collection,not 0, then this revision is in the branch
70  require also revisionid>branchid
71  '''
72  result=[]
73  qHandle=schema.newQuery()
74  try:
75  nextbranches=[]
76  qHandle.addToTableList( nameDealer.revisionTableName() )
77  qHandle.addToOutputList('distinct BRANCH_ID','branch_id')
78  qCondition=coral.AttributeList()
79  qCondition.extend('branchid','unsigned long long')
80  qCondition['branchid'].setData(branchid)
81  qResult=coral.AttributeList()
82  qResult.extend('branch_id','unsigned long long')
83  qHandle.defineOutput(qResult)
84  qHandle.setCondition('BRANCH_ID>:branchid',qCondition)
85  cursor=qHandle.execute()
86  while next(cursor):
87  nextbranches.append(cursor.currentRow()['branch_id'].data())
88  del qHandle
89  candidates=[]
90  conditionStr='BRANCH_ID=:branchid and REVISION_ID!=0'
91  qHandle=schema.newQuery()
92  qHandle.addToTableList( nameDealer.revisionTableName() )
93  qHandle.addToOutputList('REVISION_ID','revision_id')
94  qCondition=coral.AttributeList()
95  qCondition.extend('branchid','unsigned long long')
96  qCondition['branchid'].setData(branchid)
97  qResult=coral.AttributeList()
98  qResult.extend('revision_id','unsigned long long')
99  qHandle.defineOutput(qResult)
100  qHandle.setCondition(conditionStr,qCondition)
101  cursor=qHandle.execute()
102  while next(cursor):
103  candidates.append(cursor.currentRow()['revision_id'].data())
104  del qHandle
105  for c in candidates:
106  if c in nextbranches:
107  continue
108  result.append(c)
109  return result
110  except:
111  if qHandle: del qHandle
112  raise
113 
114 def branchType(schema,name):
115  '''
116  output: tag,branch
117  the difference between tag and branch: tag is an empty branch
118  select count(revision_id) from revisions where branch_name=:name
119  if >0: is real branch
120  else: is tag
121  '''
122  result='tag'
123  try:
124  qHandle=schema.newQuery()
125  qHandle.addToTableList( nameDealer.revisionTableName() )
126  qHandle.addToOutputList('count(REVISION_ID)','nchildren')
127  qCondition=coral.AttributeList()
128  qCondition.extend('branch_name','string')
129  qCondition['branch_name'].setData(name)
130  qResult=coral.AttributeList()
131  qResult.extend('nchildren','unsigned int')
132  qHandle.defineOutput(qResult)
133  conditionStr='BRANCH_NAME=:branch_name'
134  qHandle.setCondition(conditionStr,qCondition)
135  cursor=qHandle.execute()
136  while next(cursor):
137  if cursor.currentRow()['nchildren'].data()>0:
138  result='branch'
139  del qHandle
140  return result
141  except :
142  raise
143 #def revisionsInBranch(schema,branchid):
144 # '''
145 # returns all revision values in a branch/tag
146 # result=[revision_id]
147 # select r.revision_id from revisions r where r.branch_id=:branchid and r.revision_id not in (select distinct a.branch_id from revisions a where a.branch_id>:branchid)
148 # '''
149 # result=[]
150 # try:
151 # qHandle=schema.newQuery()
152 # subquery=qHandle.defineSubQuery('B')
153 # subquery.addToTableList( nameDealer.revisionTableName(),'a' )
154 # subquery.addToOutputList('distinct a.BRANCH_ID')
155 # subqueryCondition=coral.AttributeList()
156 # subqueryCondition.extend('branchid','unsigned long long')
157 # subqueryCondition['branchid'].setData(branchid)
158 # subquery.setCondition('a.BRANCH_ID>:branchid',subqueryCondition)
159 #
160 # qHandle.addToTableList( nameDealer.revisionTableName(),'r' )
161 # qHandle.addToTableList( 'B')
162 # qHandle.addToOutputList('r.REVISION_ID','revision_id')
163 # qCondition=coral.AttributeList()
164 # qCondition.extend('branchid','unsigned long long')
165 # qCondition['branchid'].setData(branchid)
166 # qResult=coral.AttributeList()
167 # qResult.extend('revision_id','unsigned long long')
168 # qHandle.defineOutput(qResult)
169 # conditionStr='r.BRANCH_ID=:branchid AND r.REVISION_ID NOT IN B'
170 # qHandle.setCondition(conditionStr,qCondition)
171 # cursor=qHandle.execute()
172 # while cursor.next():
173 # result.append(cursor.currentRow()['revision_id'].data())
174 # del qHandle
175 # return result
176 # except :
177 # raise
178 
179 def revisionsInBranchName(schema,branchname):
180  '''
181  returns all revisions in a branch/tag by name
182  '''
183  result=[]
184  try:
185  (revision_id,branch_id)=branchInfoByName(schema,branchname)
186  result=revisionsInBranch(schema,revision_id)
187  return result
188  except :
189  raise
190 def entryInBranch(schema,datatableName,entryname,branch):
191  '''
192  whether an entry(by name) already exists in the given branch
193  select e.entry_id from entrytable e,revisiontable r where r.revision_id=e.revision_id and e.name=:entryname and r.branch_name=branchname/branch_id
194  input:
195  if isinstance(branch,str):byname
196  else: byid
197  output:entry_id/None
198  '''
199  try:
200  result=None
201  byname=False
202  if isinstance(branch,str):
203  byname=True
204  qHandle=schema.newQuery()
205  qHandle.addToTableList( nameDealer.entryTableName(datatableName),'e' )
206  qHandle.addToTableList( nameDealer.revisionTableName(),'r' )
207  qHandle.addToOutputList('e.ENTRY_ID','entry_id')
208  qCondition=coral.AttributeList()
209  qCondition.extend('entryname','string')
210  qCondition['entryname'].setData(entryname)
211  qConditionStr='r.REVISION_ID=e.REVISION_ID and e.NAME=:entryname and '
212  if byname:
213  qCondition.extend('branch_name','string')
214  qCondition['branch_name'].setData(branch)
215  qConditionStr+='r.BRANCH_NAME=:branch_name'
216  else:
217  qCondition.extend('branch_id','unsigned long long')
218  qCondition['branch_id'].setData(branch)
219  qConditionStr+='r.BRANCH_ID=:branch_id'
220  qResult=coral.AttributeList()
221  qResult.extend('entry_id','unsigned long long')
222  qHandle.defineOutput(qResult)
223  qHandle.setCondition(qConditionStr,qCondition)
224  cursor=qHandle.execute()
225  while next(cursor):
226  entry_id=cursor.currentRow()['entry_id'].data()
227  result=entry_id
228  del qHandle
229  return result
230  except :
231  raise
232 
233 def dataRevisionsOfEntry(schema,datatableName,entry,revrange):
234  '''
235  all data version of the given entry whose revision falls in branch revision range
236  select d.data_id,r.revision_id from datatable d, datarevmaptable r where d.entry_id(or name )=:entry and d.data_id=r.data_id
237  input: if isinstance(entry,str): d.entry_name=:entry ; else d.entry_id=:entry
238  output: [data_id]
239  '''
240  qHandle=schema.newQuery()
241  try:
242  result=[]
243  byname=False
244  if isinstance(entry,str):
245  byname=True
246  qHandle.addToTableList( datatableName,'d' )
247  qHandle.addToTableList( nameDealer.revmapTableName(datatableName), 'r')
248  qHandle.addToOutputList('d.DATA_ID','data_id')
249  qHandle.addToOutputList('r.REVISION_ID','revision_id')
250  qCondition=coral.AttributeList()
251  qConditionStr='d.DATA_ID=r.DATA_ID and '
252  if byname:
253  qCondition.extend('entry_name','string')
254  qCondition['entry_name'].setData(entry)
255  qConditionStr+='d.ENTRY_NAME=:entry_name'
256  else:
257  qCondition.extend('entry_id','unsigned long long')
258  qCondition['entry_id'].setData(entry)
259  qConditionStr+='d.ENTRY_ID=:entry_id'
260  qResult=coral.AttributeList()
261  qResult.extend('data_id','unsigned long long')
262  qResult.extend('revision_id','unsigned long long')
263  qHandle.defineOutput(qResult)
264  qHandle.setCondition(qConditionStr,qCondition)
265  cursor=qHandle.execute()
266  while next(cursor):
267  data_id=cursor.currentRow()['data_id'].data()
268  revision_id=cursor.currentRow()['revision_id'].data()
269  if revision_id in revrange:
270  result.append(data_id)
271  return result
272  except :
273  del qHandle
274  raise
275 
276 def latestDataRevisionOfEntry(schema,datatableName,entry,revrange):
277  '''
278  return max(data_id) of all datarevisionofEntry
279  '''
280  result=dataRevisionsOfEntry(schema,datatableName,entry,revrange)
281  if result and len(result)!=0: return max(result)
282  return None
283 
284 def branchInfoByName(schema,branchName):
285  '''
286  select (revision_id,branch_id) from revisions where name=:branchName
287  '''
288  try:
289  qHandle=schema.newQuery()
290  qHandle.addToTableList( nameDealer.revisionTableName() )
291  qHandle.addToOutputList('REVISION_ID','revision_id')
292  qHandle.addToOutputList('BRANCH_ID','branch_id')
293  qCondition=coral.AttributeList()
294  qCondition.extend('name','string')
295  qCondition['name'].setData(branchName)
296  qResult=coral.AttributeList()
297  qResult.extend('revision_id','unsigned long long')
298  qResult.extend('branch_id','unsigned long long')
299  qHandle.defineOutput(qResult)
300  qHandle.setCondition('NAME=:name',qCondition)
301  cursor=qHandle.execute()
302  revision_id=None
303  branch_id=None
304  while next(cursor):
305  revision_id=cursor.currentRow()['revision_id'].data()
306  branch_id=cursor.currentRow()['branch_id'].data()
307  del qHandle
308  return (revision_id,branch_id)
309  except Exception as e :
310  raise RuntimeError(' revisionDML.branchInfoByName: '+str(e))
311 
312 
313 #=======================================================
314 #
315 # INSERT requires in update transaction
316 #
317 #=======================================================
318 def bookNewEntry(schema,datatableName):
319  '''
320  allocate new revision_id,entry_id,data_id
321  '''
322  try:
323  entrytableName=nameDealer.entryTableName(datatableName)
324  iddealer=idDealer.idDealer(schema)
325  revision_id=iddealer.generateNextIDForTable( nameDealer.revisionTableName() )
326  data_id=iddealer.generateNextIDForTable( datatableName)
327  entry_id=iddealer.generateNextIDForTable( nameDealer.entryTableName(datatableName) )
328  return (revision_id,entry_id,data_id)
329  except:
330  raise
331 
332 def bookNewRevision(schema,datatableName):
333  '''
334  allocate new revision_id,data_id
335  '''
336  try:
337  iddealer=idDealer.idDealer(schema)
338  revision_id=iddealer.generateNextIDForTable( nameDealer.revisionTableName() )
339  data_id=iddealer.generateNextIDForTable(datatableName)
340  return (revision_id,data_id)
341  except:
342  raise
343 
344 def addEntry(schema,datatableName,entryinfo,branchinfo):
345  '''
346  input:
347  entryinfo (revision_id(0),entry_id(1),entry_name(2),data_id(3))
348  branchinfo (branch_id,branch_name)
349  1.allocate and insert a new revision into the revisions table
350  2.allocate and insert a new entry into the entry table with the new revision
351  3.inset into data_rev table with new data_id ,revision)id mapping
352 
353  insert into revisions(revision_id,branch_id,branch_name,comment,ctime) values()
354  insert into datatablename_entries (entry_id,revision_id) values()
355  insert into datatablename_rev(data_id,revision_id) values()
356  '''
357  try:
358  revisiontableName=nameDealer.revisionTableName()
359  entrytableName=nameDealer.entryTableName(datatableName)
360  revtableName=nameDealer.revmapTableName(datatableName)
361 
362  db=dbUtil.dbUtil(schema)
363  tabrowDefDict={}
364  tabrowDefDict['REVISION_ID']='unsigned long long'
365  tabrowDefDict['BRANCH_ID']='unsigned long long'
366  tabrowDefDict['BRANCH_NAME']='string'
367  tabrowDefDict['CTIME']='time stamp'
368  tabrowValueDict={}
369  tabrowValueDict['REVISION_ID']=entryinfo[0]
370  tabrowValueDict['BRANCH_ID']=branchinfo[0]
371  tabrowValueDict['BRANCH_NAME']=branchinfo[1]
372  tabrowValueDict['CTIME']=coral.TimeStamp()
373  db.insertOneRow(revisiontableName,tabrowDefDict,tabrowValueDict)
374 
375  tabrowDefDict={}
376  tabrowDefDict['REVISION_ID']='unsigned long long'
377  tabrowDefDict['ENTRY_ID']='unsigned long long'
378  tabrowDefDict['NAME']='string'
379 
380  tabrowValueDict={}
381  tabrowValueDict['REVISION_ID']=entryinfo[0]
382  tabrowValueDict['ENTRY_ID']=entryinfo[1]
383  tabrowValueDict['NAME']=entryinfo[2]
384  db.insertOneRow(entrytableName,tabrowDefDict,tabrowValueDict)
385 
386  tabrowDefDict={}
387  tabrowDefDict['REVISION_ID']='unsigned long long'
388  tabrowDefDict['DATA_ID']='unsigned long long'
389  tabrowValueDict={}
390  tabrowValueDict['REVISION_ID']=entryinfo[0]
391  tabrowValueDict['DATA_ID']=entryinfo[3]
392  db.insertOneRow(revtableName,tabrowDefDict,tabrowValueDict)
393  except:
394  raise
395 
396 def addRevision(schema,datatableName,revisioninfo,branchinfo):
397  '''
398  1.insert a new revision into the revisions table
399  2.insert into data_id, revision_id pair to datatable_revmap
400  insert into revisions(revision_id,branch_id,branch_name,ctime) values()
401  insert into datatable_rev(data_id,revision_id) values())
402  input:
403  revisioninfo (revision_id(0),data_id(1))
404  branchinfo (branch_id(0),branch_name(1))
405  '''
406  try:
407  revisiontableName=nameDealer.revisionTableName()
408  revtableName=nameDealer.revmapTableName(datatableName)
409 
410  db=dbUtil.dbUtil(schema)
411  tabrowDefDict={}
412  tabrowDefDict['REVISION_ID']='unsigned long long'
413  tabrowDefDict['BRANCH_ID']='unsigned long long'
414  tabrowDefDict['BRANCH_NAME']='string'
415  tabrowDefDict['CTIME']='time stamp'
416 
417  tabrowValueDict={}
418  tabrowValueDict['REVISION_ID']=revisioninfo[0]
419  tabrowValueDict['BRANCH_ID']=branchinfo[0]
420  tabrowValueDict['BRANCH_NAME']=branchinfo[1]
421  tabrowValueDict['CTIME']=coral.TimeStamp()
422 
423  db.insertOneRow(revisiontableName,tabrowDefDict,tabrowValueDict)
424 
425  tabrowDefDict={}
426  tabrowDefDict['REVISION_ID']='unsigned long long'
427  tabrowDefDict['DATA_ID']='unsigned long long'
428  tabrowValueDict={}
429  tabrowValueDict['REVISION_ID']=revisioninfo[0]
430  tabrowValueDict['DATA_ID']=revisioninfo[1]
431  db.insertOneRow(revtableName,tabrowDefDict,tabrowValueDict)
432  except:
433  raise
434 def createBranch(schema,name,parentname,comment=''):
435  '''
436  create a new branch/tag under given parentnode
437  insert into revisions(revision_id,branch_id,branch_name,name,comment,ctime) values()
438  return (revisionid,parentid,parentname)
439  '''
440  try:
441  parentid=None
442  revisionid=0
443  if not parentname is None:
444  qHandle=schema.newQuery()
445  qHandle.addToTableList( nameDealer.revisionTableName() )
446  qHandle.addToOutputList( 'REVISION_ID','revision_id' )
447  qCondition=coral.AttributeList()
448  qCondition.extend('parentname','string')
449  qCondition['parentname'].setData(parentname)
450  qResult=coral.AttributeList()
451  qResult.extend('revision_id','unsigned long long')
452  qHandle.defineOutput(qResult)
453  qHandle.setCondition('NAME=:parentname',qCondition)
454  cursor=qHandle.execute()
455  while next(cursor):
456  parentid=cursor.currentRow()['revision_id'].data()
457  del qHandle
458  else:
459  parentname='ROOT'
460  iddealer=idDealer.idDealer(schema)
461  revisionid=iddealer.generateNextIDForTable( nameDealer.revisionTableName() )
462  db=dbUtil.dbUtil(schema)
463  tabrowDefDict={}
464  tabrowDefDict['REVISION_ID']='unsigned long long'
465  tabrowDefDict['BRANCH_ID']='unsigned long long'
466  tabrowDefDict['BRANCH_NAME']='string'
467  tabrowDefDict['NAME']='string'
468  tabrowDefDict['COMMENT']='string'
469  tabrowDefDict['CTIME']='time stamp'
470  tabrowValueDict={}
471  tabrowValueDict['REVISION_ID']=revisionid
472  tabrowValueDict['BRANCH_ID']=parentid
473  tabrowValueDict['BRANCH_NAME']=parentname
474  tabrowValueDict['NAME']=name
475  tabrowValueDict['COMMENT']=comment
476  tabrowValueDict['CTIME']=coral.TimeStamp()
477  db.insertOneRow(nameDealer.revisionTableName(),tabrowDefDict, tabrowValueDict )
478  return (revisionid,parentid,parentname)
479  except:
480  raise
481 
482 ################################################################
483 # Data Tagging API
484 ################################################################
485 def createDataTag(schema,tagname,lumitype='HF'):
486  '''
487  insert into tags(tagname,tagid,creationtime) values()
488  output:
489  tagname,tagid,creationtime
490  '''
491  if lumitype not in ['HF','PIXEL']:
492  raise ValueError('unknown lumitype '+lumitype)
493  if lumitype=='HF':
494  tagstablename=nameDealer.tagsTableName()
495  else:
496  tagstablename=nameDealer.pixeltagsTableName()
497  try:
498  iddealer=idDealer.idDealer(schema)
499  tagid=iddealer.generateNextIDForTable( tagstablename )
500  db=dbUtil.dbUtil(schema)
501  tabrowDefDict={}
502  tabrowDefDict['TAGNAME']='string'
503  tabrowDefDict['TAGID']='unsigned long long'
504  tabrowDefDict['CREATIONTIME']='time stamp'
505  tabrowValueDict={}
506  tabrowValueDict['TAGNAME']=tagname
507  tabrowValueDict['TAGID']=tagid
508  creationtime=coral.TimeStamp()
509  tabrowValueDict['CREATIONTIME']=creationtime
510  db.insertOneRow(tagstablename,tabrowDefDict, tabrowValueDict )
511  return (tagname,tagid,creationtime)
512  except:
513  raise
514 
515 def currentDataTag(schema,lumitype='HF'):
516  '''
517  select tagid,tagname from tags
518  output:(tagid,tagname)
519  '''
520  if lumitype not in ['HF','PIXEL']:
521  raise ValueError('unknown lumitype '+lumitype)
522  if lumitype=='HF':
523  tagstablename=nameDealer.tagsTableName()
524  else:
525  tagstablename=nameDealer.pixeltagsTableName()
526  tagmap={}
527  try:
528  qHandle=schema.newQuery()
529  qHandle.addToTableList( tagstablename )
530  qHandle.addToOutputList('TAGID')
531  qHandle.addToOutputList('TAGNAME')
532  qResult=coral.AttributeList()
533  qResult.extend('TAGID','unsigned long long')
534  qResult.extend('TAGNAME','string')
535  qHandle.defineOutput(qResult)
536  cursor=qHandle.execute()
537  currenttagid=0
538  while next(cursor):
539  tagid=cursor.currentRow()['TAGID'].data()
540  tagname=cursor.currentRow()['TAGNAME'].data()
541  tagmap[tagid]=tagname
542  del qHandle
543  if len(tagmap)!=0:
544  currenttagid=max(tagmap.keys())
545  if currenttagid==0:
546  raise 'currentDataTag: no tag available'
547  return (currenttagid,tagmap[currenttagid])
548  except:
549  raise
550 
551 def addRunToCurrentDataTag(schema,runnum,lumiid,trgid,hltid,lumitype='HF',comment=''):
552  '''
553  select tagid from tags
554  insert into tagruns(tagid,runnum,lumidataid,trgdataid,hltdataid,creationtime,comment) values(tagid,runnum,lumiid,trgid,hltid,creationtime,comment)
555  '''
556  if lumitype not in ['HF','PIXEL']:
557  raise ValueError('unknown lumitype '+lumitype)
558  if lumitype=='HF':
559  tagrunstablename=nameDealer.tagRunsTableName()
560  else:
561  tagrunstablename=nameDealer.pixeltagRunsTableName()
562  currenttagid=currentDataTag(schema,lumitype=lumitype)[0]
563  try:
564  db=dbUtil.dbUtil(schema)
565  tabrowDefDict={}
566  tabrowDefDict['TAGID']='unsigned long long'
567  tabrowDefDict['RUNNUM']='unsigned int'
568  tabrowDefDict['LUMIDATAID']='unsigned long long'
569  tabrowDefDict['TRGDATAID']='unsigned long long'
570  tabrowDefDict['HLTDATAID']='unsigned long long'
571  tabrowDefDict['CREATIONTIME']='time stamp'
572  tabrowDefDict['COMMENT']='string'
573  tabrowValueDict={}
574  tabrowValueDict['TAGID']=currenttagid
575  tabrowValueDict['RUNNUM']=runnum
576  tabrowValueDict['LUMIDATAID']=lumiid
577  tabrowValueDict['TRGDATAID']=trgid
578  tabrowValueDict['HLTDATAID']=hltid
579  tabrowValueDict['CREATIONTIME']=coral.TimeStamp()
580  tabrowValueDict['COMMENT']=comment
581  db.insertOneRow( tagrunstablename,tabrowDefDict, tabrowValueDict )
582  except:
583  raise
584 
585 def alldataTags(schema,lumitype='HF'):
586  '''
587  select tagname,tagid from tags,tagruns
588  if number of tags==1->open tag
589  if tagid is max ->open tag
590  for closed tag:
591  max run=max(runnum) where tagid=:tagid
592  min run
593  select min(runnum) from tagruns where tagid<=:tagid
594  for open tag:
595  max run=None
596  min run
597  select min(runnum) from tagruns where tagid<=:tagid
598  output:
599  {tagid:(name,minrun,maxrun,creationtime)}
600  '''
601  if lumitype not in ['HF','PIXEL']:
602  raise ValueError('unknown lumitype '+lumitype)
603  if lumitype=='HF':
604  tagstablename=nameDealer.tagsTableName()
605  tagrunstablename=nameDealer.tagRunsTableName()
606  else:
607  tagstablename=nameDealer.pixeltagsTableName()
608  tagrunstablename=nameDealer.pixeltagRunsTableName()
609  tagmap={}#{tagid:[tagname,minrun,maxrun,creationtime]}
610  try:
611  qHandle=schema.newQuery()
612  qHandle.addToTableList( tagstablename )
613  qCondition=coral.AttributeList()
614  qHandle.addToOutputList('TAGNAME')
615  qHandle.addToOutputList('TAGID')
616  qHandle.addToOutputList("TO_CHAR(CREATIONTIME,\'MM/DD/YY HH24:MI:SS\')",'creationtime')
617  qResult=coral.AttributeList()
618  qResult.extend('TAGNAME','string')
619  qResult.extend('TAGID','unsigned long long')
620  qResult.extend('creationtime','string')
621  qHandle.defineOutput(qResult)
622  cursor=qHandle.execute()
623  while next(cursor):
624  tagname=cursor.currentRow()['TAGNAME'].data()
625  tagid=cursor.currentRow()['TAGID'].data()
626  creationtime=cursor.currentRow()['creationtime'].data()
627  tagmap[tagid]=[tagname,0,0,creationtime]
628  del qHandle
629 
630  tagids=tagmap.keys()
631  allruns=set()
632  for tagid in tagids:
633  qConditionStr='TAGID<=:tagid'
634  qCondition=coral.AttributeList()
635  qCondition.extend('tagid','unsigned long long')
636  qCondition['tagid'].setData(tagid)
637  qHandle=schema.newQuery()
638  qHandle.addToTableList( tagrunstablename )
639  qResult=coral.AttributeList()
640  qResult.extend('RUNNUM','unsigned int')
641  qHandle.defineOutput(qResult)
642  qHandle.setCondition(qConditionStr,qCondition)
643  qHandle.addToOutputList('RUNNUM')
644  cursor=qHandle.execute()
645  while next(cursor):
646  rnum=cursor.currentRow()['RUNNUM'].data()
647  allruns.add(rnum)
648  minrun=0
649  maxrun=0
650  if len(allruns)!=0:
651  minrun=min(allruns)
652  maxrun=max(allruns)
653  tagmap[tagid][1]=minrun
654  if len(tagmap)>1 and tagid!=max(tagids):
655  tagmap[tagid][2]=maxrun
656  except:
657  raise
658  return tagmap
659 
660 def getDataTagId(schema,tagname,lumitype='HF'):
661  '''
662  select tagid from tags where tagname=:tagname
663  '''
664  if lumitype not in ['HF','PIXEL']:
665  raise ValueError('unknown lumitype '+lumitype)
666  if lumitype=='HF':
667  tagstablename=nameDealer.tagsTableName()
668  else:
669  tagstablename=nameDealer.pixeltagsTableName()
670  tagid=None
671  try:
672  qHandle=schema.newQuery()
673  qHandle.addToTableList( tagstablename )
674  qConditionStr='TAGNAME=:tagname'
675  qCondition=coral.AttributeList()
676  qCondition.extend('tagname','string')
677  qCondition['tagname'].setData(tagname)
678  qHandle.addToOutputList('TAGID')
679  qResult=coral.AttributeList()
680  qResult.extend('TAGID','unsigned long long')
681  qHandle.defineOutput(qResult)
682  qHandle.setCondition(qConditionStr,qCondition)
683  cursor=qHandle.execute()
684  while next(cursor):
685  if not cursor.currentRow()['TAGID'].isNull():
686  tagid=cursor.currentRow()['TAGID'].data()
687  del qHandle
688  except:
689  raise
690  return tagid
691 def dataIdsByTagName(schema,tagname,runlist=None,withcomment=False,lumitype='HF'):
692  '''
693  select tagid from tags where tagname=:tagname
694  input:
695  runlist: select run list, if None, all
696  output:
697  {run:(lumidataid,trgdataid,hltdataid,(creationtime,comment)}
698  '''
699  if lumitype not in ['HF','PIXEL']:
700  raise ValueError('unknown lumitype '+lumitype)
701  if lumitype=='HF':
702  tagstablename=nameDealer.tagsTableName()
703  else:
704  tagstablename=nameDealer.pixeltagsTableName()
705  tagid=None
706  try:
707  qHandle=schema.newQuery()
708  qHandle.addToTableList( tagstablename )
709  qConditionStr='TAGNAME=:tagname'
710  qCondition=coral.AttributeList()
711  qCondition.extend('tagname','string')
712  qCondition['tagname'].setData(tagname)
713  qHandle.addToOutputList('TAGID')
714  qResult=coral.AttributeList()
715  qResult.extend('TAGID','unsigned long long')
716  qHandle.defineOutput(qResult)
717  qHandle.setCondition(qConditionStr,qCondition)
718  cursor=qHandle.execute()
719  while next(cursor):
720  if not cursor.currentRow()['TAGID'].isNull():
721  tagid=cursor.currentRow()['TAGID'].data()
722  del qHandle
723  except:
724  raise
725  if tagid is None:
726  return {}
727  return dataIdsByTagId(schema,tagid,runlist=runlist,withcomment=withcomment,lumitype=lumitype)
728 
729 
730 def dataTagInfo(schema,tagname,runlist=None,lumitype='HF'):
731  '''
732  select tagid from tags where tagname=:tagname
733  select runnum,comment from tagruns where tagid<=:tagid
734  input:
735  runlist: select run list, if None, all
736  output:
737  {tagid:(name,minrun,maxrun,creationtime)}
738  '''
739  if lumitype not in ['HF','PIXEL']:
740  raise ValueError('unknown lumitype '+lumitype)
741  if lumitype=='HF':
742  tagstablename=nameDealer.tagsTableName()
743  tagrunstablename=nameDealer.tagRunsTableName()
744  else:
745  tagstablename=nameDealer.pixeltagsTableName()
746  tagrunstablename=nameDealer.pixeltagRunsTableName()
747  tagmap={}#{tagid:[tagname,minrun,maxrun,creationtime]}
748  try:
749  qHandle=schema.newQuery()
750  qHandle.addToTableList( tagstablename )
751  qCondition=coral.AttributeList()
752  qHandle.addToOutputList('TAGNAME')
753  qHandle.addToOutputList('TAGID')
754  qHandle.addToOutputList("TO_CHAR(CREATIONTIME,\'MM/DD/YY HH24:MI:SS\')",'creationtime')
755  qResult=coral.AttributeList()
756  qResult.extend('TAGNAME','string')
757  qResult.extend('TAGID','unsigned long long')
758  qResult.extend('creationtime','string')
759  qHandle.defineOutput(qResult)
760  cursor=qHandle.execute()
761  while next(cursor):
762  tagname=cursor.currentRow()['TAGNAME'].data()
763  tagid=cursor.currentRow()['TAGID'].data()
764  creationtime=cursor.currentRow()['creationtime'].data()
765  tagmap[tagid]=[tagname,0,0,creationtime]
766  del qHandle
767 
768  tagids=tagmap.keys()
769  allruns=set()
770  for tagid in tagids:
771  qConditionStr='TAGID<=:tagid'
772  qCondition=coral.AttributeList()
773  qCondition.extend('tagid','unsigned long long')
774  qCondition['tagid'].setData(tagid)
775  qHandle=schema.newQuery()
776  qHandle.addToTableList(tagrunstablename)
777  qResult=coral.AttributeList()
778  qResult.extend('RUNNUM','unsigned int')
779  qHandle.defineOutput(qResult)
780  qHandle.setCondition(qConditionStr,qCondition)
781  qHandle.addToOutputList('RUNNUM')
782  cursor=qHandle.execute()
783  while next(cursor):
784  rnum=cursor.currentRow()['RUNNUM'].data()
785  if runlist is not None and rnum not in runlist:
786  continue
787  allruns.add(rnum)
788  minrun=0
789  maxrun=0
790  if len(allruns)!=0:
791  minrun=min(allruns)
792  maxrun=max(allruns)
793  tagmap[tagid][1]=minrun
794  if len(tagmap)>1 and tagid!=max(tagids):
795  tagmap[tagid][2]=maxrun
796  except:
797  raise
798  return tagmap
799 
800 def dataIdsByTagId(schema,tagid,runlist=None,withcomment=False,lumitype='HF'):
801  '''
802  select runnum,lumidataid,trgdataid,hltdataid,comment from tagruns where TAGID<=:tagid;
803  input:
804  runlist: select run list, if None, all
805  output:
806  {run:(lumidataid,trgdataid,hltdataid,(creationtime,comment))}
807  '''
808  if lumitype not in ['HF','PIXEL']:
809  raise ValueError('unknown lumitype '+lumitype)
810  if lumitype=='HF':
811  tagrunstablename=nameDealer.tagRunsTableName()
812  else:
813  tagrunstablename=nameDealer.pixeltagRunsTableName()
814  result={}#{run:[lumiid,trgid,hltid,comment(optional)]}
815  commentdict={}#{(lumiid,trgid,hltid):[ctimestr,comment]}
816  try:
817  qHandle=schema.newQuery()
818  qHandle.addToTableList(tagrunstablename)
819  qConditionStr='TAGID<=:tagid'
820  qCondition=coral.AttributeList()
821  qCondition.extend('tagid','unsigned long long')
822  qCondition['tagid'].setData(tagid)
823  qResult=coral.AttributeList()
824  qResult.extend('RUNNUM','unsigned int')
825  qResult.extend('LUMIDATAID','unsigned long long')
826  qResult.extend('TRGDATAID','unsigned long long')
827  qResult.extend('HLTDATAID','unsigned long long')
828  if withcomment:
829  qResult.extend('COMMENT','string')
830  qResult.extend('creationtime','string')
831  qHandle.defineOutput(qResult)
832  qHandle.setCondition(qConditionStr,qCondition)
833  qHandle.addToOutputList('RUNNUM')
834  qHandle.addToOutputList('LUMIDATAID')
835  qHandle.addToOutputList('TRGDATAID')
836  qHandle.addToOutputList('HLTDATAID')
837  if withcomment:
838  qHandle.addToOutputList('COMMENT')
839  qHandle.addToOutputList("TO_CHAR(CREATIONTIME,\'MM/DD/YY HH24:MI:SS\')",'creationtime')
840  cursor=qHandle.execute()
841  while next(cursor):
842  runnum=cursor.currentRow()['RUNNUM'].data()
843  if runlist is not None and runnum not in runlist:
844  continue
845  lumidataid=0
846  if not cursor.currentRow()['LUMIDATAID'].isNull():
847  lumidataid=cursor.currentRow()['LUMIDATAID'].data()
848  trgdataid=0
849  if not cursor.currentRow()['TRGDATAID'].isNull():
850  trgdataid=cursor.currentRow()['TRGDATAID'].data()
851  hltdataid=0
852  if not cursor.currentRow()['HLTDATAID'].isNull():
853  hltdataid=cursor.currentRow()['HLTDATAID'].data()
854  if runnum not in result:
855  result[runnum]=[0,0,0]
856  if lumidataid>result[runnum][0]:
857  result[runnum][0]=lumidataid
858  if trgdataid>result[runnum][1]:
859  result[runnum][1]=trgdataid
860  if hltdataid>result[runnum][2]:
861  result[runnum][2]=hltdataid
862  if withcomment:
863  comment=''
864  creationtime=''
865  if not cursor.currentRow()['creationtime'].isNull():
866  creationtime=cursor.currentRow()['creationtime'].data()
867  if not cursor.currentRow()['COMMENT'].isNull():
868  comment=cursor.currentRow()['COMMENT'].data()
869  commentdict[(lumidataid,trgdataid,hltdataid)]=(creationtime,comment)
870  del qHandle
871  if withcomment:
872  for run,resultentry in result.items():
873  lumiid=resultentry[0]
874  trgid=resultentry[1]
875  hltid=resultentry[2]
876  if (lumiid,trgid,hltid) in commentdict:
877  resultentry.append(commentdict[(lumiid,trgid,hltid)])
878  elif (lumiid,0,0) in commentdict:
879  resultentry.append(commentdict[(lumiid,0,0)])
880  elif commentdict.has_ley((0,trgid,0)):
881  resultentry.append(commentdict[(0,trgid,0)])
882  elif commentdict.has_ley((0,0,hltid)):
883  resultentry.append(commentdict[(0,0,hltid)])
884  else:
885  resultentry.append(())
886 
887  except:
888  raise
889  return result
890 
891 def dataIdsByCurrentTag(schema,runlist=None,lumitype='HF'):
892  '''
893  dataIdsByTagId(schema,currenttagid,runlist)
894  output:
895  (currenttagname,{run:(lumidataid,trgdataid,hltdataid)})
896  '''
897  (currenttagid,currenttagname)=currentDataTag(schema)
898  result=dataIdsByTagId(schema,currenttagid,runlist=runlist,withcomment=False,lumitype=lumitype)
899  return (currenttagname,result)
900 
901 if __name__ == "__main__":
902  from . import sessionManager
903  from . import lumidbDDL
904  #myconstr='oracle://cms_orcoff_prep/cms_lumi_dev_offline'
905  #authpath='/afs/cern.ch/user/x/xiezhen'
906  myconstr='sqlite_file:test.db'
907  svc=sessionManager.sessionManager(myconstr,debugON=False)
908  session=svc.openSession(isReadOnly=False,cpp2sqltype=[('unsigned int','NUMBER(10)'),('unsigned long long','NUMBER(20)')])
909  schema=session.nominalSchema()
910  session.transaction().start(False)
911  tables=lumidbDDL.createTables(schema)
912  trunkinfo=createBranch(schema,'TRUNK',None,comment='main')
913  #print trunkinfo
914  datainfo=createBranch(schema,'DATA','TRUNK',comment='hold data')
915  #print datainfo
916  norminfo=createBranch(schema,'NORM','TRUNK',comment='hold normalization factor')
917  #print norminfo
918  (branchid,branchparent)=branchInfoByName(schema,'DATA')
919  databranchinfo=(branchid,'DATA')
920  print(databranchinfo)
921  for runnum in [1200,1211,1222,1233,1345,1222,1200]:
922  lumientryid=entryInBranch(schema,nameDealer.lumidataTableName(),str(runnum),'DATA')
923  trgentryid=entryInBranch(schema,nameDealer.trgdataTableName(),str(runnum),'DATA')
924  hltentryid=entryInBranch(schema,nameDealer.hltdataTableName(),str(runnum),'DATA')
925  if lumientryid is None:
926  (revision_id,entry_id,data_id)=bookNewEntry( schema,nameDealer.lumidataTableName() )
927  entryinfo=(revision_id,entry_id,str(runnum),data_id)
928  addEntry(schema,nameDealer.lumidataTableName(),entryinfo,databranchinfo)
929  #add data here
930  else:
932  addRevision(schema,nameDealer.lumidataTableName(),revisioninfo,databranchinfo)
933  #add data here
934  if trgentryid is None:
935  (revision_id,entry_id,data_id)=bookNewEntry( schema,nameDealer.trgdataTableName() )
936  entryinfo=(revision_id,entry_id,str(runnum),data_id)
937  addEntry(schema,nameDealer.trgdataTableName(),entryinfo,databranchinfo)
938  #add data here
939  else:
940  revisioninfo=bookNewRevision( schema,nameDealer.trgdataTableName() )
941  addRevision(schema,nameDealer.trgdataTableName(),revisioninfo,databranchinfo)
942  #add data here
943  if hltentryid is None:
944  (revision_id,entry_id,data_id)=bookNewEntry( schema,nameDealer.hltdataTableName() )
945  entryinfo=(revision_id,entry_id,str(runnum),data_id)
946  addEntry(schema,nameDealer.hltdataTableName(),entryinfo,databranchinfo)
947  #add data here
948  else:
949  revisioninfo=bookNewRevision( schema,nameDealer.hltdataTableName() )
950  addRevision(schema,nameDealer.hltdataTableName(),revisioninfo,databranchinfo)
951  #add data here
952 
953  session.transaction().commit()
954  print('test reading')
955  session.transaction().start(True)
956  print(branchType(schema,'DATA'))
957  revlist=revisionsInBranchName(schema,'DATA')
958  print('DATA revlist ',revlist)
959  lumientry_id=entryInBranch(schema,nameDealer.lumidataTableName(),'1211','DATA')
960  print(lumientry_id)
961  latestrevision=latestDataRevisionOfEntry(schema,nameDealer.lumidataTableName(),lumientry_id,revlist)
962  print('latest data_id for run 1211 ',latestrevision)
963  session.transaction().commit()
964  del session
Definition: start.py:1
def revisionsInBranchName(schema, branchname)
Definition: revisionDML.py:179
def addRunToCurrentDataTag(schema, runnum, lumiid, trgid, hltid, lumitype='HF', comment='')
Definition: revisionDML.py:551
def dataIdsByCurrentTag(schema, runlist=None, lumitype='HF')
Definition: revisionDML.py:891
def createTables(schema)
Definition: lumidbDDL.py:11
def dataIdsByTagName(schema, tagname, runlist=None, withcomment=False, lumitype='HF')
Definition: revisionDML.py:691
def dataRevisionsOfEntry(schema, datatableName, entry, revrange)
Definition: revisionDML.py:233
def revisionsInBranch(schema, branchid)
Definition: revisionDML.py:63
def revisionsInTag(schema, tagrevisionid, branchid)
Definition: revisionDML.py:14
def pixeltagsTableName()
Definition: nameDealer.py:73
def currentDataTag(schema, lumitype='HF')
Definition: revisionDML.py:515
def createDataTag(schema, tagname, lumitype='HF')
Data Tagging API.
Definition: revisionDML.py:485
def pixeltagRunsTableName()
Definition: nameDealer.py:70
S & print(S &os, JobReport::InputFile const &f)
Definition: JobReport.cc:66
def addEntry(schema, datatableName, entryinfo, branchinfo)
Definition: revisionDML.py:344
def entryTableName(dataTableName)
Definition: nameDealer.py:103
def entryInBranch(schema, datatableName, entryname, branch)
Definition: revisionDML.py:190
def bookNewRevision(schema, datatableName)
Definition: revisionDML.py:332
def alldataTags(schema, lumitype='HF')
Definition: revisionDML.py:585
def revisionTableName()
Definition: nameDealer.py:11
T min(T a, T b)
Definition: MathUtil.h:58
def revmapTableName(dataTableName)
Definition: nameDealer.py:100
def bookNewEntry(schema, datatableName)
Definition: revisionDML.py:318
def addRevision(schema, datatableName, revisioninfo, branchinfo)
Definition: revisionDML.py:396
def trgdataTableName()
Definition: nameDealer.py:46
def branchInfoByName(schema, branchName)
Definition: revisionDML.py:284
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:82
def tagsTableName()
Definition: nameDealer.py:67
def hltdataTableName()
Definition: nameDealer.py:58
def dataTagInfo(schema, tagname, runlist=None, lumitype='HF')
Definition: revisionDML.py:730
def dataIdsByTagId(schema, tagid, runlist=None, withcomment=False, lumitype='HF')
Definition: revisionDML.py:800
def createBranch(schema, name, parentname, comment='')
Definition: revisionDML.py:434
#define str(s)
def tagRunsTableName()
Definition: nameDealer.py:64
def branchType(schema, name)
Definition: revisionDML.py:114
def lumidataTableName()
Definition: nameDealer.py:19
def getDataTagId(schema, tagname, lumitype='HF')
Definition: revisionDML.py:660
def latestDataRevisionOfEntry(schema, datatableName, entry, revrange)
Definition: revisionDML.py:276