/usr/share/pyshared/zope/index/field/index.py is in python-zope.index 3.6.4-0ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 | ##############################################################################
#
# Copyright (c) 2002 Zope Foundation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE
#
##############################################################################
"""Field index
"""
import BTrees
import persistent
import zope.interface
from BTrees.Length import Length
from zope.index import interfaces
from zope.index.field.sorting import SortingIndexMixin
class FieldIndex(SortingIndexMixin, persistent.Persistent):
zope.interface.implements(
interfaces.IInjection,
interfaces.IStatistics,
interfaces.IIndexSearch,
)
family = BTrees.family32
def __init__(self, family=None):
if family is not None:
self.family = family
self.clear()
def clear(self):
"""Initialize forward and reverse mappings."""
# The forward index maps indexed values to a sequence of docids
self._fwd_index = self.family.OO.BTree()
# The reverse index maps a docid to its index value
self._rev_index = self.family.IO.BTree()
self._num_docs = Length(0)
def documentCount(self):
"""See interface IStatistics"""
return self._num_docs()
def wordCount(self):
"""See interface IStatistics"""
return len(self._fwd_index)
def index_doc(self, docid, value):
"""See interface IInjection"""
rev_index = self._rev_index
if docid in rev_index:
if docid in self._fwd_index.get(value, ()):
# no need to index the doc, its already up to date
return
# unindex doc if present
self.unindex_doc(docid)
# Insert into forward index.
set = self._fwd_index.get(value)
if set is None:
set = self.family.IF.TreeSet()
self._fwd_index[value] = set
set.insert(docid)
# increment doc count
self._num_docs.change(1)
# Insert into reverse index.
rev_index[docid] = value
def unindex_doc(self, docid):
"""See interface IInjection"""
rev_index = self._rev_index
value = rev_index.get(docid)
if value is None:
return # not in index
del rev_index[docid]
try:
set = self._fwd_index[value]
set.remove(docid)
except KeyError: #pragma NO COVERAGE
# This is fishy, but we don't want to raise an error.
# We should probably log something.
# but keep it from throwing a dirty exception
set = 1
if not set:
del self._fwd_index[value]
self._num_docs.change(-1)
def apply(self, query):
if len(query) != 2 or not isinstance(query, tuple):
raise TypeError("two-length tuple expected", query)
return self.family.IF.multiunion(
self._fwd_index.values(*query))
|