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