1 ALTER TABLE user ADD COLUMN suggestion_up_to_date INTEGER DEFAULT 0;
3 update user set suggestion_up_to_date = 0;
6 CREATE TRIGGER suggestion_not_up_to_date_trigger AFTER INSERT ON subscription_log
9 update user set suggestion_up_to_date = 0 where user_ptr_id = (select user_id from device where id = new.device_id);
14 DROP PROCEDURE IF EXISTS update_suggestion $$
15 CREATE PROCEDURE update_suggestion()
17 DECLARE deadlock INT DEFAULT 0;
18 DECLARE attempts INT DEFAULT 0;
19 DECLARE done INT DEFAULT 0;
20 DECLARE user_help INT DEFAULT 0;
21 DECLARE pod_count INT DEFAULT 0;
22 DECLARE cur1 CURSOR FOR SELECT user_ptr_id FROM user where suggestion_up_to_date = 0;
23 DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
27 DROP TABLE IF EXISTS suggestion_pod;
28 CREATE TABLE suggestion_pod (
31 DROP TABLE IF EXISTS suggestion_user;
32 CREATE TABLE suggestion_user (
36 try_loop:WHILE (attempts<3) DO
38 DECLARE deadlock_detected CONDITION FOR 1213;
39 DECLARE EXIT HANDLER FOR deadlock_detected
50 FETCH cur1 INTO user_help;
53 DELETE FROM suggestion where user_id=user_help;
54 DELETE FROM suggestion_pod;
55 DELETE FROM suggestion_user;
57 insert into suggestion_pod (select podcast_id from current_subscription where user_id=user_help);
59 SELECT count(*) into pod_count FROM suggestion_pod;
62 insert into suggestion_user (select user_id from public_subscription, suggestion_pod where podcast_id=podID group by user_id);
63 insert into suggestion (select user_help, podcast_id, count(podcast_id) as priority
64 from public_subscription, suggestion_user
65 where user_id=userID and podcast_id not in (select * from suggestion_pod)
66 group by user_help, podcast_id order by priority DESC LIMIT 10);
68 insert into suggestion (select user_help, podcast_id, subscription_count, NULL as priority from toplist
69 group by user_help, podcast_id order by subscription_count DESC LIMIT 10);
71 update user set suggestion_up_to_date = 1 where user_ptr_id = user_help;
73 UNTIL done END REPEAT;
82 SET attempts=attempts+1;
87 call FAIL('Suggestion are not updated!');
90 DROP TABLE IF EXISTS suggestion_user;
91 DROP TABLE IF EXISTS suggestion_pod;
97 DROP PROCEDURE IF EXISTS update_suggestion_for $$
98 CREATE PROCEDURE update_suggestion_for(IN user_par INT)
100 DECLARE deadlock INT DEFAULT 0;
101 DECLARE attempts INT DEFAULT 0;
102 DECLARE pod_count INT DEFAULT 0;
103 DECLARE utd INT DEFAULT 0;
105 DROP TABLE IF EXISTS suggestion_pod;
106 CREATE TABLE suggestion_pod (
109 DROP TABLE IF EXISTS suggestion_user;
110 CREATE TABLE suggestion_user (
114 try_loop:WHILE (attempts<3) DO
116 DECLARE deadlock_detected CONDITION FOR 1213;
117 DECLARE EXIT HANDLER FOR deadlock_detected
125 SELECT suggestion_up_to_date into utd FROM user where user_ptr_id = user_par;
127 DELETE FROM suggestion where user_id=user_par;
128 DELETE FROM suggestion_pod;
129 DELETE FROM suggestion_user;
131 insert into suggestion_pod (select podcast_id from current_subscription where user_id=user_par);
133 SELECT count(*) into pod_count FROM suggestion_pod;
135 IF pod_count > 0 THEN
136 insert into suggestion_user (select user_id from public_subscription, suggestion_pod where podcast_id=podID group by user_id);
137 insert into suggestion (select user_par, podcast_id, count(podcast_id) as priority
138 from public_subscription, suggestion_user
139 where user_id=userID and podcast_id not in (select * from suggestion_pod)
140 group by user_par, podcast_id order by priority DESC LIMIT 10);
142 insert into suggestion (select user_par, podcast_id, subscription_count, NULL as priority from toplist
143 group by user_par, podcast_id order by subscription_count DESC LIMIT 10);
145 update user set suggestion_up_to_date = 1 where user_ptr_id = user_par;
152 SET attempts=attempts+1;
157 call FAIL('Suggestion are not updated!');
160 DROP TABLE IF EXISTS suggestion_user;
161 DROP TABLE IF EXISTS suggestion_pod;
168 DROP TABLE IF EXISTS episode_log;
169 CREATE TABLE episode_log (
170 id INT PRIMARY KEY AUTO_INCREMENT NOT NULL,
171 user_id INT NOT NULL,
172 episode_id INT NOT NULL,
174 action ENUM ('download', 'play', 'delete', 'new') NOT NULL,
175 timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
176 playmark INT DEFAULT 0,
177 FOREIGN KEY (user_id) REFERENCES auth_user (id),
178 FOREIGN KEY (episode_id) REFERENCES episode (id),
179 FOREIGN KEY (device_id) REFERENCES device (id),
180 UNIQUE (user_id, episode_id, timestamp)
183 ALTER TABLE toplist ADD COLUMN old_place INTEGER DEFAULT 0;
186 DROP PROCEDURE IF EXISTS update_toplist $$
187 CREATE PROCEDURE update_toplist()
189 DECLARE deadlock INT DEFAULT 0;
190 DECLARE attempts INT DEFAULT 0;
192 DROP TABLE IF EXISTS toplist_temp;
193 CREATE TABLE toplist_temp (
194 podcast_id INT PRIMARY KEY REFERENCES podcast (id),
195 subscription_count INT NOT NULL DEFAULT 0,
196 old_place INT DEFAULT 0,
200 try_loop:WHILE (attempts<3) DO
202 DECLARE deadlock_detected CONDITION FOR 1213;
203 DECLARE EXIT HANDLER FOR deadlock_detected
211 DELETE FROM toplist_temp;
212 INSERT INTO toplist_temp (SELECT a.podcast_id, COUNT(*) AS count_subscription,
213 (select (id - (select min(id) from toplist) + 1) from toplist where podcast_id = a.podcast_id)
214 FROM (SELECT DISTINCT podcast_id, user_id
215 FROM public_subscription) a
216 GROUP BY podcast_id);
218 INSERT INTO toplist (podcast_id, subscription_count, id, old_place) (SELECT podcast_id, subscription_count, NULL, old_place FROM toplist_temp
219 ORDER BY subscription_count DESC LIMIT 100);
226 SET attempts=attempts+1;
231 call FAIL('Toplist is not updated!');
233 DROP TABLE IF EXISTS toplist_temp;