m )Ec@s dZdkZdklZdklZdklZlZl Z l Z dk l Z dk ZdklZdklZdklZd klZd klZd klZd klZd klZdklZdkl Z gZ!edZ"defdYZ#dS(sVBase for bi-directional indexes. $Id: UnIndex.py 68095 2006-05-11 10:07:45Z chrisw $ N(sescape(s getLogger(s IITreeSetsIISetsunions intersection(sIOBTree(sOOBTree(s SimpleItem(s ConflictError(s implements(sPluggableIndex(s safe_callable(sparseIndexRequest(sIPluggableIndex(s ISortIndex(sIUniqueValueIndexs Zope.UnIndextUnIndexcBs#tZdZeieifZeee e e e e e dZ dZ dZdZdZdZdZedZd Zd Ze d Ze d d ZdZdZdZdZd edZdZdZ e ddZ!dZ"dZ#dZ$RS(s&Simple forward and reverse index. c Csd}||_||_||_d|_d|_||d|} t | t o| i d|_ nt | |_ g}|i D]}|o||iqq~|_ |i p|g|_ ntii|_|idS(sCreate an unindex UnIndexes are indexes that contain two index components, the forward index (like plain index objects) and an inverted index. The inverted index is so that objects can be unindexed even when the old value of the object is not known. e.g. self._index = {datum:[documentId1, documentId2]} self._unindex = {documentId:datum} If any item in self._index has a length-one value, the value is an integer, and not a set. There are special cases in the code to deal with this. The arguments are: 'id' -- the name of the item attribute to index. This is either an attribute name or a record key. 'ignore_ex' -- should be set to true if you want the index to ignore exceptions raised while indexing instead of propagating them. 'call_methods' -- should be set to true if you want the index to call the attribute 'id' (note: 'id' should be callable!) You will also need to pass in an object in the index and uninded methods for this to work. 'extra' -- a mapping object that keeps additional index-related parameters - subitem 'indexed_attrs' can be string with comma separated attribute names or a list 'caller' -- reference to the calling object (usually a (Z)Catalog instance cCs8t|to|i||Snt|||SdS(s5 return a value for a given key of a dict/record 'o' N(t isinstancetotdicttgettktdefaulttgetattr(RRR((tD/data/zmath/zope/lib/python/Products/PluginIndexes/common/UnIndex.pyt_get[stortandt indexed_attrst,N(sorR (R tidtselft ignore_ext call_methodst operatorst useOperatortextratiaRtstrtsplitR tlistt_[1]tattrtstriptBTreestLengtht_lengthtclear( RRRRRtcallerRRR R((Rt__init__2s '      8 cCs |iS(N(RR(R((Rt__len__wscCs|iS(N(RR(R((RtgetIdzscCs.tii|_t|_t|_dS(N(RRRRtOOBTreet_indextIOBTreet_unindex(R((RR}s cCs |i S(N(RR'(R((Rt __nonzero__scCsph}xc|iiD]R}t|to d}n|\}}t |}|i |dd||RF(RR4RKRLRRORQRR((RRJs&     cCsMy-t||}t|o |}nWntj o t}nX|S(N(RRKRRQt safe_callableR;R3(RRKRRQ((RRP s  cCs t|iS(s" return number of indexed objects N(R0RR'(R((Rt numObjectsscCs t|S(s" return of distinct values indexedN(R0R(R((Rt indexSizescCs|ii|t}|tjodSn|i||y|i|=Wn3tj o nt i d|dt nXdS(si Unindex the object with integer id 'documentId' and don't raise an exception if we fail s2Attempt to unindex nonexistent document with id %sR6N( RR'RR4R3t unindexRecordtNoneRCR:R=tdebugtTrue(RR4RV((Rtunindex_object!s  cCst||i|i}|id jod Sn|i}d }d } |i d|i }||ijotdt|n|djo t} nt} |i dd } | o^d} g}| iddjo|idn| iddjo|idqn|i dd o1|iiid } | d | d } }n| djod|jot|i}nd }d|jot|i} nd } | o|i || }n|i |}x|D]>\}}t$|t%ot&|f}n| ||}qWnvxr|iD]g} |i | d }|d jot&f}n$t$|t%ot&|f}n| ||}q5Wt$|t%ot&|f}n|d jot&|iffSn||iffSd S( sEApply the index to query parameters given in the request arg. The request argument should be a mapping object. If the request does not have a key which matches the "id" of the index instance, then None is returned. If the request *does* have a key which matches the "id" of the index instance, one of a few things can happen: - if the value is a blank string, None is returned (in order to support requests from web forms where you can't tell a blank string from empty). - if the value is a nonblank string, turn the value into a single-element sequence, and proceed. - if the value is a sequence, return a union search. If the request contains a parameter with the name of the column + '_usage', it is sniffed for information on how to handle applying the index. If the request contains a parameter with the name of the column = '_operator' this overrides the default method ('or') to combine search results. Valid values are "or" and "and". If None is not returned as a result of the abovementioned constraints, 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. FAQ answer: to search a Field Index for documents that have a blank string as their value, wrap the request value up in a tuple ala: request = {'id':('',)} toperatorsoperator not valid: %sR trangetminitmaxtusaget:iiN((tparseIndexRequesttrequestRRt query_optionstrecordR1RWR%tindextrtoprRRR[Rt RuntimeErrortescapetuniontset_funct intersectiont range_parmtopr_argstfindtappendR_tlowerRR]tloR^thiR*tsetlistRtsetRR,tIISetR.(RRbtcidttypeRuRnR[ReRrRmRgRkRsR.RRtRdRf((Rt _apply_index3sh&         cCs ||ijodSndSdS(s!has unique values for column nameiiN(tnameRR(RRz((RthasUniqueValuesForscCst|d|igS(s' return sequence of indexed attributes R N(RRR(R((RRGsicCs|djo |i}n||ijogSn|pt|iiSnqg}x]|iiD]L}|i|}t |t o d}n t|}|i||fqfWt|SdS(sreturns the unique values for name if withLengths is true, returns a sequence of tuples of (value, length) iN(RzRWRRt withLengthsttupleR%R1trltiRuRR,tlR0Rp(RRzR|RuRRR~((Rt uniqueValuess      cCs |i|S(N(RR'R(RR((RtkeyForDocumentscCs|iS(N(RR'(R((RtdocumentToKeyMapscCs`g}xS|iiD]B\}}t|tot|f}n|i||fqW|S(N( R*RR%RtvRR,RvRp(RR*RR((RR*s (%R@t __module__t__doc__tPluggableIndextUniqueValueIndext SortIndext__implements__t implementstIPluggableIndextIUniqueValueIndext ISortIndexRWR!R"R#RR(R)R2R3R5RCRFRMRJRPRTRURZRxRyR{RGRRRR*(((RR)s4 E       (     c    ($RRAtcgiRitloggingt getLoggertBTrees.IIBTreeRERvRjRltBTrees.IOBTreeR&t BTrees.LengthRtBTrees.OOBTreeR$tOFS.SimpleItemt SimpleItemtZODB.POSExceptionR:tzope.interfaceRtProducts.PluginIndexesRtProducts.PluginIndexes.commonRSt"Products.PluginIndexes.common.utilRat!Products.PluginIndexes.interfacesRRRR3R=R(R=RR:RiR$RjRvR3RR&RRARRlRRRSRaRRRRE((Rt?s&