Ejemplo n.º 1
0
    def _fast_query(cls, thing1s, thing2s, properties = None, **kw):
        """Find all of the relations of this class between all of the
           members of thing1_ids and thing2_ids"""
        thing1s, thing1s_is_single = tup(thing1s, True)
        thing2s, thing2s_is_single = tup(thing2s, True)

        if not thing1s or not thing2s:
            return {}

        # grab the last time each thing1 modified this relation class so we can
        # know which relations not to even bother looking up
        if thing1s:
            from r2.models.last_modified import LastModified
            fullnames = [cls._thing1_cls._fullname_from_id36(thing1._id36)
                         for thing1 in thing1s]
            timestamps = LastModified.get_multi(fullnames,
                                                cls._cf.column_family)

        # build up a list of ids to look up, throwing out the ones that the
        # timestamp fetched above indicates are pointless
        ids = set()
        thing1_ids, thing2_ids = {}, {}
        for thing1 in thing1s:
            last_modification = timestamps.get(thing1._fullname)

            if not last_modification:
                continue

            for thing2 in thing2s:
                key = cls._rowkey(thing1._id36, thing2._id36)

                if key in ids:
                    continue

                if thing2._date > last_modification:
                    continue

                ids.add(key)
                thing2_ids[thing2._id36] = thing2
            thing1_ids[thing1._id36] = thing1

        # all relations must load these properties, even if unrequested
        if properties is not None:
            properties = set(properties)

            properties.add('thing1_id')
            properties.add('thing2_id')

        rels = {}
        if ids:
            rels = cls._byID(ids, properties=properties).values()

        if thing1s_is_single and thing2s_is_single:
            if rels:
                assert len(rels) == 1
                return rels[0]
            else:
                raise NotFound("<%s %r>" % (cls.__name__,
                                            cls._rowkey(thing1s[0]._id36,
                                                        thing2s[0]._id36)))

        return dict(((thing1_ids[rel.thing1_id], thing2_ids[rel.thing2_id]), rel)
                    for rel in rels)
Ejemplo n.º 2
0
    def _fast_query(cls, thing1s, thing2s, properties=None, **kw):
        """Find all of the relations of this class between all of the
           members of thing1_ids and thing2_ids"""
        thing1s, thing1s_is_single = tup(thing1s, True)
        thing2s, thing2s_is_single = tup(thing2s, True)

        if not thing1s or not thing2s:
            return {}

        # grab the last time each thing1 modified this relation class so we can
        # know which relations not to even bother looking up
        if thing1s:
            from r2.models.last_modified import LastModified
            fullnames = [
                cls._thing1_cls._fullname_from_id36(thing1._id36)
                for thing1 in thing1s
            ]
            timestamps = LastModified.get_multi(fullnames,
                                                cls._cf.column_family)

        # build up a list of ids to look up, throwing out the ones that the
        # timestamp fetched above indicates are pointless
        ids = set()
        thing1_ids, thing2_ids = {}, {}
        for thing1 in thing1s:
            last_modification = timestamps.get(thing1._fullname)

            if not last_modification:
                continue

            for thing2 in thing2s:
                key = cls._rowkey(thing1._id36, thing2._id36)

                if key in ids:
                    continue

                if thing2._date > last_modification:
                    continue

                ids.add(key)
                thing2_ids[thing2._id36] = thing2
            thing1_ids[thing1._id36] = thing1

        # all relations must load these properties, even if unrequested
        if properties is not None:
            properties = set(properties)

            properties.add('thing1_id')
            properties.add('thing2_id')

        rels = {}
        if ids:
            rels = cls._byID(ids, properties=properties).values()

        if thing1s_is_single and thing2s_is_single:
            if rels:
                assert len(rels) == 1
                return rels[0]
            else:
                raise NotFound(
                    "<%s %r>" %
                    (cls.__name__,
                     cls._rowkey(thing1s[0]._id36, thing2s[0]._id36)))

        return dict(
            ((thing1_ids[rel.thing1_id], thing2_ids[rel.thing2_id]), rel)
            for rel in rels)