Skip to content

Commit

Permalink
Optimize loadBefore for tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
jamadden committed Jul 30, 2019
1 parent c6500cf commit c7812ba
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 16 deletions.
6 changes: 5 additions & 1 deletion CHANGES.rst
Expand Up @@ -7,7 +7,11 @@

- Eliminate runtime dependency on ZEO. See :issue:`293`.

- Fix a rare race condition allocating OIDs on MySQL. See :issue:`283`.
- Fix a rare race condition allocating OIDs on MySQL. See
:issue:`283`.

- Optimize the ``loadBefore`` method. It appears to be mostly used in
the tests.

3.0a6 (2019-07-29)
==================
Expand Down
14 changes: 0 additions & 14 deletions src/relstorage/storage/__init__.py
Expand Up @@ -137,9 +137,6 @@ class RelStorage(LegacyMethodsMixin,

_oids = ReadOnlyOIDs()


__initting = True

# Attributes in our dictionary that shouldn't have stale()/no_longer_stale()
# called on them. At this writing, it's just the type object.
_STALE_IGNORED_ATTRS = (
Expand Down Expand Up @@ -264,17 +261,6 @@ def __init__(self, adapter, name=None, create=None,
storer = BlobStorer(self.blobhelper, self._store_connection)
copy_storage_methods(self, storer)

self.__initting = False

# __setattr__ is here during refactoring to help us make sure we're setting what we
# want to set.
# XXX: Remove before release.
def __setattr__(self, name, value):
# undo is special cased for reltestbase._make_readonly
if not self.__initting and name not in dir(self) and name != 'undo':
raise AttributeError("Cannot set %s" % name)
object.__setattr__(self, name, value)

def __repr__(self):
return "<%s at %x keep_history=%s phase=%r cache=%r>" % (
self.__class__.__name__,
Expand Down
23 changes: 22 additions & 1 deletion src/relstorage/storage/load.py
Expand Up @@ -28,6 +28,7 @@

from ZODB.utils import p64 as int64_to_8bytes
from ZODB.utils import u64 as bytes8_to_int64
from ZODB.utils import maxtid

from relstorage.cache.interfaces import CacheConsistencyError
from .util import storage_method
Expand Down Expand Up @@ -187,12 +188,32 @@ def loadSerial(self, oid, serial):
@storage_method
@Metric(method=True, rate=0.1)
def loadBefore(self, oid, tid):
"""Return the most recent revision of oid before tid committed."""
"""
Return the most recent revision of oid before tid committed.
"""
if tid is maxtid or tid == maxtid:
# This is probably from ZODB.utils.load_current(), which
# is really trying to just get the current state of the
# object. So return that, formatted in the way this method
# returns it: (state, tid of state, tid_after_state) where
# tid_after_state will naturally be None
return self.load(oid) + (None,)
oid_int = bytes8_to_int64(oid)

# TODO: This makes three separate queries, and also bypasses the cache.
# We should be able to fix at least the multiple queries.

if self.store_connection:
# Allow loading data from later transactions
# for conflict resolution.

# XXX: This doesn't seem to be used in conflict
# resolution. ZODB.ConflictResolution.tryToResolveConflict
# calls loadSerial(); About the only call in ZODB to
# loadBefore() is from BlobStorage.undo() (which
# RelStorage does not extend). Mixing and matching calls
# between connections using different isolation levels
# isn't great. Can we stop doing this?
cursor = self.store_connection.cursor
else:
cursor = self.load_connection.cursor
Expand Down

0 comments on commit c7812ba

Please sign in to comment.