Skip to content

Commit

Permalink
optimization: pass current row around in ORM instead of looking it up
Browse files Browse the repository at this point in the history
  • Loading branch information
jeanqasaur committed Jun 26, 2015
1 parent 0a7c910 commit 45f468c
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions jeevesdb/JeevesModel.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import JeevesLib
from JeevesLib import fexpr_cast
from fast.AST import Facet, FObject, Unassigned, FExpr, FNull
#from conf.settings import TEST_OPTIMIZATIONS as optimize_flag
import JeevesModelUtils

class JeevesQuerySet(QuerySet):
Expand All @@ -38,7 +37,7 @@ def get_env(obj, fields, env):
if var_name in env and env[var_name] != value:
return None
env[var_name] = value
acquire_label_by_name(self.model._meta.app_label, var_name)
acquire_label_by_name(self.model._meta.app_label, var_name, obj=obj)
for field, subs in fields.iteritems() if fields else []:
if field and get_env(getattr(obj, field), subs, env) is None:
return None
Expand Down Expand Up @@ -72,7 +71,7 @@ def get(self, use_base_env=False, skip_optimize=False, **kwargs):
cur = FObject(row)
for var_name, val in conditions.iteritems():
label = acquire_label_by_name(self.model._meta.app_label
, var_name)
, var_name, obj=row)
viewer = JeevesLib.get_viewer()
if has_viewer and not skip_optimize:
if JeevesLib.concretize(viewer, label):
Expand Down Expand Up @@ -120,7 +119,8 @@ def all(self):
for vname, vval in cond.iteritems():
if vname not in env:
vlabel = acquire_label_by_name(
self.model._meta.app_label, vname)
self.model._meta.app_label, vname
, obj=val)
JeevesLib.jeevesState.pathenv.push(vlabel, vval)
popcount += 1
elif env[vname] != vval:
Expand All @@ -139,7 +139,8 @@ def all(self):
for vname, vval in cond.iteritems():
if vname not in env:
vlabel = acquire_label_by_name(
self.model._meta.app_label, vname)
self.model._meta.app_label, vname
, obj=val)
label = JeevesLib.concretize(viewer, vlabel)
if label == vval:
elements.append(val)
Expand All @@ -155,7 +156,8 @@ def delete(self):
for vname, vval in cond.iteritems():
if vname not in JeevesLib.jeevesState.pathenv.getEnv():
vlabel = acquire_label_by_name(
self.model._meta.app_label, vname)
self.model._meta.app_label, vname
, obj=val)
JeevesLib.jeevesState.pathenv.push(vlabel, vval)
popcount += 1
val.delete()
Expand Down Expand Up @@ -215,7 +217,7 @@ def clone(old):
setattr(ans, fld.attname, getattr(old, fld.attname))
return ans

def acquire_label_by_name(app_label, label_name):
def acquire_label_by_name(app_label, label_name, obj=None):
"""Gets a label by name.
"""
if JeevesLib.doesLabelExist(label_name):
Expand All @@ -227,7 +229,9 @@ def acquire_label_by_name(app_label, label_name):
# TODO: optimization: most of the time this obj will be the one we are
# already fetching
# Get the current row.
obj = model.objects.get(use_base_env=True, skip_optimize=True, jeeves_id=jeeves_id)
if obj == None:
obj = model.objects.get(use_base_env=True
, skip_optimize=True, jeeves_id=jeeves_id)
restrictor = getattr(model, 'jeeves_restrict_' + field_name)
JeevesLib.restrict(label, lambda ctxt: restrictor(obj, ctxt), True)
return label
Expand Down

0 comments on commit 45f468c

Please sign in to comment.