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)
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()
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})
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 })
def _get_read_key(self): segments = [ self.storage.add_prefix('uid'), manager.make_uid(self.instance), 'read_at' ] return get_key(*segments)
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()]
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
def get_read_key(self, instance): segments = [ self.storage.add_prefix('uid'), app.backend.get_uid(instance), 'read_at' ] return get_key(*segments)
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()
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
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]
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
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
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
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