2 import CommonUtils, IdGenerator, Node, DBImpl
4 """Class manages tag inventory
7 """Input: coral session handle
12 self.
__tagInventoryTableColumns = {
'tagid':
'unsigned long',
'tagname':
'string',
'pfn':
'string',
'recordname':
'string',
'objectname':
'string',
'labelname':
'string'}
17 """Drop inventory related tables
20 transaction=self.__session.transaction()
21 transaction.start(
False)
22 schema = self.__session.nominalSchema()
27 transaction.rollback()
28 raise Exception, str(er)
32 """Check if inventory table exists
35 transaction=self.__session.transaction()
36 transaction.start(
True)
37 schema = self.__session.nominalSchema()
42 transaction.rollback()
43 raise Exception, str(er)
46 """Create tag inventory table. Existing table will be deleted.
49 transaction=self.__session.transaction()
51 schema = self.__session.nominalSchema()
53 description = coral.TableDescription();
55 for columnName, columnType
in self.__tagInventoryTableColumns.items():
56 description.insertColumn(columnName, columnType)
58 description.setNotNullConstraint(columnName,
True)
67 self.__tagInventoryTableHandle.privilegeManager().grantToPublic( coral.privilege_Select )
73 transaction.rollback()
74 raise Exception, str(er)
76 """Add entry into the inventory.\n
77 Input: base tag info. If identical data found already exists, do nothing
78 Output: tagid. if tagid=0, there's no new entry added, i.e.no new tagid
82 transaction=self.__session.transaction()
85 transaction.start(
True)
86 schema = self.__session.nominalSchema()
88 condition=
'tagname=:tagname'
89 conditionbindDict=coral.AttributeList()
90 conditionbindDict.extend(
'tagname',
'string')
91 conditionbindDict[
'tagname'].setData(leafNode.tagname)
92 if len(leafNode.pfn)!=0:
93 condition+=
' AND pfn=:pfn'
94 conditionbindDict.extend(
'pfn',
'string')
95 conditionbindDict[
'pfn'].setData(leafNode.pfn)
96 query.setCondition(condition,conditionbindDict)
98 cursor=query.execute()
99 while( cursor.next() ):
101 tagid=cursor.currentRow()[
'tagid'].
data()
106 if duplicate
is False:
107 transaction.start(
False)
110 tabrowValueDict={
'tagid':tagid,
'tagname':leafNode.tagname,
'objectname':leafNode.objectname,
'pfn':leafNode.pfn,
'labelname':leafNode.labelname,
'recordname':leafNode.recordname}
118 except Exception, er:
119 transaction.rollback()
120 raise Exception, str(er)
123 """ clone all existing entries only servicename in pfn are different
124 return collection of new (oldtagid,newtagid) pair
127 transaction=self.__session.transaction()
130 transaction.start(
True)
133 query.addToOutputList(columnName)
134 cursor=query.execute()
136 tagid=cursor.currentRow()[
'tagid'].
data()
137 tagname=cursor.currentRow()[
'tagname'].
data()
138 pfn=cursor.currentRow()[
'pfn'].
data()
139 (servicename,schemaname)=pfn.rsplit(
'/',1)
140 newpfn=(
'/').
join([newservicename,schemaname])
142 objname=cursor.currentRow()[
'objectname'].
data()
143 redname=cursor.currentRow()[
'recordname'].
data()
144 labname=cursor.currentRow()[
'labelname'].
data()
146 r=(tagid,tagname,newpfn,objname,redname,labname)
150 except Exception, er:
151 transaction.rollback()
152 raise Exception, str(er)
167 raise "addEntry returns 0"
168 newtaglinks.append((oldtagid,n))
172 raise Exception, str(e)
175 """ replace all existing entries replace service name in pfn
176 no change in other parameters
178 transaction=self.__session.transaction()
181 transaction.start(
True)
183 query.addToOutputList(
'pfn')
184 cursor=query.execute()
186 pfn=cursor.currentRow()[
'pfn'].
data()
190 except Exception, er:
191 transaction.rollback()
193 raise Exception, str(er)
195 transaction.start(
False)
197 inputData = coral.AttributeList()
198 inputData.extend(
'newpfn',
'string')
199 inputData.extend(
'oldpfn',
'string')
201 (servicename,schemaname)=pfn.rsplit(
'/',1)
202 newpfn=(
'/').
join([newservicename,schemaname])
203 inputData[
'newpfn'].setData(newpfn)
204 inputData[
'oldpfn'].setData(pfn)
205 editor.updateRows(
"pfn = :newpfn",
"pfn = :oldpfn", inputData )
208 transaction.rollback()
209 raise Exception, str(e)
212 """ clone an existing entry with different pfn parameter
213 Input: sourcetagid, pfn.
214 Output: tagid of the new entry. Return 0 in case no new entry created or required entry already exists.
226 transaction=self.__session.transaction()
227 transaction.start(
False)
228 schema = self.__session.nominalSchema()
231 tabrowValueDict={
'tagid':newtagid,
'tagname':nd.tagname,
'objectname':nd.objectname,
'pfn':pfn,
'labelname':nd.labelname,
'recordname':nd.recordname}
239 except Exception, er:
240 transaction.rollback()
241 raise Exception, str(er)
244 """Get basic tag from inventory by tagName+pfn. pfn can be empty\n
247 throw if more than one entry is found.
250 transaction=self.__session.transaction()
252 transaction.start(
True)
255 query.addToOutputList(columnName)
256 conditionData = coral.AttributeList()
257 condition =
"tagname=:tagname"
258 conditionData.extend(
'tagname',
'string' )
259 conditionData[
'tagname'].setData(tagName)
261 condition +=
" AND pfn=:pfn"
262 conditionData.extend(
'pfn',
'string' )
263 conditionData[
'pfn'].setData(pfn)
264 query.setCondition(condition,conditionData)
265 cursor = query.execute()
267 while ( cursor.next() ):
269 raise ValueError,
"tagName "+tagName+
" is not unique, please further specify parameter pfn"
271 leafnode.tagid=cursor.currentRow()[
'tagid'].
data()
272 leafnode.tagname=cursor.currentRow()[
'tagname'].
data()
273 leafnode.objectname=cursor.currentRow()[
'objectname'].
data()
274 leafnode.pfn=cursor.currentRow()[
'pfn'].
data()
275 leafnode.labelname=cursor.currentRow()[
'labelname'].
data()
276 leafnode.recordname=cursor.currentRow()[
'recordname'].
data()
281 transaction.rollback()
282 raise Exception, str(e)
284 """Get basic tag from inventory by id.\n
289 transaction=self.__session.transaction()
291 transaction.start(
True)
294 query.addToOutputList(columnName)
295 condition =
"tagid=:tagid"
296 conditionData = coral.AttributeList()
297 conditionData.extend(
'tagid',
'unsigned long' )
298 conditionData[
'tagid'].setData(tagId)
299 query.setCondition( condition, conditionData)
300 cursor = query.execute()
301 while ( cursor.next() ):
303 leafnode.tagid=cursor.currentRow()[
'tagid'].
data()
304 leafnode.tagname=cursor.currentRow()[
'tagname'].
data()
305 leafnode.objectname=cursor.currentRow()[
'objectname'].
data()
306 leafnode.pfn=cursor.currentRow()[
'pfn'].
data()
307 leafnode.labelname=cursor.currentRow()[
'labelname'].
data()
308 leafnode.recordname=cursor.currentRow()[
'recordname'].
data()
313 transaction.rollback()
314 raise Exception, str(e)
316 """Get all entries in the inventory
317 Output: list of leafNode objects
320 transaction=self.__session.transaction()
322 transaction.start(
True)
325 query.addToOutputList(columnName)
326 cursor = query.execute()
327 while ( cursor.next() ):
329 leafnode.tagid=cursor.currentRow()[
'tagid'].
data()
330 leafnode.tagname=cursor.currentRow()[
'tagname'].
data()
331 leafnode.objectname=cursor.currentRow()[
'objectname'].
data()
332 leafnode.pfn=cursor.currentRow()[
'pfn'].
data()
333 leafnode.recordname=cursor.currentRow()[
'recordname'].
data()
334 leafnode.labelname=cursor.currentRow()[
'labelname'].
data()
335 result.append(leafnode)
340 transaction.rollback()
341 raise Exception, str(e)
343 """get tagids correspond to a given tag name
345 transaction=self.__session.transaction()
348 transaction.start(
True)
350 condition=
'tagname = :tagname'
351 conditionBindData=coral.AttributeList()
352 conditionBindData.extend(
'tagname',
'string')
353 conditionBindData[
'tagname'].setData(name)
354 query.addToOutputList(tagid)
355 query.setCondition(condition,conditionBindData)
356 cursor = query.execute()
357 while ( cursor.next() ):
358 tagid=cursor.currentRow()[
'tagid'].
data()
362 transaction.rollback()
363 raise Exception, str(e)
366 """Delete all entries in the inventory
368 transaction=self.__session.transaction()
370 transaction.start(
False)
371 schema = self.__session.nominalSchema()
373 inputData = coral.AttributeList()
379 transaction.rollback()
380 raise Exception, str(e)
383 """Delete entry with given tag name
386 transaction=self.__session.transaction()
387 transaction.start(
False)
388 schema = self.__session.nominalSchema()
390 inputData = coral.AttributeList()
391 inputData.extend(
"tagname",
"string" )
392 inputData[0].setData(tagname)
398 transaction.rollback()
399 raise Exception, str(e)
402 """Replace the run time label of the given tag
405 transaction=self.__session.transaction()
406 transaction.start(
False)
407 schema = self.__session.nominalSchema()
408 inputData = coral.AttributeList()
409 inputData.extend(
"labelname",
"string" )
410 inputData.extend(
"tagname",
"string" )
411 inputData[0].setData(label)
412 inputData[1].setData(tagname)
414 editor.updateRows(
"labelname=:labelname",
"tagname=:tagname", inputData )
417 transaction.rollback()
418 raise Exception, str(e)
421 """insert a chunk of entries.
422 Input: entries [{tagid:unsigned long, tagname:string , pfn:string , recordname:string , objectname:string, labelname:string }]
423 Output: {oldtagid:newtagid} of the inserted entries. If tag already exists, old tagid is returned
425 transaction=self.__session.transaction()
436 if n.tagname==e[
'tagname']
and n.pfn==e[
'pfn']:
437 results[n.tagid]=n.tagid
439 transaction.start(
False)
441 query.addToOutputList(
'nextID')
443 cursor = query.execute()
446 nextid=cursor.currentRow()[0].
data()
448 inputData = coral.AttributeList()
449 inputData.extend(
'delta',
'unsigned long' )
456 inputData[
'delta'].setData(delta)
457 idEditor.updateRows(
'nextID = nextID + :delta',
'',inputData)
460 insertdata=coral.AttributeList()
461 insertdata.extend(
'tagid',
'unsigned long')
462 insertdata.extend(
'tagname',
'string')
463 insertdata.extend(
'pfn',
'string')
464 insertdata.extend(
'recordname',
'string')
465 insertdata.extend(
'objectname',
'string')
466 insertdata.extend(
'labelname',
'string')
467 bulkOperation=dataEditor.bulkInsert(insertdata,delta)
468 for entry
in entries:
469 insertdata[
'tagid'].setData(nextid)
470 insertdata[
'tagname'].setData(entry[
'tagname'])
471 insertdata[
'pfn'].setData(entry[
'pfn'])
472 insertdata[
'recordname'].setData(entry[
'recordname'])
473 insertdata[
'objectname'].setData(entry[
'objectname'])
474 insertdata[
'labelname'].setData(entry[
'labelname'])
475 bulkOperation.processNextIteration()
476 results[entry[
'tagid']]=nextid
478 bulkOperation.flush()
484 transaction.rollback()
485 raise Exception, str(e)
487 if __name__ ==
"__main__":
490 svc = coral.ConnectionService()
491 session = svc.connect(
'sqlite_file:testInventory.db',
492 accessMode = coral.access_Update )
495 inv.createInventoryTable()
497 tagentry.tagname=
'ecalpedestalsfromonline'
498 tagentry.objectname=
'EcalPedestals'
499 tagentry.pfn=
'oracle://devdb10/CMS_COND_ECAL'
500 tagentry.recordname=
'EcalPedestalsRcd'
501 tagentry.labelname=
''
502 inv.addEntry(tagentry)
503 tagentry.tagname=
'crap'
504 tagentry.objectname=
'MyPedestals'
505 tagentry.pfn=
'oracle://devdb10/CMS_COND_ME'
506 tagentry.recordname=
'MyPedestalsRcd'
507 tagentry.labelname=
'mylabel'
508 inv.addEntry(tagentry)
509 result=inv.getAllEntries()
510 print 'get all##\t',result
511 result=inv.getEntryByName(
'ecalpedestalsfromonline',
'oracle://devdb10/CMS_COND_ECAL')
512 print 'get ecalpedestalsfromonline##\t',result
513 result=inv.getEntryByName(
'crap',
'oracle://devdb10/CMS_COND_ME')
514 print 'get crap##\t',result
515 result=inv.getEntryById(0)
516 print 'get by id 0##\t',result
517 inv.deleteEntryByName(
'ecalpedestalsfromonline')
518 print 'TESTING getEntryByName'
519 result=inv.getEntryByName(
'ecalpedestalsfromonline',
'oracle://devdb10/CMS_COND_ECAL')
520 print 'get ecalpedestalsfromonline##\t',result
521 result=inv.getEntryByName(
'crap',
'oracle://devdb10/CMS_COND_ME')
522 print 'get crap##\t',result
523 print 'TESTING cloneEntry'
524 newid=inv.cloneEntry(result.tagid,
'fontier://crap/crap')
526 print 'TESTING addEntriesReplaceService'
527 newtagids=inv.addEntriesReplaceService(
'oracle://cms_orcoff_int')
528 print 'new tag ids ',newtagids
529 print 'TESTING modifyEntriesReplaceService'
530 inv.modifyEntriesReplaceService(
'oracle://cms_orcoff_int9r')
531 print 'TESTING bulkInsertEntries'
533 entries.append({
'tagid':10,
'tagname':
'tag1',
'pfn':
'dbdb',
'recordname':
'myrcd',
'objectname':
'bobo',
'labelname':
''})
534 entries.append({
'tagid':11,
'tagname':
'tag2',
'pfn':
'dbdb',
'recordname':
'mdrcd',
'objectname':
'bobo',
'labelname':
''})
535 entries.append({
'tagid':12,
'tagname':
'tag3',
'pfn':
'dbdb',
'recordname':
'ndrcd',
'objectname':
'bobo',
'labelname':
''})
536 entries.append({
'tagid':13,
'tagname':
'tag4',
'pfn':
'dbdb',
'recordname':
'ndrcd',
'objectname':
'bobo',
'labelname':
''})
537 a=inv.bulkInsertEntries(entries)
542 print "Failed in unit test"
def addEntriesReplaceService
def modifyEntriesReplaceService
__tagInventoryTableNotNullColumns
static std::string join(char **cmd)
__tagInventoryTableHandle
__tagInventoryTableColumns
char data[epos_bytes_allocation]