faster list query

This commit is contained in:
j 2016-02-11 23:53:32 +05:30
parent a71dab1c91
commit ed02b1772d
2 changed files with 20 additions and 8 deletions

View file

@ -95,7 +95,8 @@ class Item(db.Model):
@classmethod @classmethod
def find(cls, data): def find(cls, data):
return Parser(cls, user_items, Find, Sort).find(data) from user.models import list_items
return Parser(cls, user_items, list_items, Find, Sort).find(data)
@classmethod @classmethod
def remove_many(cls, ids): def remove_many(cls, ids):

View file

@ -38,12 +38,13 @@ def get_operator(op, type='str'):
class Parser(object): class Parser(object):
def __init__(self, model, user_items, find, sort): def __init__(self, model, user_items, list_items, find, sort):
self._model = model self._model = model
self._find = find self._find = find
self._sort = sort self._sort = sort
self._user = model.users.mapper.class_ self._user = model.users.mapper.class_
self._user_items = user_items self._user_items = user_items
self._list_items = list_items
self._list = model.lists.mapper.class_ self._list = model.lists.mapper.class_
self.item_keys = model.item_keys self.item_keys = model.item_keys
self.filter_keys = model.filter_keys self.filter_keys = model.filter_keys
@ -129,14 +130,24 @@ class Parser(object):
u = self._user.query.filter_by(nickname=nickname).one() u = self._user.query.filter_by(nickname=nickname).one()
else: else:
u = self._user.query.filter_by(id=settings.USER_ID).one() u = self._user.query.filter_by(id=settings.USER_ID).one()
if nickname: if not name:
ids = self._user_items.select().where(
self._user_items.columns['user_id'].is_(u.id)
).with_only_columns(
['item_id']
)
elif nickname:
peer = utils.get_peer(u.id) peer = utils.get_peer(u.id)
if name:
ids = peer.info['lists'].get(name, []) ids = peer.info['lists'].get(name, [])
else:
ids = list(peer.library.keys())
else: else:
l = self._list.query.filter_by(user_id=u.id, name=name).one() l = self._list.query.filter_by(user_id=u.id, name=name).one()
if l.type == 'static':
ids = self._list_items.select().where(
self._list_items.columns['list_id'].is_(l.id)
).with_only_columns(
['item_id']
)
else:
ids = l.get_items().options(load_only('id')) ids = l.get_items().options(load_only('id'))
in_op = operators.notin_op if exclude else operators.in_op in_op = operators.notin_op if exclude else operators.in_op
q = in_op(self._model.id, ids) q = in_op(self._model.id, ids)