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