Skip to content
This repository has been archived by the owner on May 13, 2020. It is now read-only.

Commit

Permalink
Patch pymongo a bit, so that the hash of the DBRef is not computed all
Browse files Browse the repository at this point in the history
the time but cached. DBRef's are generally considered immutable.
  • Loading branch information
strichter committed Apr 3, 2012
1 parent 9684e5f commit 96d07cd
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/mongopersist/__init__.py
@@ -1 +1,5 @@
# Make a package.

from . import pymongo
pymongo.patch()
del pymongo
32 changes: 32 additions & 0 deletions src/mongopersist/pymongo.py
@@ -0,0 +1,32 @@
##############################################################################
#
# Copyright (c) 2011 Zope Foundation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""PyMongo Patches"""

def lazy_hash(self):
"""Get a hash value for this :class:`DBRef`.
.. versionadded:: 1.1
"""
if self.__hash is None:
self.__hash = self.orig_hash()
return self.__hash

def patch():
# ObjectId should get patched too, but it is hard, since it uses slots
# *and* rquires the original object reference to be around (otherwise it
# creates BSON encoding errors.
import bson.dbref
bson.dbref.DBRef.__hash = None
bson.dbref.DBRef.orig_hash = bson.dbref.DBRef.__hash__
bson.dbref.DBRef.__hash__ = lazy_hash

0 comments on commit 96d07cd

Please sign in to comment.