]> git.tuebingen.mpg.de Git - paraslash.git/blob - mood.c
mood.c: Remove row_is_admissible().
[paraslash.git] / mood.c
1 /* Copyright (C) 2007 Andre Noll <maan@tuebingen.mpg.de>, see file COPYING. */
2
3 /** \file mood.c Paraslash's mood handling functions. */
4
5 #include <regex.h>
6 #include <osl.h>
7 #include <lopsub.h>
8
9 #include "para.h"
10 #include "error.h"
11 #include "string.h"
12 #include "afh.h"
13 #include "afs.h"
14 #include "list.h"
15 #include "mood.h"
16
17 /*
18  * Mood parser API. It's overkill to have an own header file for
19  * these declarations as they are only needed in this .c file.
20  */
21 struct mp_context;
22 int mp_init(const char *definition, int nbytes, struct mp_context **result,
23                  char **errmsg);
24 bool mp_eval_row(const struct osl_row *aft_row, struct mp_context *ctx);
25 void mp_shutdown(struct mp_context *ctx);
26
27 /**
28  * Contains statistical data of the currently admissible audio files.
29  *
30  * It is used to assign normalized score values to each admissible audio file.
31  */
32 struct afs_statistics {
33         /** Sum of num played over all admissible files. */
34         int64_t num_played_sum;
35         /** Sum of last played times over all admissible files. */
36         int64_t last_played_sum;
37         /** Quadratic deviation of num played count. */
38         int64_t num_played_qd;
39         /** Quadratic deviation of last played time. */
40         int64_t last_played_qd;
41         /** Correction factor for the num played score. */
42         int64_t num_played_correction;
43         /** Correction factor for the last played score. */
44         int64_t last_played_correction;
45         /** Common divisor of the correction factors. */
46         int64_t normalization_divisor;
47         /** Number of admissible files */
48         unsigned num;
49 };
50 static struct afs_statistics statistics = {.normalization_divisor = 1};
51
52 struct mood {
53         /** The name of this mood. */
54         char *name;
55         /** Info for the bison parser. */
56         struct mp_context *parser_context;
57 };
58
59 /*
60  * If current_mood is NULL then no mood is currently open. If
61  * current_mood->name is NULL, the dummy mood is currently open.
62  */
63 static struct mood *current_mood;
64
65 /*
66  * Find the position of the most-significant set bit.
67  *
68  * Copied and slightly adapted from the linux source tree, version 4.9.39
69  * (2017-07).
70  */
71 __a_const static uint32_t fls64(uint64_t v)
72 {
73         int n = 63;
74         const uint64_t ones = ~(uint64_t)0U;
75
76         if ((v & (ones << 32)) == 0) {
77                 n -= 32;
78                 v <<= 32;
79         }
80         if ((v & (ones << (64 - 16))) == 0) {
81                 n -= 16;
82                 v <<= 16;
83         }
84         if ((v & (ones << (64 - 8))) == 0) {
85                 n -= 8;
86                 v <<= 8;
87         }
88         if ((v & (ones << (64 - 4))) == 0) {
89                 n -= 4;
90                 v <<= 4;
91         }
92         if ((v & (ones << (64 - 2))) == 0) {
93                 n -= 2;
94                 v <<= 2;
95         }
96         if ((v & (ones << (64 - 1))) == 0)
97                 n -= 1;
98         return n;
99 }
100
101 /*
102  * Compute the integer square root floor(sqrt(x)).
103  *
104  * Taken 2007 from the linux source tree.
105  */
106 __a_const static uint64_t int_sqrt(uint64_t x)
107 {
108         uint64_t op = x, res = 0, one = 1;
109
110         one = one << (fls64(x) & ~one);
111         while (one != 0) {
112                 if (op >= res + one) {
113                         op = op - (res + one);
114                         res = res + 2 * one;
115                 }
116                 res /= 2;
117                 one /= 4;
118         }
119         return res;
120 }
121
122 static void destroy_mood(struct mood *m)
123 {
124         if (!m)
125                 return;
126         mp_shutdown(m->parser_context);
127         free(m->name);
128         free(m);
129 }
130
131 static struct mood *alloc_new_mood(const char *name)
132 {
133         struct mood *m = zalloc(sizeof(struct mood));
134         if (name)
135                 m->name = para_strdup(name);
136         return m;
137 }
138
139 static int load_mood(const struct osl_row *mood_row, struct mood **m,
140                 char **errmsg)
141 {
142         char *mood_name;
143         struct osl_object mood_def;
144         int ret;
145
146         ret = mood_get_name_and_def_by_row(mood_row, &mood_name, &mood_def);
147         if (ret < 0) {
148                 if (errmsg)
149                         *errmsg = make_message(
150                                 "could not read mood definition");
151                 return ret;
152         }
153         assert(*mood_name);
154         *m = alloc_new_mood(mood_name);
155         PARA_INFO_LOG("opening mood %s\n", mood_name);
156         ret = mp_init(mood_def.data, mood_def.size, &(*m)->parser_context, errmsg);
157         osl_close_disk_object(&mood_def);
158         if (ret < 0)
159                 destroy_mood(*m);
160         return ret;
161 }
162
163 static int check_mood(struct osl_row *mood_row, void *data)
164 {
165         struct para_buffer *pb = data;
166         char *mood_name, *errmsg;
167         struct osl_object mood_def;
168         struct mood *m;
169         int ret = mood_get_name_and_def_by_row(mood_row, &mood_name, &mood_def);
170
171         if (ret < 0) {
172                 para_printf(pb, "cannot read mood\n");
173                 return ret;
174         }
175         if (!*mood_name) /* ignore dummy row */
176                 goto out;
177         m = alloc_new_mood("check");
178         ret = mp_init(mood_def.data, mood_def.size, &m->parser_context,
179                 &errmsg);
180         if (ret < 0) {
181                 para_printf(pb, "%s: %s\n", mood_name, errmsg);
182                 free(errmsg);
183                 para_printf(pb, "%s\n", para_strerror(-ret));
184         } else
185                 destroy_mood(m);
186         ret = 1; /* don't fail the loop on invalid mood definitions */
187 out:
188         osl_close_disk_object(&mood_def);
189         return ret;
190 }
191
192 /**
193  * Check all moods for syntax errors.
194  *
195  * \param aca Only ->pbout is used for diagnostics.
196  *
197  * \return Negative on fatal errors. Inconsistent mood definitions are not
198  * considered an error.
199  */
200 int mood_check_callback(struct afs_callback_arg *aca)
201 {
202         para_printf(&aca->pbout, "checking moods...\n");
203         return osl(osl_rbtree_loop(moods_table, BLOBCOL_ID, &aca->pbout,
204                 check_mood));
205 }
206
207 /*
208  * The normalized num_played and last_played values are defined as
209  *
210  *      nn := -(np - mean_n) / sigma_n and nl := -(lp - mean_l) / sigma_l
211  *
212  *  For a (hypothetical) file with np = 0 and lp = now we thus have
213  *
214  *      nn =  mean_n / sigma_n =: hn > 0
215  *      nl = -(now - mean_l) / sigma_l =: hl < 0
216  *
217  * We design the score function so that both contributions get the same
218  * weight. Define the np and lp score of an arbitrary file as
219  *
220  *      sn := nn * -hl and sl := nl * hn
221  *
222  * Example:
223  *      num_played mean/sigma: 87/14
224  *      last_played mean/sigma: 45/32 days
225  *
226  *      We have hn = 87 / 14 = 6.21 and hl = -45 / 32 = -1.41. Multiplying
227  *      nn of every file with the correction factor 1.41 and nl with
228  *      6.21 makes the weight of the two contributions equal.
229  *
230  * The total score s := sn + sl has the representation
231  *
232  *      s = -cn * (np - mean_n) - cl * (lp - mean_l)
233  *
234  * with positive correction factors
235  *
236  *      cn = (now - mean_l) / (sqrt(ql) * sqrt(qn) / n)
237  *      cl = mean_n / (sqrt(ql) * sqrt(qn) / n)
238  *
239  * where ql and qn are the quadratic deviations stored in the statistics
240  * structure and n is the number of admissible files. To avoid integer
241  * overflows and rounding errors we store the common divisor of the
242  * correction factors separately.
243  */
244 static int64_t normalized_value(int64_t x, int64_t n, int64_t sum, int64_t qd)
245 {
246         if (!n || !qd)
247                 return 0;
248         return 100 * (n * x - sum) / (int64_t)int_sqrt(n) / (int64_t)int_sqrt(qd);
249 }
250
251 static long compute_score(struct afs_info *afsi)
252 {
253         long score = -normalized_value(afsi->num_played, statistics.num,
254                 statistics.num_played_sum, statistics.num_played_qd);
255         score -= normalized_value(afsi->last_played, statistics.num,
256                 statistics.last_played_sum, statistics.last_played_qd);
257         return score / 2;
258 }
259
260 static int add_afs_statistics(const struct osl_row *row)
261 {
262         uint64_t n, x, s, q;
263         struct afs_info afsi;
264         int ret;
265
266         ret = get_afsi_of_row(row, &afsi);
267         if (ret < 0)
268                 return ret;
269         n = statistics.num;
270         x = afsi.last_played;
271         s = statistics.last_played_sum;
272         if (n > 0) {
273                 q = (x > s / n)? x - s / n : s / n - x;
274                 statistics.last_played_qd += q * q * n / (n + 1);
275         }
276         statistics.last_played_sum += x;
277
278         x = afsi.num_played;
279         s = statistics.num_played_sum;
280         if (n > 0) {
281                 q = (x > s / n)? x - s / n : s / n - x;
282                 statistics.num_played_qd += q * q * n / (n + 1);
283         }
284         statistics.num_played_sum += x;
285         statistics.num++;
286         return 1;
287 }
288
289 static int del_afs_statistics(const struct osl_row *row)
290 {
291         uint64_t n, s, q, a, new_s;
292         struct afs_info afsi;
293         int ret;
294         ret = get_afsi_of_row(row, &afsi);
295         if (ret < 0)
296                 return ret;
297         n = statistics.num;
298         assert(n);
299         if (n == 1) {
300                 memset(&statistics, 0, sizeof(statistics));
301                 statistics.normalization_divisor = 1;
302                 return 1;
303         }
304
305         s = statistics.last_played_sum;
306         q = statistics.last_played_qd;
307         a = afsi.last_played;
308         new_s = s - a;
309         statistics.last_played_sum = new_s;
310         statistics.last_played_qd = q + s * s / n - a * a
311                 - new_s * new_s / (n - 1);
312
313         s = statistics.num_played_sum;
314         q = statistics.num_played_qd;
315         a = afsi.num_played;
316         new_s = s - a;
317         statistics.num_played_sum = new_s;
318         statistics.num_played_qd = q + s * s / n - a * a
319                 - new_s * new_s / (n - 1);
320
321         statistics.num--;
322         return 1;
323 }
324
325 /*
326  * At mood open time we determine the set of admissible files for the given
327  * mood where each file is identified by a pointer to a row of the audio file
328  * table. In the first pass the pointers are added to a temporary array and
329  * statistics are computed. When all admissible files have been processed in
330  * this way, the score of each admissible file is computed and the (row, score)
331  * pair is added to the score table. This has to be done in a second pass
332  * since the score depends on the statistics. Finally, the array is freed.
333  */
334 struct admissible_array {
335         /** Files are admissible wrt. this mood. */
336         struct mood *m;
337         /** The size of the array */
338         unsigned size;
339         /** Pointer to the array of admissible files. */
340         struct osl_row **array;
341 };
342
343 /*
344  * Check whether the given audio file is admissible. If it is, add it to array
345  * of admissible files.
346  */
347 static int add_if_admissible(struct osl_row *aft_row, void *data)
348 {
349         struct admissible_array *aa = data;
350
351         if (!mp_eval_row(aft_row, aa->m->parser_context))
352                 return 0;
353         if (statistics.num >= aa->size) {
354                 aa->size *= 2;
355                 aa->size += 100;
356                 aa->array = arr_realloc(aa->array, aa->size,
357                         sizeof(struct osl_row *));
358         }
359         aa->array[statistics.num] = aft_row;
360         return add_afs_statistics(aft_row);
361 }
362
363 /**
364  * Compute the new quadratic deviation in case one element changes.
365  *
366  * \param n Number of elements.
367  * \param old_qd The quadratic deviation before the change.
368  * \param old_val The value that was replaced.
369  * \param new_val The replacement value.
370  * \param old_sum The sum of all elements before the update.
371  *
372  * \return The new quadratic deviation resulting from replacing old_val
373  * by new_val.
374  *
375  * Given n real numbers a_1, ..., a_n, their sum S = a_1 + ... + a_n,
376  * their quadratic deviation
377  *
378  * q = (a_1 - S/n)^2 + ... + (a_n - S/n)^2,
379  *
380  * and a real number b, the quadratic deviation q' of a_1,...a_{n-1}, b (ie.
381  * the last number a_n was replaced by b) may be computed in O(1) time in terms
382  * of n, q, a_n, b, and S as
383  *
384  *      q' = q + d * s - (2 * S + d) * d / n
385  *         = q + d * (s - 2 * S / n - d /n),
386  *
387  * where d = b - a_n, and s = b + a_n.
388  *
389  * Example: n = 3, a_1 = 3, a_2 = 5, a_3 = 7, b = 10. Then S = 15, q = 8, d = 3,
390  * s = 17, so
391  *
392  *      q + d * s - (2 * S + d) * d / n = 8 + 51 - 33 = 26,
393  *
394  * which equals q' = (3 - 6)^2 + (5 - 6)^2 + (10 - 6)^2.
395  *
396  */
397 _static_inline_ int64_t update_quadratic_deviation(int64_t n, int64_t old_qd,
398                 int64_t old_val, int64_t new_val, int64_t old_sum)
399 {
400         int64_t delta = new_val - old_val;
401         int64_t sigma = new_val + old_val;
402         return old_qd + delta * (sigma - 2 * old_sum / n - delta / n);
403 }
404
405 static int update_afs_statistics(struct afs_info *old_afsi,
406                 struct afs_info *new_afsi)
407 {
408         unsigned n;
409         int ret = get_num_admissible_files(&n);
410
411         if (ret < 0)
412                 return ret;
413         assert(n);
414
415         statistics.last_played_qd = update_quadratic_deviation(n,
416                 statistics.last_played_qd, old_afsi->last_played,
417                 new_afsi->last_played, statistics.last_played_sum);
418         statistics.last_played_sum += new_afsi->last_played - old_afsi->last_played;
419
420         statistics.num_played_qd = update_quadratic_deviation(n,
421                 statistics.num_played_qd, old_afsi->num_played,
422                 new_afsi->num_played, statistics.num_played_sum);
423         statistics.num_played_sum += new_afsi->num_played - old_afsi->num_played;
424         return 1;
425 }
426
427 static int add_to_score_table(const struct osl_row *aft_row)
428 {
429         long score;
430         struct afs_info afsi;
431         int ret = get_afsi_of_row(aft_row, &afsi);
432
433         if (ret < 0)
434                 return ret;
435         score = compute_score(&afsi);
436         return score_add(aft_row, score);
437 }
438
439 static int delete_from_statistics_and_score_table(const struct osl_row *aft_row)
440 {
441         int ret = del_afs_statistics(aft_row);
442         if (ret < 0)
443                 return ret;
444         return score_delete(aft_row);
445 }
446
447 /**
448  * Delete one entry from the statistics and from the score table.
449  *
450  * \param aft_row The audio file which is no longer admissible.
451  *
452  * \return Positive on success, negative on errors.
453  *
454  * \sa \ref score_delete().
455  */
456 static int mood_delete_audio_file(const struct osl_row *aft_row)
457 {
458         int ret;
459
460         ret = row_belongs_to_score_table(aft_row, NULL);
461         if (ret < 0)
462                 return ret;
463         if (!ret) /* not admissible, nothing to do */
464                 return 1;
465         return delete_from_statistics_and_score_table(aft_row);
466 }
467
468 /**
469  * Compute the new score of an audio file wrt. the current mood.
470  *
471  * \param aft_row Determines the audio file.
472  * \param old_afsi The audio file selector info before updating.
473  *
474  * The \a old_afsi argument may be \p NULL which indicates that no changes to
475  * the audio file info were made.
476  *
477  * \return Positive on success, negative on errors.
478  */
479 static int mood_update_audio_file(const struct osl_row *aft_row,
480                 struct afs_info *old_afsi)
481 {
482         long score, percent;
483         int ret, is_admissible, was_admissible = 0;
484         struct afs_info afsi;
485         unsigned rank;
486
487         if (!current_mood)
488                 return 1; /* nothing to do */
489         ret = row_belongs_to_score_table(aft_row, &rank);
490         if (ret < 0)
491                 return ret;
492         was_admissible = ret;
493         is_admissible = mp_eval_row(aft_row, current_mood->parser_context);
494         if (!was_admissible && !is_admissible)
495                 return 1;
496         if (was_admissible && !is_admissible)
497                 return delete_from_statistics_and_score_table(aft_row);
498         if (!was_admissible && is_admissible) {
499                 ret = add_afs_statistics(aft_row);
500                 if (ret < 0)
501                         return ret;
502                 return add_to_score_table(aft_row);
503         }
504         /* update score */
505         ret = get_afsi_of_row(aft_row, &afsi);
506         if (ret < 0)
507                 return ret;
508         if (old_afsi) {
509                 ret = update_afs_statistics(old_afsi, &afsi);
510                 if (ret < 0)
511                         return ret;
512         }
513         score = compute_score(&afsi);
514         PARA_DEBUG_LOG("score: %li\n", score);
515         percent = (score + 100) / 3;
516         if (percent > 100)
517                 percent = 100;
518         else if (percent < 0)
519                 percent = 0;
520         PARA_DEBUG_LOG("moving from rank %u to %li%%\n", rank, percent);
521         return score_update(aft_row, percent);
522 }
523
524 /* sse: seconds since epoch. */
525 static void log_statistics(int64_t sse)
526 {
527         unsigned n = statistics.num;
528         int mean_days, sigma_days;
529
530         assert(current_mood);
531         PARA_NOTICE_LOG("loaded mood %s\n", current_mood->name?
532                 current_mood->name : "(dummy)");
533         if (!n) {
534                 PARA_WARNING_LOG("no admissible files\n");
535                 return;
536         }
537         PARA_NOTICE_LOG("%u admissible files\n", statistics.num);
538         mean_days = (sse - statistics.last_played_sum / n) / 3600 / 24;
539         sigma_days = int_sqrt(statistics.last_played_qd / n) / 3600 / 24;
540         PARA_NOTICE_LOG("last_played mean/sigma: %d/%d days\n", mean_days, sigma_days);
541         PARA_NOTICE_LOG("num_played mean/sigma: %" PRId64 "/%" PRIu64 "\n",
542                 statistics.num_played_sum / n,
543                 int_sqrt(statistics.num_played_qd / n));
544         PARA_NOTICE_LOG("num_played correction factor: %" PRId64 "\n",
545                 statistics.num_played_correction);
546         PARA_NOTICE_LOG("last_played correction factor: %" PRId64 "\n",
547                 statistics.last_played_correction);
548         PARA_NOTICE_LOG("normalization divisor: %" PRId64 "\n",
549                 statistics.normalization_divisor);
550 }
551
552 /**
553  * Close the current mood.
554  *
555  * Frees all resources of the current mood.
556  */
557 void close_current_mood(void)
558 {
559         destroy_mood(current_mood);
560         current_mood = NULL;
561         memset(&statistics, 0, sizeof(statistics));
562         statistics.normalization_divisor = 1;
563 }
564
565 static void compute_correction_factors(int64_t sse)
566 {
567         struct afs_statistics *s = &statistics;
568
569         if (s->num > 0) {
570                 s->normalization_divisor = int_sqrt(s->last_played_qd)
571                         * int_sqrt(s->num_played_qd) / s->num / 100;
572                 s->num_played_correction = sse - s->last_played_sum / s->num;
573                 s->last_played_correction = s->num_played_sum / s->num;
574         }
575         if (s->num_played_correction == 0)
576                 s->num_played_correction = 1;
577         if (s->normalization_divisor == 0)
578                 s->normalization_divisor = 1;
579         if (s->last_played_correction == 0)
580                 s->last_played_correction = 1;
581 }
582
583 /**
584  * Change the current mood.
585  *
586  * \param mood_name The name of the mood to open.
587  * \param errmsg Error description is returned here.
588  *
589  * If \a mood_name is \a NULL, load the dummy mood that accepts every audio file
590  * and uses a scoring method based only on the \a last_played information.
591  *
592  * The errmsg pointer may be NULL, in which case no error message will be
593  * returned. If a non-NULL pointer is given, the caller must free *errmsg.
594  *
595  * If there is already an open mood, it will be closed first.
596  *
597  * \return Positive on success, negative on errors.
598  *
599  * \sa struct \ref afs_info::last_played, \ref mp_eval_row().
600  */
601 int change_current_mood(const char *mood_name, char **errmsg)
602 {
603         int i, ret;
604         struct admissible_array aa = {
605                 .size = 0,
606                 .array = NULL
607         };
608         /*
609          * We can not use the "now" pointer from sched.c here because we are
610          * called before schedule(), which initializes "now".
611          */
612         struct timeval rnow;
613
614         if (mood_name) {
615                 struct mood *m;
616                 struct osl_row *row;
617                 struct osl_object obj;
618
619                 if (!*mood_name) {
620                         *errmsg = make_message("empty mood name");
621                         return -ERRNO_TO_PARA_ERROR(EINVAL);
622                 }
623                 obj.data = (char *)mood_name;
624                 obj.size = strlen(mood_name) + 1;
625                 ret = osl(osl_get_row(moods_table, BLOBCOL_NAME, &obj, &row));
626                 if (ret < 0) {
627                         if (errmsg)
628                                 *errmsg = make_message("no such mood: %s",
629                                         mood_name);
630                         return ret;
631                 }
632                 ret = load_mood(row, &m, errmsg);
633                 if (ret < 0)
634                         return ret;
635                 close_current_mood();
636                 current_mood = m;
637         } else { /* load dummy mood */
638                 close_current_mood();
639                 current_mood = alloc_new_mood(NULL);
640         }
641         aa.m = current_mood;
642         PARA_NOTICE_LOG("computing statistics of admissible files\n");
643         ret = audio_file_loop(&aa, add_if_admissible);
644         if (ret < 0) {
645                 if (errmsg)
646                         *errmsg = make_message("audio file loop failed");
647                 goto out;
648         }
649         clock_get_realtime(&rnow);
650         compute_correction_factors(rnow.tv_sec);
651         log_statistics(rnow.tv_sec);
652         for (i = 0; i < statistics.num; i++) {
653                 ret = add_to_score_table(aa.array[i]);
654                 if (ret < 0) {
655                         if (errmsg)
656                                 *errmsg = make_message(
657                                         "could not add row to score table");
658                         goto out;
659                 }
660         }
661         ret = statistics.num;
662 out:
663         free(aa.array);
664         if (ret < 0)
665                 close_current_mood();
666         return ret;
667 }
668
669 /*
670  * Close and re-open the current mood.
671  *
672  * This function is called on events which render the current list of
673  * admissible files useless, for example if an attribute is removed from the
674  * attribute table.
675  *
676  * If no mood is currently open, the function returns success.
677  */
678 static int reload_current_mood(void)
679 {
680         int ret;
681         char *mood_name = NULL;
682
683         ret = clear_score_table();
684         if (ret < 0)
685                 return ret;
686         if (!current_mood)
687                 return 1;
688         PARA_NOTICE_LOG("reloading %s\n", current_mood->name?
689                 current_mood->name : "(dummy)");
690         if (current_mood->name)
691                 mood_name = para_strdup(current_mood->name);
692         close_current_mood();
693         ret = change_current_mood(mood_name, NULL);
694         free(mood_name);
695         return ret;
696 }
697
698 /**
699  * Notification callback for the moods table.
700  *
701  * \param event Type of the event just occurred.
702  * \param pb Unused.
703  * \param data Its type depends on the event.
704  *
705  * This function updates the score table according to the event that has
706  * occurred. Two actions are possible: (a) reload the current mood, or (b)
707  * add/remove/update the row of the score table which corresponds to the audio
708  * file that has been modified or whose afs info has been changed. It depends
709  * on the type of the event which action (if any) is performed.
710  *
711  * The callbacks of command handlers such as com_add() or com_touch() which
712  * modify the audio file table call this function. The virtual streaming system
713  * also calls this after it has updated the afs info of the file it is about to
714  * stream (the one with the highest score). If the file stays admissible, its
715  * score is recomputed so that a different file is picked next time.
716  *
717  * \return Standard.
718  */
719 int moods_event_handler(enum afs_events event, __a_unused struct para_buffer *pb,
720                 void *data)
721 {
722         if (!current_mood)
723                 return 0;
724         switch (event) {
725         /*
726          * The three blob events might change the set of admissible files,
727          * so we must reload the score list.
728          */
729         case BLOB_RENAME:
730         case BLOB_REMOVE:
731         case BLOB_ADD:
732                 if (data == moods_table || data == playlists_table)
733                         return 1; /* no reload necessary for these */
734                 return reload_current_mood();
735         /* these also require reload of the score table */
736         case ATTRIBUTE_ADD:
737         case ATTRIBUTE_REMOVE:
738         case ATTRIBUTE_RENAME:
739                 return reload_current_mood();
740         /* changes to the aft only require to re-examine the audio file */
741         case AFSI_CHANGE: {
742                 struct afsi_change_event_data *aced = data;
743                 return mood_update_audio_file(aced->aft_row, aced->old_afsi);
744                 }
745         case AFHI_CHANGE:
746         case AUDIO_FILE_RENAME:
747         case AUDIO_FILE_ADD:
748                 return mood_update_audio_file(data, NULL);
749         case AUDIO_FILE_REMOVE:
750                 return mood_delete_audio_file(data);
751         default:
752                 return 1;
753         }
754 }