Further caching and optimization, making some operations noticeable faster in the end.

This commit is contained in:
Griatch 2012-04-26 17:47:25 +02:00
parent 1a6ef5d983
commit 6e08c011a1
13 changed files with 78 additions and 73 deletions

View file

@ -105,9 +105,10 @@ def get_and_merge_cmdsets(caller):
# Gather cmdsets from location, objects in location or carried # Gather cmdsets from location, objects in location or carried
local_objects_cmdsets = [None] local_objects_cmdsets = [None]
location = None try:
if hasattr(caller, "location"):
location = caller.location location = caller.location
except Exception:
location = None
if location and not caller_cmdset.no_objs: if location and not caller_cmdset.no_objs:
# Gather all cmdsets stored on objects in the room and # Gather all cmdsets stored on objects in the room and
# also in the caller's inventory and the location itself # also in the caller's inventory and the location itself

View file

@ -158,7 +158,7 @@ def at_search_result(msg_obj, ostring, results, global_search=False):
if hasattr(result, "location") and result.location == msg_obj: if hasattr(result, "location") and result.location == msg_obj:
invtext = " (carried)" invtext = " (carried)"
if show_dbref: if show_dbref:
dbreftext = "(#%i)" % result.id dbreftext = "(#%i)" % result.dbid
string += "\n %i-%s%s%s" % (num+1, result.name, string += "\n %i-%s%s%s" % (num+1, result.name,
dbreftext, invtext) dbreftext, invtext)
results = None results = None

View file

