Commit 51f2d1c0 authored by Chris McDonough's avatar Chris McDonough

Merge 'dont-update-metadata' fix from 2.6 branch. When the caller specifies...

Merge 'dont-update-metadata' fix from 2.6 branch.  When the caller specifies that only a particular set of indexes should be updated, don't update the object metadata unconditionally.
parent 802b2476
...@@ -287,35 +287,14 @@ class Catalog(Persistent, Acquisition.Implicit, ExtensionClass.Base): ...@@ -287,35 +287,14 @@ class Catalog(Persistent, Acquisition.Implicit, ExtensionClass.Base):
""" get an index wrapped in the catalog """ """ get an index wrapped in the catalog """
return self.indexes[name].__of__(self) return self.indexes[name].__of__(self)
# the cataloging API def updateMetadata(self, object, uid):
""" Given an object and a uid, update the column data for the
def catalogObject(self, object, uid, threshold=None,idxs=[]): uid with the object data iff the object has changed """
"""
Adds an object to the Catalog by iteratively applying it
all indexes.
'object' is the object to be cataloged
'uid' is the unique Catalog identifier for this object
"""
data = self.data data = self.data
index = self.uids.get(uid, None)
# meta_data is stored as a tuple for efficiency
newDataRecord = self.recordify(object) newDataRecord = self.recordify(object)
index=self.uids.get(uid, None) if index is None:
if index is not None:
# old data
if data.get(index, 0) != newDataRecord:
# Update the meta-data, if necessary
data[index] = newDataRecord
else:
# new data
if type(data) is IOBTree: if type(data) is IOBTree:
# New style, get random id # New style, get random id
...@@ -340,7 +319,34 @@ class Catalog(Persistent, Acquisition.Implicit, ExtensionClass.Base): ...@@ -340,7 +319,34 @@ class Catalog(Persistent, Acquisition.Implicit, ExtensionClass.Base):
index = data.keys()[-1] + 1 index = data.keys()[-1] + 1
else: else:
index=0 index=0
# meta_data is stored as a tuple for efficiency
data[index] = newDataRecord
else:
if data.get(index, 0) != newDataRecord:
data[index] = newDataRecord data[index] = newDataRecord
return index
# the cataloging API
def catalogObject(self, object, uid, threshold=None,idxs=None):
"""
Adds an object to the Catalog by iteratively applying it
all indexes.
'object' is the object to be cataloged
'uid' is the unique Catalog identifier for this object
"""
if idxs is None:
idxs = []
data = self.data
index = self.uids.get(uid, None)
if index is None: # we are inserting new data
index = self.updateMetadata(object, uid)
try: self.__len__.change(1) try: self.__len__.change(1)
except AttributeError: pass # No managed length (old-style) except AttributeError: pass # No managed length (old-style)
...@@ -348,6 +354,14 @@ class Catalog(Persistent, Acquisition.Implicit, ExtensionClass.Base): ...@@ -348,6 +354,14 @@ class Catalog(Persistent, Acquisition.Implicit, ExtensionClass.Base):
self.uids[uid] = index self.uids[uid] = index
self.paths[index] = uid self.paths[index] = uid
else: # we are updating old data
if not idxs:
# if the caller specifies that we should update only a
# specific set of indexes, we don't do a metadata update.
self.updateMetadata(object, uid)
# do indexing
total = 0 total = 0
if idxs==[]: use_indexes = self.indexes.keys() if idxs==[]: use_indexes = self.indexes.keys()
......
...@@ -174,6 +174,23 @@ class TestZCatalog(unittest.TestCase): ...@@ -174,6 +174,23 @@ class TestZCatalog(unittest.TestCase):
sr = self._catalog.search(query) sr = self._catalog.search(query)
self.assertEqual(len(sr), 3) self.assertEqual(len(sr), 3)
class dummy(ExtensionClass.Base):
att1 = 'att1'
att2 = 'att2'
att3 = ['att3']
def __init__(self, num):
self.num = num
def col1(self):
return 'col1'
def col2(self):
return 'col2'
def col3(self):
return ['col3']
class TestCatalogObject(unittest.TestCase): class TestCatalogObject(unittest.TestCase):
upper = 1000 upper = 1000
...@@ -215,23 +232,6 @@ class TestCatalogObject(unittest.TestCase): ...@@ -215,23 +232,6 @@ class TestCatalogObject(unittest.TestCase):
self._catalog.addColumn('att3') self._catalog.addColumn('att3')
self._catalog.addColumn('num') self._catalog.addColumn('num')
class dummy(ExtensionClass.Base):
att1 = 'att1'
att2 = 'att2'
att3 = ['att3']
def __init__(self, num):
self.num = num
def col1(self):
return 'col1'
def col2(self):
return 'col2'
def col3(self):
return ['col3']
for x in range(0, self.upper): for x in range(0, self.upper):
self._catalog.catalogObject(dummy(self.nums[x]), `x`) self._catalog.catalogObject(dummy(self.nums[x]), `x`)
self._catalog.aq_parent = dummy('foo') # fake out acquisition self._catalog.aq_parent = dummy('foo') # fake out acquisition
...@@ -404,6 +404,19 @@ class TestCatalogObject(unittest.TestCase): ...@@ -404,6 +404,19 @@ class TestCatalogObject(unittest.TestCase):
self.assertEqual(a.actual_result_count, self.upper) self.assertEqual(a.actual_result_count, self.upper)
self.assertEqual(a[0].num, self.upper - 1) self.assertEqual(a[0].num, self.upper - 1)
def testUpdateIndexLeavesMetadataAlone(self):
ob = dummy(9999)
self._catalog.catalogObject(ob, `9999`)
brain = self._catalog(num=9999)[0]
self.assertEqual(brain.att1, 'att1')
ob.att1 = 'foobar'
self._catalog.catalogObject(ob, `9999`, idxs=['num'])
brain = self._catalog(num=9999)[0]
self.assertEqual(brain.att1, 'att1')
self._catalog.catalogObject(ob, `9999`)
brain = self._catalog(num=9999)[0]
self.assertEqual(brain.att1, 'foobar')
class objRS(ExtensionClass.Base): class objRS(ExtensionClass.Base):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment