00001
00002
00003
00004
00005 import coral
00006 from RecoLuminosity.LumiDB import nameDealer,idDealer,dbUtil
00007
00008
00009
00010 def revisionsInTag(schema,tagrevisionid,branchid):
00011 '''
00012 returns all revisions before tag in selected branch
00013 select revision_id from revisions where revision_id!=0 and revision_id<tagrevisionid and branch_id=:branchid
00014 result=[revision_id]
00015 '''
00016 result=[]
00017 qHandle=schema.newQuery()
00018 try:
00019 nextbranches=[]
00020 qHandle.addToTableList( nameDealer.revisionTableName() )
00021 qHandle.addToOutputList('distinct BRANCH_ID','branch_id')
00022 qCondition=coral.AttributeList()
00023 qCondition.extend('branchid','unsigned long long')
00024 qCondition['branchid'].setData(branchid)
00025 qResult=coral.AttributeList()
00026 qResult.extend('branch_id','unsigned long long')
00027 qHandle.defineOutput(qResult)
00028 qHandle.setCondition('BRANCH_ID>:branchid',qCondition)
00029 cursor=qHandle.execute()
00030 while cursor.next():
00031 nextbranches.append(cursor.currentRow()['branch_id'].data())
00032 del qHandle
00033 candidates=[]
00034 conditionStr='REVISION_ID!=0 and BRANCH_ID=:branchid and REVISION_ID<:tagrevisionid'
00035 qHandle=schema.newQuery()
00036 qHandle.addToTableList( nameDealer.revisionTableName() )
00037 qHandle.addToOutputList('REVISION_ID','revision_id')
00038 qCondition=coral.AttributeList()
00039 qCondition.extend('branchid','unsigned long long')
00040 qCondition.extend('tagrevisionid','unsigned long long')
00041 qCondition['branchid'].setData(branchid)
00042 qCondition['tagrevisionid'].setData(tagrevisionid)
00043 qResult=coral.AttributeList()
00044 qResult.extend('revision_id','unsigned long long')
00045 qHandle.defineOutput(qResult)
00046 qHandle.setCondition(conditionStr,qCondition)
00047 cursor=qHandle.execute()
00048 while cursor.next():
00049 candidates.append(cursor.currentRow()['revision_id'].data())
00050 del qHandle
00051 for c in candidates:
00052 if c in nextbranches:
00053 continue
00054 result.append(c)
00055 return result
00056 except:
00057 if qHandle:del qHandle
00058 raise
00059 def revisionsInBranch(schema,branchid):
00060 '''
00061 returns all revision values in a branch
00062 result=[revision_id]
00063 select distinct branch_id from revisions where branch_id>:branchid;
00064 select revision_id from revisions where branch_id=:branchid ;
00065 if the branchid matches and the revisionid is not in the branchid collection,not 0, then this revision is in the branch
00066 require also revisionid>branchid
00067 '''
00068 result=[]
00069 qHandle=schema.newQuery()
00070 try:
00071 nextbranches=[]
00072 qHandle.addToTableList( nameDealer.revisionTableName() )
00073 qHandle.addToOutputList('distinct BRANCH_ID','branch_id')
00074 qCondition=coral.AttributeList()
00075 qCondition.extend('branchid','unsigned long long')
00076 qCondition['branchid'].setData(branchid)
00077 qResult=coral.AttributeList()
00078 qResult.extend('branch_id','unsigned long long')
00079 qHandle.defineOutput(qResult)
00080 qHandle.setCondition('BRANCH_ID>:branchid',qCondition)
00081 cursor=qHandle.execute()
00082 while cursor.next():
00083 nextbranches.append(cursor.currentRow()['branch_id'].data())
00084 del qHandle
00085 candidates=[]
00086 conditionStr='BRANCH_ID=:branchid and REVISION_ID!=0'
00087 qHandle=schema.newQuery()
00088 qHandle.addToTableList( nameDealer.revisionTableName() )
00089 qHandle.addToOutputList('REVISION_ID','revision_id')
00090 qCondition=coral.AttributeList()
00091 qCondition.extend('branchid','unsigned long long')
00092 qCondition['branchid'].setData(branchid)
00093 qResult=coral.AttributeList()
00094 qResult.extend('revision_id','unsigned long long')
00095 qHandle.defineOutput(qResult)
00096 qHandle.setCondition(conditionStr,qCondition)
00097 cursor=qHandle.execute()
00098 while cursor.next():
00099 candidates.append(cursor.currentRow()['revision_id'].data())
00100 del qHandle
00101 for c in candidates:
00102 if c in nextbranches:
00103 continue
00104 result.append(c)
00105 return result
00106 except:
00107 if qHandle: del qHandle
00108 raise
00109
00110 def branchType(schema,name):
00111 '''
00112 output: tag,branch
00113 the difference between tag and branch: tag is an empty branch
00114 select count(revision_id) from revisions where branch_name=:name
00115 if >0: is real branch
00116 else: is tag
00117 '''
00118 result='tag'
00119 try:
00120 qHandle=schema.newQuery()
00121 qHandle.addToTableList( nameDealer.revisionTableName() )
00122 qHandle.addToOutputList('count(REVISION_ID)','nchildren')
00123 qCondition=coral.AttributeList()
00124 qCondition.extend('branch_name','string')
00125 qCondition['branch_name'].setData(name)
00126 qResult=coral.AttributeList()
00127 qResult.extend('nchildren','unsigned int')
00128 qHandle.defineOutput(qResult)
00129 conditionStr='BRANCH_NAME=:branch_name'
00130 qHandle.setCondition(conditionStr,qCondition)
00131 cursor=qHandle.execute()
00132 while cursor.next():
00133 if cursor.currentRow()['nchildren'].data()>0:
00134 result='branch'
00135 del qHandle
00136 return result
00137 except :
00138 raise
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175 def revisionsInBranchName(schema,branchname):
00176 '''
00177 returns all revisions in a branch/tag by name
00178 '''
00179 result=[]
00180 try:
00181 (revision_id,branch_id)=branchInfoByName(schema,branchname)
00182 result=revisionsInBranch(schema,revision_id)
00183 return result
00184 except :
00185 raise
00186 def entryInBranch(schema,datatableName,entryname,branch):
00187 '''
00188 whether an entry(by name) already exists in the given branch
00189 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
00190 input:
00191 if isinstance(branch,str):byname
00192 else: byid
00193 output:entry_id/None
00194 '''
00195 try:
00196 result=None
00197 byname=False
00198 if isinstance(branch,str):
00199 byname=True
00200 qHandle=schema.newQuery()
00201 qHandle.addToTableList( nameDealer.entryTableName(datatableName),'e' )
00202 qHandle.addToTableList( nameDealer.revisionTableName(),'r' )
00203 qHandle.addToOutputList('e.ENTRY_ID','entry_id')
00204 qCondition=coral.AttributeList()
00205 qCondition.extend('entryname','string')
00206 qCondition['entryname'].setData(entryname)
00207 qConditionStr='r.REVISION_ID=e.REVISION_ID and e.NAME=:entryname and '
00208 if byname:
00209 qCondition.extend('branch_name','string')
00210 qCondition['branch_name'].setData(branch)
00211 qConditionStr+='r.BRANCH_NAME=:branch_name'
00212 else:
00213 qCondition.extend('branch_id','unsigned long long')
00214 qCondition['branch_id'].setData(branch)
00215 qConditionStr+='r.BRANCH_ID=:branch_id'
00216 qResult=coral.AttributeList()
00217 qResult.extend('entry_id','unsigned long long')
00218 qHandle.defineOutput(qResult)
00219 qHandle.setCondition(qConditionStr,qCondition)
00220 cursor=qHandle.execute()
00221 while cursor.next():
00222 entry_id=cursor.currentRow()['entry_id'].data()
00223 result=entry_id
00224 del qHandle
00225 return result
00226 except :
00227 raise
00228
00229 def dataRevisionsOfEntry(schema,datatableName,entry,revrange):
00230 '''
00231 all data version of the given entry whose revision falls in branch revision range
00232 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
00233 input: if isinstance(entry,str): d.entry_name=:entry ; else d.entry_id=:entry
00234 output: [data_id]
00235 '''
00236 qHandle=schema.newQuery()
00237 try:
00238 result=[]
00239 byname=False
00240 if isinstance(entry,str):
00241 byname=True
00242 qHandle.addToTableList( datatableName,'d' )
00243 qHandle.addToTableList( nameDealer.revmapTableName(datatableName), 'r')
00244 qHandle.addToOutputList('d.DATA_ID','data_id')
00245 qHandle.addToOutputList('r.REVISION_ID','revision_id')
00246 qCondition=coral.AttributeList()
00247 qConditionStr='d.DATA_ID=r.DATA_ID and '
00248 if byname:
00249 qCondition.extend('entry_name','string')
00250 qCondition['entry_name'].setData(entry)
00251 qConditionStr+='d.ENTRY_NAME=:entry_name'
00252 else:
00253 qCondition.extend('entry_id','unsigned long long')
00254 qCondition['entry_id'].setData(entry)
00255 qConditionStr+='d.ENTRY_ID=:entry_id'
00256 qResult=coral.AttributeList()
00257 qResult.extend('data_id','unsigned long long')
00258 qResult.extend('revision_id','unsigned long long')
00259 qHandle.defineOutput(qResult)
00260 qHandle.setCondition(qConditionStr,qCondition)
00261 cursor=qHandle.execute()
00262 while cursor.next():
00263 data_id=cursor.currentRow()['data_id'].data()
00264 revision_id=cursor.currentRow()['revision_id'].data()
00265 if revision_id in revrange:
00266 result.append(data_id)
00267 return result
00268 except :
00269 del qHandle
00270 raise
00271
00272 def latestDataRevisionOfEntry(schema,datatableName,entry,revrange):
00273 '''
00274 return max(data_id) of all datarevisionofEntry
00275 '''
00276 result=dataRevisionsOfEntry(schema,datatableName,entry,revrange)
00277 if result and len(result)!=0: return max(result)
00278 return None
00279
00280 def branchInfoByName(schema,branchName):
00281 '''
00282 select (revision_id,branch_id) from revisions where name=:branchName
00283 '''
00284 try:
00285 qHandle=schema.newQuery()
00286 qHandle.addToTableList( nameDealer.revisionTableName() )
00287 qHandle.addToOutputList('REVISION_ID','revision_id')
00288 qHandle.addToOutputList('BRANCH_ID','branch_id')
00289 qCondition=coral.AttributeList()
00290 qCondition.extend('name','string')
00291 qCondition['name'].setData(branchName)
00292 qResult=coral.AttributeList()
00293 qResult.extend('revision_id','unsigned long long')
00294 qResult.extend('branch_id','unsigned long long')
00295 qHandle.defineOutput(qResult)
00296 qHandle.setCondition('NAME=:name',qCondition)
00297 cursor=qHandle.execute()
00298 revision_id=None
00299 branch_id=None
00300 while cursor.next():
00301 revision_id=cursor.currentRow()['revision_id'].data()
00302 branch_id=cursor.currentRow()['branch_id'].data()
00303 del qHandle
00304 return (revision_id,branch_id)
00305 except Exception,e :
00306 raise RuntimeError(' revisionDML.branchInfoByName: '+str(e))
00307
00308
00309
00310
00311
00312
00313
00314 def bookNewEntry(schema,datatableName):
00315 '''
00316 allocate new revision_id,entry_id,data_id
00317 '''
00318 try:
00319 entrytableName=nameDealer.entryTableName(datatableName)
00320 iddealer=idDealer.idDealer(schema)
00321 revision_id=iddealer.generateNextIDForTable( nameDealer.revisionTableName() )
00322 data_id=iddealer.generateNextIDForTable( datatableName)
00323 entry_id=iddealer.generateNextIDForTable( nameDealer.entryTableName(datatableName) )
00324 return (revision_id,entry_id,data_id)
00325 except:
00326 raise
00327
00328 def bookNewRevision(schema,datatableName):
00329 '''
00330 allocate new revision_id,data_id
00331 '''
00332 try:
00333 iddealer=idDealer.idDealer(schema)
00334 revision_id=iddealer.generateNextIDForTable( nameDealer.revisionTableName() )
00335 data_id=iddealer.generateNextIDForTable(datatableName)
00336 return (revision_id,data_id)
00337 except:
00338 raise
00339
00340 def addEntry(schema,datatableName,entryinfo,branchinfo):
00341 '''
00342 input:
00343 entryinfo (revision_id(0),entry_id(1),entry_name(2),data_id(3))
00344 branchinfo (branch_id,branch_name)
00345 1.allocate and insert a new revision into the revisions table
00346 2.allocate and insert a new entry into the entry table with the new revision
00347 3.inset into data_rev table with new data_id ,revision)id mapping
00348
00349 insert into revisions(revision_id,branch_id,branch_name,comment,ctime) values()
00350 insert into datatablename_entries (entry_id,revision_id) values()
00351 insert into datatablename_rev(data_id,revision_id) values()
00352 '''
00353 try:
00354 revisiontableName=nameDealer.revisionTableName()
00355 entrytableName=nameDealer.entryTableName(datatableName)
00356 revtableName=nameDealer.revmapTableName(datatableName)
00357
00358 db=dbUtil.dbUtil(schema)
00359 tabrowDefDict={}
00360 tabrowDefDict['REVISION_ID']='unsigned long long'
00361 tabrowDefDict['BRANCH_ID']='unsigned long long'
00362 tabrowDefDict['BRANCH_NAME']='string'
00363 tabrowDefDict['CTIME']='time stamp'
00364 tabrowValueDict={}
00365 tabrowValueDict['REVISION_ID']=entryinfo[0]
00366 tabrowValueDict['BRANCH_ID']=branchinfo[0]
00367 tabrowValueDict['BRANCH_NAME']=branchinfo[1]
00368 tabrowValueDict['CTIME']=coral.TimeStamp()
00369 db.insertOneRow(revisiontableName,tabrowDefDict,tabrowValueDict)
00370
00371 tabrowDefDict={}
00372 tabrowDefDict['REVISION_ID']='unsigned long long'
00373 tabrowDefDict['ENTRY_ID']='unsigned long long'
00374 tabrowDefDict['NAME']='string'
00375
00376 tabrowValueDict={}
00377 tabrowValueDict['REVISION_ID']=entryinfo[0]
00378 tabrowValueDict['ENTRY_ID']=entryinfo[1]
00379 tabrowValueDict['NAME']=entryinfo[2]
00380 db.insertOneRow(entrytableName,tabrowDefDict,tabrowValueDict)
00381
00382 tabrowDefDict={}
00383 tabrowDefDict['REVISION_ID']='unsigned long long'
00384 tabrowDefDict['DATA_ID']='unsigned long long'
00385 tabrowValueDict={}
00386 tabrowValueDict['REVISION_ID']=entryinfo[0]
00387 tabrowValueDict['DATA_ID']=entryinfo[3]
00388 db.insertOneRow(revtableName,tabrowDefDict,tabrowValueDict)
00389 except:
00390 raise
00391
00392 def addRevision(schema,datatableName,revisioninfo,branchinfo):
00393 '''
00394 1.insert a new revision into the revisions table
00395 2.insert into data_id, revision_id pair to datatable_revmap
00396 insert into revisions(revision_id,branch_id,branch_name,ctime) values()
00397 insert into datatable_rev(data_id,revision_id) values())
00398 input:
00399 revisioninfo (revision_id(0),data_id(1))
00400 branchinfo (branch_id(0),branch_name(1))
00401 '''
00402 try:
00403 revisiontableName=nameDealer.revisionTableName()
00404 revtableName=nameDealer.revmapTableName(datatableName)
00405
00406 db=dbUtil.dbUtil(schema)
00407 tabrowDefDict={}
00408 tabrowDefDict['REVISION_ID']='unsigned long long'
00409 tabrowDefDict['BRANCH_ID']='unsigned long long'
00410 tabrowDefDict['BRANCH_NAME']='string'
00411 tabrowDefDict['CTIME']='time stamp'
00412
00413 tabrowValueDict={}
00414 tabrowValueDict['REVISION_ID']=revisioninfo[0]
00415 tabrowValueDict['BRANCH_ID']=branchinfo[0]
00416 tabrowValueDict['BRANCH_NAME']=branchinfo[1]
00417 tabrowValueDict['CTIME']=coral.TimeStamp()
00418
00419 db.insertOneRow(revisiontableName,tabrowDefDict,tabrowValueDict)
00420
00421 tabrowDefDict={}
00422 tabrowDefDict['REVISION_ID']='unsigned long long'
00423 tabrowDefDict['DATA_ID']='unsigned long long'
00424 tabrowValueDict={}
00425 tabrowValueDict['REVISION_ID']=revisioninfo[0]
00426 tabrowValueDict['DATA_ID']=revisioninfo[1]
00427 db.insertOneRow(revtableName,tabrowDefDict,tabrowValueDict)
00428 except:
00429 raise
00430 def createBranch(schema,name,parentname,comment=''):
00431 '''
00432 create a new branch/tag under given parentnode
00433 insert into revisions(revision_id,branch_id,branch_name,name,comment,ctime) values()
00434 return (revisionid,parentid,parentname)
00435 '''
00436 try:
00437 parentid=None
00438 revisionid=0
00439 if not parentname is None:
00440 qHandle=schema.newQuery()
00441 qHandle.addToTableList( nameDealer.revisionTableName() )
00442 qHandle.addToOutputList( 'REVISION_ID','revision_id' )
00443 qCondition=coral.AttributeList()
00444 qCondition.extend('parentname','string')
00445 qCondition['parentname'].setData(parentname)
00446 qResult=coral.AttributeList()
00447 qResult.extend('revision_id','unsigned long long')
00448 qHandle.defineOutput(qResult)
00449 qHandle.setCondition('NAME=:parentname',qCondition)
00450 cursor=qHandle.execute()
00451 while cursor.next():
00452 parentid=cursor.currentRow()['revision_id'].data()
00453 del qHandle
00454 else:
00455 parentname='ROOT'
00456 iddealer=idDealer.idDealer(schema)
00457 revisionid=iddealer.generateNextIDForTable( nameDealer.revisionTableName() )
00458 db=dbUtil.dbUtil(schema)
00459 tabrowDefDict={}
00460 tabrowDefDict['REVISION_ID']='unsigned long long'
00461 tabrowDefDict['BRANCH_ID']='unsigned long long'
00462 tabrowDefDict['BRANCH_NAME']='string'
00463 tabrowDefDict['NAME']='string'
00464 tabrowDefDict['COMMENT']='string'
00465 tabrowDefDict['CTIME']='time stamp'
00466 tabrowValueDict={}
00467 tabrowValueDict['REVISION_ID']=revisionid
00468 tabrowValueDict['BRANCH_ID']=parentid
00469 tabrowValueDict['BRANCH_NAME']=parentname
00470 tabrowValueDict['NAME']=name
00471 tabrowValueDict['COMMENT']=comment
00472 tabrowValueDict['CTIME']=coral.TimeStamp()
00473 db.insertOneRow(nameDealer.revisionTableName(),tabrowDefDict, tabrowValueDict )
00474 return (revisionid,parentid,parentname)
00475 except:
00476 raise
00477
00478 if __name__ == "__main__":
00479 import sessionManager
00480 import lumidbDDL
00481
00482
00483 myconstr='sqlite_file:test.db'
00484 svc=sessionManager.sessionManager(myconstr,debugON=False)
00485 session=svc.openSession(isReadOnly=False,cpp2sqltype=[('unsigned int','NUMBER(10)'),('unsigned long long','NUMBER(20)')])
00486 schema=session.nominalSchema()
00487 session.transaction().start(False)
00488 tables=lumidbDDL.createTables(schema)
00489 trunkinfo=createBranch(schema,'TRUNK',None,comment='main')
00490
00491 datainfo=createBranch(schema,'DATA','TRUNK',comment='hold data')
00492
00493 norminfo=createBranch(schema,'NORM','TRUNK',comment='hold normalization factor')
00494
00495 (branchid,branchparent)=branchInfoByName(schema,'DATA')
00496 databranchinfo=(branchid,'DATA')
00497 print databranchinfo
00498 for runnum in [1200,1211,1222,1233,1345,1222,1200]:
00499 lumientryid=entryInBranch(schema,nameDealer.lumidataTableName(),str(runnum),'DATA')
00500 trgentryid=entryInBranch(schema,nameDealer.trgdataTableName(),str(runnum),'DATA')
00501 hltentryid=entryInBranch(schema,nameDealer.hltdataTableName(),str(runnum),'DATA')
00502 if lumientryid is None:
00503 (revision_id,entry_id,data_id)=bookNewEntry( schema,nameDealer.lumidataTableName() )
00504 entryinfo=(revision_id,entry_id,str(runnum),data_id)
00505 addEntry(schema,nameDealer.lumidataTableName(),entryinfo,databranchinfo)
00506
00507 else:
00508 revisioninfo=bookNewRevision( schema,nameDealer.lumidataTableName() )
00509 addRevision(schema,nameDealer.lumidataTableName(),revisioninfo,databranchinfo)
00510
00511 if trgentryid is None:
00512 (revision_id,entry_id,data_id)=bookNewEntry( schema,nameDealer.trgdataTableName() )
00513 entryinfo=(revision_id,entry_id,str(runnum),data_id)
00514 addEntry(schema,nameDealer.trgdataTableName(),entryinfo,databranchinfo)
00515
00516 else:
00517 revisioninfo=bookNewRevision( schema,nameDealer.trgdataTableName() )
00518 addRevision(schema,nameDealer.trgdataTableName(),revisioninfo,databranchinfo)
00519
00520 if hltentryid is None:
00521 (revision_id,entry_id,data_id)=bookNewEntry( schema,nameDealer.hltdataTableName() )
00522 entryinfo=(revision_id,entry_id,str(runnum),data_id)
00523 addEntry(schema,nameDealer.hltdataTableName(),entryinfo,databranchinfo)
00524
00525 else:
00526 revisioninfo=bookNewRevision( schema,nameDealer.hltdataTableName() )
00527 addRevision(schema,nameDealer.hltdataTableName(),revisioninfo,databranchinfo)
00528
00529
00530 session.transaction().commit()
00531 print 'test reading'
00532 session.transaction().start(True)
00533 print branchType(schema,'DATA')
00534 revlist=revisionsInBranchName(schema,'DATA')
00535 print 'DATA revlist ',revlist
00536 lumientry_id=entryInBranch(schema,nameDealer.lumidataTableName(),'1211','DATA')
00537 print lumientry_id
00538 latestrevision=latestDataRevisionOfEntry(schema,nameDealer.lumidataTableName(),lumientry_id,revlist)
00539 print 'latest data_id for run 1211 ',latestrevision
00540 session.transaction().commit()
00541 del session