Commit bac86db1 authored by Jim Fulton's avatar Jim Fulton

*** empty log message ***

parent c1a6ff90
##############################################################################
#
# Copyright
#
# Copyright 1996 Digital Creations, L.C., 910 Princess Anne
# Street, Suite 300, Fredericksburg, Virginia 22401 U.S.A. All
# rights reserved.
#
##############################################################################
__doc__='''Simple column indexes
$Id: Index.py,v 1.1 1997/09/08 18:52:04 jim Exp $'''
__version__='$Revision: 1.1 $'[11:-2]
from oiTree import BTree
from intSet import intSet
import operator
ListType=type([])
class Index:
"""Index object interface"""
def _init(self,data,schema,id):
"""Create an index
The arguments are:
'data' -- a mapping from integer object ids to objects or records,
'schema' -- a mapping from item name to index into data records.
If 'data' is a mapping to objects, then schema should ne 'None'.
'id' -- the name of the item attribute to index. This is either
an attribute name or a record key.
"""
self._data=data
self._schema=schema
self.id=id
self._index=BTree()
self._reindex()
def clear(self):
self._init()
def _reindex(self,start=0):
"""Recompute index data for data with ids >= start."""
index=self._index
if not start: index.clear()
id=self.id
if self._schema is None:
f=getattr
else:
f=operator.__getitem__
id=self._schema[id]
for i,row in self._data.items(start):
k=f(row,id)
try: set=index[k]
except KeyError:
set=intSet()
index[k]=set
set.insert(i)
def index_item(self,i):
"""Recompute index data for data with ids >= start."""
index=self._index
id=self.id
if self._schema is None:
f=getattr
else:
f=operator.__getitem__
id=self._schema[id]
row=self._data[i]
k=f(row,id)
try: set=index[k]
except KeyError:
set=intSet()
index[k]=set
set.insert(i)
def unindex_item(self,i):
"""Recompute index data for data with ids >= start."""
index=self._index
id=self.id
if self._schema is None:
f=getattr
else:
f=operator.__getitem__
id=self._schema[id]
row=self._data[i]
k=f(row,id)
try:
set=index[k]
set.remove(k)
except KeyError: pass
def _apply_index(self, request):
"""Apply the index to query parameters given in the argument, request
The argument should be a mapping object.
If the request does not contain the needed parameters, then None is
returned.
Otherwise two objects are returned. The first object is a
ResultSet containing the record numbers of the matching
records. The second object is a tuple containing the names of
all data fields used.
"""
id=self.id
try: keys=request[id]
except: return None
if type(keys) is not ListType: keys=[keys]
index=self._index
r=None
for key in keys:
try:
set=index[key]
if r is None: r=set
else: r = r.union(set)
except KeyError: pass
if r is not None: return r, (id,)
##############################################################################
#
# $Log: Index.py,v $
# Revision 1.1 1997/09/08 18:52:04 jim
# *** empty log message ***
#
#
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