示例#1
0
    def _delete(self, action):
        with self.client.map() as pipe:
            for key in self._get_keys(action):
                pipe.decr(get_key(key, 'count'))
                pipe.decr(get_key(key, 'verb', action.verb, 'count'))

                pipe.zrem(key, '%s' % action.uid)
                pipe.zrem(get_key(key, 'verb', action.verb), '%s' % action.uid)
示例#2
0
    def _delete(self, action):
        with self.client.map() as pipe:
            for key in self._get_keys(action):
                pipe.decr(get_key(key, 'count'))
                pipe.decr(get_key(key, 'verb', action.verb, 'count'))

                pipe.zrem(key, '%s' % action.uid)
                pipe.zrem(get_key(key, 'verb', action.verb), '%s' % action.uid)
示例#3
0
    def delete(self, instance, action):
        with self.client.pipeline() as pipe:
            for key in self._get_keys(instance, action):
                pipe.decr(get_key(key, 'count'))
                pipe.decr(get_key(key, 'verb', action.verb, 'count'))

                pipe.zrem(key, '%s' % action.uid)
                pipe.zrem(get_key(key, 'verb', action.verb), '%s' % action.uid)

            pipe.execute()
示例#4
0
    def _save(self, action):
        with self.client.map() as pipe:
            for key in self._get_keys(action):
                pipe.incr(get_key(key, 'count'))
                pipe.incr(get_key(key, 'verb', action.verb, 'count'))

                pipe.zadd(key, **{'%s' % action.uid: action.timestamp})

                pipe.zadd(get_key(key, 'verb', action.verb),
                          **{'%s' % action.uid: action.timestamp})
示例#5
0
    def _save(self, action):
        with self.client.map() as pipe:
            for key in self._get_keys(action):
                pipe.incr(get_key(key, 'count'))
                pipe.incr(get_key(key, 'verb', action.verb, 'count'))

                pipe.zadd(key, **{
                    '%s' % action.uid: action.timestamp
                })

                pipe.zadd(get_key(key, 'verb', action.verb), **{
                    '%s' % action.uid: action.timestamp
                })
示例#6
0
    def _get_read_key(self):
        segments = [
            self.storage.add_prefix('uid'),
            manager.make_uid(self.instance), 'read_at'
        ]

        return get_key(*segments)
示例#7
0
    def _transform(self, scores):
        with self.qs.map() as pipe:
            for uid, score in scores:
                pipe.hgetall(get_key(self.prefix, 'uid', uid))

            return [Action.from_data(data)
                    for data in pipe.execute()]
示例#8
0
    def _make_key(self, name, action=None, target=None):
        segments = [
            self.storage.add_prefix('uid'),
            manager.make_uid(self.instance),
            name,
        ]

        if target:
            if isinstance(target, six.string_types):
                segments += ['target', target]

            else:
                if isinstance(target, models.Model) or issubclass(
                        target, models.Model):
                    segments += ['target', registry.get_identifier(target)]

        if action:
            if isinstance(action, six.string_types):
                segments += ['verb', action]
            elif issubclass(action, Action):
                segments += ['verb', action.verb]

        key = get_key(*segments)

        return key
示例#9
0
    def _get_read_key(self):
        segments = [
            self.storage.add_prefix('uid'),
            manager.make_uid(self.instance),
            'read_at'
        ]

        return get_key(*segments)
示例#10
0
    def get_read_key(self, instance):
        segments = [
            self.storage.add_prefix('uid'),
            app.backend.get_uid(instance),
            'read_at'
        ]

        return get_key(*segments)
示例#11
0
    def save(self, instance, action):
        if action.uid is None:
            self._save(action)

        with self.client.pipeline() as pipe:
            for key in self._get_keys(instance, action):
                pipe.incr(get_key(key, 'count'))
                pipe.incr(get_key(key, 'verb', action.verb, 'count'))

                pipe.zadd(key, **{
                    '%s' % action.uid: action.timestamp
                })

                pipe.zadd(get_key(key, 'verb', action.verb), **{
                    '%s' % action.uid: action.timestamp
                })

            pipe.execute()
示例#12
0
    def _get_count(self, name, action=None, target=None):
        key = get_key(self._make_key(name, action=action, target=target), 'count')

        result = self.client.get(key)

        if result:
            return int(result)

        return 0
示例#13
0
    def _transform(self, scores):
        results = []

        with self.qs.map() as pipe:
            for uid, score in scores:
                results.append(pipe.hgetall(get_key(self.prefix, 'uid', uid)))

        return [Action.from_data(data)
                for data in results if data]
示例#14
0
    def _get_count(self, name, action=None, target=None):
        key = get_key(self._make_key(name, action=action, target=target),
                      'count')

        result = self.client.get(key)

        if result:
            return int(result)

        return 0
示例#15
0
    def _get_keys(self, action):
        identifier = registry.get_identifier(self.instance)

        prefix = self.storage.add_prefix('uid')

        uid = manager.make_uid(self.instance)

        keys = [
            get_key(prefix, uid, 'private'),
            get_key(prefix, uid, 'private', 'target', identifier)
        ]

        if action.actor == self.instance:
            keys.append(get_key(prefix, uid, 'public'))
            keys.append(get_key(prefix, uid, 'public', 'target', identifier))

        if action.target is not None and action.target != action.actor:
            identifier = registry.get_identifier(action.target)

            keys.append(get_key(prefix, uid, 'private', 'target', identifier))

            if action.actor == self.instance:
                keys.append(
                    get_key(prefix, uid, 'public', 'target', identifier))

        return keys
示例#16
0
    def _get_keys(self, action):
        identifier = registry.get_identifier(self.instance)

        prefix = self.storage.add_prefix('uid')

        uid = manager.make_uid(self.instance)

        keys = [
            get_key(prefix, uid, 'private'),
            get_key(prefix, uid, 'private', 'target', identifier)
        ]

        if action.actor == self.instance:
            keys.append(get_key(prefix, uid, 'public'))
            keys.append(get_key(prefix, uid, 'public', 'target', identifier))

        if action.target is not None and action.target != action.actor:
            identifier = registry.get_identifier(action.target)

            keys.append(get_key(prefix, uid, 'private', 'target', identifier))

            if action.actor == self.instance:
                keys.append(get_key(prefix, uid, 'public', 'target', identifier))

        return keys
示例#17
0
    def _transform(self, scores):
        with self.qs.pipeline() as pipe:
            for uid, score in scores:
                pipe.hgetall(get_key(self.prefix, 'uid', uid))

            results = pipe.execute()

            actions = []

            for data in results:
                if not data:
                    continue

                try:
                    action = self.backend.get_action(data)
                except ActionInvalid as e:
                    logger.exception(e)
                else:
                    actions.append(action)

            return actions
示例#18
0
    def _make_key(self, name, action=None, target=None):
        segments = [
            self.storage.add_prefix('uid'),
            manager.make_uid(self.instance),
            name,
        ]

        if target:
            if isinstance(target, six.string_types):
                segments += ['target', target]

            else:
                if isinstance(target, models.Model) or issubclass(target, models.Model):
                    segments += ['target', registry.get_identifier(target)]

        if action:
            if isinstance(action, six.string_types):
                segments += ['verb', action]
            elif issubclass(action, Action):
                segments += ['verb', action.verb]

        key = get_key(*segments)

        return key