Commit 421fc6e5 authored by Chris McDonough's avatar Chris McDonough

Revert feature of never updaing metadata if index is specified in...

Revert feature of never updaing metadata if index is specified in catalog_object.  This broke several applications.  Instead, we provide the catalog_object method (and the Catalog.py's catalogObject method) with an update_metadata keyword argument.  If the update_metadata keyword argument is set false (the default is true), metadata is not updated.
parent 51f2d1c0
......@@ -328,15 +328,23 @@ class Catalog(Persistent, Acquisition.Implicit, ExtensionClass.Base):
# the cataloging API
def catalogObject(self, object, uid, threshold=None,idxs=None):
def catalogObject(self, object, uid, threshold=None, idxs=None,
update_metadata=1):
"""
Adds an object to the Catalog by iteratively applying it
Adds an object to the Catalog by iteratively applying it to
all indexes.
'object' is the object to be cataloged
'uid' is the unique Catalog identifier for this object
If 'idxs' is specified (as a sequence), apply the object only
to the named indexes.
If 'update_metadata' is true (the default), also update metadata for
the object. If the object is new to the catalog, this flag has
no effect (metadata is always created for new objects).
"""
if idxs is None:
......@@ -354,10 +362,7 @@ class Catalog(Persistent, Acquisition.Implicit, ExtensionClass.Base):
self.uids[uid] = index
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.
elif update_metadata: # we are updating and we need to update metadata
self.updateMetadata(object, uid)
# do indexing
......
......@@ -12,7 +12,7 @@
#
##############################################################################
"""
$Id: IZCatalog.py,v 1.6 2003/06/09 20:02:48 sidnei Exp $
$Id: IZCatalog.py,v 1.7 2003/10/07 18:20:58 chrism Exp $
"""
from Interface import Interface
......@@ -66,13 +66,18 @@ class IZCatalog(Interface):
"""
def catalog_object(obj, uid, idxs=[]):
def catalog_object(obj, uid, idxs=None, update_metadata=1):
"""Catalogs the object 'obj' with the unique identifier 'uid'.
The uid must be a physical path, either absolute or relative to
the catalog.
If provided, idxs specifies the names of indexes to update.
If update_metadata is specified (the default), the object's metadata
is updated. If it is not, the metadata is left untouched. This
flag has no effect if the object is not yet cataloged (metadata
is always added for new objects).
"""
def uncatalog_object(uid):
......
......@@ -454,7 +454,9 @@ class ZCatalog(Folder, Persistent, Implicit):
if not obj:
obj = self.resolve_url(p, REQUEST)
if obj is not None:
self.catalog_object(obj, p, idxs=[name])
# don't update metadata when only reindexing a single
# index via the UI
self.catalog_object(obj, p, idxs=[name], update_metadata=0)
def manage_reindexIndex(self, ids=None, REQUEST=None, RESPONSE=None,
......@@ -483,7 +485,7 @@ class ZCatalog(Folder, Persistent, Implicit):
return Splitter.availableSplitters
def catalog_object(self, obj, uid=None, idxs=[]):
def catalog_object(self, obj, uid=None, idxs=None, update_metadata=1):
""" wrapper around catalog """
if uid is None:
......@@ -497,7 +499,8 @@ class ZCatalog(Folder, Persistent, Implicit):
elif not isinstance(uid,types.StringType):
raise CatalogError('The object unique id must be a string.')
self._catalog.catalogObject(obj, uid, None,idxs)
self._catalog.catalogObject(obj, uid, None, idxs,
update_metadata=update_metadata)
# None passed in to catalogObject as third argument indicates
# that we shouldn't try to commit subtransactions within any
# indexing code. We throw away the result of the call to
......
......@@ -137,25 +137,31 @@ class TestAddDelIndexes(CatalogBase, unittest.TestCase):
# Test of the ZCatalog object, as opposed to Catalog
class zdummy(ExtensionClass.Base):
def __init__(self, num):
self.num = num
def title(self):
return '%d' % self.num
class TestZCatalog(unittest.TestCase):
def setUp(self):
self._catalog = ZCatalog.ZCatalog('Catalog')
self._catalog.resolve_path = self._resolve_num
self._catalog.addIndex('title', 'KeywordIndex')
self._catalog.addColumn('title')
self.upper = 10
class dummy(ExtensionClass.Base):
def __init__(self, num):
self.num = num
def title(self):
return '%d' % self.num
self.d = {}
for x in range(0, self.upper):
# make uid a string of the number
self._catalog.catalog_object(dummy(x), str(x))
ob = zdummy(x)
self.d[str(x)] = ob
self._catalog.catalog_object(ob, str(x))
def _resolve_num(self, num):
return self.d[num]
def testGetMetadataForUID(self):
testNum = str(self.upper - 3) # as good as any..
......@@ -174,6 +180,26 @@ class TestZCatalog(unittest.TestCase):
sr = self._catalog.search(query)
self.assertEqual(len(sr), 3)
def testUpdateMetadata(self):
self._catalog.catalog_object(zdummy(1), '1')
data = self._catalog.getMetadataForUID('1')
self.assertEqual(data['title'], '1')
self._catalog.catalog_object(zdummy(2), '1', update_metadata=0)
data = self._catalog.getMetadataForUID('1')
self.assertEqual(data['title'], '1')
self._catalog.catalog_object(zdummy(2), '1', update_metadata=1)
data = self._catalog.getMetadataForUID('1')
self.assertEqual(data['title'], '2')
# update_metadata defaults to true, test that here
self._catalog.catalog_object(zdummy(1), '1')
data = self._catalog.getMetadataForUID('1')
self.assertEqual(data['title'], '1')
def testReindexIndexDoesntDoMetadata(self):
self.d['0'].num = 9999
self._catalog.reindexIndex('title', {})
data = self._catalog.getMetadataForUID('0')
self.assertEqual(data['title'], '0')
class dummy(ExtensionClass.Base):
att1 = 'att1'
......@@ -404,13 +430,13 @@ class TestCatalogObject(unittest.TestCase):
self.assertEqual(a.actual_result_count, self.upper)
self.assertEqual(a[0].num, self.upper - 1)
def testUpdateIndexLeavesMetadataAlone(self):
def testUpdateMetadataFalse(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'])
self._catalog.catalogObject(ob, `9999`, update_metadata=0)
brain = self._catalog(num=9999)[0]
self.assertEqual(brain.att1, 'att1')
self._catalog.catalogObject(ob, `9999`)
......
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