@ -82,7 +82,6 @@ DefaultLock: Exits: controls who may traverse the exit to
""" """
from django.conf import settings from django.conf import settings
from src.utils import search
from src.utils import utils from src.utils import utils
_PERMISSION_HIERARCHY = [p.lower() for p in settings.PERMISSION_HIERARCHY] _PERMISSION_HIERARCHY = [p.lower() for p in settings.PERMISSION_HIERARCHY]
@ -123,7 +122,7 @@ def perm(accessing_obj, accessed_obj, *args, **kwargs):
try: try:
perm = args[0].lower() perm = args[0].lower()
permissions = [p.lower() for p in accessing_obj.permissions] permissions = [p.lower() for p in accessing_obj.permissions]
except AttributeError, IndexError: except (AttributeError, IndexError):
return False return False
if perm in permissions: if perm in permissions:
@ -193,8 +192,8 @@ def dbref(accessing_obj, accessed_obj, *args, **kwargs):
dbref = int(args[0].strip().strip('#')) dbref = int(args[0].strip().strip('#'))
except ValueError: except ValueError:
return False return False
if hasattr(accessing_obj, 'id'): if hasattr(accessing_obj, 'dbid'):
return dbref == accessing_obj.id return dbref == accessing_obj.dbid
return False return False
def pdbref(accessing_obj, accessed_obj, *args, **kwargs): def pdbref(accessing_obj, accessed_obj, *args, **kwargs):
@ -255,8 +254,7 @@ def attr(accessing_obj, accessed_obj, *args, **kwargs):
"compare based on type" "compare based on type"
try: try:
return CF_MAPPING.get(typ, 'default')(val1, val2) return CF_MAPPING.get(typ, 'default')(val1, val2)
except Exception, e: except Exception:
#print e
# this might happen if we try to compare two things that cannot be compared # this might happen if we try to compare two things that cannot be compared
return False return False
@ -376,7 +374,7 @@ def holds(accessing_obj, accessed_obj, *args, **kwargs):
# helper function. Compares both dbrefs and keys/aliases. # helper function. Compares both dbrefs and keys/aliases.
objid = str(objid) objid = str(objid)
dbref = utils.dbref(objid) dbref = utils.dbref(objid)
if dbref and any((True for obj in contents if obj.id == dbref)): if dbref and any((True for obj in contents if obj.dbid == dbref)):
return True return True
objid = objid.lower() objid = objid.lower()
return any((True for obj in contents return any((True for obj in contents
@ -386,13 +384,13 @@ def holds(accessing_obj, accessed_obj, *args, **kwargs):
return check_holds(args[0]) return check_holds(args[0])
else: else:
try: try:
if check_holds(accessed_obj.id): if check_holds(accessed_obj.dbid):
#print "holds: accessed_obj.id - True" #print "holds: accessed_obj.id - True"
return True return True
except Exception: except Exception:
pass pass
#print "holds: accessed_obj.obj.id -", hasattr(accessed_obj, "obj") and check_holds(accessed_obj.obj.id) #print "holds: accessed_obj.obj.id -", hasattr(accessed_obj, "obj") and check_holds(accessed_obj.obj.id)
return hasattr(accessed_obj, "obj") and check_holds(accessed_obj.obj.id) return hasattr(accessed_obj, "obj") and check_holds(accessed_obj.obj.dbid)
def superuser(*args, **kwargs): def superuser(*args, **kwargs):
""" """

View file

@ -346,7 +346,7 @@ class LockHandler(object):
if (not no_superuser_bypass if (not no_superuser_bypass
and ((hasattr(accessing_obj, 'is_superuser') and accessing_obj.is_superuser) and ((hasattr(accessing_obj, 'is_superuser') and accessing_obj.is_superuser)
or (hasattr(accessing_obj, 'player') and hasattr(accessing_obj.player, 'is_superuser') and accessing_obj.player.is_superuser) or (hasattr(accessing_obj, 'player') and hasattr(accessing_obj.player, 'is_superuser') and accessing_obj.player.is_superuser)
or (hasattr(accessing_obj, 'get_player') and (accessing_obj.get_player()==None or accessing_obj.get_player().is_superuser)))): or (hasattr(accessing_obj, 'get_player') and (not accessing_obj.get_player() or accessing_obj.get_player().is_superuser)))):
# we grant access to superusers and also to protocol instances that not yet has any player assigned to them (the # we grant access to superusers and also to protocol instances that not yet has any player assigned to them (the
# latter is a safety feature since superuser cannot be authenticated at some point during the connection). # latter is a safety feature since superuser cannot be authenticated at some point during the connection).
return True return True

View file

@ -247,14 +247,10 @@ class ObjectDB(TypedObject):
"Setter. Allows for self.player = value" "Setter. Allows for self.player = value"
if isinstance(player, TypeClass): if isinstance(player, TypeClass):
player = player.dbobj player = player.dbobj
self.db_player = player
self.save()
_set_cache(self, "player", player) _set_cache(self, "player", player)
#@player.deleter #@player.deleter
def __player_del(self): def __player_del(self):
"Deleter. Allows for del self.player" "Deleter. Allows for del self.player"
self.db_player = None
self.save()
_del_cache(self, "player") _del_cache(self, "player")
player = property(__player_get, __player_set, __player_del) player = property(__player_get, __player_set, __player_del)
@ -747,7 +743,7 @@ class ObjectDB(TypedObject):
default_home_id = int(settings.CHARACTER_DEFAULT_HOME) default_home_id = int(settings.CHARACTER_DEFAULT_HOME)
try: try:
default_home = ObjectDB.objects.get(id=default_home_id) default_home = ObjectDB.objects.get(id=default_home_id)
if default_home.id == self.id: if default_home.dbid == self.dbid:
# we are deleting default home! # we are deleting default home!
default_home = None default_home = None
except Exception: except Exception:
@ -758,7 +754,7 @@ class ObjectDB(TypedObject):
for obj in objs: for obj in objs:
home = obj.home home = obj.home
# Obviously, we can't send it back to here. # Obviously, we can't send it back to here.
if not home or (home and home.id == self.id): if not home or (home and home.dbid == self.dbid):
obj.home = default_home obj.home = default_home
# If for some reason it's still None... # If for some reason it's still None...
@ -767,14 +763,14 @@ class ObjectDB(TypedObject):
string += "now has a null location." string += "now has a null location."
obj.location = None obj.location = None
obj.msg("Something went wrong! You are dumped into nowhere. Contact an admin.") obj.msg("Something went wrong! You are dumped into nowhere. Contact an admin.")
logger.log_errmsg(string % (obj.name, obj.id)) logger.log_errmsg(string % (obj.name, obj.dbid))
return return
if obj.has_player: if obj.has_player:
if home: if home:
string = "Your current location has ceased to exist," string = "Your current location has ceased to exist,"
string += " moving you to %s(#%d)." string += " moving you to %s(#%d)."
obj.msg(string % (home.name, home.id)) obj.msg(string % (home.name, home.dbid))
else: else:
# Famous last words: The player should never see this. # Famous last words: The player should never see this.
string = "This place should not exist ... contact an admin." string = "This place should not exist ... contact an admin."

View file

@ -353,16 +353,14 @@ class Object(TypeClass):
This has be located at this level, having it in the This has be located at this level, having it in the
parent doesn't work. parent doesn't work.
""" """
result = self.id == other
if not result and hasattr(other, "id"):
result = self.id == other.id
if not result:
try: try:
result = other and self.user.id == other.user.id return self.dbref == other or self.dbref == other.dbref
except AttributeError: except AttributeError:
pass # compare players instead
return result try:
return self.player.uid == other or self.player.uid == other.player.uid
except AttributeError:
return False
## hooks called by the game engine ## hooks called by the game engine

View file

@ -257,10 +257,10 @@ class PlayerDB(TypedObject):
# #
def __str__(self): def __str__(self):
return smart_str("%s(player %i)" % (self.name, self.id)) return smart_str("%s(player %i)" % (self.name, self.dbid))
def __unicode__(self): def __unicode__(self):
return u"%s(player#%i)" % (self.name, self.id) return u"%s(player#%i)" % (self.name, self.dbid)
# this is required to properly handle attributes and typeclass loading # this is required to properly handle attributes and typeclass loading
_typeclass_paths = settings.PLAYER_TYPECLASS_PATHS _typeclass_paths = settings.PLAYER_TYPECLASS_PATHS

View file

@ -32,7 +32,7 @@ class ScriptClass(TypeClass):
parent doesn't work. parent doesn't work.
""" """
try: try:
return other.id == self.id return other.dbid == self.dbid
except Exception: except Exception:
return False return False
@ -61,7 +61,7 @@ class ScriptClass(TypeClass):
def _step_err_callback(self, e): def _step_err_callback(self, e):
"callback for runner errors" "callback for runner errors"
cname = self.__class__.__name__ cname = self.__class__.__name__
estring = "Script %s(#%i) of type '%s': at_repeat() error '%s'." % (self.key, self.id, cname, e.getErrorMessage()) estring = "Script %s(#%i) of type '%s': at_repeat() error '%s'." % (self.key, self.dbid, cname, e.getErrorMessage())
try: try:
self.dbobj.db_obj.msg(estring) self.dbobj.db_obj.msg(estring)
except Exception: except Exception:
@ -183,7 +183,7 @@ class ScriptClass(TypeClass):
try: try:
self._stop_task() self._stop_task()
except Exception, e: except Exception, e:
logger.log_trace("Stopping script %s(%s)" % (self.key, self.id)) logger.log_trace("Stopping script %s(%s)" % (self.key, self.dbid))
pass pass
try: try:
self.dbobj.delete() self.dbobj.delete()

View file

@ -139,10 +139,7 @@ class ServerSession(Session):
""" """
Get the player associated with this session Get the player associated with this session
""" """
if self.logged_in: return self.logged_in and self.player
return self.player
else:
return None
def get_character(self): def get_character(self):
""" """

View file

@ -14,7 +14,6 @@ There are two similar but separate stores of sessions:
import time import time
from django.conf import settings from django.conf import settings
from django.contrib.auth.models import User
from src.server.models import ServerConfig from src.server.models import ServerConfig
from src.commands.cmdhandler import CMD_LOGINSTART from src.commands.cmdhandler import CMD_LOGINSTART
@ -259,12 +258,7 @@ class ServerSessionHandler(SessionHandler):
""" """
Given a player, return any matching sessions. Given a player, return any matching sessions.
""" """
username = player.user.username uid = player.uid
try:
uobj = User.objects.get(username=username)
except User.DoesNotExist:
return None
uid = uobj.id
return [session for session in self.sessions.values() if session.logged_in and session.uid == uid] return [session for session in self.sessions.values() if session.logged_in and session.uid == uid]
def sessions_from_character(self, character): def sessions_from_character(self, character):

View file

@ -853,7 +853,7 @@ class TypedObject(SharedMemoryModel):
_db_model_name = "typeclass" # used by attributes to safely store objects _db_model_name = "typeclass" # used by attributes to safely store objects
def __eq__(self, other): def __eq__(self, other):
return other and hasattr(other, 'id') and self.id == other.id return other and hasattr(other, 'dbid') and self.dbid == other.dbid
def __str__(self): def __str__(self):
return smart_str("%s" % self.key) return smart_str("%s" % self.key)
@ -881,16 +881,35 @@ class TypedObject(SharedMemoryModel):
return _GA(typeclass, propname) return _GA(typeclass, propname)
else: else:
raise AttributeError raise AttributeError
#@property
_dbid_cache = None
def __dbid_get(self):
"""
Caches and returns the unique id of the object.
Use this instead of self.id, which is not cached.
"""
dbid = _GA(self, "_dbid_cache")
if not dbid:
dbid = _GA(self, "id")
_SA(self, "_dbid_cache", dbid)
return dbid
def __dbid_set(self, value):
raise Exception("dbid cannot be set!")
def __dbid_del(self):
raise Exception("dbid cannot be deleted!")
dbid = property(__dbid_get, __dbid_set, __dbid_del)
#@property #@property
def __dbref_get(self): def __dbref_get(self):
""" """
Returns the object's dbref id on the form #NN. Returns the object's dbref on the form #NN.
Alternetively, use obj.id directly to get dbref
without any #.
""" """
return "#%s" % str(_GA(self, "id")) return "#%s" % _GA(self, "_TypedObject__dbid_get")()
dbref = property(__dbref_get) def __dbref_set(self):
raise Exception("dbref cannot be set!")
def __dbref_del(self):
raise Exception("dbref cannot be deleted!")
dbref = property(__dbref_get, __dbref_set, __dbref_del)
# typeclass property # typeclass property
#@property #@property

View file

@ -91,6 +91,8 @@ class TypeClass(object):
transparently include the properties on transparently include the properties on
self.dbobj. Note that dbobj properties have self.dbobj. Note that dbobj properties have
priority, so if you define a same-named priority, so if you define a same-named
property on the class, it will NOT be property on the class, it will NOT be
accessible through getattr. accessible through getattr.
""" """
@ -115,8 +117,8 @@ class TypeClass(object):
try: try:
return _GA(dbobj,"get_attribute_raise")(propname) return _GA(dbobj,"get_attribute_raise")(propname)
except AttributeError: except AttributeError:
string = "Object: '%s' not found on %s(%s), nor on its typeclass %s." string = "Object: '%s' not found on %s(#%s), nor on its typeclass %s."
raise AttributeError(string % (propname, dbobj, dbobj.dbref, dbobj.typeclass_path)) raise AttributeError(string % (propname, dbobj, dbobj.dbid, dbobj.typeclass_path))
def __setattr__(self, propname, value): def __setattr__(self, propname, value):
""" """
@ -185,9 +187,9 @@ class TypeClass(object):
try: try:
dbobj.del_attribute_raise(propname) dbobj.del_attribute_raise(propname)
except AttributeError: except AttributeError:
string = "Object: '%s' not found on %s(%s), nor on its typeclass %s." string = "Object: '%s' not found on %s(#%s), nor on its typeclass %s."
raise AttributeError(string % (propname, dbobj, raise AttributeError(string % (propname, dbobj,
dbobj.dbref, dbobj.dbid,
dbobj.typeclass_path,)) dbobj.typeclass_path,))
def __str__(self): def __str__(self):