Example #1
0
def mass_weightedIntersection(L):
    "A list of (mapping, weight) pairs -> their weightedIntersection IFBucket."
    L = [(x, wx) for (x, wx) in L if x is not None]
    if len(L) < 2:
        return _trivial(L)
    # Intersect with smallest first.  We expect the input maps to be
    # IFBuckets, so it doesn't hurt to get their lengths repeatedly
    # (len(Bucket) is fast; len(BTree) is slow).
    L.sort(lambda x, y: cmp(len(x[0]), len(y[0])))
    (x, wx), (y, wy) = L[:2]
    dummy, result = weightedIntersection(x, y, wx, wy)
    for x, wx in L[2:]:
        dummy, result = weightedIntersection(result, x, 1, wx)
    return result
Example #2
0
    def apply(self, query):
        results = []
        for index_name, index_query in query.items():
            index = self.indexes.get(index_name)
            if index is None:
                continue
            r = index.apply(index_query)
            if r is None:
                continue
            if not r:
                # empty results
                return r
            results.append((len(r), r))

        if not results:
            # no applicable indexes, so catalog was not applicable
            return None

        results.sort()  # order from smallest to largest

        _, result = results.pop(0)
        for _, r in results:
            _, result = weightedIntersection(result, r)

        return result
Example #3
0
    def apply(self, cache, context=None):
        results = []
        for term in self.terms:
            result = term.cached_apply(cache, context)
            if not result:
                # Empty results
                return result
            results.append(result)

        if len(results) == 1:
            return results[0]

        # Sort results to have the smallest set first to optimize the
        # set operation.
        results.sort(key=lambda r: len(r))

        result = results.pop(0)
        for r in results:
            if self.weighted:
                _, result = weightedIntersection(result, r)
            else:
                result = intersection(result, r)
            if not result:
                # Empty results
                return result

        return result
Example #4
0
    def entries(self, index=None):
        if index is None:
            result = self.index
        else:
            _t, result = weightedIntersection(index, self.index)

        return result.byValue(0)
Example #5
0
 def applySorting(self, index=None):
     if index is not None:
         _t, res = weightedIntersection(self.uids, index)
     else:
         res = self.uids
     self.uids = [id for weight, id in res.byValue(minValue)]
     self.uids_keys = self.uids
     self._len = len(self.uids)
Example #6
0
    def apply(self, query, sort_on=None, indexes=None):
        results = []
        for index_name, index_query in query.items():
            if indexes and index_name in indexes:
                index = indexes[index_name]
            else:
                index = self.getIndex(index_name)

            if index is None:
                continue

            r = index.apply(index_query)

            if r is None:
                continue
            if not r:
                # empty results
                return r
            results.append((len(r), r))

        if not results:
            # no applicable indexes, so catalog was not applicable
            return None

        results.sort() # order from smallest to largest
        _t, result = results.pop(0)

        for _t, r in results:
            _t, result = weightedIntersection(result, r)

        # sort result by index
        if sort_on:
            sort_index = self.getIndex(sort_on)
            if sort_index is not None:
                sortable = ISortable(sort_index, None)
                if sortable is None:
                    raise interfaces.NotSortableIndex(
                        _("Index doesn't support sorting."))
                result = sortable.sort(result)
            else:
                raise interfaces.InvalidIndex(
                    _('Invalid index: ${sort_on}', mapping={'sort_on':sort_on}))

        return result
Example #7
0
    def apply(self, context=None):
        results = []
        for term in self.terms:
            r = term.apply(context)
            if not r:
                # empty results
                return r
            results.append((len(r), r))

        if not results:
            # no applicable terms at all
            # XXX should this be possible?
            return IFBTree()

        results.sort()

        _, result = results.pop(0)
        for _, r in results:
            _, result = weightedIntersection(result, r)
        return result