Skip to content

Commit

Permalink
Merge pull request #5 from zopefoundation/intid-index
Browse files Browse the repository at this point in the history
Introduce IntId index that is more optimized to index values by their…
  • Loading branch information
thefunny42 committed Aug 11, 2017
2 parents 98c6cfc + f7bddf9 commit 292e4fc
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 5 deletions.
1 change: 0 additions & 1 deletion .travis.yml
@@ -1,6 +1,5 @@
language: python
python:
- 2.6
- 2.7
install:
- python bootstrap.py
Expand Down
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
3 changes: 3 additions & 0 deletions buildout.cfg
Expand Up @@ -14,6 +14,9 @@ auto-checkout =
grokcore.catalog =
grokcore.site = 1.7

setuptools = 34.4.1
zc.buildout = 2.9.4

[interpreter]
recipe = zc.recipe.egg
eggs = grokcore.site
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 292e4fc

Please sign in to comment.