Commit e28101d5 authored by Casey Duncan's avatar Casey Duncan

Added new APIS:

  - Catalog.getIndex returns an acquisition wrapped index, use instead of
    Catalog.indexes[...]
  - ZCatalog.getIndexObjects returns the list of index obs also acquisition
    wrapped. Its use is preferred over the previous index_objects method.

Changed Catalog code to utilize getIndex. This is mostly a neutral change except for in clear() which did not used to wrap before calling the index.
Help docs and interfaces updated to reflect the change.
parent 002207ab
......@@ -81,8 +81,9 @@ class Catalog(Persistent, Acquisition.Implicit, ExtensionClass.Base):
try: self.__len__.set(0)
except AttributeError: self.__len__=BTrees.Length.Length()
for x in self.indexes.values():
x.clear()
for index in self.indexes.values():
if hasattr(index, '__of__'): index=index.__of__(self)
index.clear()
def _convertBTrees(self, threshold=200):
......@@ -252,9 +253,6 @@ class Catalog(Persistent, Acquisition.Implicit, ExtensionClass.Base):
if not name:
raise 'Invalid Index Name', 'Name of index is empty'
# this is currently a succesion of hacks. Indexes should be
# pluggable and managable
indexes = self.indexes
if isinstance(index_type, types.StringType):
......@@ -275,6 +273,9 @@ class Catalog(Persistent, Acquisition.Implicit, ExtensionClass.Base):
del indexes[name]
self.indexes = indexes
def getIndex(self, name):
""" get an index wrapped in the catalog """
return self.indexes[name].__of__(self)
# the cataloging API
......@@ -341,13 +342,8 @@ class Catalog(Persistent, Acquisition.Implicit, ExtensionClass.Base):
if idxs==[]: use_indexes = self.indexes.keys()
else: use_indexes = idxs
for item in use_indexes:
x = self.indexes[item]
## tricky! indexes need to acquire now, and because they
## are in a standard dict __getattr__ isn't used, so
## acquisition doesn't kick in, we must explicitly wrap!
x = x.__of__(self)
for name in use_indexes:
x = self.getIndex(name)
if hasattr(x, 'index_object'):
blah = x.index_object(index, object, threshold)
total = total + blah
......@@ -372,12 +368,12 @@ class Catalog(Persistent, Acquisition.Implicit, ExtensionClass.Base):
data = self.data
uids = self.uids
paths = self.paths
indexes = self.indexes
indexes = self.indexes.keys()
rid = uids.get(uid, None)
if rid is not None:
for x in indexes.values():
x = x.__of__(self)
for name in indexes:
x = self.getIndex(name)
if hasattr(x, 'unindex_object'):
x.unindex_object(rid)
del data[rid]
......@@ -393,7 +389,7 @@ class Catalog(Persistent, Acquisition.Implicit, ExtensionClass.Base):
def uniqueValuesFor(self, name):
""" return unique values for FieldIndex name """
return self.indexes[name].uniqueValues()
return self.getIndex(name).uniqueValues()
def hasuid(self, uid):
""" return the rid if catalog contains an object with uid """
......@@ -424,8 +420,8 @@ class Catalog(Persistent, Acquisition.Implicit, ExtensionClass.Base):
def getIndexDataForRID(self, rid):
result = {}
for (id, index) in self.indexes.items():
result[id] = index.__of__(self).getEntryForObject(rid, "")
for name in self.indexes.keys():
result[name] = self.getIndex(name).getEntryForObject(rid, "")
return result
## This is the Catalog search engine. Most of the heavy lifting happens below
......@@ -465,7 +461,7 @@ class Catalog(Persistent, Acquisition.Implicit, ExtensionClass.Base):
if used is None:
used = {}
for i in self.indexes.keys():
index = self.indexes[i].__of__(self)
index = self.getIndex(i)
_apply_index = getattr(index, "_apply_index", None)
if _apply_index is None:
continue
......@@ -633,6 +629,9 @@ class Catalog(Persistent, Acquisition.Implicit, ExtensionClass.Base):
def searchResults(self, REQUEST=None, used=None, _merge=1, **kw):
if REQUEST is None and not kw:
# Try to acquire request if we get no args for bw compat
REQUEST = getattr(self, 'REQUEST', None)
args = CatalogSearchArgumentsMap(REQUEST, kw)
sort_index = self._getSortIndex(args)
# Perform searches with indexes and sort_index
......
......@@ -12,7 +12,7 @@
#
##############################################################################
"""
$Id: IZCatalog.py,v 1.1 2002/07/29 14:10:48 jim Exp $
$Id: IZCatalog.py,v 1.2 2002/08/14 19:10:14 caseman Exp $
"""
from Interface import Interface
......@@ -108,6 +108,13 @@ class IZCatalog(Interface):
def index_objects():
"""Returns a sequence of actual index objects.
NOTE: This returns unwrapped indexes! You should probably use
getIndexObjects instead. Some indexes expect to be wrapped.
"""
def getIndexObjects():
"""Returns a list of acquisition wrapped index objects
"""
def searchResults(REQUEST=None, **kw):
......@@ -154,8 +161,7 @@ class IZCatalog(Interface):
There are some rules to consider when querying this method:
- an empty query mapping (or a bogus REQUEST) returns all
items in the
catalog.
items in the catalog.
- results from a query involving only field/keyword
indexes, e.g. {'id':'foo'} and no 'sort_on' will be
......
......@@ -133,7 +133,7 @@ class ZCatalog(Folder, Persistent, Implicit):
('Search ZCatalog',
['searchResults', '__call__', 'uniqueValuesFor',
'getpath', 'schema', 'indexes', 'index_objects',
'getpath', 'schema', 'indexes', 'index_objects', 'getIndexObjects'
'all_meta_types', 'valid_roles', 'resolve_url',
'getobject'],
['Anonymous', 'Manager']),
......@@ -561,8 +561,15 @@ class ZCatalog(Folder, Persistent, Implicit):
return self._catalog.indexes.keys()
def index_objects(self):
# This method returns unwrapped indexes!
# You should probably use getIndexObjects instead
return self._catalog.indexes.values()
def getIndexObjects(self):
# Return a list of wrapped(!) indexes
catalog = self._catalog
return [index.__of__(catalog) for index in catalog.indexes.values()]
def _searchable_arguments(self):
r = {}
n={'optional':1}
......
......@@ -121,17 +121,20 @@ class ZCatalog:
"""
def indexes():
def index_objects():
"""
Returns a sequence of names that correspond to indexes.
Returns a sequence of actual index objects.
NOTE: This returns unwrapped indexes! You should probably use
getIndexObjects instead. Some indexes expect to be wrapped.
"""
def index_objects():
def getIndexObjects():
"""
Returns a sequence of actual index objects.
Returns a list of acquisition wrapped index objects
"""
......
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