Rename audio_info status items and get rid of duplicate afh info.
[paraslash.git] / score.c
1 /*
2  * Copyright (C) 2007 Andre Noll <maan@systemlinux.org>
3  *
4  * Licensed under the GPL v2. For licencing details see COPYING.
5  */
6
7 /** \file score.c Scoring functions to determine the audio file streaming order. */
8 #include "para.h"
9 #include "error.h"
10 #include "string.h"
11 #include "afh.h"
12 #include "afs.h"
13 #include "list.h"
14
15 static struct osl_table *score_table;
16
17 static int ptr_compare(const struct osl_object *obj1, const struct osl_object *obj2)
18 {
19         void *d1 = *(void **)obj1->data;
20         void *d2 = *(void **)obj2->data;
21         return NUM_COMPARE(d1, d2);
22 }
23
24 /**
25  * Compare the score of two audio files
26  *
27  * \param obj1 Pointer to the first score object.
28  * \param obj2 Pointer to the second score object.
29  *
30  * This function first compares the score values as usual integers. If they compare as
31  * equal, the address of \a obj1 and \a obj2 are compared. So this compare function
32  * returns zero if and only if \a obj1 and \a obj2 point to the same memory area.
33  *
34  * \sa osl_compare_function.
35  */
36 static int score_compare(const struct osl_object *obj1, const struct osl_object *obj2)
37 {
38         int d1 = *(int*)obj1->data;
39         int d2 = *(int*)obj2->data;
40         int ret = NUM_COMPARE(d2, d1);
41
42         if (ret)
43                 return ret;
44         return NUM_COMPARE(obj2->data, obj1->data);
45 }
46
47 /**
48  * The score table consists of two columns: The \a aft_row column contains
49  * pointers to the rows of the audio file table,  and the score column contains
50  * the current score of the audio file associated with that row.
51  */
52 enum score_table_columns {
53         /** The row of the audio file. */
54         SCORECOL_AFT_ROW,
55         /** The score */
56         SCORECOL_SCORE,
57         /** This table has two columns */
58         NUM_SCORE_COLUMNS
59 };
60
61 static struct osl_column_description score_cols[] = {
62         [SCORECOL_AFT_ROW] = {
63                 .storage_type = OSL_NO_STORAGE,
64                 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE | OSL_DONT_FREE,
65                 .name = "aft_row",
66                 .compare_function = ptr_compare,
67                 .data_size = sizeof(void *)
68         },
69         [SCORECOL_SCORE] = {
70                 .storage_type = OSL_NO_STORAGE,
71                 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
72                 .name = "score",
73                 .compare_function = score_compare,
74                 .data_size = sizeof(long)
75         }
76 };
77
78 static struct osl_table_description score_table_desc = {
79         .name = "score",
80         .num_columns = NUM_SCORE_COLUMNS,
81         .flags = 0,
82         .column_descriptions = score_cols
83 };
84
85 /**
86  * Compute the number of files in score table.
87  *
88  * \param num Result is returned here.
89  *
90  * \return Positive on success, negative on errors.
91  *
92  * \sa osl_get_num_rows().
93  */
94 int get_num_admissible_files(unsigned *num)
95 {
96         return osl_get_num_rows(score_table, num);
97 }
98
99 /**
100  * Get the score of the audio file associated with given row of the score table.
101  *
102  * \param score_row Pointer to the row in the score table.
103  * \param score Result is returned here on success.
104  *
105  * On errors (negative return value) the content of \a score is undefined.
106  *
107  * \return The return value of the underlying call to osl_get_object().
108  */
109 static int get_score_of_row(void *score_row, long *score)
110 {
111         struct osl_object obj;
112         int ret = osl_get_object(score_table, score_row, SCORECOL_SCORE, &obj);
113
114         if (ret >= 0)
115                 *score = *(long *)obj.data;
116         return ret;
117 }
118
119 /**
120  * Add an entry to the table of admissible files.
121  *
122  * \param aft_row The audio file to be added.
123  * \param score The score for this file.
124  *
125  * \return The return value of the underlying call to osl_add_row().
126  */
127 int score_add(const struct osl_row *aft_row, long score)
128 {
129         int ret;
130         struct osl_object score_objs[NUM_SCORE_COLUMNS];
131         size_t size;
132
133         assert(aft_row);
134         size = score_table_desc.column_descriptions[SCORECOL_AFT_ROW].data_size;
135         score_objs[SCORECOL_AFT_ROW].data = aft_row;
136         score_objs[SCORECOL_AFT_ROW].size = size;
137
138         size = score_table_desc.column_descriptions[SCORECOL_SCORE].data_size;
139         score_objs[SCORECOL_SCORE].data = para_malloc(size);
140         score_objs[SCORECOL_SCORE].size = size;
141         *(int *)(score_objs[SCORECOL_SCORE].data) = score;
142
143 //      PARA_DEBUG_LOG("adding %p\n", *(void **) (score_objs[SCORECOL_AFT_ROW].data));
144         ret = osl_add_row(score_table, score_objs);
145         if (ret < 0) {
146                 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret));
147                 free(score_objs[SCORECOL_SCORE].data);
148         }
149         return ret;
150 }
151
152 static int get_nth_score(unsigned n, long *score)
153 {
154         struct osl_row *row;
155         int ret = osl_get_nth_row(score_table, SCORECOL_SCORE, n, &row);
156
157         if (ret < 0)
158                 return ret;
159         return get_score_of_row(row, score);
160 }
161
162 /**
163  * Replace a row of the score table.
164  *
165  * \param aft_row Determines the audio file to change.
166  * \param percent The position to re-insert the audio file.
167  *
168  * The percent parameter must be between \p 0 and 100 and. A value of zero
169  * means to re-insert the audio file into the score table with a score lower
170  * than any other admissible file.
171  *
172  * \return Positive on success, negative on errors. Possible errors: Errors
173  * returned by osl_get_row(), get_num_admissible_files(), osl_get_nth_row(),
174  * osl_get_object(), osl_update_object().
175  */
176 int score_update(const struct osl_row *aft_row, long percent)
177 {
178         struct osl_row *row;
179         long new_score;
180         unsigned n, new_pos;
181         struct osl_object obj = {.data = aft_row, .size = sizeof(aft_row)};
182         int ret = osl_get_row(score_table, SCORECOL_AFT_ROW, &obj, &row);
183
184         if (ret < 0)
185                 return ret;
186         ret = get_num_admissible_files(&n);
187         if (ret < 0)
188                 return ret;
189         new_pos = 1 + (n - 1) * percent / 100;
190         ret = get_nth_score(new_pos, &new_score);
191         if (ret < 0)
192                 return ret;
193         new_score--;
194         obj.size = sizeof(long);
195         obj.data = para_malloc(obj.size);
196         *(long *)obj.data = new_score;
197         PARA_DEBUG_LOG("new score: %ld, rank %u/%u\n", new_score, new_pos, n);
198         return osl_update_object(score_table, row, SCORECOL_SCORE, &obj);
199 }
200
201 /**
202  * Given an admissible file, get the corresponding row in the aft and the score.
203  *
204  * \param score_row Determines the admissible file.
205  * \param score Result pointer.
206  * \param aft_row Result pointer.
207  *
208  * \return Negative on errors, positive on success. Possible errors: Errors
209  * returned by osl_get_object().
210  */
211 int get_score_and_aft_row(struct osl_row *score_row, long *score,
212                 struct osl_row **aft_row)
213 {
214         struct osl_object obj;
215         int ret = get_score_of_row(score_row, score);
216
217         if (ret < 0)
218                 return ret;
219         ret = osl_get_object(score_table, score_row, SCORECOL_AFT_ROW, &obj);
220         if (ret < 0)
221                 return ret;
222         *aft_row = obj.data;
223         return 1;
224 }
225
226 static int get_score_row_from_aft_row(const struct osl_row *aft_row,
227                 struct osl_row **score_row)
228 {
229         struct osl_object obj = {.data = aft_row, .size = sizeof(aft_row)};
230         return osl_get_row(score_table, SCORECOL_AFT_ROW, &obj, score_row);
231
232 }
233
234 /**
235  * Loop over all files in the score table.
236  *
237  * \param data A pointer to arbitrary data.
238  * \param func Function to be called for each admissible file.
239  *
240  * \return The return value of the underlying call to osl_rbtree_loop().
241  *
242  * This is used for the ls command. The \a data parameter is passed as the
243  * second argument to \a func.
244  *
245  * \sa admissible_file_loop_reverse().
246  */
247 int admissible_file_loop(void *data, osl_rbtree_loop_func *func)
248 {
249         return osl_rbtree_loop(score_table, SCORECOL_SCORE, data, func);
250 }
251
252 /**
253  * Loop over all files in the score table in reverse order.
254  *
255  * \param data As in admissible_file_loop().
256  * \param func As in admissible_file_loop().
257  *
258  * \return Same return value as admissible_file_loop().
259  *
260  * \sa admissible_file_loop(), osl_rbtree_loop_reverse().
261  */
262 int admissible_file_loop_reverse(void *data, osl_rbtree_loop_func *func)
263 {
264         return osl_rbtree_loop_reverse(score_table, SCORECOL_SCORE, data, func);
265 }
266
267 /**
268  * Get the admissible audio file with highest score.
269  *
270  * \param aft_row Points to the row in the aft of the "best" audio file.
271  * \param score Highest score value in the score table.
272  *
273  * \return Positive on success, negative on errors. Possible errors: Errors
274  * returned by osl_rbtree_last_row(), osl_get_object().
275  */
276 int score_get_best(struct osl_row **aft_row, long *score)
277 {
278         struct osl_row *row;
279         struct osl_object obj;
280         int ret = osl_rbtree_last_row(score_table, SCORECOL_SCORE, &row);
281
282         if (ret < 0)
283                 return ret;
284         ret = osl_get_object(score_table, row, SCORECOL_AFT_ROW, &obj);
285         if (ret < 0)
286                 return ret;
287         *aft_row = obj.data;
288         return get_score_of_row(row, score);
289 }
290
291 /**
292  * Remove an entry from the rbtree of admissible files.
293  *
294  * \param aft_row The file which is no longer admissible.
295  *
296  * \return Positive on success, negative on errors. Possible errors:
297  * Errors returned by osl_get_row() and osl_del_row().
298  *
299  * \sa score_add(), score_shutdown().
300  */
301 int score_delete(const struct osl_row *aft_row)
302 {
303         struct osl_row *score_row;
304         int ret = get_score_row_from_aft_row(aft_row, &score_row);
305
306         if (ret < 0)
307                 return ret;
308         return osl_del_row(score_table, score_row);
309 }
310
311 /**
312  * Find out whether an audio file is contained in the score table.
313  *
314  * \param aft_row The row of the audio file table.
315  * \param rank Result pointer
316  *
317  * \return Positive, if \a aft_row belongs to the audio file table,
318  * zero if not, negative on errors. If \a aft_row was found, and \a rank
319  * is not \p NULL, the rank of \a aft_row is returned in \a rank.
320  */
321 int row_belongs_to_score_table(const struct osl_row *aft_row, unsigned *rank)
322 {
323         struct osl_row *score_row;
324         int ret = get_score_row_from_aft_row(aft_row, &score_row);
325
326         if (ret == -E_RB_KEY_NOT_FOUND)
327                 return 0;
328         if (ret < 0)
329                 return ret;
330         if (!rank)
331                 return 1;
332         ret = osl_get_rank(score_table, score_row, SCORECOL_SCORE, rank);
333         if (ret < 0)
334                 return ret;
335         return 1;
336 }
337
338 /* Close the score table. */
339 static void score_close(void)
340 {
341         osl_close_table(score_table, OSL_FREE_VOLATILE);
342         score_table = NULL;
343 }
344
345 /**
346  * Open the score table.
347  *
348  * \param dir Unused.
349  *
350  * \return The return value of the underlying call to osl_open_table().
351  */
352 static int score_open(__a_unused const char *dir)
353 {
354         score_table_desc.dir = NULL; /* this table has only volatile columns */
355         return osl_open_table(&score_table_desc, &score_table);
356 }
357
358 /**
359  * Remove all entries from the score table, but keep the table open.
360  *
361  * \return Standard.
362  */
363 int clear_score_table(void)
364 {
365         score_close();
366         return score_open(NULL);
367 }
368
369 static int score_event_handler(__a_unused enum afs_events event,
370                 __a_unused struct para_buffer *pb, __a_unused void *data)
371 {
372         return 1;
373 }
374
375 /**
376  * Initialize the scoring subsystem.
377  *
378  * \param t The members of \a t are filled in by the function.
379  */
380 void score_init(struct afs_table *t)
381 {
382         t->name = score_table_desc.name;
383         t->open = score_open;
384         t->close = score_close;
385         t->event_handler = score_event_handler;
386 }