[Zope-Checkins] CVS: Zope/lib/python/Products/PluginIndexes/TextIndexNG - WordIdDocumentIdStorage.py:1.1.2.1

Andreas Jung andreas@digicool.com
Mon, 11 Mar 2002 18:23:55 -0500


Update of /cvs-repository/Zope/lib/python/Products/PluginIndexes/TextIndexNG
In directory cvs.zope.org:/tmp/cvs-serv6468

Added Files:
      Tag: ajung-textindexng-branch
	WordIdDocumentIdStorage.py 
Log Message:
added


=== Added File Zope/lib/python/Products/PluginIndexes/TextIndexNG/WordIdDocumentIdStorage.py ===
##############################################################################
#
# Copyright (c) 2001 Zope Corporation and Contributors. All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (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
#
##############################################################################

__doc__ = """storage for wordIds/documentIds with forward and backup indexes """
__version__= '$Id: WordIdDocumentIdStorage.py,v 1.1.2.1 2002/03/11 23:23:55 andreasjung Exp $'

from Persistence import Persistent
from BTrees.IIBTree import IISet
from BTrees.IOBTree import IOBTree
from types import IntType


class WordIdDocumentIdStorage(Persistent):
    """ storage to keep the mapping wordId to sequence
        of document ids and vis-versa.
    """

    def __init__(self):
        self.clear()


    def clear(self):
        self.forwardIdx = IOBTree()
        self.reverseIdx = IOBTree()


    def insert(self, wordIds, docId):
        """ insert entries: 
            wordId is either an integer or a sequence of integers.
            docId is an integer.
        """

        if isinstance(wordIds,IntType): wordIds = [wordIds]

        # forward index 
        idx = self.forwardIdx

        for wordId in wordIds:

            try:
                idx[wordId].insert(docId)
            except:
                idx[wordId] = IISet()
                idx[wordId].insert(doctId)


        # reverse index
        idx = self.reverseIdx
        
        try:
            idx[docId].update(wordIds)
        except:
            idx[docId] = IISet(wordIds)


    def removeDocument(self, docId):
        """ remove a document and all its words from the storage """

        wordIds = self.reverseIdx[ docId ]

        for wid in wordIds:
            self.forwardIdx[wid].remove( docId )


    def getDocumentIdsForWordId(self, wordId):
        """ return the sequence of document ids for a word id """

        return self.forwardIdx[ wordId ]