Skip to content

Commit

Permalink
Collector #1332: added in-place migration for
Browse files Browse the repository at this point in the history
Catalog.__len__ -> Catalog._length
  • Loading branch information
zopyx committed Oct 12, 2004
1 parent 19d6f9f commit 8b78529
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 46 deletions.
60 changes: 14 additions & 46 deletions Catalog.py
Expand Up @@ -78,66 +78,36 @@ def __init__(self, vocabulary=None, brains=None):
# object unique identifier to the rid, and self.paths is a
# mapping of the rid to the unique identifier.

# Note that it was unfortunate to use __len__ as the attribute
# name here. New-style classes cache slot methods in C slot
# pointers. The result is that instances can't override slots.
# This is not easy to change on account of old objects with
# __len__ attr.

self.__len__ = BTrees.Length.Length()
self._length = BTrees.Length.Length()
self.clear()

if brains is not None:
self._v_brains = brains

self.updateBrains()


def __len__(self):
try:
return self.__dict__['__len__']()
except KeyError:
# Fallback for *really* old catalogs that don't have
# Length objects.
return len(self.data)
return self._length()

def migrate__len__(self):
""" migration of old __len__ magic for Zope 2.8 """
if not hasattr(self, '_length'):
n = self.__dict__['__len__']()
del self.__dict__['__len__']
self._length = BTrees.Length.Length(n)

def clear(self):
""" clear catalog """

self.data = IOBTree() # mapping of rid to meta_data
self.uids = OIBTree() # mapping of uid to rid
self.paths = IOBTree() # mapping of rid to uid

# convert old-style Catalog object to new in-place
try: self.__len__.set(0)
except AttributeError: self.__len__=BTrees.Length.Length()
self._length.set(0)

for index in self.indexes.keys():
self.getIndex(index).clear()

def _convertBTrees(self, threshold=200):

from BTrees.convert import convert

if type(self.data) is not IOBTree:
data=self.data
self.data=IOBTree()
convert(data, self.data, threshold)

self.__len__=BTrees.Length.Length(len(data))

uids=self.uids
self.uids=OIBTree()
convert(uids, self.uids, threshold)

paths=self.paths
self.paths=IOBTree()
convert(paths, self.paths, threshold)


for index in self.indexes.values():
if hasattr(index, '__of__'): index=index.__of__(self)
index._convertBTrees(threshold)

def updateBrains(self):
self.useBrains(self._v_brains)

Expand Down Expand Up @@ -369,9 +339,7 @@ def catalogObject(self, object, uid, threshold=None, idxs=None,
if index is None: # we are inserting new data
index = self.updateMetadata(object, uid)

try: self.__len__.change(1)
except AttributeError: pass # No managed length (old-style)

self._length.change(1)
self.uids[uid] = index
self.paths[index] = uid

Expand Down Expand Up @@ -421,8 +389,8 @@ def uncatalogObject(self, uid):
del data[rid]
del paths[rid]
del uids[uid]
try: self.__len__.change(-1)
except AttributeError: pass # No managed length
self._length.change(-1)

else:
LOG.error('uncatalogObject unsuccessfully '
'attempted to uncatalog an object '
Expand Down
3 changes: 3 additions & 0 deletions ZCatalog.py
Expand Up @@ -183,6 +183,9 @@ def __init__(self, id, title='', vocab_id=None, container=None):
self._catalog = Catalog()

def __len__(self):
# Perform a migration of _catalog.__len__ to _catalog._length
# to avoid with new-style class caching issues (see #1332)
self._catalog.migrate__len__()
return len(self._catalog)


Expand Down

0 comments on commit 8b78529

Please sign in to comment.