From f2f432420410d1ae22fb872d9d5d2ff18a7d53fe Mon Sep 17 00:00:00 2001 From: =?utf8?q?Stefan=20K=C3=B6gl?= Date: Fri, 25 Jan 2013 17:46:51 +0000 Subject: [PATCH] Revert "bundle queries for episode states by URLs (fixes bug 1588)" This reverts commit 5038eaca9a00a4eaa0f8841b8cb2c1582cf65743. --- mygpo/api/advanced/__init__.py | 16 +++++------ mygpo/db/couchdb/episode_state.py | 57 +++++++++++++++++++-------------------- 2 files changed, 33 insertions(+), 40 deletions(-) diff --git a/mygpo/api/advanced/__init__.py b/mygpo/api/advanced/__init__.py index 5126554f..daff083f 100644 --- a/mygpo/api/advanced/__init__.py +++ b/mygpo/api/advanced/__init__.py @@ -56,7 +56,7 @@ from mygpo.db.couchdb.episode import episode_by_id, \ from mygpo.db.couchdb.podcast import podcast_for_url from mygpo.db.couchdb.podcast_state import subscribed_podcast_ids_by_device from mygpo.db.couchdb.episode_state import get_podcasts_episode_states, \ - episode_states_for_ref_urls, get_episode_actions + episode_state_for_ref_urls, get_episode_actions # keys that are allowed in episode actions @@ -343,18 +343,14 @@ def update_episodes(user, actions, now, ua_string): # Prepare the updates for each episode state obj_funs = [] - states = episode_states_for_ref_urls(grouped_actions.keys()) - assert len(states) == len(grouped_actions.keys()) + for (p_url, e_url), action_list in grouped_actions.iteritems(): + episode_state = episode_state_for_ref_urls(user, p_url, e_url) - for (urls, action_list), state in zip(grouped_actions.items(), states): - - assert urls == (state.ref_url, state.episode_ref_url) - - if any(a['action'] == 'play' for a in action_list): - auto_flattr_episodes.append(state.episode) + if any(a['action'] == 'play' for a in actions): + auto_flattr_episodes.append(episode_state.episode) fun = partial(update_episode_actions, action_list=action_list) - obj_funs.append( (state, fun) ) + obj_funs.append( (episode_state, fun) ) bulk_save_retry(obj_funs) diff --git a/mygpo/db/couchdb/episode_state.py b/mygpo/db/couchdb/episode_state.py index 37aa610a..3cae20e7 100644 --- a/mygpo/db/couchdb/episode_state.py +++ b/mygpo/db/couchdb/episode_state.py @@ -210,47 +210,44 @@ def episode_listener_count_timespan(episode, start=None, end={}): -def episode_states_for_ref_urls(user, ref_urls): - """ Returns episode-states for the episodes specified by URLs - - user: the user for which states should be returned - ref_urls: a list of (podcast_url, episode_url) tuples, specifying episodes - """ +def episode_state_for_ref_urls(user, podcast_url, episode_url): if not user: raise QueryParameterMissing('user') - if not ref_urls: - raise QueryParameterMissing('ref_urls') + if not podcast_url: + raise QueryParameterMissing('podcast_url') - # expand ref_urls to include the user-id - keys = [ [user._id, p_url, e_url] for (p_url, e_url) in ref_urls ] + if not episode_url: + raise QueryParameterMissing('episode_url') - res = EpisodeUserState.view('episode_states/by_ref_urls', - keys = keys, - limit = len(keys), - include_docs = True, - ) - states = list(res) + cache_key = 'episode-state-%s-%s-%s' % (user._id, + sha1(podcast_url).hexdigest(), + sha1(episode_url).hexdigest()) - for p_url, e_url in ref_urls: + state = cache.get(cache_key) + if state: + return state - state = states[0] if states else None + res = EpisodeUserState.view('episode_states/by_ref_urls', + key = [user._id, podcast_url, episode_url], + limit = 1, + include_docs=True, + ) - # response include an episode state - if state and (state.ref_url==p_url and state.episode_ref_url==e_url): - state = states.pop(0) - state.ref_url = episode_url - state.podcast_ref_url = podcast_url - yield state + if res: + state = res.first() + state.ref_url = episode_url + state.podcast_ref_url = podcast_url + cache.set(cache_key, state, 60*60) + return state - # there is no episode state - create one - else: - podcast = podcast_for_url(p_url, create=True) - episode = episode_for_podcast_id_url(podcast.get_id(), e_url, - create=True) - yield episode_state_for_user_episode(user, episode) + else: + podcast = podcast_for_url(podcast_url, create=True) + episode = episode_for_podcast_id_url(podcast.get_id(), episode_url, + create=True) + return episode_state_for_user_episode(user, episode) -- 2.11.4.GIT