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): ...@@ -81,8 +81,9 @@ class Catalog(Persistent, Acquisition.Implicit, ExtensionClass.Base):
try: self.__len__.set(0) try: self.__len__.set(0)
except AttributeError: self.__len__=BTrees.Length.Length() except AttributeError: self.__len__=BTrees.Length.Length()
for x in self.indexes.values(): for index in self.indexes.values():
x.clear() if hasattr(index, '__of__'): index=index.__of__(self)
index.clear()
def _convertBTrees(self, threshold=200): def _convertBTrees(self, threshold=200):
...@@ -252,9 +253,6 @@ class Catalog(Persistent, Acquisition.Implicit, ExtensionClass.Base): ...@@ -252,9 +253,6 @@ class Catalog(Persistent, Acquisition.Implicit, ExtensionClass.Base):
if not name: if not name:
raise 'Invalid Index Name', 'Name of index is empty' 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 indexes = self.indexes
if isinstance(index_type, types.StringType): if isinstance(index_type, types.StringType):
...@@ -274,8 +272,11 @@ class Catalog(Persistent, Acquisition.Implicit, ExtensionClass.Base): ...@@ -274,8 +272,11 @@ class Catalog(Persistent, Acquisition.Implicit, ExtensionClass.Base):
indexes = self.indexes indexes = self.indexes
del indexes[name] del indexes[name]
self.indexes = indexes self.indexes = indexes
def getIndex(self, name):
""" get an index wrapped in the catalog """
return self.indexes[name].__of__(self)
# the cataloging API # the cataloging API
def catalogObject(self, object, uid, threshold=None,idxs=[]): def catalogObject(self, object, uid, threshold=None,idxs=[]):
...@@ -341,13 +342,8 @@ class Catalog(Persistent, Acquisition.Implicit, ExtensionClass.Base): ...@@ -341,13 +342,8 @@ class Catalog(Persistent, Acquisition.Implicit, ExtensionClass.Base):
if idxs==[]: use_indexes = self.indexes.keys() if idxs==[]: use_indexes = self.indexes.keys()
else: use_indexes = idxs else: use_indexes = idxs
for item in use_indexes: for name in use_indexes:
x = self.indexes[item] x = self.getIndex(name)
## 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)
if hasattr(x, 'index_object'): if hasattr(x, 'index_object'):
blah = x.index_object(index, object, threshold) blah = x.index_object(index, object, threshold)
total = total + blah total = total + blah
...@@ -372,12 +368,12 @@ class Catalog(Persistent, Acquisition.Implicit, ExtensionClass.Base): ...@@ -372,12 +368,12 @@ class Catalog(Persistent, Acquisition.Implicit, ExtensionClass.Base):
data = self.data data = self.data
uids = self.uids uids = self.uids
paths = self.paths paths = self.paths
indexes = self.indexes indexes = self.indexes.keys()
rid = uids.get(uid, None) rid = uids.get(uid, None)
if rid is not None: if rid is not None:
for x in indexes.values(): for name in indexes:
x = x.__of__(self) x = self.getIndex(name)
if hasattr(x, 'unindex_object'): if hasattr(x, 'unindex_object'):
x.unindex_object(rid) x.unindex_object(rid)
del data[rid] del data[rid]
...@@ -393,7 +389,7 @@ class Catalog(Persistent, Acquisition.Implicit, ExtensionClass.Base): ...@@ -393,7 +389,7 @@ class Catalog(Persistent, Acquisition.Implicit, ExtensionClass.Base):
def uniqueValuesFor(self, name): def uniqueValuesFor(self, name):
""" return unique values for FieldIndex name """ """ return unique values for FieldIndex name """
return self.indexes[name].uniqueValues() return self.getIndex(name).uniqueValues()
def hasuid(self, uid): def hasuid(self, uid):
""" return the rid if catalog contains an object with uid """ """ return the rid if catalog contains an object with uid """
...@@ -424,8 +420,8 @@ class Catalog(Persistent, Acquisition.Implicit, ExtensionClass.Base): ...@@ -424,8 +420,8 @@ class Catalog(Persistent, Acquisition.Implicit, ExtensionClass.Base):
def getIndexDataForRID(self, rid): def getIndexDataForRID(self, rid):
result = {} result = {}
for (id, index) in self.indexes.items(): for name in self.indexes.keys():
result[id] = index.__of__(self).getEntryForObject(rid, "") result[name] = self.getIndex(name).getEntryForObject(rid, "")
return result return result
## This is the Catalog search engine. Most of the heavy lifting happens below ## This is the Catalog search engine. Most of the heavy lifting happens below
...@@ -465,7 +461,7 @@ class Catalog(Persistent, Acquisition.Implicit, ExtensionClass.Base): ...@@ -465,7 +461,7 @@ class Catalog(Persistent, Acquisition.Implicit, ExtensionClass.Base):
if used is None: if used is None:
used = {} used = {}
for i in self.indexes.keys(): for i in self.indexes.keys():
index = self.indexes[i].__of__(self) index = self.getIndex(i)
_apply_index = getattr(index, "_apply_index", None) _apply_index = getattr(index, "_apply_index", None)
if _apply_index is None: if _apply_index is None:
continue continue
...@@ -633,6 +629,9 @@ class Catalog(Persistent, Acquisition.Implicit, ExtensionClass.Base): ...@@ -633,6 +629,9 @@ class Catalog(Persistent, Acquisition.Implicit, ExtensionClass.Base):
def searchResults(self, REQUEST=None, used=None, _merge=1, **kw): 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) args = CatalogSearchArgumentsMap(REQUEST, kw)
sort_index = self._getSortIndex(args) sort_index = self._getSortIndex(args)
# Perform searches with indexes and sort_index # Perform searches with indexes and sort_index
......
...@@ -12,7 +12,7 @@ ...@@ -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 from Interface import Interface
...@@ -108,6 +108,13 @@ class IZCatalog(Interface): ...@@ -108,6 +108,13 @@ class IZCatalog(Interface):
def index_objects(): def index_objects():
"""Returns a sequence of actual 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): def searchResults(REQUEST=None, **kw):
...@@ -154,8 +161,7 @@ class IZCatalog(Interface): ...@@ -154,8 +161,7 @@ class IZCatalog(Interface):
There are some rules to consider when querying this method: There are some rules to consider when querying this method:
- an empty query mapping (or a bogus REQUEST) returns all - an empty query mapping (or a bogus REQUEST) returns all
items in the items in the catalog.
catalog.
- results from a query involving only field/keyword - results from a query involving only field/keyword
indexes, e.g. {'id':'foo'} and no 'sort_on' will be indexes, e.g. {'id':'foo'} and no 'sort_on' will be
......
...@@ -133,7 +133,7 @@ class ZCatalog(Folder, Persistent, Implicit): ...@@ -133,7 +133,7 @@ class ZCatalog(Folder, Persistent, Implicit):
('Search ZCatalog', ('Search ZCatalog',
['searchResults', '__call__', 'uniqueValuesFor', ['searchResults', '__call__', 'uniqueValuesFor',
'getpath', 'schema', 'indexes', 'index_objects', 'getpath', 'schema', 'indexes', 'index_objects', 'getIndexObjects'
'all_meta_types', 'valid_roles', 'resolve_url', 'all_meta_types', 'valid_roles', 'resolve_url',
'getobject'], 'getobject'],
['Anonymous', 'Manager']), ['Anonymous', 'Manager']),
...@@ -561,7 +561,14 @@ class ZCatalog(Folder, Persistent, Implicit): ...@@ -561,7 +561,14 @@ class ZCatalog(Folder, Persistent, Implicit):
return self._catalog.indexes.keys() return self._catalog.indexes.keys()
def index_objects(self): def index_objects(self):
# This method returns unwrapped indexes!
# You should probably use getIndexObjects instead
return self._catalog.indexes.values() 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): def _searchable_arguments(self):
r = {} r = {}
......
...@@ -121,18 +121,21 @@ class ZCatalog: ...@@ -121,18 +121,21 @@ class ZCatalog:
""" """
def indexes():
"""
Returns a sequence of names that correspond to indexes.
"""
def index_objects(): def index_objects():
""" """
Returns a sequence of actual 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): def searchResults(REQUEST=None, **kw):
......
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