kernel: improve workflow

- add index on workitem, instance
- add cache on wkf

bzr revid: ced-81afcda4514578d17d998c4e3a8ff516d3346b97
This commit is contained in:
ced 2007-07-26 08:31:36 +00:00
parent 1029e1323b
commit cfbf768654
3 changed files with 38 additions and 4 deletions

View File

@ -28,6 +28,7 @@
##############################################################################
from osv import fields, osv
import netsvc
class workflow(osv.osv):
_name = "workflow"
@ -42,6 +43,20 @@ class workflow(osv.osv):
_defaults = {
'on_create': lambda *a: True
}
def write(self, cr, user, ids, vals, context=None):
if not context:
context={}
wf_service = netsvc.LocalService("workflow")
wf_service.clear_cache(cr, user)
return super(workflow, self).write(cr, user, ids, vals, context=context)
def create(self, cr, user, vals, context=None):
if not context:
context={}
wf_service = netsvc.LocalService("workflow")
wf_service.clear_cache(cr, user)
return super(workflow, self).create(cr, user, vals, context=context)
workflow()
class wkf_activity(osv.osv):
@ -100,6 +115,12 @@ class wkf_instance(osv.osv):
'res_type': fields.char('Resource Model', size=64),
'state': fields.char('State', size=32),
}
def _auto_init(self, cr):
super(wkf_instance, self)._auto_init(cr)
cr.execute('SELECT indexname FROM pg_indexes WHERE indexname = \'wkf_instance_res_id_res_type_state_index\'')
if not cr.fetchone():
cr.execute('CREATE INDEX wkf_instance_res_id_res_type_state_index ON wkf_instance (res_id, res_type, state)')
cr.commit()
wkf_instance()
class wkf_workitem(osv.osv):
@ -110,7 +131,7 @@ class wkf_workitem(osv.osv):
_columns = {
'act_id': fields.many2one('workflow.activity', 'Activity', required=True, ondelete="cascade"),
'subflow_id': fields.many2one('workflow.instance', 'Subflow', ondelete="cascade"),
'inst_id': fields.many2one('workflow.instance', 'Instance', required=True, ondelete="cascade"),
'inst_id': fields.many2one('workflow.instance', 'Instance', required=True, ondelete="cascade", select=1),
'state': fields.char('State', size=64),
}
wkf_workitem()

View File

@ -42,6 +42,11 @@ class workflow_service(netsvc.Service):
self.exportMethod(self.trg_validate)
self.exportMethod(self.trg_redirect)
self.exportMethod(self.trg_trigger)
self.exportMethod(self.clear_cache)
self.wkf_on_create_cache={}
def clear_cache(self, cr, uid):
self.wkf_on_create_cache[cr.dbname]={}
def trg_write(self, uid, res_type, res_id, cr):
ident = (uid,res_type,res_id)
@ -63,8 +68,14 @@ class workflow_service(netsvc.Service):
def trg_create(self, uid, res_type, res_id, cr):
ident = (uid,res_type,res_id)
cr.execute('select id from wkf where osv=%s and on_create=True', (res_type,))
for (wkf_id,) in cr.fetchall():
self.wkf_on_create_cache.setdefault(cr.dbname, {})
if res_type in self.wkf_on_create_cache[cr.dbname]:
wkf_ids = self.wkf_on_create_cache[cr.dbname][res_type]
else:
cr.execute('select id from wkf where osv=%s and on_create=True', (res_type,))
wkf_ids = cr.fetchall()
self.wkf_on_create_cache[cr.dbname][res_type] = wkf_ids
for (wkf_id,) in wkf_ids:
instance.create(cr, ident, wkf_id)
def trg_validate(self, uid, res_type, res_id, signal, cr):

View File

@ -71,7 +71,9 @@ def process(cr, workitem, ident, signal=None, force_running=False):
if trans['trigger_model']:
ids = wkf_expr._eval_expr(cr,ident,workitem,trans['trigger_expr_id'])
for id in ids:
cr.execute('insert into wkf_triggers (model,res_id,instance_id,workitem_id) values (%s,%d,%d,%d)', (trans['trigger_model'],id,workitem['inst_id'], workitem['id']))
cr.execute('select nextval(\'wkf_workitem_id_seq\')')
id =cr.fetchone()[0]
cr.execute('insert into wkf_triggers (model,res_id,instance_id,workitem_id,id) values (%s,%d,%d,%d,%d)', (trans['trigger_model'],id,workitem['inst_id'], workitem['id'], id))
return True