Skip to content

Commit

Permalink
Introduce IntId index that is more optimized to index values by their…
Browse files Browse the repository at this point in the history
… int ids.
  • Loading branch information
thefunny42 committed Aug 10, 2017
1 parent 98c6cfc commit 4b6bb0c
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 4 deletions.
4 changes: 1 addition & 3 deletions CHANGES.txt
Expand Up @@ -4,15 +4,13 @@ CHANGES
2.3 (unreleased)
================

- Nothing changed yet.

- Introduce IntId index that is more optimized to index values by their int ids.

2.2.1 (2016-01-29)
==================

- Update tests.


2.2 (2015-11-20)
================

Expand Down
2 changes: 1 addition & 1 deletion src/grokcore/catalog/__init__.py
Expand Up @@ -3,4 +3,4 @@
from grokcore.catalog.interfaces import IAttributeIndexDefinition
from grokcore.catalog.components import IndexesClass, Indexes
from grokcore.catalog.index import IndexDefinition, AttributeIndexDefinition
from grokcore.catalog.index import Field, Text, Set, Value, Datetime
from grokcore.catalog.index import Field, Text, Set, Value, Datetime, IntId
42 changes: 42 additions & 0 deletions src/grokcore/catalog/index.py
Expand Up @@ -16,6 +16,7 @@
import sys
import calendar
import BTrees.Length
import zope.component
import zope.container.contained
import zope.catalog.interfaces
import zope.catalog.attribute
Expand All @@ -26,6 +27,7 @@
from zope.catalog.interfaces import IAttributeIndex
from zope.catalog.field import FieldIndex
from zope.catalog.text import TextIndex
from zope.intid.interfaces import IIntIds
from zc.catalog.catalogindex import SetIndex, ValueIndex
from martian.error import GrokError, GrokImportError
from martian.util import frame_is_class
Expand Down Expand Up @@ -62,6 +64,7 @@ def setup(self, catalog, name, context, module_info):
# the index with the given attributes.
catalog[name] = self.index_class(*self._args, **self._kw)


class AttributeIndexDefinition(object):
"""The definition of a particular index in a
:data:`grokcore.catalog.Indexes` class.
Expand Down Expand Up @@ -213,9 +216,48 @@ class DatetimeIndex(
pass


class _IntIdIndex(zope.index.field.index.FieldIndex):

def clear(self):
self._fwd_index = BTrees.IOBTree.IOBTree()
self._rev_index = BTrees.IIBTree.IIBTree()
self._num_docs = BTrees.Length.Length(0)

def _get_value_id(self, value):
if value is None:
return None
if isinstance(value, int):
return value
intids = zope.component.getUtility(IIntIds)
return intids.getId(value)

def index_doc(self, docid, value):
return super(_IntIdIndex, self).index_doc(
docid, self._get_value_id(value))

def apply(self, query):
value = self._get_value_id(query)
return super(_IntIdIndex, self).apply((value, value))


class IntIdIndex(
zope.catalog.attribute.AttributeIndex,
_IntIdIndex,
zope.container.contained.Contained):
pass


class Datetime(AttributeIndexDefinition):
"""A :class:`grokcore.catalog.Indexes` index specifically meant for
datetime objects.
"""
index_class = DatetimeIndex


class IntId(AttributeIndexDefinition):
"""A :class:`grokcore.catalog.Indexes` index specifically meant to index
values with their intid.
"""
index_class = IntIdIndex

0 comments on commit 4b6bb0c

Please sign in to comment.